// Scrolling API Menu V3.1
#include <WiFi.h>
#include <U8g2lib.h>
#include "config.h" // your ssid/password
// OLED object (same as your environment)
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
// Buttons (same pins you used before)
#define BUTTON_UP_PIN 27
#define BUTTON_SELECT_PIN 25
#define BUTTON_DOWN_PIN 26
// Menu
const int NUM_ITEMS = 5;
const char *menu_item[NUM_ITEMS] = {
"Sunrise Sunset",
"Dad Jokes",
"Useless Facts",
"Quotes",
"Chuck Jokes"
};
// === Start OLED menu variables ===
int item_selected = 0;
int item_sel_previous = 0;
int item_sel_next = 0;
// button edge flags
int button_up_clicked = 0;
int button_select_clicked = 0;
int button_down_clicked = 0;
// screen state
int current_screen = 0; // 0 = menu, 1 = selected-item screen
// === End OLED menu variables ===
void setup() {
Serial.begin(115200);
// WiFi connect (blocking) - keep this in setup
// === Start WiFi connection ===
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi IP: ");
Serial.println(WiFi.localIP());
// === End WiFi connection ===
// OLED init
// === Start u8g2 setup ===
u8g2.begin();
u8g2.setFontMode(0);
// === End u8g2 setup ===
// === Button setup ===
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP);
// === End button setup ===
}
void loop() {
// === handle UP/DOWN navigation (only when on menu) ===
if (current_screen == 0) {
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) {
item_selected--;
button_up_clicked = 1;
if (item_selected < 0) item_selected = NUM_ITEMS - 1;
}
if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) {
item_selected++;
button_down_clicked = 1;
if (item_selected >= NUM_ITEMS) item_selected = 0;
}
if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) {
button_up_clicked = 0;
}
if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) {
button_down_clicked = 0;
}
}
// === SELECT toggles between menu <-> item screen ===
if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) {
button_select_clicked = 1;
if (current_screen == 0) {
current_screen = 1; // enter selected-item screen
} else {
current_screen = 0; // go back to menu
}
}
if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) {
button_select_clicked = 0;
}
// === compute prev/next for menu display ===
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) item_sel_previous = NUM_ITEMS - 1;
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) item_sel_next = 0;
// --- Draw screen (one page cycle)
u8g2.firstPage();
do {
if (current_screen == 0) {
// Menu display (simple - 3 items: prev, selected, next)
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 15, menu_item[item_sel_previous]);
u8g2.setFont(u8g2_font_7x14B_tf); // bold for selected
u8g2.drawStr(25, 37, menu_item[item_selected]);
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(26, 59, menu_item[item_sel_next]);
// small scrollbar indicator (right side)
u8g2.drawFrame(120, 0, 8, 64);
int handleY = (64 / NUM_ITEMS) * item_selected;
u8g2.drawBox(121, handleY, 6, (64 / NUM_ITEMS));
}
else { // current_screen == 1
// Show immediate feedback that SELECT was pressed (no HTTP yet)
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 20, "HTTP request...");
u8g2.drawStr(0, 40, "Press SELECT to return");
}
} while (u8g2.nextPage());
delay(20); // small delay to ease CPU (keeps responsiveness)
}