#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define K_FIRE 13
#define K_RIGHT 27
#define K_DOWN 14
#define K_LEFT 25
#define K_UP 26
String menuItems[] = {"StarWars", "Doom", "PacMan", "Pingpong", "FlappyBird", "Osci", "Counter", "Logic I/O", "WiFi"};
int currentMenuIndex = 0;
int totalMenuItems = sizeof(menuItems) / sizeof(menuItems[0]);
extern bool inCounterMode;
extern bool inStarWarsMode;
extern bool inDoomMode;
void setup() {
pinMode(PIN_, INPUT_PULLUP);
pinMode(PIN_START_BUTTON, INPUT_PULLUP);
pinMode(PIN_UP_BUTTON, INPUT_PULLUP);
pinMode(PIN_DOWN_BUTTON , INPUT_PULLUP);
pinMode(PIN_RIGHT_BUTTON, INPUT_PULLUP);
pinMode(PIN_LEFT_BUTTON, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(2); // Larger font size
display.setTextColor(WHITE); // White text
display.setTextWrap(false); // No text wrapping
display.display();
}
void loop() {
if (inStarWarsMode) {
starWarsLoop();
} else if (inCounterMode) {
counterLoop();
} else if (inDoomMode) {
doomLoop();
} else {
if (digitalRead(BUTTON_UP) == LOW) {
currentMenuIndex--;
if (currentMenuIndex < 0) currentMenuIndex = totalMenuItems - 1;
delay(200);
}
if (digitalRead(BUTTON_DOWN) == LOW) {
currentMenuIndex++;
if (currentMenuIndex >= totalMenuItems) currentMenuIndex = 0;
delay(200);
}
if (digitalRead(BUTTON_SELECT) == LOW) {
// Handle menu selection
handleMenuSelection(currentMenuIndex);
delay(200);
}
displayMenu();
}
}
void displayMenu() {
display.clearDisplay();
int startIdx = currentMenuIndex - (currentMenuIndex % 3);
for (int i = 0; i < 3; i++) {
if (startIdx + i >= totalMenuItems) break;
if (startIdx + i == currentMenuIndex) {
display.setTextColor(BLACK, WHITE); // Highlight selection
} else {
display.setTextColor(WHITE, BLACK);
}
// Calculate the X position to center the text (justify)
int16_t x1, y1;
uint16_t width, height;
display.getTextBounds(menuItems[startIdx + i], 0, 0, &x1, &y1, &width, &height);
int xPos = (SCREEN_WIDTH - width) / 2;
display.setCursor(xPos, i * 20); // Spacing between menu items
display.println(menuItems[startIdx + i]);
}
display.display();
}
void handleMenuSelection(int index) {
switch (index) {
case 0:
enterStarWarsMode();
break;
case 1:
// Jalankan Doom
enterDoomMode();
break;
case 2:
// Jalankan PacMan
break;
case 3:
// Jalankan Pingpong
break;
case 4:
// Jalankan FlappyBird
break;
case 5:
// Jalankan Osci
break;
case 6:
enterCounterMode();
break;
case 7:
// Jalankan Logic I/O
break;
case 8:
// Jalankan WiFi
break;
}
}