#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "bg.h" // 128x64px imagen fullscreen
#include "icons.h" // 16x16px iconos
#define SCREEN_WIDTH 128 // OLED pixeles Ancho
#define SCREEN_HEIGHT 64 // OLED pixeles Alto
// Declaraciones SSD1306 display I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const unsigned char scrollbar_empty [] PROGMEM = { 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40 }; // image for scrollbar background 3x8px
const unsigned char scrollbar_filled [] PROGMEM = { 0x00, 0x40, 0xe0, 0xe0, 0xe0, 0xe0, 0x40, 0x00}; // image for scrollbar handle 3x8px
const int NUM_ITEMS = 8; // Numero de items de la lista
const int MAX_ITEM_LENGTH = 20; // Maximo de caracteres
char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = { // array de nombres
{ "3D Cubo" },
{ "Bateria" },
{ "Dashb" },
{ "Fuego" },
{ "GPS" },
{ "Grande" },
{ "ParQ" },
{ "Turbo" }
};
int item_selected = 0; // cual item en el menu esta seleccionado
int item_selected_displayed = 10; // variable para no redibujar siempre la pantalla
int item_sel_previous; // item anterior
int item_sel_next; // siguiente item
#define BUTTON_UP_PIN 7 // Boton Arriba
#define BUTTON_SELECT_PIN 6 // Boton Seleccionar
#define BUTTON_DOWN_PIN 5 // Boton Abajo
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.drawBitmap(0, 0, epd_bitmap_bg_no_labels, 128, 64, WHITE);
display.display();
pinMode(BUTTON_UP_PIN, INPUT_PULLUP); // Boton Arriba
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP); // Boton Seleccionar
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP); // Boton Abajo
}
void loop() {
if (digitalRead(BUTTON_UP_PIN) == LOW) { // Boton Arriba es presionado
item_selected = (item_selected - 1 + NUM_ITEMS) % NUM_ITEMS; // switch to the previous item
updateText();
}
if (digitalRead(BUTTON_DOWN_PIN) == LOW) { // Boton Abajo es presionado
item_selected = (item_selected + 1) % NUM_ITEMS; // cambiar al siguiente item
updateText();
}
if (digitalRead(BUTTON_SELECT_PIN) == LOW) { // Boton seleccionado es presionado
//
}
if (item_selected_displayed != item_selected) { // solo redibuja si el item es diferente al anterior
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) {item_sel_previous = NUM_ITEMS - 1;} //para genereal el menu rotativo
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) {item_sel_next = 0;} // para genereal el menu rotativo
display.setCursor(25, 0);
display.println(menu_items[item_sel_previous]);
display.drawBitmap(6, 0, bitmap_icons[item_sel_previous], 16,16 , WHITE);
display.setCursor(25, 24);
display.println(menu_items[item_selected]);
display.drawBitmap(6, 24, bitmap_icons[item_selected], 16,16 , WHITE);
display.setCursor(25, 48);
display.println(menu_items[item_sel_next]);
display.drawBitmap(6, 48, bitmap_icons[item_sel_next], 16,16 , WHITE);
// dibujar scrollbar
for (byte i=0; i<8; i++) { // for 8 objetos
if (i == item_selected) {
display.drawBitmap(125, i*8, scrollbar_filled, 3,8 , WHITE);
} else {
display.drawBitmap(125, i*8, scrollbar_empty, 3,8 , WHITE);
}
}
display.display();// metodo que necesita la libreria para dibujar
item_selected_displayed = item_selected; // actualizar el item desplegado
}
}
void updateText(){
display.fillRect(3, 0, 115, 20, BLACK);
display.fillRect(3, 24, 115, 20, BLACK);
display.fillRect(3, 48, 115, 20, BLACK);
display.fillRect(125, 0, 3, 64, BLACK);
}