#include<EEPROM.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
const unsigned char upir_logo [] PROGMEM = { // this is another way how to define images, using binary notation
B00010101, B11010111,
B00010101, B01000101,
B00010101, B10010110,
B00011001, B00010101
};
// 'icon_control_mode', 16x16px
const unsigned char bitmap_icon_control_mode [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x0f, 0xe0, 0x18, 0x10, 0x10, 0x08, 0x33, 0xcc, 0x26, 0x64,
0x24, 0x24, 0x00, 0x00, 0xd7, 0x77, 0xf5, 0xdf, 0xf5, 0x54, 0xb2, 0x67, 0x00, 0x00, 0x00, 0x00
};
// 'alram1', 16x16px
const unsigned char bitmap_icon_alram1 [] PROGMEM = {
0x00, 0x00, 0x30, 0x38, 0xef, 0xcc, 0x58, 0x34, 0x21, 0x18, 0x61, 0x08, 0x41, 0x08, 0x41, 0x04,
0x41, 0xe4, 0x40, 0x0c, 0x60, 0x08, 0x20, 0x18, 0x10, 0x30, 0x3f, 0xf0, 0x33, 0x18, 0x00, 0x00
};
// 'alram2', 16x16px
const unsigned char bitmap_icon_alram2 [] PROGMEM = {
0x00, 0x00, 0x30, 0x38, 0xef, 0xcc, 0x58, 0x34, 0x21, 0x18, 0x61, 0x08, 0x41, 0x08, 0x41, 0x04,
0x41, 0xe4, 0x40, 0x0c, 0x60, 0x08, 0x20, 0x18, 0x10, 0x30, 0x3f, 0xf0, 0x33, 0x18, 0x00, 0x00
};
// 'icon_date', 16x16px
const unsigned char bitmap_icon_date [] PROGMEM = {
0x00, 0x00, 0x08, 0x10, 0x6f, 0xf6, 0x6f, 0xf6, 0x6f, 0xf6, 0x7f, 0xfe, 0x40, 0x00, 0x40, 0x00,
0x59, 0x9a, 0x40, 0x02, 0x59, 0x9a, 0x40, 0x02, 0x59, 0x9a, 0x40, 0x02, 0x7f, 0xfe, 0x00, 0x00
};
// 'icon_clock', 16x16px
const unsigned char bitmap_icon_clock [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x08, 0x10, 0x00, 0x00, 0x10, 0x08, 0x10, 0x08,
0x10, 0x88, 0x10, 0x48, 0x00, 0x20, 0x08, 0x10, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'icon_exit', 16x16px
const unsigned char bitmap_icon_exit [] PROGMEM = {
0x00, 0x00, 0x3f, 0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x20, 0x04,
0x20, 0x04, 0x20, 0x28, 0x20, 0x28, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3f, 0xe0, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 384)
const unsigned char* bitmap_icons[6] = {
bitmap_icon_control_mode,
bitmap_icon_alram1,
bitmap_icon_alram2,
bitmap_icon_date,
bitmap_icon_clock,
bitmap_icon_exit
};
//------------------------------------------------------
// ScreenShots for Display
//------------------------------------------------------
// 'images', 16x16px
const unsigned char bitmap_screenshot_exit [] PROGMEM = {
0xff, 0xff, 0xc0, 0x1f, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xd7, 0xdf, 0xd7, 0xdf, 0xfb,
0xdf, 0xfb, 0xdf, 0xd7, 0xdf, 0xd7, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xc0, 0x1f, 0xff, 0xff
};
// 'scrollbar_background', 8x64px
const unsigned char bitmap_scrollbar_background [] PROGMEM = {
0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00
};
// 'item_sel_outline', 128x21px
const unsigned char bitmap_item_sel_outline [] PROGMEM = {
0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
};
// 'screenshot_oval', 128x64px
const unsigned char bitmap_screenshot_oval [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00,
0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00,
0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00,
0x00, 0x03, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00,
0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00,
0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00,
0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00,
0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00,
0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00,
0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00,
0x00, 0x03, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00,
0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00,
0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00,
0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00,
0x00, 0x00, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const int NUM_ITEMS = 6; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 16; // maximum characters for the item name
// char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Contro Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
//------------------------------------------------------
//Input & Button Logic
//------------------------------------------------------
const int numOfInputs = 3;
#define BUTTON_SELECT_PIN 2 // pin for SELECT button
#define BUTTON_DOWN_PIN 3 // pin for DOWN button
#define BUTTON_UP_PIN 4 // pin for UP button
const int inputPins[numOfInputs] = {BUTTON_UP_PIN,BUTTON_DOWN_PIN,BUTTON_SELECT_PIN};
uint8_t inputState[numOfInputs] = {HIGH,HIGH,HIGH};
uint8_t lastInputState[numOfInputs] = {HIGH,HIGH,HIGH};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW};
uint32_t lastDebounceTime[numOfInputs] = {0,0,0};
uint32_t debounceDelay = 5, Last_Delay_Count = 0;
uint16_t Page_Count = 0, Element_Count = 0, No_of_Pages = 6, No_of_Elements = 6, Counter = 0;
char Array_Month [12][8] = {"Jan", "Fab", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char page1 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Control Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
char page2 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Perennial", "Seasonial", "Exit"};
char page3 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Contro Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
char page4 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Contro Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
char page5 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Contro Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
char page6 [NUM_ITEMS] [MAX_ITEM_LENGTH] = {"Contro Mode", "Timer 1", "Timer 2", "Date", "Time", "Exit"};
uint16_t ptr_page[6];
uint8_t ADD_Control_Mode = 0x00, ADD_Control_Mode_Start_Date = 0x01;
uint8_t Mode_Select = 0, Seasonal_Count = 0, Last_Count = 0;
uint8_t Last_State = 0, buttonState = 0;
uint16_t Year = 1989, Month = 0, Day = 1;
bool Count_Selection = 0, Seasonal_Start_Time_bit = 0;
//------------------------------------------------------
//Setup & Initialization
//------------------------------------------------------
void setup() {
Serial.begin(9600);
EEPROM.write(ADD_Control_Mode, 0x00); //Activate_Perennial_Mode()
ptr_page[0] = (int) &page1[0];
ptr_page[1] = (int) &page2[0];
ptr_page[2] = (int) &page3[0];
ptr_page[3] = (int) &page4[0];
ptr_page[4] = (int) &page5[0];
ptr_page[5] = (int) &page6[0];
u8g.setColorIndex(1); // set the color to white
for(int i = 0; i < numOfInputs; i++){
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
u8g.firstPage(); // required for page drawing mode for u8g library
do {
u8g.drawBitmapP( 0, 0, 128/8, 64, bitmap_screenshot_oval); // draw screenshot
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr(32, 35, "Lay Light");
} while ( u8g.nextPage() ); // required for page drawing mode with u8g library
delay(2000);
printScreen();
Mode_Select = EEPROM.read(ADD_Control_Mode);
}
//------------------------------------------------------
//Main Loop
//------------------------------------------------------
void loop() {
setInputFlags();
if(Mode_Select == 0) resolveInputFlags();
else Activate_Seasonal_Mode();
}
//------------------------------
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay)
{
if (reading != inputState[i]) {
inputState[i] = reading;
// Serial.println(inputState[i]);
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
//----------------------------
//---------------------------------------------
void resolveInputFlags()
{
for(int i = 0; i < numOfInputs; i++)
{
if(inputFlags[i] == HIGH)
{
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
//-------------------------------------------
void inputAction(int input)
{
switch (input)
{
case 0:
if (Element_Count == No_of_Elements - 1 || Page_Count == 0 || (Page_Count == 2 && Element_Count == 2))
Element_Count = 0;
else Element_Count++;
break;
case 1:
if(Element_Count == 0){
switch (Page_Count){
case 2:
Element_Count = 2;
break;
default: Element_Count = No_of_Elements - 1;
}
}
else Element_Count--;
break;
case 2:
if(Page_Count == 0) {
Page_Count++;
Element_Count = 0;
} else if(Page_Count == 1) {
switch (Element_Count) {
case 0:
Page_Count = 10;
// Element_Count = 0;
break;
case 1:
Page_Count = 11;
Last_Count = Element_Count;
Element_Count = 0;
break;
case 2:
Page_Count = 21;
Last_Count = Element_Count;
Element_Count = 0;
break;
case 3:
Page_Count = 31;
Last_Count = Element_Count;
Element_Count = 0;
break;
case 4:
Page_Count = 41;
Last_Count = Element_Count;
Element_Count = 0;
break;
case 5:
Page_Count = 0;
Last_Count = Element_Count;
Element_Count = 0;
break;
}
} else if(Page_Count == 10){
switch (Element_Count){
case 0:
Mode_Select = LOW;
EEPROM.write(ADD_Control_Mode, 0x00); //Perennial Mode Selected.
u8g.firstPage();
do {
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr((128-(13*7))/2, 25, "Perennial Mode");
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr((128-(8*8))/2, 25+20+2+2, "Selected");
} while ( u8g.nextPage() ); // required for page drawing mode with u8g library
Serial.println(EEPROM.read(ADD_Control_Mode));
delay(2000);
break;
case 1:
Mode_Select = HIGH;
EEPROM.write(ADD_Control_Mode, 0xff); //Seasonal Mode Selected.
Serial.println(EEPROM.read(ADD_Control_Mode));
break;
case 2:
Page_Count = 1;
Element_Count = Last_Count;
break;
}
}
// Serial.print("Last_Page_Count is");
// Serial.println(Last_Page_Count);
// switch (Page_Count){
// case 1:
// if(Element_Count ==5) Page_Count = 0;
// break;
// case 2:
// uint8_t Last_Page_Count = Page_Count;
// if(Element_Count ==2) Page_Count--;
// else if (Element_Count ==1) EEPROM.write(ADD_Control_Mode, 0xff);//Activate_Seasonal_Mode();
// else if (Element_Count ==0) EEPROM.write(ADD_Control_Mode, 0x00);//Activate_Perennial_Mode();
// break;
// // default: Page_Count++;
// }
// if(Page_Count == Last_Page_Count && Page_Count < 5) Page_Count++;
// else Page_Count = 0;
Serial.print("Page_Count");
Serial.println(Page_Count);
Serial.print("Element_Count");
Serial.println(Element_Count);
// Element_Count = 0;
break;
}
}
//-------------------------------------------
void printScreen()
{
// Serial.println(Page_Count);
u8g.firstPage();
do {
switch (Page_Count) { // MENU SCREEN
case 0:
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr((128-(7*9))/2, 10, "Lay Light");
u8g.setScale2x2(); // Scale up all draw procedures
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr(((128-(3*14))/2)/2, (18+20+2)/2, "OFF");
u8g.undoScale(); // IMPORTANT: Switch back to normal mode
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr((128-(9*9))/2, 15+20+20+2+2, "Next 2:00PM");
break;
case 1:
// selected item background
u8g.drawBitmapP(0, 22, 128/8, 21, bitmap_item_sel_outline);
u8g.setFont(u8g_font_7x14r);
u8g.drawStr(35, 15, "Settings");
// draw selected item as icon + label in bold font
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr(25, 15+20+2, page1[Element_Count]);
u8g.drawBitmapP( 4, 24, 16/8, 16, bitmap_icons[Element_Count]);
// draw next item as icon + label
u8g.setFont(u8g_font_7x14r);
u8g.drawStr(25, 15+20+20+2+2, page1[Element_Count+1]);
u8g.drawBitmapP( 4, 46, 16/8, 16, bitmap_icons[Element_Count+1]);
// draw scrollbar background
u8g.drawBitmapP(128-8, 0, 8/8, 64, bitmap_scrollbar_background);
// draw scrollbar handle
u8g.drawBox(125, 64/No_of_Elements * Element_Count, 3, 64/No_of_Elements);
break;
case 10:
// selected item background
u8g.drawBitmapP(0, 22, 128/8, 21, bitmap_item_sel_outline);
u8g.setFont(u8g_font_7x14r);
u8g.drawStr((128-(11*9))/2, 15, "Control Mode");
// draw selected item as icon + label in bold font
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr(25, 15+20+2, page2[Element_Count]);
// u8g.drawBitmapP( 4, 24, 16/8, 16, bitmap_icons[Element_Count]);
// draw next item as icon + label
u8g.setFont(u8g_font_7x14r);
u8g.drawStr(25, 15+20+20+2+2, page2[Element_Count+1]);
// u8g.drawBitmapP( 4, 46, 16/8, 16, bitmap_icons[Element_Count+1]);
// draw scrollbar background
u8g.drawBitmapP(128-8, 0, 8/8, 64, bitmap_scrollbar_background);
// draw scrollbar handle
u8g.drawBox(125, 64/2 * Element_Count, 3, 64/2);
break;
}
} while ( u8g.nextPage() ); // required for page drawing mode with u8g library
}
void Activate_Seasonal_Mode(){
char Date_Buffer [32];
char Data_Buffer [16];
char Select_Month[2][8] = {"Start", "Stop"};
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
switch (i) {
case 0:
if(Month < 12 && Count_Selection == 0) Month++;
else if(Day < 31 && Count_Selection ==1) Day++;
break;
case 1:
if(Month > 0 && Count_Selection == 0) Month--;
else if(Day > 0 && Count_Selection ==1) Day--;
break;
case 2:
if(Count_Selection == HIGH && Seasonal_Start_Time_bit == LOW) {
EEPROM.write(ADD_Control_Mode_Start_Date, Month);
EEPROM.write(ADD_Control_Mode_Start_Date + 1, Day);
Seasonal_Start_Time_bit = HIGH;
Count_Selection = LOW;
} else Count_Selection = HIGH;
break;
}
inputFlags[i] = LOW;
}
}
if(Mode_Select != 0 ){
sprintf (Date_Buffer, "%s %02d", &Array_Month[Month], Day);
u8g.firstPage();
do {
u8g.setFont(u8g_font_7x14Br);
u8g.drawStr((128-(12*8))/2, 15, "Seasonal Mode");
u8g.drawStr((128-(6*8))/2, 35+20, Date_Buffer);
u8g.drawLine((128-(6*8))/2 + (Count_Selection * 25), 35+20+5, (128-(6*8))/2 + 20 + (Count_Selection * 25), 35+20+5);
sprintf (Data_Buffer, "Enter %s Date", &Select_Month[Seasonal_Start_Time_bit]);
u8g.drawStr((128-(14*8))/2, 35, Data_Buffer );
} while ( u8g.nextPage() ); // required for page drawing mode with u8g library
}
if(Seasonal_Start_Time_bit == HIGH){
Month = 0;
Day = 0;
}
}