From a1bb7f0d42eddc32f7d44a138ad52cf8d3b485ad Mon Sep 17 00:00:00 2001 From: Michal Schwarz Date: Fri, 27 Jan 2023 20:59:40 +0100 Subject: [PATCH] checksum support --- README.md | 2 +- client/src/main.cpp | 93 ++++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 3de3897..a44baa0 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ I choose this approach because it's easier (and more fun) for me to implement th 1. ~~Add support for iCal.~~ 1. ~~Fix the icons at the bottom, make more of them available, make them more random.~~ 1. ~~Indicate possible WiFi outage or server unavailability on the display.~~ -1. Refresh the display only if image has changed (=check image checksum against the previous value). This should allow the portal calendar to ask server periodically more often but still sleep a lot and preserve energy. +1. ~~Refresh the display only if image has changed (=check image checksum against the previous value). This should allow the portal calendar to ask server periodically more often but still sleep a lot and preserve energy.~~ 1. Maybe add support for a weather forecast (but I'll probably create a different project just for this purpose). 1. Replace the ESP8266 ePaper module with what [original project](https://github.com/wuspy/portal_calendar) uses, i.e. specific low power ESP32 board + separate e-Paper hat [^1]. diff --git a/client/src/main.cpp b/client/src/main.cpp index f38c46d..53990a8 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -59,6 +59,7 @@ GxEPD2_BW display( WiFiManager wifiManager; WiFiClient wifiClient; // for HTTP requests +String lastChecksum = ""; void setup() { Serial.begin(115200); @@ -116,7 +117,6 @@ void showRawBitmapFrom_HTTP(const char* host, int16_t bytes_per_row, int16_t rows_at_once) { bool connection_ok = false; - bool valid = false; // valid format to be handled uint32_t startTime = millis(); if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return; @@ -158,54 +158,61 @@ void showRawBitmapFrom_HTTP(const char* host, } DEBUG_PRINT("Parsing bitmap header"); - if (read16(wifiClient) == 0x4D4D) // "MM" signature + String line = wifiClient.readStringUntil('\n'); + if (line != "MM") // signature { - uint32_t bytes_read = 2; // read so far - DEBUG_PRINT("w=%d, h=%d", w, h); - - valid = true; - // display.clearScreen(); - - for (uint16_t row = 0; row < h; row += rows_at_once) // for each line - { - if (!connection_ok || !(wifiClient.connected() || wifiClient.available())) - break; - delay(1); // yield() to avoid WDT - yield(); - - uint32_t got = read8n(wifiClient, input_row_mono_buffer, - bytes_per_row * rows_at_once); - bytes_read += got; - - if (!connection_ok) { - Serial.print("Error: got no more after "); - Serial.print(bytes_read); - Serial.println(" bytes read!"); - error("Read from HTTP server failed."); - break; - } + Serial.println("bitmap format not handled."); + error("Invalid bitmap received."); + } + + line = wifiClient.readStringUntil('\n'); // checksum + DEBUG_PRINT("Last checksum was: %s", lastChecksum.c_str()); + DEBUG_PRINT("New checksum is: %s", line.c_str()); + if (line == lastChecksum) { + DEBUG_PRINT("Not refreshing, image is unchanged"); + return; + }; + lastChecksum = line; + + // display.clearScreen(); + + uint32_t bytes_read = 0; // read so far + for (uint16_t row = 0; row < h; row += rows_at_once) // for each line + { + if (!connection_ok || !(wifiClient.connected() || wifiClient.available())) + break; + delay(1); // yield() to avoid WDT + yield(); + + uint32_t got = + read8n(wifiClient, input_row_mono_buffer, bytes_per_row * rows_at_once); + bytes_read += got; + + if (!connection_ok) { + Serial.print("Error: got no more after "); + Serial.print(bytes_read); + Serial.println(" bytes read!"); + error("Read from HTTP server failed."); + break; + } #ifdef USE_GRAYSCALE_DISPLAY - // https://github.com/ZinggJM/GxEPD2_4G/blob/master/src/epd/GxEPD2_750_T7.cpp - display.writeImage_4G(input_row_mono_buffer, 8, x, y + row, w, 1, false, - false, false); + // https://github.com/ZinggJM/GxEPD2_4G/blob/master/src/epd/GxEPD2_750_T7.cpp + display.writeImage_4G(input_row_mono_buffer, 8, x, y + row, w, 1, false, + false, false); #else - display.writeImage(input_row_mono_buffer, x, y + row, w, rows_at_once); + display.writeImage(input_row_mono_buffer, x, y + row, w, rows_at_once); #endif - } // end line - Serial.print("downloaded and displayed in "); - Serial.print(millis() - startTime); - Serial.println(" ms"); - display.refresh(); - - Serial.print("bytes read "); - Serial.println(bytes_read); - } + } // end line + Serial.print("downloaded and displayed in "); + Serial.print(millis() - startTime); + Serial.println(" ms"); + display.refresh(); + + Serial.print("bytes read "); + Serial.println(bytes_read); + wifiClient.stop(); - if (!valid) { - Serial.println("bitmap format not handled."); - error("Invalid bitmap received."); - } } void stopWiFi() {