From 59330768c5e5b9e32cad8554e8c8c1b41be9caf6 Mon Sep 17 00:00:00 2001 From: chrispr Date: Tue, 15 Feb 2022 20:16:27 -0500 Subject: [PATCH] Changed graph render to match nightscout; constant NTP resync to avoid massive clock drift during deep sleep --- NightscoutEInkClient.ino | 44 +++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/NightscoutEInkClient.ino b/NightscoutEInkClient.ino index 8a484bb..5d87cdd 100644 --- a/NightscoutEInkClient.ino +++ b/NightscoutEInkClient.ino @@ -39,6 +39,8 @@ void setup() { while (!Serial) { delay(10); } Serial.println("Nightscout CGM BS Monitor starting..."); + print_wakeup_reason(); + ConnectToWifi(); struct tm timeinfo; @@ -101,11 +103,27 @@ void setup() { SleepUntilNextReading(lastSyncTime); } +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 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; + case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; + 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 initTime() { struct tm timeinfo; - if(!getLocalTime(&timeinfo)) - configTime(0, 0, ntpServer); + //if(!getLocalTime(&timeinfo)) + configTime(0, 0, ntpServer); while(!getLocalTime(&timeinfo)){ Serial.println("Sleeping until time is set.."); delay(100); @@ -137,7 +155,7 @@ void SleepUntilNextReading(time_t lastReading) { Serial.println(" seconds diff between lastReading and current time"); int secondsToSleep = 300 - (int)timeDiff; //Add one more to not jump the gun - secondsToSleep += 3; + secondsToSleep += 20; if(secondsToSleep > 0 && secondsToSleep < 300) { //secondsToSleep = secondsToSleep + 7; Serial.print("Sleeping "); @@ -210,7 +228,10 @@ void TurnOffEPD() { } void SleepFiveMins() { - esp_sleep_enable_timer_wakeup(285 * uS_TO_S_FACTOR); + int secondsToSleep = 300; + Serial.print("Sleeping "); + Serial.println(secondsToSleep); + esp_sleep_enable_timer_wakeup(secondsToSleep * uS_TO_S_FACTOR); Serial.flush(); TurnOffEPD(); esp_deep_sleep_start(); @@ -220,18 +241,21 @@ void RenderGraph(JsonArray sgvRecords) { boolean redrawScreen = true; double x = 0; double y = 100; - double cnt = 0; + double cnt = -45; - for(JsonVariant v : sgvRecords) { + //for(JsonVariant v : sgvRecords) { + for(int i=sgvRecords.size(); i >= 0; i--) { - JsonObject obj = v.as(); + JsonObject obj = sgvRecords[i].as(); //v.as(); Serial.println(obj["sgv"].as()); if(obj["device"].as().indexOf("xDrip") == -1) continue; - if(cnt > 45) + if(cnt > 0) continue; - Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, 0,45, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); + //Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, 0,45, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); + Graph(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, -45,0, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); + //GraphReversed(display, cnt, obj["sgv"].as(), 20, 110, 210, 105, 0,45, 5,60,180,10,"", "", "", COLOR1, COLOR1, COLOR2, COLOR1, COLOR0, redrawScreen); cnt += 5; } } @@ -443,7 +467,7 @@ void Graph(ThinkInk_290_Tricolor_Z10 &d, double x, double y, double gx, double g d.setTextColor(tcolor, bcolor); d.setCursor(temp, gy + 5); // precision is default Arduino--this could really use some format control - d.println(int(i)); + d.println(abs(int(i))); } //now draw the labels