#include "menus.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define upButton 10
#define downButton 11
#define selectButton 12
#define DITEKAN LOW
#define BOUNCE 100
#define LINELENGTH 21
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
bool runOnce = true;
// ---------------------------------------------------------------------------------------------------
void printFourLines( PGM_P base, int line = 0 ) { // displays 4 lines from selected menu stored in flash
// default starting line is the first line
base += line * LINELENGTH; // starting with specified line
lcd.clear();
for ( int k = 0; k < 4; k++ ) { // up to you to make sure
lcd.setCursor(0, k); // that it does not display past end of menu
for ( int i = 0; i < LINELENGTH-1; i++ ) {
lcd.print((char) pgm_read_byte( base + i) );
}
base += LINELENGTH;
}
}
// ---------------------------------------------------------------------------------------------------
void initScreen() {
for (int i = 0; i < introLineCount; i += 4) { // introLineCount is calculated at compile time and
printFourLines( intro[0], i ); delay(500); // a constant is inserted
}
}
// ---------------------------------------------------------------------------------------------------
void menuPERINGATAN() {
printFourLines( periMenu[0] ); // display 4 lines from menu
delay(3000);
lcd.clear();
}
// ---------------------------------------------------------------------------------------------------
void menuSETTING() {
int line = 0;
bool update = true;
while (1) {
if (update) { // update screen only when it changes
printFourLines( settingsMenu[0], line ); // display 4 lines from menu
update = false;
}
else {
switch (customKeypad.getKey()) {
case 'A': line--; if (line < 0) line = 4; update = true; break;
case 'B': line++; if (line > 4) line = 0; update = true; break;
case '1': submenuCONFIGMODE(); break;
case '2': submenuSTATUSSENSOR(); break;
case '3': submenuMANUALOUTPUT(); break;
case '4': submenuTIMECHECK(); break;
case '5': submenuWIFICHECK(); break;
case '0': runOnce = true; return; break;
}
}
}
}
// ---------------------------------------------------------------------------------------------------
void submenuCONFIGMODE() {
printFourLines( configMenu[0] ); // display 4 lines from menu
while(1) {
switch (customKeypad.getKey()) {
case '0': menuSETTING(); break;
}
}
}
// ---------------------------------------------------------------------------------------------------
void submenuSTATUSSENSOR() {
int line = 0;
bool update = true;
while (1) {
if (update) { // update screen only when it changes
printFourLines( statusMenu[0], line ); // display 4 lines from menu
update = false;
}
else {
switch (customKeypad.getKey()) {
case 'A': line--; if (line < 0) line = 5; update = true; break; // scroll down
case 'B': line++; if (line > 5) line = 0; update = true; break; // scroll up
case '0': menuSETTING(); break;
}
}
}
}
// ---------------------------------------------------------------------------------------------------
void submenuMANUALOUTPUT() {
int line = 0;
bool update = true;
while (1) { // loop
if (update) { // update screen only when it changes
printFourLines( manualMenu[0], line ); // display 4 lines from menu, starting at (line)
update = false;
}
else {
switch (customKeypad.getKey()) {
case 'A': line--; if (line < 0) line = 4; update = true; break;
case 'B': line++; if (line > 4) line = 0; update = true; break;
case '0': menuSETTING(); break;
}
}
}
}
// ---------------------------------------------------------------------------------------------------
void submenuTIMECHECK() {
printFourLines( timeMenu[0] ); // display 4 lines from menu
while(1) {
switch (customKeypad.getKey()) {
case '0': menuSETTING(); break;
}
}
}
// ---------------------------------------------------------------------------------------------------
void submenuWIFICHECK() {
printFourLines( WiFiMenu[0] ); // display 4 lines from menu
while(1) {
switch (customKeypad.getKey()) {
case '0': menuSETTING(); break;
}
}
}
// ---------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
lcd.init();
lcd.backlight();
initScreen();
}
// ---------------------------------------------------------------------------------------------------
void loop() {
//char customKey = customKeypad.getKey();
if (runOnce) {
printFourLines( loopMenu[0] ); // display 4 lines from menu
runOnce = false;
}
switch (customKeypad.getKey()) {
case '0':
menuSETTING();
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'B':
case 'C':
case 'D':
menuPERINGATAN();
break;
}
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print(F("Times : 10:30"));
// lcd.setCursor(0, 1);
// lcd.print(F("Days : Thursday"));
// lcd.setCursor(0, 2);
// lcd.print(F("Months : September"));
// lcd.setCursor(0, 3);
// lcd.print(F("Years : 2023"));
// delay(5000);
// lcd.clear();
}