#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);
// Botones
const int btnVolver = 25;
const int btnArriba = 26;
const int btnAbajo = 27;
const int btnSeleccionar = 14;
// Menú principal
const char* menuItems[] = {"Calc", "Dev"};
int menuLength = sizeof(menuItems)/sizeof(menuItems[0]);
int currentSelection = 0;
// Estado de pantalla
enum Screen { MENU, CALC, DEV };
Screen currentScreen = MENU;
// Texto para Dev
const char* devText[] = {
"ESP32 NodeMCU-32S:",
"WiFi + Bluetooth",
"Consumo ultra bajo",
"Microcontrolador dual-core",
"Compatible con Arduino IDE",
"Admite deep sleep",
"Ideal IoT y proyectos embebidos",
"OLED 128x64 con I2C"
};
int devLines = sizeof(devText)/sizeof(devText[0]);
int devScroll = 0;
// Calculadora
String input1 = "";
String input2 = "";
char oper = '\0';
bool enteringSecond = false;
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Fallo al iniciar pantalla OLED"));
for(;;);
}
pinMode(btnVolver, INPUT_PULLUP);
pinMode(btnArriba, INPUT_PULLUP);
pinMode(btnAbajo, INPUT_PULLUP);
pinMode(btnSeleccionar, INPUT_PULLUP);
drawMenu();
}
void loop() {
// Botón Arriba
if(digitalRead(btnArriba) == LOW){
if(currentScreen == MENU){
currentSelection--;
if(currentSelection < 0) currentSelection = menuLength - 1;
drawMenu();
} else if(currentScreen == DEV){
if(devScroll > 0) devScroll--;
drawDev();
} else if(currentScreen == CALC){
incrementCurrent();
drawCalc();
}
delay(150);
}
// Botón Abajo
if(digitalRead(btnAbajo) == LOW){
if(currentScreen == MENU){
currentSelection++;
if(currentSelection >= menuLength) currentSelection = 0;
drawMenu();
} else if(currentScreen == DEV){
if(devScroll < devLines - 4) devScroll++;
drawDev();
} else if(currentScreen == CALC){
decrementCurrent();
drawCalc();
}
delay(150);
}
// Botón Volver
if(digitalRead(btnVolver) == LOW){
if(currentScreen == CALC){
input1 = "";
input2 = "";
oper = '\0';
enteringSecond = false;
drawCalc();
} else {
currentScreen = MENU;
drawMenu();
}
delay(150);
}
// Botón Seleccionar
if(digitalRead(btnSeleccionar) == LOW){
if(currentScreen == MENU){
if(currentSelection == 0){ // Calc
currentScreen = CALC;
input1 = "";
input2 = "";
oper = '\0';
enteringSecond = false;
drawCalc();
} else if(currentSelection == 1){ // Dev
currentScreen = DEV;
devScroll = 0;
drawDev();
}
} else if(currentScreen == CALC){
handleCalcSelect();
}
delay(150);
}
}
// ------------------- DIBUJOS -------------------
void drawMenu(){
display.clearDisplay();
display.setTextSize(1);
for(int i=0; i<menuLength; i++){
if(i == currentSelection)
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
else
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, i*10);
display.println(menuItems[i]);
}
display.display();
}
void drawDev(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
for(int i=0; i<4; i++){
int idx = i + devScroll;
if(idx < devLines){
display.setCursor(0, i*12);
display.println(devText[idx]);
}
}
display.display();
}
void drawCalc(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Calc:");
display.setCursor(0,12);
display.print(input1);
if(oper != '\0') display.print(oper);
if(enteringSecond) display.print(input2);
display.display();
}
// ------------------- CALCULADORA -------------------
void handleCalcSelect(){
if(!enteringSecond){
if(input1.length() == 0) input1 = "0";
oper = '+';
enteringSecond = true;
} else if(input2.length() == 0){
input2 = "0";
} else {
// Calcular
float n1 = input1.toFloat();
float n2 = input2.toFloat();
float result = 0;
switch(oper){
case '+': result = n1 + n2; break;
case '-': result = n1 - n2; break;
case '*': result = n1 * n2; break;
case '/': result = n2 != 0 ? n1 / n2 : 0; break;
}
input1 = String(result);
input2 = "";
oper = '\0';
enteringSecond = false;
}
drawCalc();
}
// Cambia el dígito actual en input1 o input2
void incrementCurrent(){
if(!enteringSecond){
if(input1.length() == 0) input1 = "0";
int val = input1.toInt();
val = (val + 1) % 1000; // máximo 999
input1 = String(val);
} else {
if(input2.length() == 0) input2 = "0";
int val = input2.toInt();
val = (val + 1) % 1000;
input2 = String(val);
}
}
void decrementCurrent(){
if(!enteringSecond){
if(input1.length() == 0) input1 = "0";
int val = input1.toInt();
val = (val - 1 + 1000) % 1000;
input1 = String(val);
} else {
if(input2.length() == 0) input2 = "0";
int val = input2.toInt();
val = (val - 1 + 1000) % 1000;
input2 = String(val);
}
}