#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include "avdweb_Switch.h"
#include <EEPROM.h>
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);    //  M0/ESP32/ESP8266/mega2560/Uno/Leonardo
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex启用GPIO 33.
RTC_DATA_ATTR int bootCount = 0;
unsigned long Time,Time1;
unsigned int option=1,SetOption=1,FireTime=3,RestTime=5,Cycle=10;
const byte BTN01Pin = 33;   //按键1、2、3、4分别接ESP 33、13、12、14
const byte BTN02Pin = 13;
const byte BTN03Pin = 12;
const byte BTN04Pin = 14;
const byte TQ2SA_Pin = 27;
boolean Battery_page=false,Menu_Page=false,ManualModePage=false,TimerModePage=false,BreathModePage=false,DeviceSettingPage=false,
sleepMode=false,BTNlongpress=false;      //中间继电器;
Switch BTN01 = Switch(BTN01Pin);
Switch BTN02 = Switch(BTN02Pin);
Switch BTN03 = Switch(BTN03Pin);
Switch BTN04 = Switch(BTN04Pin);
struct MyObject {
  unsigned int field1;
  unsigned int field2;
  unsigned int field3;
};
int eeAddress = 0;
void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : u8g2.sleepOff();sleepMode=false;/*Serial.println("Wakeup caused by external signal using RTC_IO");*/ break;//由外部信号使用RTC_IO引起的唤醒
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;//使用RTC_CNTL的外部信号引起的唤醒
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;//由计时器引起的唤醒
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;//由触摸板引起的唤醒
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;//由触摸板引起的唤醒
   // default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;//由触摸板引起的唤醒
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(TQ2SA_Pin,OUTPUT);
  digitalWrite(TQ2SA_Pin,HIGH);
  EEPROM.begin(1024);
  MyObject customVar;
  EEPROM.get(eeAddress, customVar);  //读内存.
  FireTime = customVar.field1;
  RestTime = customVar.field2;
  Cycle = customVar.field3;
  //  FireTime = EEPROM.read(0);
  //  RestTime = EEPROM.read(2);
  // Cycle = EEPROM.read(4);
 // pinMode(13, INPUT_PULLUP);
 // pinMode(12, INPUT_PULLUP);
 // pinMode(14, INPUT_PULLUP);
  u8g2.begin();    //初始化函数
  u8g2.enableUTF8Print();        // 为Arduino print()函数启用UTF8支持
  u8g2.setFontPosTop();   // *使用drawStr显示字符串时,默认标准为显示字符的左下角坐标。
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.setFontDirection(0); 
  Battery_page=true;
  Time=millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  print_wakeup_reason();
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,0); //pin33 1 = High, 0 = Low 按键1休眠唤醒
  if(Battery_page){
   u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_lucasarts_scumm_subtitle_o_tf);
    u8g2.drawStr(30, 5, "Battery");
    u8g2.setFont(u8g2_font_6x13_t_hebrew);
    u8g2.drawStr(22, 30, "Voltage:     V"); 
    u8g2.setCursor(5, 45);
    u8g2.print("Percentage:     %");        
  } while ( u8g2.nextPage() ); 
    delay(2000);
    Battery_page=false;
    Menu_Page=true;
  }
  if(Menu_Page){
    u8g2.firstPage();
     do {
      OptionMenuitem();
      u8g2.setFont(u8g_font_ncenB08);
      if(option==1)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);u8g2.drawStr(22, 1, "Manual Mode");
      if(option==2)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);u8g2.drawStr(25, 17, "Timer Mode");
      if(option==3)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);u8g2.drawStr(25, 33, "Breath Mode");
      if(option==4)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);u8g2.drawStr(19, 49, "Device Setting");

     }while ( u8g2.nextPage() );  
    if(millis()-Time>10000){           //10秒后休眠;
       Menu_Page=false;
       u8g2.sleepOn();
       delay(500);
       esp_deep_sleep_start();    //启用休眠
     } 
  }
  if(ManualModePage){
    u8g2.firstPage();
     do {
      //setMenuitem();
      u8g2.setFont(u8g2_font_lucasarts_scumm_subtitle_o_tf);
      u8g2.drawStr(5, 5, "Manual Mode");
      u8g2.setFont(u8g2_font_6x13_t_hebrew);
      u8g2.drawStr(1, 35, "Press&Hold Down Arrow");                      //Press & Hold "Down Arrow"
     }while ( u8g2.nextPage() );
     BTN04.poll();
     if(BTN04.on()){
       digitalWrite(TQ2SA_Pin,LOW);
       Time=millis();
     } else digitalWrite(TQ2SA_Pin,HIGH);
     if(millis()-Time>5000){           //5秒后退回主菜单
       ManualModePage=false;
       Menu_Page=true;
       Time=millis();
     }
     BTN01.poll();
     if(BTN01.singleClick()){          //单击1键退回主菜单
       ManualModePage=false;
       Menu_Page=true;
       Time=millis();
     }
  }
  if(TimerModePage){
    u8g2.firstPage();
     do {
      //setMenuitem();
      u8g2.setFont(u8g2_font_lucasarts_scumm_subtitle_o_tf);
      u8g2.drawStr(13, 1, "Timer Mode");
      u8g2.setFont(u8g_font_ncenB08);
      u8g2.drawStr(10, 24, "Fire      Rest    Cycle");  //Fire Rest Cycle
      u8g2.drawStr(1, 53, "Cycle count :");
      u8g2.setCursor(16, 36);  //u8g2.setPrintPos(10, 35);
      { u8g2.setFont(u8g_font_ncenB10);
        u8g2.print(FireTime);
      }
      u8g2.setCursor(60, 36);
      { //u8g.setFont(u8g_font_ncenB08);
        u8g2.print(RestTime);
      }
      u8g2.setCursor(95, 36);
      { //u8g.setFont(u8g_font_ncenB08);
        u8g2.print(Cycle);
      }
      u8g2.setCursor(76, 53);
      { u8g2.setFont(u8g_font_ncenB08);
        u8g2.print(Cycle);
      }
      }while ( u8g2.nextPage() ); 
    BTN01.poll();
     if(BTN01.singleClick()){          //单击1键退回主菜单
       TimerModePage=false;
       Menu_Page=true;
       Time=millis();
     }
    if(millis()-Time>5000){           //5秒后退回主菜单
       TimerModePage=false;
       Menu_Page=true;
       Time=millis();
     }


  }
  if(DeviceSettingPage){
    u8g2.setColorIndex(1);
    u8g2.firstPage();
     do {
      //setMenuitem();
      u8g2.setFont(u8g2_font_lucasarts_scumm_subtitle_o_tf);
      u8g2.drawStr(0, 1, "Device");
      u8g2.drawStr(62, 1, "Setting");
      u8g2.setFont(u8g_font_ncenB08);
      u8g2.drawStr(18, 24, " Fire :");  //Fire Rest Cycle
      u8g2.drawStr(17, 38, " Rest :");
      u8g2.drawStr(12, 53, "Cycle :");
      DeviceSetting();
      if(SetOption==1)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);
      u8g2.setCursor(60, 24);  //u8g2.setPrintPos(10, 35);
      { u8g2.setFont(u8g_font_ncenB08);
        u8g2.print(FireTime);
      }
      if(SetOption==2)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);
      u8g2.setCursor(60, 38);
      { //u8g.setFont(u8g_font_ncenB08);
        u8g2.print(RestTime);
      }
      if(SetOption==3)u8g2.setColorIndex(0);else u8g2.setColorIndex(1);
      u8g2.setCursor(60, 53);
      { //u8g.setFont(u8g_font_ncenB08);
        u8g2.print(Cycle);
      }
    
   }while ( u8g2.nextPage() );
   BTN01.poll();
     if(BTN01.singleClick()){          //单击1键退回主菜单
       DeviceSettingPage=false;
       Menu_Page=true;
       Time=millis();
       MyObject customVar = {FireTime, RestTime,Cycle};
       EEPROM.put(eeAddress, customVar);
      // EEPROM.write(0, FireTime);
      // EEPROM.write(2, RestTime);
      // EEPROM.write(4, Cycle);
       EEPROM.commit();
       delay(100);
     }
    if(millis()-Time>5000){           //5秒后退回主菜单
       DeviceSettingPage=false;
       Menu_Page=true;
       Time=millis();
       MyObject customVar = {FireTime, RestTime,Cycle};
       EEPROM.put(eeAddress, customVar);
      // EEPROM.write(0, FireTime);
      // EEPROM.write(2, RestTime);
      // EEPROM.write(4, Cycle);
       EEPROM.commit();
       delay(100);
     }
  }
 // Serial.println("Going to sleep now");
 // esp_deep_sleep_start();    //启用休眠

}

