#include <WiFi.h>
#include <U8g2lib.h>
#include "config.h" // WiFi SSID and password stored here
// OLED setup
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
// Button pins
#define BUTTON_UP_PIN 27
#define BUTTON_SELECT_PIN 25
#define BUTTON_DOWN_PIN 26
// Track button state
bool lastUpState = HIGH;
bool lastDownState = HIGH;
bool lastSelectState = HIGH;
int current_screen = 0;
void setup() {
Serial.begin(115200);
// Initialize OLED
u8g2.begin();
// Buttons
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP);
// WiFi connect (blocking) - keep this in setup
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi IP: ");
Serial.println(WiFi.localIP());
// Initial screen
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(0, 10, "Boot OK");
} while (u8g2.nextPage());
}
void loop() {
// Read buttons
bool upState = digitalRead(BUTTON_UP_PIN);
bool downState = digitalRead(BUTTON_DOWN_PIN);
bool selectState = digitalRead(BUTTON_SELECT_PIN);
// Handle UP button
if (lastUpState == HIGH && upState == LOW) {
current_screen++;
if (current_screen > 2) current_screen = 0;
}
// Handle DOWN button
if (lastDownState == HIGH && downState == LOW) {
current_screen--;
if (current_screen < 0) current_screen = 2;
}
// Handle SELECT button
if (lastSelectState == HIGH && selectState == LOW) {
Serial.println("Select pressed");
}
// Save button states
lastUpState = upState;
lastDownState = downState;
lastSelectState = selectState;
// Render screen
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB08_tr);
if (current_screen == 0) {
u8g2.drawStr(0, 20, "Screen 0");
} else if (current_screen == 1) {
u8g2.drawStr(0, 20, "Screen 1");
} else if (current_screen == 2) {
u8g2.drawStr(0, 20, "Screen 2");
}
} while (u8g2.nextPage());
delay(100);
}