/**
ESP32 Circuit Connections
-------------------
+---------+-------------------------+
| ESP32 | Connected to |
+---------+-------------------------+
| GPIO0 | Boot Button |
| GPIO1 | TX0 (UART0) |
| GPIO2 | N/C G-PIO |
| GPIO3 | RX0 (UART0) |
| GPIO4 | N/C G-PIO |
| GPIO5 | N/C G-PIO |
| GPIO12 | G-PIO (Rot Encoder SW) |
| GPIO13 | G-PIO (Rot Encoder DT) | _B_PIN
| GPIO14 | G-PIO (Rot Encoder CLK)| _A_PIN
| GPIO15 | N/C G-PIO |
| GPIO16 | N/C G-PIO |
| GPIO17 | N/C G-PIO |
| GPIO18 | SPI CLK |
| GPIO19 | SPI MISO |
| GPIO21 | I2C (SDA OLED) |
| GPIO22 | I2C (SCL OLED) |
| GPIO23 | SPI MOSI |
| GPIO25 | N/C G-PIO |
| GPIO26 | N/C G-PIO |
| GPIO27 | N/C G-PIO |
| GPIO32 | N/C ADC1_CH4 |
| GPIO33 | N/C ADC1_CH5 |
| GPIO34 | N/C ADC1_CH6(input only)|
| GPIO35 | N/C ADC1_CH7(input only)|
| GPIO36 | N/CADC1_CH0(input only) |
| GPIO39 | ADC1_CH3(input only) |
| 3V3 | Power (3.3V) |
| GND | Ground |
| EN | Enable (Reset if GND) |
| VIN | Input Voltage (5V-12V) |
+---------+-------------------------+
**/
#include <Arduino.h>
#include <AiEsp32RotaryEncoder.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
// Definición de pines para el encoder rotativo y el botón
#define ROTARY_ENCODER_A_PIN 14
#define ROTARY_ENCODER_B_PIN 13
#define ROTARY_ENCODER_BUTTON_PIN 12
#define ROTARY_ENCODER_VCC_PIN -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Inicialización del encoder rotativo
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN,
ROTARY_ENCODER_B_PIN,
ROTARY_ENCODER_BUTTON_PIN,
ROTARY_ENCODER_VCC_PIN,
4);
int currentMenuOption = 0;
const char* menuOptions[] = {"opcion1", "opcion2", "opcion3"};
const int numOptions = sizeof(menuOptions) / sizeof(menuOptions[0]);
void setup() {
Serial.begin(115200);
//Serial.println("Hello, ESP32!");
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
rotaryEncoder.begin();
rotaryEncoder.setup([] { rotaryEncoder.readEncoder_ISR(); });
rotaryEncoder.setBoundaries(0, numOptions - 1, false);
updateMenu();
}
void loop() {
// Leer la posición del encoder rotativo
int newPosition = rotaryEncoder.readEncoder();
if (newPosition != currentMenuOption) {
currentMenuOption = newPosition;
updateMenu();
}
if (rotaryEncoder.isEncoderButtonClicked()) {
executeMenuOption(currentMenuOption);
}
delay(100);
}
void updateMenu() {
display.clearDisplay();
for (int i = 0; i < numOptions; i++) {
if (i == currentMenuOption) {
// Invertir colores para la opción seleccionada
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Texto negro sobre fondo blanco
} else {
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); // Texto blanco sobre fondo negro
}
display.setCursor(0, i * 20); // Configurar posición del cursor (ajustar según el tamaño del texto)
display.println(menuOptions[i]);
}
display.display();
}
void executeMenuOption(int option) {
Serial.print("Selected: ");
// Agregar funcionalidad para cada opción
switch (option) {
case 0:
Serial.println("Selected: OPC1");
// Agrega la funcionalidad para opción 1 aquí
break;
case 1:
Serial.println("Selected: OPC2");
// Agrega la funcionalidad para opción 2 aquí
break;
case 2:
Serial.println("Selected: OPC3");
// Agrega la funcionalidad para opción 3 aquí
break;
default:
Serial.println("Invalid option");
break;
}
}