#include <LiquidCrystal_I2C.h>
#include "Button.h"
#include "ScreenDisplay.h"
void incrSelection(int);
void moveDirection();
void goBack();
const int LEFT_BUTTON_PIN = 7;
const int UP_BUTTON_PIN = 8;
const int DOWN_BUTTON_PIN = 12;
const int RIGHT_BUTTON_PIN = 13;
const int DISPLAY_PERIOD = 20;
LiquidCrystal_I2C lcd(0x27,16,2);
long int lastDisplayUpdate = 0;
// MENU SETUP
GeneralDisplay display1(nullptr, true);
ScreenDisplay* currentDisplay = nullptr;
DisplayItem display1Items[] = {
DisplayItem("Item1"),
DisplayItem("Item2"),
DisplayItem("Item3"),
DisplayItem("Item4"),
DisplayItem("Item5"),
DisplayItem("Item6")
};
// BUTTON SETUP
Button b1(LEFT_BUTTON_PIN, []() -> void {//goBack();
} );
Button b2(UP_BUTTON_PIN, []() -> void {//incrSelection(-1);
currentDisplay->moveUp();
} );
Button b3(DOWN_BUTTON_PIN, []() -> void {//incrSelection(1);
currentDisplay->moveDown();
} );
Button b4(RIGHT_BUTTON_PIN, []() -> void {//moveDirection();
} );
void setup() {
// LCD Setup
lcd.init();
lcd.backlight();
// Serial Setup
Serial.begin(115200);
// button setup
b1.init();
b2.init();
b3.init();
b4.init();
// menu setup
display1.init(display1Items, sizeof(display1Items)/sizeof(DisplayItem));
currentDisplay = &display1;
//Serial.println(currentDisplay->getLine1());
//Serial.println(currentDisplay->getLine2());
}
void loop() {
b1.update();
b2.update();
b3.update();
b4.update();
if (millis() - lastDisplayUpdate >= DISPLAY_PERIOD) {
display();
}
}
// void incrSelection(int incr) {
// cursor += incr;
// // if cursor pushes over second line
// if (cursor > 1) {
// cursor = 1;
// // add increment, checking within bounds
// selection = selection >= (numTitles-2) ? numTitles-2 : selection += incr;
// }
// // if cursor pushes behind first line
// else if (cursor < 0) {
// cursor = 0;
// // add increment, checking within bounds
// selection = selection <= 0 ? 0 : selection += incr;
// }
// }
// void moveDirection() {
// // find address of new menu
// Menu* newMenu = currentMenu->getItems()[selection+cursor].getDirection();
// goToMenu(newMenu);
// }
// void goBack() {
// // get parent of menu
// Menu* parent = currentMenu->getParent();
// goToMenu(parent);
// }
// void goToMenu(Menu* dest) {
// // check destination exists
// if (dest == nullptr) { return; }
// // update current menu to new menu
// currentMenu = dest;
// // update numTitles
// numTitles = currentMenu->getSize();
// // reset cursor / selection
// cursor = 0;
// selection = 0;
// }
void display() {
// calculate update period
long int updatePeriod = millis() - lastDisplayUpdate;
char line1[17];
char line2[17];
// update line data
snprintf(line1, sizeof(line1), "%-16s", currentDisplay->getLine1());
snprintf(line2, sizeof(line2), "%-16s", currentDisplay->getLine2());
// print
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
lastDisplayUpdate = millis();
}