diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 40bdfa0..fc7cfef 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -1,6 +1,6 @@ #include "Watchy.h" -DS3232RTC Watchy::RTC(false); +DS3232RTC Watchy::RTC(false); GxEPD2_BW Watchy::display(GxEPD2_154_D67(CS, DC, RESET, BUSY)); RTC_DATA_ATTR int guiState; @@ -51,10 +51,10 @@ void Watchy::init(String datetime){ tm.Second = 0; time_t t = makeTime(tm); RTC.set(t); - RTC.read(currentTime); + RTC.read(currentTime); showWatchFace(true); //partial updates on tick } - break; + break; #endif case ESP_SLEEP_WAKEUP_EXT0: //RTC Alarm RTC.alarm(ALARM_2); //resets the alarm flag in the RTC @@ -80,10 +80,10 @@ void Watchy::init(String datetime){ void Watchy::deepSleep(){ #ifndef ESP_RTC esp_sleep_enable_ext0_wakeup(RTC_PIN, 0); //enable deep sleep wake on RTC interrupt - #endif + #endif #ifdef ESP_RTC esp_sleep_enable_timer_wakeup(60000000); - #endif + #endif esp_sleep_enable_ext1_wakeup(BTN_PIN_MASK, ESP_EXT1_WAKEUP_ANY_HIGH); //enable deep sleep wake on button press esp_deep_sleep_start(); } @@ -92,7 +92,7 @@ void Watchy::_rtcConfig(String datetime){ if(datetime != NULL){ const time_t FUDGE(30);//fudge factor to allow for upload time, etc. (seconds, YMMV) tmElements_t tm; - tm.Year = getValue(datetime, ':', 0).toInt() - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t + tm.Year = getValue(datetime, ':', 0).toInt() - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t tm.Month = getValue(datetime, ':', 1).toInt(); tm.Day = getValue(datetime, ':', 2).toInt(); tm.Hour = getValue(datetime, ':', 3).toInt(); @@ -112,146 +112,144 @@ void Watchy::_rtcConfig(String datetime){ } void Watchy::handleButtonPress(){ - uint64_t wakeupBit = esp_sleep_get_ext1_wakeup_status(); - //Menu Button - if (wakeupBit & MENU_BTN_MASK){ - if(guiState == WATCHFACE_STATE){//enter menu state if coming from watch face - showMenu(menuIndex, false); - }else if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item - switch(menuIndex) - { - case 0: - showBattery(); - break; - case 1: - showBuzz(); - break; - case 2: - showAccelerometer(); - break; - case 3: - setTime(); - break; - case 4: - setupWifi(); - break; - case 5: - showUpdateFW(); - break; - default: - break; - } - }else if(guiState == FW_UPDATE_STATE){ - updateFWBegin(); - } - } - //Back Button - else if (wakeupBit & BACK_BTN_MASK){ - if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu - RTC.alarm(ALARM_2); //resets the alarm flag in the RTC - RTC.read(currentTime); - showWatchFace(false); - }else if(guiState == APP_STATE){ - showMenu(menuIndex, false);//exit to menu if already in app - }else if(guiState == FW_UPDATE_STATE){ - showMenu(menuIndex, false);//exit to menu if already in app - } - } - //Up Button - else if (wakeupBit & UP_BTN_MASK){ - if(guiState == MAIN_MENU_STATE){//increment menu index - menuIndex--; - if(menuIndex < 0){ - menuIndex = MENU_LENGTH - 1; - } - showMenu(menuIndex, true); - } - } - //Down Button - else if (wakeupBit & DOWN_BTN_MASK){ - if(guiState == MAIN_MENU_STATE){//decrement menu index - menuIndex++; - if(menuIndex > MENU_LENGTH - 1){ - menuIndex = 0; - } - showMenu(menuIndex, true); - } - } - - /***************** fast menu *****************/ - bool timeout = false; - long lastTimeout = millis(); - pinMode(MENU_BTN_PIN, INPUT); - pinMode(BACK_BTN_PIN, INPUT); - pinMode(UP_BTN_PIN, INPUT); - pinMode(DOWN_BTN_PIN, INPUT); - while(!timeout){ - if(millis() - lastTimeout > 5000){ - timeout = true; - }else{ - if(digitalRead(MENU_BTN_PIN) == 1){ - lastTimeout = millis(); - if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item - switch(menuIndex) - { - case 0: - showBattery(); + uint64_t wakeupBit = esp_sleep_get_ext1_wakeup_status(); + //Menu Button + if (wakeupBit & MENU_BTN_MASK) { + if (guiState == WATCHFACE_STATE) {//enter menu state if coming from watch face + showMenu(menuIndex, false); + } else if (guiState == MAIN_MENU_STATE) {//if already in menu, then select menu item + switch(menuIndex) { + case 0: + showBattery(); break; - case 1: + case 1: showBuzz(); - break; - case 2: + break; + case 2: showAccelerometer(); break; - case 3: + case 3: setTime(); break; - case 4: + case 4: setupWifi(); - break; - case 5: + break; + case 5: showUpdateFW(); break; - default: - break; - } - }else if(guiState == FW_UPDATE_STATE){ - updateFWBegin(); + default: + break; } - }else if(digitalRead(BACK_BTN_PIN) == 1){ - lastTimeout = millis(); - if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu + } else if(guiState == FW_UPDATE_STATE) { + updateFWBegin(); + } + } + //Back Button + else if (wakeupBit & BACK_BTN_MASK) { + if (guiState == MAIN_MENU_STATE) {//exit to watch face if already in menu RTC.alarm(ALARM_2); //resets the alarm flag in the RTC RTC.read(currentTime); showWatchFace(false); - break; //leave loop - }else if(guiState == APP_STATE){ + } else if (guiState == APP_STATE) { showMenu(menuIndex, false);//exit to menu if already in app - }else if(guiState == FW_UPDATE_STATE){ + } else if (guiState == FW_UPDATE_STATE) { showMenu(menuIndex, false);//exit to menu if already in app - } - }else if(digitalRead(UP_BTN_PIN) == 1){ - lastTimeout = millis(); - if(guiState == MAIN_MENU_STATE){//increment menu index + } + } + //Up Button + else if (wakeupBit & UP_BTN_MASK) { + if(guiState == MAIN_MENU_STATE) {//increment menu index menuIndex--; if(menuIndex < 0){ menuIndex = MENU_LENGTH - 1; - } - showFastMenu(menuIndex); - } - }else if(digitalRead(DOWN_BTN_PIN) == 1){ - lastTimeout = millis(); - if(guiState == MAIN_MENU_STATE){//decrement menu index + } + showMenu(menuIndex, true); + } + } + //Down Button + else if (wakeupBit & DOWN_BTN_MASK) { + if(guiState == MAIN_MENU_STATE){//decrement menu index menuIndex++; - if(menuIndex > MENU_LENGTH - 1){ + if(menuIndex > MENU_LENGTH - 1) { menuIndex = 0; } - showFastMenu(menuIndex); - } - } - } - } - display.hibernate(); + showMenu(menuIndex, true); + } + } + + /***************** fast menu *****************/ + bool timeout = false; + long lastTimeout = millis(); + pinMode(MENU_BTN_PIN, INPUT); + pinMode(BACK_BTN_PIN, INPUT); + pinMode(UP_BTN_PIN, INPUT); + pinMode(DOWN_BTN_PIN, INPUT); + while(!timeout) { + if (millis() - lastTimeout > 5000) { + timeout = true; + } else { + if (digitalRead(MENU_BTN_PIN) == 1) { + lastTimeout = millis(); + if (guiState == MAIN_MENU_STATE) {//if already in menu, then select menu item + switch(menuIndex) { + case 0: + showBattery(); + break; + case 1: + showBuzz(); + break; + case 2: + showAccelerometer(); + break; + case 3: + setTime(); + break; + case 4: + setupWifi(); + break; + case 5: + showUpdateFW(); + break; + default: + break; + } + } else if (guiState == FW_UPDATE_STATE) { + updateFWBegin(); + } + } else if (digitalRead(BACK_BTN_PIN) == 1) { + lastTimeout = millis(); + if (guiState == MAIN_MENU_STATE) {//exit to watch face if already in menu + RTC.alarm(ALARM_2); //resets the alarm flag in the RTC + RTC.read(currentTime); + showWatchFace(false); + break; //leave loop + } else if (guiState == APP_STATE) { + showMenu(menuIndex, false);//exit to menu if already in app + } else if (guiState == FW_UPDATE_STATE) { + showMenu(menuIndex, false);//exit to menu if already in app + } + } else if (digitalRead(UP_BTN_PIN) == 1) { + lastTimeout = millis(); + if (guiState == MAIN_MENU_STATE) {//increment menu index + menuIndex--; + if(menuIndex < 0){ + menuIndex = MENU_LENGTH - 1; + } + showFastMenu(menuIndex); + } + } else if (digitalRead(DOWN_BTN_PIN) == 1) { + lastTimeout = millis(); + if (guiState == MAIN_MENU_STATE) {//decrement menu index + menuIndex++; + if (menuIndex > MENU_LENGTH - 1) { + menuIndex = 0; + } + showFastMenu(menuIndex); + } + } + } + } + display.hibernate(); } void Watchy::showMenu(byte menuIndex, bool partialRefresh){ @@ -265,24 +263,24 @@ void Watchy::showMenu(byte menuIndex, bool partialRefresh){ int16_t yPos; const char *menuItems[] = {"Check Battery", "Vibrate Motor", "Show Accelerometer", "Set Time", "Setup WiFi", "Update Firmware"}; - for(int i=0; i