#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <Keypad.h>
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip
const byte ROWS = 4; // Four rows
const byte COLS = 8; // columns
// Define the Keymap
char hexaKeys[ROWS][COLS] = {
{'A', 'B', '1', '2', '3', 'I', 'J', 'K'},
{'C', 'D', '4', '5', '6', 'L', 'M', 'N'},
{'E', 'F', '7', '8', '9', 'O', '#', 'P'},
{'G', 'H', '.', '0', '*', 'Q', 'R', 'S'},
};
byte rowPins[ROWS] = {10, 11, 12, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
enum {MENU_NN, MENU_NUM};
struct menuItem_t {
byte id;
char type;
char* text;
};
menuItem_t menu[] = {
{10, MENU_NN, "Login"},
{20, MENU_NN, "AlarmeTexto1"},
{21, MENU_NN, "AlarmeTexto2"},
{30, MENU_NN, "Config-1"},
{31, MENU_NN, "Config-2"},
{32, MENU_NUM, "Config-3"},
{33, MENU_NN, "Config-4"},
{40, MENU_NN, "Status1"},
{41, MENU_NN, "Status2"},
{42, MENU_NN, "Status3"},
{50, MENU_NUM, "ReprovaCav1"},
{51, MENU_NUM, "ReprovaCav2"},
{60, MENU_NUM, "Parada"},
{70, MENU_NN, "Ajuste"},
{80, MENU_NUM, "Setup"},
};
#define NUMMENUITEMS (sizeof(menu)/sizeof(menu[0]))
int findMenuIndex(byte id)
{
for (int i = 0; i < NUMMENUITEMS; i++)
{
if (id == menu[i].id) return i;
}
return -1; // id not found
}
int currentMenuID;
int currentMenuIndex = -1;
boolean currentMenuChanged;
char menuInput[21];
boolean menuInputChanged;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////Processing of Input Key/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int handleMenuKey(char c)
{ // return value is -1 if key has been handled by no value has been entered
// return value is menuIndex of item for which a value has been entered
int retVal = -1;
currentMenuChanged = false;
menuInputChanged = false;
if (c == 0) return retVal;
int menuID = currentMenuID;
if (c == '#' && currentMenuID >= 0 && strlen(menuInput) > 0)
{
retVal = currentMenuIndex;
currentMenuID = 0; // Abort/Exit menu
currentMenuIndex = -1;
currentMenuChanged = true;
return retVal; // we have finished entering a value
}
else if (c == '*' && strlen(menuInput) > 0)
{
menuInput[strlen(menuInput) - 1] = '\0'; // clear last input character
menuInputChanged = true;
}
else if (c == '*' && currentMenuID >= 0)
{
currentMenuID = 0; // Abort/Exit menu
currentMenuIndex = -1;
currentMenuChanged = true;
}
else if (c >= 'A' && c <= 'H') // key 'A'...'H' pressed
{
int i = c - 'A';
if (i != menuID / 10 - 1) menuID = 10 + i * 10; // activate main menu
else if (findMenuIndex(menuID + 1) >= 0) menuID++; // increase sub menu
else menuID = 10 + i * 10; // from last sub menu return to main menu
if (menuID != currentMenuID) // has menuID changed?
{
currentMenuChanged = true;
currentMenuID = menuID;
currentMenuIndex = findMenuIndex(menuID);
memset(menuInput, 0, sizeof(menuInput));
}
}
else if (c >= '0' && c <= '9' && menu[currentMenuIndex].type == MENU_NUM) // key'0'...'9' pressed and numeric input
{
if (strlen(menuInput) < sizeof(menuInput) - 1)
{
menuInput[strlen(menuInput)] = c;
menuInputChanged = true;
}
}
return retVal;
}