#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <DHT.h> // Librería para el sensor de temperatura y humedad
#include <iconos.h>
#include <fondo.h>
// Vamos a usar bus I2C del Esp32
// SDA GPIO 21
// SCL GPIO 22
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#define DHTPIN 26 // Definimos el pin digital donde se conecta el sensor
//#define DHTTYPE DHT11 // Dependiendo del tipo de sensor
//DHT dht(DHTPIN, DHTTYPE); // Inicializamos el sensor DHT11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Inicializamos la pantalla OLED
const unsigned char barra [] PROGMEM = {
0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0, 0xe0
};
const int NUM_OBJETOS = 7; // Número de objetos de la lista
const int MAX_LONGITUD_OBJETOS = 20; //Máximo de caracteres
char objetos [NUM_OBJETOS] [MAX_LONGITUD_OBJETOS] = { // Array de nombres de los iconos
{"Temp"},
{"Notas"},
{"Anim"},
{"Bateria"},
{"Opciones"},
{"Luz"},
{"Juego"}
};
int objeto_sel = 0; // Qué objeto en el menu está seleccionado
int objeto_mostrado = 10;
int objeto_sel_anterior; // Objeto anterior
int objeto_sel_siguiente; // Objeto siguiente
#define BOTON_ARRIBA 12
#define BOTON_SELECCIONAR 13
#define BOTON_ABAJO 14
#define PIN_LED 15
#define FRAME_DELAY (20)
#define FRAME_WIDTH (64)
#define FRAME_HEIGHT (64)
#define FRAME_COUNT_gears (sizeof(gears) / sizeof(gears[0]))
#define FRAME_COUNT_sun (sizeof(sun) / sizeof(sun[0]))
int frame = 0;
int pantalla = 0;
bool saliranimaciones = false;
bool salirtemperatura = false;
bool apagarluz = false;
bool salirjuego = false;
// void sensores(){
// float h = dht.readHumidity(); // Leemos la humedad relativa
// float t = dht.readTemperature(); // Leemos la temperatura en grados centígrados (por defecto)
// // Comprobamos si ha habido algún error en la lectura
// if (isnan(h) || isnan(t)) {
// Serial.println("Error obteniendo los datos del sensor DHT11");
// return;
// }
// float inca = dht.computeHeatIndex(t, h, false); // Calcular el índice de calor en grados centígrados
// Serial.println(t);
// Serial.println(h);
// Serial.println(inca);
// }
void menu(){
display.drawBitmap(0,0,fondo_vacio,128,64,WHITE); // Dibuja el diseño del fondo sin etiquetas ni símbolos
objeto_sel_anterior = objeto_sel - 1;
if (objeto_sel_anterior < 0){objeto_sel_anterior = NUM_OBJETOS - 1;} // Generamos el menu rotativo
objeto_sel_siguiente = objeto_sel + 1;
if (objeto_sel_siguiente >= NUM_OBJETOS){objeto_sel_siguiente = 0;}
display.setCursor(25, 0);
display.println(objetos[objeto_sel_anterior]);
display.drawBitmap(6,0,bitmap_iconos[objeto_sel_anterior],16,16,WHITE);
display.setCursor(25, 24);
display.println(objetos[objeto_sel]);
display.drawBitmap(6,24,bitmap_iconos[objeto_sel],16,16,WHITE);
display.setCursor(25, 48);
display.println(objetos[objeto_sel_siguiente]);
display.drawBitmap(6,48,bitmap_iconos[objeto_sel_siguiente],16,16,WHITE);
display.drawBitmap(122,0,fondo_barra,8,64,WHITE);
for (byte i=0;i<NUM_OBJETOS;i++){
if(i == objeto_sel){
display.drawBitmap(125,i*11,barra,3,8,WHITE);
}
}
display.display(); // Con esta línea dibujamos
delay(200);
}
void juegoloop(){
display.clearDisplay();
display.setCursor(2,0);
display.drawBitmap(0,20,logotetrapolis,100,40,1);
display.display();
if (digitalRead(BOTON_SELECCIONAR) == LOW){
salirjuego = true;
}
}
void appAnimloop(){
display.clearDisplay();
display.drawBitmap(32, 0, gears[frame], FRAME_WIDTH, FRAME_HEIGHT, 1);
display.display();
frame = (frame + 1) % FRAME_COUNT_gears;
delay(FRAME_DELAY);
if (digitalRead(BOTON_SELECCIONAR) == LOW){
saliranimaciones = true;
}
}
void appTemploop(){
//sensores();
display.setCursor(3, 0);
display.print("Temp: ");
display.setCursor(3, 24);
display.print("Hum: ");
display.setCursor(3, 48);
display.print("Index: ");
display.display();
if (digitalRead(BOTON_SELECCIONAR) == LOW){
salirtemperatura = true;
}
}
void appLuzloop(){
display.clearDisplay();
display.drawBitmap(32, 0, sun[frame], FRAME_WIDTH, FRAME_HEIGHT, 1);
display.display();
frame = (frame + 1) % FRAME_COUNT_sun;
delay(FRAME_DELAY);
if (digitalRead(BOTON_SELECCIONAR) == LOW){
apagarluz = true;
}
}
void actualizar_home(){
if (digitalRead(BOTON_ARRIBA) == LOW) {
objeto_sel = (objeto_sel - 1 + NUM_OBJETOS) % NUM_OBJETOS;
updateText();
menu();
}
if (digitalRead(BOTON_ABAJO) == LOW) {
objeto_sel = (objeto_sel + 1) % NUM_OBJETOS;
updateText();
menu();
}
if (digitalRead(BOTON_SELECCIONAR) == LOW) {
if (pantalla > 1){
pantalla = 0;
}
display.clearDisplay();
display.display();
delay(400);
switch(objeto_sel){
case 0:
while(!salirtemperatura){
appTemploop();
}
salirtemperatura = false;
break;
case 1:
break;
case 2:
while(!saliranimaciones){
appAnimloop();
}
saliranimaciones = false;
break;
case 3:
break;
case 4:
break;
case 5:
digitalWrite(PIN_LED, HIGH);
while(!apagarluz){
appLuzloop();
}
apagarluz = false;
digitalWrite(PIN_LED, LOW);
break;
case 6:
while(!salirjuego){
juegoloop();
}
salirjuego = false;
}
frame = 0;
display.clearDisplay();
display.display();
pantalla = 0;
delay(300);
menu();
}
}
void updateText(){
// Esta función borra todo lo que hay en la pantalla
display.fillRect(3, 0, 116, 20, BLACK);
display.fillRect(4, 24, 116, 17, BLACK);
display.fillRect(3, 48, 116, 20, BLACK);
display.fillRect(125, 0, 3, 64, BLACK);
}
void setup() {
// Inicia el serial
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = Genera el voltaje de 3,3V internamente para la pantalla oled
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
//dht.begin();
delay(2000);
// Muestra el contenido inicial en la pantalla
// La librería muestra en un inicio el logo de Adafruit
display.display();
delay(1000);
// Limpia el Buffer de la pantalla
display.clearDisplay();
display.setTextSize(2); // Define el tamaño del texto
display.setTextColor(WHITE); // Define el color del texto
display.display(); // Actualiza la pantalla
pinMode(BOTON_ARRIBA, INPUT_PULLUP);
pinMode(BOTON_SELECCIONAR, INPUT_PULLUP);
pinMode(BOTON_ABAJO, INPUT_PULLUP);
pinMode(PIN_LED, OUTPUT);
menu();
}
void loop() {
actualizar_home();
}