Skip to content

Commit

Permalink
checksum support
Browse files Browse the repository at this point in the history
  • Loading branch information
misch2 committed Jan 27, 2023
1 parent d863afb commit a1bb7f0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down
93 changes: 50 additions & 43 deletions client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GxEPD2_BW<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT / 2> display(

WiFiManager wifiManager;
WiFiClient wifiClient; // for HTTP requests
String lastChecksum = "";

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a1bb7f0

Please sign in to comment.