#include <U8g2lib.h>
// I2Cピンの設定(Arduino Nanoの場合、変更後のピン設定)
#define OLED_SDA A4
#define OLED_SCL A5
// OLEDの初期化設定
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ OLED_SCL, /* data=*/ OLED_SDA, /* reset=*/ U8X8_PIN_NONE);
// 漢字画像(BMP)から変換したHEXデータ(横16×縦16ピクセル)
const unsigned char yar_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xF8, 0x3F, 0x0C, 0x02, 0x06, 0x02, 0xF8, 0x3F, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x03, 0xFE, 0x7F, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, };
const unsigned char mon_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0xF8, 0x3F, 0x18, 0x30, 0x18, 0x30, 0x18, 0x30, 0xF8, 0x3F, 0x18, 0x30, 0x18, 0x30, 0xF8, 0x3F, 0x18, 0x30, 0x08, 0x30, 0x0C, 0x30, 0x04, 0x30, 0x06, 0x1F, 0x00, 0x00, };
const unsigned char sun_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0xFC, 0x3F, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0xFC, 0x3F, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0x0C, 0x30, 0xF8, 0x1F, 0x00, 0x00, };
const unsigned char tue_bmp[] PROGMEM = {0x00, 0x00, 0x80, 0x00, 0x80, 0x01, 0x84, 0x21, 0x84, 0x31, 0x8C, 0x31, 0x88, 0x31, 0x88, 0x11, 0x88, 0x19, 0x80, 0x01, 0xC0, 0x02, 0x60, 0x06, 0x30, 0x1C, 0x0C, 0x70, 0x02, 0x00, 0x00, 0x00, };
const unsigned char wed_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0xBE, 0x61, 0xA0, 0x1B, 0xA0, 0x0F, 0xB0, 0x07, 0xB0, 0x05, 0x90, 0x0D, 0x98, 0x19, 0x8C, 0x31, 0x86, 0x61, 0x90, 0x01, 0xE0, 0x01, 0x00, 0x00, };
const unsigned char thu_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0xFC, 0x7F, 0x80, 0x01, 0xC0, 0x03, 0xC0, 0x07, 0xE0, 0x0D, 0xB0, 0x09, 0x98, 0x19, 0x8C, 0x71, 0x82, 0x61, 0x80, 0x01, 0x80, 0x01, 0x00, 0x00, };
const unsigned char fri_bmp[] PROGMEM = {0x00, 0x00, 0xC0, 0x03, 0x60, 0x06, 0x30, 0x1C, 0x1C, 0x30, 0xF7, 0xEF, 0x80, 0x01, 0x80, 0x01, 0xFE, 0x7F, 0x80, 0x01, 0x8C, 0x31, 0x98, 0x11, 0x98, 0x19, 0x90, 0x09, 0xFE, 0x7F, 0x00, 0x00, };
const unsigned char sat_bmp[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xFC, 0x3F, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xFE, 0x7F, 0x00, 0x00, };
const unsigned char jik_bmp[] PROGMEM = {0x00, 0x04, 0x1E, 0x04, 0xD3, 0x7F, 0x13, 0x04, 0x13, 0x04, 0x13, 0x04, 0xD3, 0x7F, 0x1F, 0x20, 0x13, 0x30, 0xD3, 0x7F, 0x93, 0x21, 0x13, 0x23, 0x13, 0x22, 0x1E, 0x20, 0x00, 0x3F, 0x00, 0x00, };
const unsigned char fun_bmp[] PROGMEM = {0x00, 0x00, 0x30, 0x0C, 0x18, 0x08, 0x08, 0x18, 0x0C, 0x30, 0x06, 0x60, 0xFA, 0x5F, 0x60, 0x30, 0x40, 0x30, 0x60, 0x30, 0x20, 0x30, 0x30, 0x30, 0x18, 0x30, 0x0C, 0x30, 0xC6, 0x1F, 0x00, 0x00, };
const unsigned char byo_bmp[] PROGMEM = {0x00, 0x00, 0x3E, 0x04, 0x08, 0x04, 0x88, 0x25, 0xBE, 0x65, 0x8C, 0x65, 0x8C, 0x44, 0x8C, 0x44, 0x9C, 0xE4, 0x6E, 0xE4, 0x2A, 0x24, 0x08, 0x37, 0x08, 0x18, 0x08, 0x0C, 0x88, 0x03, 0x00, 0x00, };
const unsigned char ttt_bmp[] PROGMEM = {0x80, 0x01, 0x80, 0x03, 0x80, 0x07, 0x80, 0x0F, 0xFF, 0x1D, 0xFF, 0x39, 0x01, 0x70, 0x01, 0xE0, 0x01, 0xE0, 0x01, 0x70, 0xFF, 0x39, 0xFF, 0x1D, 0x80, 0x0F, 0x80, 0x07, 0x80, 0x03, 0x80, 0x01, };
// スイッチ入力ピンの定義
#define SWITCH_SELECT 4
#define SWITCH_ACTION 5
enum MenuState {
MAIN_MENU,
SELECT_AA,
SELECT_BB,
DISPLAY_AA,
DISPLAY_BB
};
MenuState state = MAIN_MENU;
int selection = 0; // 0: AA, 1: BB
void setup(void) {
u8g2.begin(); // OLEDの初期化
u8g2.setFlipMode(0); // 画面の向きを設定
pinMode(SWITCH_SELECT, INPUT_PULLUP);
pinMode(SWITCH_ACTION, INPUT_PULLUP);
}
void loop(void) {
// スイッチ入力の読み取り
bool selectPressed = digitalRead(SWITCH_SELECT) == LOW;
bool actionPressed = digitalRead(SWITCH_ACTION) == LOW;
// 状態に応じた動作
switch (state) {
case MAIN_MENU:
displayMainMenu();
if (selectPressed) {
state = (selection == 0) ? SELECT_AA : SELECT_BB;
delay(200); // デバウンス
}
if (actionPressed) {
state = (selection == 0) ? DISPLAY_AA : DISPLAY_BB;
delay(200); // デバウンス
}
break;
case SELECT_AA:
displaySelectAA();
if (selectPressed) {
selection = 1;
state = SELECT_BB;
delay(200); // デバウンス
}
if (actionPressed) {
state = DISPLAY_AA;
delay(200); // デバウンス
}
break;
case SELECT_BB:
displaySelectBB();
if (selectPressed) {
selection = 0;
state = SELECT_AA;
delay(200); // デバウンス
}
if (actionPressed) {
state = DISPLAY_BB;
delay(200); // デバウンス
}
break;
case DISPLAY_AA:
displayHelloWorld();
if (actionPressed) {
state = MAIN_MENU;
delay(200); // デバウンス
}
break;
case DISPLAY_BB:
displayImages();
if (actionPressed) {
state = MAIN_MENU;
delay(200); // デバウンス
}
break;
}
}
void displayMainMenu() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 10, "MENU");
u8g2.drawStr(20, 30, "AA");
u8g2.drawStr(20, 50, "BB");
if (selection == 0) {
u8g2.drawStr(0, 30, "▶AA");
} else {
u8g2.drawStr(0, 50, "▶BB");
}
u8g2.sendBuffer();
}
void displaySelectAA() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 10, "MENU");
u8g2.drawStr(0, 30, "▶AA");
u8g2.drawStr(20, 50, "BB");
u8g2.sendBuffer();
}
void displaySelectBB() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 10, "MENU");
u8g2.drawStr(20, 30, "AA");
u8g2.drawStr(0, 50, "▶BB");
u8g2.sendBuffer();
}
void displayHelloWorld() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 30, "Hello World!");
u8g2.sendBuffer();
}
void displayImages() {
u8g2.clearBuffer();
u8g2.drawXBMP(0, 0, 16, 16, mon_bmp);
u8g2.drawXBMP(16, 0, 16, 16, tue_bmp);
u8g2.drawXBMP(32, 0, 16, 16, wed_bmp);
u8g2.drawXBMP(48, 0, 16, 16, ttt_bmp);
u8g2.sendBuffer();
}