#include <U8glib.h>
#include "anim.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // FAST I2C / TWI
#define BUTTON_UP_PIN 4 // pin for UP button
#define BUTTON_DOWN_PIN 8 // pin for DOWN button
#define BUTTON_ENTER_PIN 12 // pin for ENTER button
int item_selected = 0; // which item in the menu is selected
int item_sel_previous; // previous item - Item before the selected one
int item_sel_next; // next item - Item after the selected one
int button_up_clicked = 0;
int button_down_clicked = 0;
int button_enter_clicked = 0;
void setup() {
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_ENTER_PIN, INPUT_PULLUP);
}
void loop() {
if((digitalRead (BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // UP button is pressed
item_selected = item_selected - 1;
button_up_clicked = 1; //set button click to only perform once
if (item_selected < 0) {
item_selected = NUM_ITEMS - 1;
}
}
if((digitalRead (BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // DOWN button is pressed
item_selected = item_selected + 1;
button_down_clicked = 1; //set button click to only perform once
if (item_selected >= NUM_ITEMS) {
item_selected = 0;
}
}
if((digitalRead (BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) { // unclick
button_up_clicked = 0;
}
if((digitalRead (BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) { // unclick
button_down_clicked = 0;
}
item_sel_previous = item_selected - 1;
if(item_sel_previous < 0) {item_sel_previous = NUM_ITEMS-1;}
item_sel_next = item_selected + 1;
if(item_sel_next >= NUM_ITEMS) {item_sel_next = 0;}
u8g.firstPage();
do{
// previous item
u8g.setFont(u8g_font_7x14);
u8g.drawStr(26,15,menu_items[item_sel_previous]);
u8g.drawBitmapP(4,2,16/8,16,bitmap_icons[item_sel_previous]);
// selected item
u8g.setFont(u8g_font_7x14B);
u8g.drawStr(26,37,menu_items[item_selected]);
u8g.drawBitmapP(4,24,16/8,16,bitmap_icons[item_selected]);
// next item
u8g.setFont(u8g_font_7x14);
u8g.drawStr(26,61,menu_items[item_sel_next]);
u8g.drawBitmapP(4,48,16/8,16,bitmap_icons[item_sel_next]);
u8g.drawBitmapP(0,22,128/8,21,epd_bitmap_Selected_background);
u8g.drawBitmapP(120,0,8/8,64,epd_bitmap_Scroll_Bar);
} while ( u8g.nextPage() );
}