#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/pgmspace.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_UP 12
#define BUTTON_DOWN 4
#define BUTTON_SELECT 8
int menuIndex = 0;
bool inRunningManScreen = false;
const char* menuItems[] = {"Start", "Settings", "About", "Exit"};
const int menuValues[] = {5, 12, 8, 20};
const int menuLength = sizeof(menuItems) / sizeof(menuItems[0]);
const uint8_t rotatingImage[] PROGMEM = {
0x3C, 0x42, 0x81, 0xA5, 0x81, 0x99, 0x42, 0x3C // Example 8x8 image data
};
const uint8_t runningMan[] PROGMEM = {
0x18, 0x3C, 0x7E, 0x18, 0x18, 0x3C, 0x24, 0x42 // Example 8x8 running man icon
};
void setup() {
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
Wire.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;);
}
display.clearDisplay();
}
void loop() {
if (digitalRead(BUTTON_SELECT) == LOW) {
if (inRunningManScreen) {
inRunningManScreen = false;
} else {
executeMenuAction(menuIndex);
}
delay(200);
}
if (!inRunningManScreen) {
if (digitalRead(BUTTON_UP) == LOW) {
menuIndex = (menuIndex - 1 + menuLength) % menuLength;
delay(200);
}
if (digitalRead(BUTTON_DOWN) == LOW) {
menuIndex = (menuIndex + 1) % menuLength;
delay(200);
}
drawMenu();
} else {
drawRunningManScreen();
}
}
void drawMenu() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
for (int i = 0; i < menuLength; i++) {
if (i == menuIndex) {
display.setCursor(10, i * 10);
display.print("> ");
} else {
display.setCursor(20, i * 10);
}
display.print(menuItems[i]);
display.print(" (");
display.print(menuValues[i]);
display.print(")");
}
drawRotatingImage();
display.display();
}
void drawRotatingImage() {
static int angle = 0;
display.drawBitmap(100, 10, rotatingImage, 8, 8, SSD1306_WHITE);
angle = (angle + 90) % 360;
}
void drawRunningManScreen() {
display.clearDisplay();
display.setCursor(40, 30);
display.print("Running...");
display.drawBitmap(60, 10, runningMan, 8, 8, SSD1306_WHITE);
display.display();
}
void executeMenuAction(int index) {
if (index == 0) { // Start option selected
inRunningManScreen = true;
} else {
display.clearDisplay();
display.setCursor(10, 30);
display.print("Selected: ");
display.print(menuItems[index]);
display.print(" (");
display.print(menuValues[index]);
display.print(")");
display.display();
delay(1000);
}
}