#include "U8glib.h"
#include <EEPROM.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
#define KEY_NONE 0
#define KEY_PREV 1
#define KEY_NEXT 2
#define KEY_SELECT 3
uint8_t uiKeyPrev = 8;
uint8_t uiKeyNext = 9;
uint8_t uiKeySelect = 12;
uint8_t uiKeyCodeFirst = KEY_NONE;
uint8_t uiKeyCodeSecond = KEY_NONE;
uint8_t uiKeyCode = KEY_NONE;
int dInk = 0;
int egg = 0;
int alarmTemp = 0;
int fanTemp = 0;
boolean needSave;
//boolean menu;
void uiSetup(void) {
// configure input keys
pinMode(uiKeyPrev, INPUT_PULLUP); // set pin to input with pullup
pinMode(uiKeyNext, INPUT_PULLUP); // set pin to input with pullup
pinMode(uiKeySelect, INPUT_PULLUP); // set pin to input with pullup
}
void uiStep(void) {
uiKeyCodeSecond = uiKeyCodeFirst;
if ( digitalRead(uiKeyPrev) == LOW )
uiKeyCodeFirst = KEY_PREV;
else if ( digitalRead(uiKeyNext) == LOW )
uiKeyCodeFirst = KEY_NEXT;
else if ( digitalRead(uiKeySelect) == LOW )
uiKeyCodeFirst = KEY_SELECT;
else
uiKeyCodeFirst = KEY_NONE;
if ( uiKeyCodeSecond == uiKeyCodeFirst )
uiKeyCode = uiKeyCodeFirst;
else
uiKeyCode = KEY_NONE;
}
#define MENU_ITEMS 5
const char *menu_strings[MENU_ITEMS] = {"SETTINGS", "DAY INK", "EGG SETUP", "ALARM D.TEMP", "FAN D.TEMP" };
const int menu_ee[MENU_ITEMS] = {0, 100, 110, 106, 108 };
int menuValue[MENU_ITEMS];
uint8_t menuCurrent = 0;
//uint8_t menu_redraw_required = 0;
uint8_t last_key_code = KEY_NONE;
void drawMenu(void) {
uint8_t i, h;
u8g_uint_t w, d;
u8g.setFont(u8g_font_6x13);
u8g.setFontRefHeightText();
u8g.setFontPosTop();
h = u8g.getFontAscent() - u8g.getFontDescent();
w = u8g.getWidth();
for ( i = 0; i < MENU_ITEMS; i++ ) {
u8g.setDefaultForegroundColor();
if ( i == menuCurrent ) {
u8g.drawBox(0, i * h + 1, w, h);
u8g.setDefaultBackgroundColor();
}
u8g.drawStr(5, i * h, menu_strings[i]);
u8g.setPrintPos(105, i * h);
u8g.print(menuValue[i]);
}
}
void updateMenu(void) {
if ( uiKeyCode != KEY_NONE && last_key_code == uiKeyCode ) {
return;
}
last_key_code = uiKeyCode;
switch ( uiKeyCode ) {
case KEY_SELECT:
menuCurrent++;
if ( menuCurrent >= MENU_ITEMS ) {
menuCurrent = 0;
break;
}
if ( needSave == 1 ) {
EEPROM_int_write(menu_ee[menuCurrent - 1], menuValue[menuCurrent - 1]);
needSave = 0;
}
break;
case KEY_NEXT:
menuValue[menuCurrent]++;
needSave = 1;
break;
case KEY_PREV:
menuValue[menuCurrent]--;
needSave = 1;
break;
}
Serial.print("menuCurrent=");
Serial.println(menuCurrent);
}
void readSet() {
menuValue[1] = EEPROM_int_read(100);
menuValue[2] = EEPROM_int_read(110);
menuValue[3] = EEPROM_int_read(106);
menuValue[4] = EEPROM_int_read(108);
}
void draw(void) {
//u8g.setFont(u8g_font_unifont);
u8g.setFont(u8g_font_6x13);
u8g.drawStr( 07, 18, "Incubator 0.1");
u8g.drawStr( 07, 58, "with Arduino");
}
void setup() {
Serial.begin(115200);
readSet();
uiSetup();
}
void loop() {
Serial.println("loop");
uiStep(); // check for key press
if ( menuCurrent == 0 ) {
u8g.firstPage();
do {
draw();
Serial.println("draw");
} while ( u8g.nextPage() );
}
else {
u8g.firstPage();
do {
drawMenu();
} while ( u8g.nextPage() );
}
updateMenu(); // update menu bar
delay(100);
}
int EEPROM_int_read(int addr) {
byte raw[2];
for (byte i = 0; i < 2; i++) raw[i] = EEPROM.read(addr + i);
int &num = (int&)raw;
return num;
}
void EEPROM_int_write(int addr, int num) {
byte raw[2];
(int&)raw = num;
for (byte i = 0; i < 2; i++) EEPROM.write(addr + i, raw[i]);
}