// LCD MANAGEMENT WITH https://github.com/duinoWitchery/hd44780/tree/master
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
// RTC MANAGEMENT WITH https://github.com/adafruit/RTClib
#include <RTClib.h>
RTC_DS1307 rtc;
// BUTTON MANAGEMENT WITH https://github.com/Dlloydev/Toggle
#include <Toggle.h>
struct Button {
char * name;
byte pin;
Toggle button;
Button(const char * n, const byte p) : name(n), pin(p), button(p) {}
void begin() {
button.begin(pin);
}
};
enum {SELECT, LEFT, UP, DOWN, RIGHT, RESET}; // the indexes in the buttons array
Button buttons[] = {
{"select", 4},
{"left", 5},
{"up", 6},
{"down", 7},
{"right", 8},
{"reset", 9}
};
enum {STANDARD, CONFIG} mode = STANDARD;
DateTime configDate;
void configure() {
for (auto&b : buttons) b.begin();
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
int result = lcd.begin(LCD_COLS, LCD_ROWS);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
}
void displayTime() {
static char prevTime[9]; // "HH:MM:SS";
static char prevDate[9]; // "HH:MM:SS";
char currentTime[9]; // "HH:MM:SS";
char currentDate[9]; // "DD/MM/YY";
DateTime now = rtc.now();
snprintf(currentTime, sizeof currentTime, "%02:%02:%02", now.hour(), now.minute(), now.second());
snprintf(currentDate, sizeof currentDate, "%02/%02/%02", now.day(), now.month(), now.year());
Serial.print(currentTime); Serial.write(' '); Serial.println(prevTime);
if (strcmp(currentTime, prevTime) != 0) {
lcd.setCursor(0, 0);
lcd.print(currentTime);
strcpy(currentTime, prevTime);
}
if (strcmp(currentDate, prevDate) != 0) {
lcd.setCursor(0, 1);
lcd.print(currentDate);
strcpy(currentDate, prevDate);
}
}
void poll() {
for (auto&b : buttons) b.button.poll();
}
void setup() {
configure();
}
void loop() {
poll();
if (mode == STANDARD) {
displayTime();
if (buttons[SELECT].button.onPress()) {
configDate = rtc.now();
lcd.setCursor(1, 0); // blink cursor under hour
lcd.blink();
mode = CONFIG;
}
else if (mode == CONFIG) {
}
else {
// we will never get there
}
for (auto&b : buttons) {
if (b.button.onPress()) {
Serial.print(b.name);
Serial.println(" pressed.");
}
if (b.button.onRelease()) {
Serial.print(b.name);
Serial.println(" released.");
}
}
}
}
SELECT
LEFT
UP
RIGHT
DOWN
RESET
WIRES HAVE BEEN HIDDEN TO KEEP THE SCREEN READABLE