#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
U8GLIB_SSD1306_64X48 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
// U8GLIB_SSD1306_128X64 u8g(13, 11, 8, 9, 10); // SPI connection
// for SPI connection, use this wiring:
// GND > GND
// VCC > 5V
// SCL > 13
// SDA > 11
// RES > 10
// DC > 9
// CS > 8
// all the arrays below are generated from images using image2cpp website
// scroll down to see the actual code
#define BUTTON_UP_PIN 12 // pin for UP button
#define BUTTON_SELECT_PIN 8 // pin for SELECT button
#define BUTTON_DOWN_PIN 4 // pin for DOWN button
#define DEMO_PIN 13 // pin for demo mode, use switch or wire to enable or disable demo mode, see more details below
int button_up_clicked = 0; // only perform action when button is clicked, and wait until another press
int button_select_clicked = 0; // same as above
int button_down_clicked = 0; // same as above
int item_selected = 1; // which item in the menu is selected
int item_sel_previous; // previous item - used in the menu screen to draw the item before the selected one
int item_sel_next; // next item - used in the menu screen to draw next item after the selected one
int current_screen = 0; // 0 = menu, 1 = screenshot, 2 = qr
int demo_mode = 0; // when demo mode is set to 1, it automatically goes over all the screens, 0 = control menu with buttons
int demo_mode_state = 0; // demo mode state = which screen and menu item to display
int demo_mode_delay = 0; // demo mode delay = used to slow down the screen switching
int Health_stat = 0; // initial value for the health stat
int Accuracy_stat = 0; // initial value for the health stat
int Dexterity_stat = 0; // initial value for the health stat
char Stat_values [21] [3] = { // array with item names
{ "20" },
{ "19" },
{ "18" },
{ "17" },
{ "16" },
{ "15" },
{ "14" },
{ "13" },
{ "12" },
{ "11" },
{ "10" },
{ "9" },
{ "8" },
{ "7" },
{ "6" },
{ "5" },
{ "4" },
{ "3" },
{ "2" },
{ "1" },
{ "0" }
};
void setup() {
u8g.setColorIndex(1); // set the color to white
// define pins for buttons
// INPUT_PULLUP means the button is HIGH when not pressed, and LOW when pressed
// since it´s connected between some pin and GND
pinMode(BUTTON_UP_PIN, INPUT_PULLUP); // up button
pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP); // select button
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP); // down button
pinMode(DEMO_PIN, INPUT_PULLUP);
}
void loop() {
// when pin 13 is LOW (DEMO_PIN), enable demo mode
// this could be done either by using a switch
// or simply by connecting the wire between pin 13 and GND
// (those pins are next to each other)
if (digitalRead(DEMO_PIN) == LOW) {
demo_mode = 1; // enable demo mode
}
else {
demo_mode = 0; // disable demo mode
}
if (demo_mode == 1) { // when demo mode is active, automatically switch between all the screens and menu items
demo_mode_delay++; // increase demo mode delay
if (demo_mode_delay > 15) { // after some time, switch to another screen - change this value to make it slower/faster
demo_mode_delay = 0;
demo_mode_state++; // increase counter
if (demo_mode_state >= NUM_ITEMS*3) {demo_mode_state=0;} // jump back to the first screen
}
if (demo_mode_state % 3 == 0) {current_screen = 0; item_selected = demo_mode_state/3; } // menu screen
else if (demo_mode_state % 3 == 1) {current_screen = 1; item_selected = demo_mode_state/3;} // screenshots screen
else if (demo_mode_state % 3 == 2) {current_screen = 2; item_selected = demo_mode_state/3;} // qr codes screen
} // end demo mode section
if (current_screen == 0) { // MENU SCREEN
// up and down buttons only work for the menu screen
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // up button clicked - jump to previous menu item
item_selected = item_selected - 1; // select previous item
button_up_clicked = 1; // set button to clicked to only perform the action once
if (item_selected < 0) { // if first item was selected, jump to last item
item_selected = NUM_ITEMS-1;
}
}
else if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // down button clicked - jump to next menu item
item_selected = item_selected + 1; // select next item
button_down_clicked = 1; // set button to clicked to only perform the action once
if (item_selected >= NUM_ITEMS) { // last item was selected, jump to first menu item
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;
}
}
if (current_screen == 1) { // Stat SCREEN
// up and down buttons will change the Stat value
if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // up button clicked - jump to previous menu item
if (item_selected == 0) { // if accuracy was selected, increase the value of stat
Accuracy_stat = Accuracy_stat-1;
if (Accuracy_stat < 0 ) {
Accuracy_stat == 21;
};
}
if (item_selected == 1) { // if accuracy was selected, increase the value of stat
Health_stat = Health_stat-1;
if (Health_stat < 0 ) {
Health_stat == 21;
};
}
if (item_selected == 2) { // if accuracy was selected, increase the value of stat
Dexterity_stat = Dexterity_stat-1;
if (Dexterity_stat < 0 ) {
Dexterity_stat == 21;
};
}
button_up_clicked = 1; // set button to clicked to only perform the action once
}
else if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // down button clicked - jump to next menu item
if (item_selected == 0 ) {
Accuracy_stat = Accuracy_stat+1;
if (Accuracy_stat > 21 ) {
Accuracy_stat == 0;
};
}
if (item_selected == 1 ) {
Health_stat = Health_stat+1;
if (Health_stat > 21 ) {
Health_stat == 0;
};
}
if (item_selected == 2 ) {
Dexterity_stat = Dexterity_stat+1;
if (Dexterity_stat > 21 ) {
Dexterity_stat == 0;
};
}
button_down_clicked = 1; // set button to clicked to only perform the action once
}
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;
}
}
if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) { // select button clicked, jump between screens
button_select_clicked = 1; // set button to clicked to only perform the action once
if (current_screen == 0) {current_screen = 1;} // menu items screen --> screenshots screen
else {current_screen = 0;} // qr codes screen --> menu items screen
}
if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) { // unclick
button_select_clicked = 0;
}
// set correct values for the previous and next items
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) {item_sel_previous = NUM_ITEMS - 1;} // previous item would be below first = make it the last
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) {item_sel_next = 0;} // next item would be after last = make it the first
u8g.firstPage(); // required for page drawing mode for u8g library
do {
// draw previous item as icon + label
u8g.setFont(u8g_font_5x7) ; //(u8g_font_7x14);
u8g.drawStr(22, 8, menu_items[item_sel_previous]);
u8g.drawBitmapP( 4, 0-4, 16/8, 16, bitmap_icons[item_sel_previous]);
// draw selected item as icon + label in bold font
u8g.setFont(u8g_font_5x7r); //(u8g_font_7x14B);
u8g.drawStr(22, 26, menu_items[item_selected]);
u8g.drawBitmapP( 4, 16, 16/8, 16, bitmap_icons[item_selected]);
// draw next item as icon + label
u8g.setFont(u8g_font_5x7); //(u8g_font_7x14);
u8g.drawStr(22, 45, menu_items[item_sel_next]);
u8g.drawBitmapP( 4, 32+4, 16/8, 16, bitmap_icons[item_sel_next]);
// draw stat values for each item
if (item_sel_previous == 0) {
u8g.drawStr(64-17, 8 , Stat_values[Accuracy_stat]);
}
if (item_selected == 0) {
u8g.drawStr(64-17, 26 , Stat_values[Accuracy_stat]);
}
if (item_sel_next == 0) {
u8g.drawStr(64-17, 45 , Stat_values[Accuracy_stat]);
}
if (item_sel_previous == 1) {
u8g.drawStr(64-17, 8 , Stat_values[Health_stat]);
}
if (item_selected == 1) {
u8g.drawStr(64-17, 26 , Stat_values[Health_stat]);
}
if (item_sel_next == 1) {
u8g.drawStr(64-17, 45 , Stat_values[Health_stat]);
}
if (item_sel_previous == 2) {
u8g.drawStr(64-17, 8 , Stat_values[Dexterity_stat]);
}
if (item_selected == 2) {
u8g.drawStr(64-17, 26 , Stat_values[Dexterity_stat]);
}
if (item_sel_next == 2) {
u8g.drawStr(64-17, 45 , Stat_values[Dexterity_stat]);
}
if (current_screen == 0) { // MENU SCREEN
// selected item background
//u8g.drawBitmapP(0, 22, 64/8, 21, bitmap_item_sel_outline);
u8g.drawFrame(2, 14, 64-6 , 20);
// draw scrollbar background
u8g.drawBitmapP(64-8, 0, 8/8, 64, bitmap_scrollbar_background);
// draw scrollbar handle
u8g.drawBox(61, 48/3 * item_selected, 3, 48/3);
//NUM_ITEMS * item_selected, 3, 64/NUM_ITEMS); when using all items
}
else if (current_screen == 1) { // highlight the stat value
u8g.drawFrame(43, 14, 16 , 20);
//u8g.drawStr(45, 14 , Stat_values[item_selected]);
}
} while ( u8g.nextPage() ); // required for page drawing mode with u8g library
}