Skip to content

Commit

Permalink
Fallback to last known refresh time if network err
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjtwomey committed Jul 25, 2023
1 parent e1d6083 commit 1775c3c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 0 additions & 2 deletions include/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#define SECONDS_IN_DAY 86400
#define SECONDS_IN_YEAR SECONDS_IN_DAY * 365

// Fallback time to refresh.
#define FALLBACK_REFRESH_TIME "09:00:00"
// The number of seconds to sleep if RTC not configured correctly.
#define FALLBACK_SLEEP_SECONDS 120

Expand Down
2 changes: 2 additions & 0 deletions src/defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
char serverURL[] = "http://localhost:8080/download.png";
// The number of times to attempt downloading or drawing the server image.
int serverRetries = 3;
// Fallback time to refresh.
char serverDefaultRefreshTime[9] = "08:45:00";

// Wifi config.
char wifiSSID[] = "XXXX";
Expand Down
9 changes: 7 additions & 2 deletions src/display_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ void displayMessage(const char* msg, int batteryRemainingPercent) {
board.setTextColor(BLACK);
board.setTextWrap(true);
board.getTextBounds(msg, 0, 0, &x, &y, &w, &h);
board.fillRect(0, 0, E_INK_HEIGHT, h * 1.5, 0x8080);
board.setCursor(cX - w / 2, cY + h / 2);
board.fillRect(0, 0, E_INK_HEIGHT, h * 2.5, 0x8080);
board.setCursor(cX - w / 2, cY + h * 1.5);
board.setTextColor(0xFFFF);
board.print(msg);

String nowFmt = nowTzFmt();
board.setFont(&Merienda_Regular12pt7b);
board.setCursor(12, 24);
board.print(nowFmt);

displayBatteryStatus(batteryRemainingPercent, true);

board.display();
Expand Down
17 changes: 11 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
#include "file_utils.h"
#endif

RTC_DATA_ATTR int bootCount = 0;
// Default to a known refresh time.
// (len("XX:XX:XX") = 8) + 1 = 9
RTC_DATA_ATTR char nextRefreshTime[9];

// inkplate10 board driver
Inkplate board(INKPLATE_3BIT);

void setup() {
++bootCount;

Serial.begin(115200);
// Init inkplate board.
board.begin();
Expand All @@ -28,7 +35,7 @@ void setup() {
time_t bootTime = board.rtcGetEpoch();
setTime(bootTime);

log(LOG_NOTICE, "##### Inkplate10 Weather Calendar wake up #####");
logf(LOG_NOTICE, "##### Inkplate10 Weather Calendar boot #%d #####", bootCount);
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0:
Expand Down Expand Up @@ -89,7 +96,7 @@ void setup() {
const char* errMsg = "Failed to open config file";
logf(LOG_ERROR, errMsg);
displayMessage(errMsg, batteryRemainingPercent);
sleep(FALLBACK_REFRESH_TIME);
sleep(serverDefaultRefreshTime);
}

// Attempt to parse yaml file.
Expand All @@ -100,14 +107,15 @@ void setup() {
const char* errMsg = "Failed to load config from file";
logf(LOG_ERROR, "failed to deserialize YAML: %s", dse.c_str());
displayMessage(errMsg, batteryRemainingPercent);
sleep(FALLBACK_REFRESH_TIME);
sleep(serverDefaultRefreshTime);
}
file.close();

// Assign config values.
JsonObject serverCfg = doc["server"];
serverURL = serverCfg["url"];
serverRetries = serverCfg["retries"];
serverDefaultRefreshTime = serverCfg["default_refresh_time"];

// Wifi config.
JsonObject wifiCfg = doc["wifi"];
Expand Down Expand Up @@ -161,9 +169,6 @@ void setup() {

int32_t defaultLen = E_INK_WIDTH * E_INK_HEIGHT * 8 + 100;
uint8_t *buf = 0;
// Default to a known refresh time.
// (len("XX:XX:XX") = 8) + 1 = 9
char nextRefreshTime[9] = FALLBACK_REFRESH_TIME;
do {
logf(LOG_DEBUG, "calendar download attempt #%d", attempts + 1);

Expand Down

0 comments on commit 1775c3c

Please sign in to comment.