void OptionMenuitem()              //菜单选择
{
  BTN03.poll();
  if(BTN03.singleClick()){ option--;if(option<1)option=4;Time=millis(); }
  BTN04.poll();
  if(BTN04.singleClick()){ option++;if(option>4)option=1;Time=millis(); }
  BTN02.poll();
  if(BTN02.singleClick()){
    //u8g2.clearBuffer();
    switch (option){
      case 1:{
        Menu_Page=false;
        ManualModePage=true;
        Time=millis();
        break;
      }
      case 2:{
        Menu_Page=false;
        TimerModePage=true;
        Time=millis();
        break;
      }
      case 3:{
        Menu_Page=false;
        BreathModePage=true;
        Time=millis();
        break;
      }
      case 4:{
        Menu_Page=false;
        DeviceSettingPage=true;
        Time=millis();
        break;
      }
    }
  }
  Menuitem();
 // Serial.println(option);
}
void Menuitem()
{
  u8g2.setColorIndex(1);
  switch (option) {
    case 1: {
        u8g2.drawBox(16, 0, 86, 14);
        break;
      }
    case 2: {
        u8g2.drawBox(16, 16, 86, 14);
        break;
      }
    case 3: {
        u8g2.drawBox(16, 32, 86, 14);
        break;
      }
    case 4: {
        u8g2.drawBox(16, 48, 86, 14);
        break;
      }  
  }
}
void DeviceSetting()              //FireTime、RestTime、Cycle时间设置
{
  BTN02.poll();
  if(BTN02.singleClick()){
    SetOption++;
    Time=millis();
    if(SetOption>3)SetOption=1;
    BTNlongpress=false;
  }
  u8g2.setColorIndex(1);
  switch (SetOption) {
    case 1: {
        u8g2.drawBox(58, 22, 20, 14);
        BTN03.poll();
         if(BTN03.singleClick()){
          FireTime++;
          Time=millis();
          if(FireTime>10)FireTime=10;
         }
        BTN04.poll();
         if(BTN04.singleClick()){
          FireTime--;
          Time=millis();
          if(FireTime<1)FireTime=1;
         } 
        break;
      }
    case 2: {
        u8g2.drawBox(58, 36, 30, 14);
        BTN03.poll();
         if(BTN03.singleClick()){
          RestTime++;
          Time=millis();
          if(RestTime>3600)RestTime=3600;
         }
        if(BTN03.doubleClick()) BTNlongpress=!BTNlongpress;
        if(BTN03.on()){
          if(BTNlongpress)RestTime++;
          Time=millis();
          if(RestTime>3600)RestTime=3600;
        } 
        BTN04.poll();
         if(BTN04.singleClick()){
          RestTime--;
          Time=millis();
          if(RestTime<1)RestTime=1;
         }
         if(BTN04.doubleClick()) BTNlongpress=!BTNlongpress;
        if(BTN04.on()){
          if(BTNlongpress) RestTime--;
          Time=millis();
          if(RestTime<1)RestTime=1;
        }  
        break;
      }
    case 3: {
        u8g2.drawBox(58, 51, 30, 14);
        BTN03.poll();
         if(BTN03.singleClick()){
          Cycle++;
          Time=millis();
          if(Cycle>100)Cycle=100;
         }
        if(BTN03.doubleClick()) BTNlongpress=!BTNlongpress;
        if(BTN03.on()){
          if(BTNlongpress)Cycle++;
          Time=millis();
          if(Cycle>100)Cycle=100;
        } 
        BTN04.poll();
         if(BTN04.singleClick()){
          Cycle--;
          Time=millis();
          if(Cycle<1)Cycle=1;
         }
        if(BTN04.doubleClick()) BTNlongpress=!BTNlongpress;
        if(BTN04.on()){
          if(BTNlongpress) Cycle--;
          Time=millis();
          if(Cycle<1)Cycle=1;
        } 
        break;
      } 
  }
}