/*
Red Button (4) not used in this case!
*/
#include <RTClib.h>
#include <U8g2lib.h>
#include <Bounce2.h>
byte button_pins[] = {23, 22, 24, 25}; // button pins, 23,22 = up/down, 24 = select, 25 = Home
#define NUMBUTTONS sizeof(button_pins)
Bounce * buttons = new Bounce[NUMBUTTONS];
U8X8_SSD1306_128X64_NONAME_HW_I2C display(U8X8_PIN_NONE);
RTC_DS1307 rtc;
#define MENU_SIZE_1 5 // Menu 1
char *menu1[MENU_SIZE_1] = { "Home", "Option 2", "Option 3", "Option 4", "Option 5" };
#define MENU_SIZE_HOME 2 // Home
char *home[MENU_SIZE_HOME] = { "Menu 1", "Option 2"};
String OPEN_MENU = "Home";
int cursor = 0;
int TARGET_TIME[] = {9, 48, 0}; // Alarm Time
int targethour = TARGET_TIME[0];
int targetminute = TARGET_TIME[1];
int targetsecond = TARGET_TIME[2];
const int LOCKSCREEN_DELAY = 1; // Delay until the lockscreen comes back in in Minutes after last Button press
void setup() {
Serial.begin(9600); // Start Serial Monitor
for (int i = 0; i < NUMBUTTONS; i++) {
buttons[i].attach( button_pins[i], INPUT_PULLUP); // setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms
}
if ( ! display.begin()) { // Start Display
Serial.println("Couldn't find Display");
Serial.flush();
abort(); // Stop the Program
} else {
display.setPowerSave(0);
display.setFont(u8x8_font_pxplusibmcgathin_f);
}
if (! rtc.begin()) { // Start Conection to RTC
Serial.println("Couldn't find RTC");
Serial.flush();
abort(); // Stop the Program
}
showLockScreen();
}
int hour;
int minute;
int second;
int CurrentTimecodecooldown = 0;
int CurrentTimecode = 0;
void loop() {
DateTime now = rtc.now(); // Setting up RTC
hour = now.hour();
minute = now.minute();
second = now.second();
CurrentTimecode = hour * 60 + minute;
if (targethour == hour && targetminute == minute && targetsecond == second) {
Alarm();
}
for (int i = 0; i < NUMBUTTONS; i++) {
buttons[i].update(); // Update the Bounce instance
if ( buttons[i].fell() ) { // If it fell
if (OPEN_MENU == "Lockscreen") {
showHome(); // show Home if last window was Lockscreen
CurrentTimecodecooldown = CurrentTimecode + LOCKSCREEN_DELAY; // Update Delay
} else { // Do not execute the buttons when the menu is not showen
CurrentTimecodecooldown = CurrentTimecode + LOCKSCREEN_DELAY; // Update Delay
if (i == 3) {
if (OPEN_MENU != "Home") {
showHome(); // show Home if it is not already open
}
}
else if (i == 2) {
// goto select
button_select();
} else {
// erase previous cursor:
display.setCursor(0, cursor);
display.print(' ');
if (i == 0) { // up
button_up();
}
else { // down
button_down();
}
// show cursor at new line:
display.setCursor(0, cursor);
display.print('>');
}
}
} else if (CurrentTimecodecooldown <= CurrentTimecode && OPEN_MENU != "Lockscreen") {
showLockScreen(); // Show Lockscreen
}
}
}
void button_select() {
if ( OPEN_MENU == "Home") {
//execute choise
if (cursor == 0) {
showMenu1();
}
//print selection
display.clearLine(7);
display.setCursor(0, 7);
display.print(">>");
display.print(home[cursor]);
} else if (OPEN_MENU == "Menu_1") {
// execute choice
if (cursor == 0) {
showHome();
}//else if (cursor == 1) {}
display.clearLine(7);
display.setCursor(0, 7);
display.print(">>");
display.print(menu1[cursor]);
}
}
void button_up() {
cursor++;
if (OPEN_MENU == "Home") {
if (cursor > (MENU_SIZE_HOME - 1)) cursor = 0;
} else if (OPEN_MENU == "Menu_1") {
if (cursor > (MENU_SIZE_1 - 1)) cursor = 0;
}
}
void button_down() {
cursor--;
if (OPEN_MENU == "Home") {
if (cursor < 0) cursor = (MENU_SIZE_HOME - 1);
} else if (OPEN_MENU == "Menu_1") {
if (cursor < 0) cursor = (MENU_SIZE_1 - 1);
}
}
// Menus (OLED - Display)
void showMenu1() {
display.clearDisplay();
Serial.println("Opening Menu1");
OPEN_MENU = "Menu_1";
// show menu items:
for (int i = 0; i < MENU_SIZE_1; i++) {
display.drawString(2, i, menu1[i]);
}
display.setCursor(0, 0);
display.print('>');
}
void showHome() {
display.clearDisplay();
Serial.println("Opening Home");
OPEN_MENU = "Home";
// show menu items:
for (int i = 0; i < MENU_SIZE_HOME; i++) {
display.drawString(2, i, home[i]);
}
display.setCursor(0, 0);
display.print('>');
}
void showLockScreen() {
display.clearDisplay();
Serial.println("Opening Lockscreen");
OPEN_MENU = "Lockscreen";
cursor = 0;
display.setCursor(2, 2);
display.print("Hey,");
display.setCursor(2, 3);
display.print("How are You?");
}
void Alarm() {
Serial.print("Alarm");
display.clearDisplay();
OPEN_MENU = "Alarm";
display
}