From f6ef4ce802625c6c6a1afa51fd19b6b47bc4c224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sori=C4=87?= Date: Tue, 3 Sep 2024 12:03:30 +0200 Subject: [PATCH] Fixed a buffer overflow in Inkplate 10 example https://github.com/SolderedElectronics/Inkplate-Arduino-library/issues/256 This still needs to be updated for all other Inkplate boards --- .../Inkplate10_Google_Calendar.ino | 57 +++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino index 59f5c562..74123397 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino @@ -107,7 +107,29 @@ void setup() display.setTextColor(0, 7); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -142,6 +164,26 @@ void loop() // Never here } +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -457,9 +499,14 @@ void drawData() getToFrom(entries[entriesNum].time, timeStart, timeEnd, &entries[entriesNum].day, &entries[entriesNum].timeStamp); } + // Increment the counter ++entriesNum; + // If we're over the limit, exit the loading loop + if(entriesNum == 128) + { + break; + } } - // Sort entries by time qsort(entries, entriesNum, sizeof(entry), cmp); @@ -468,9 +515,11 @@ void drawData() bool clogged[3] = {0}; int cloggedCount[3] = {0}; + // If required, uncomment this line of debug if required, could be useful: + //Serial.println("Displaying events one by one. There is " + String(entriesNum) + " events to display."); // Displaying events one by one for (int i = 0; i < entriesNum; ++i) - { + { // If column overflowed just add event to not shown if (entries[i].day != -1 && clogged[entries[i].day]) ++cloggedCount[entries[i].day]; @@ -480,7 +529,6 @@ void drawData() // We store how much height did one event take up int shift = 0; bool s = drawEvent(&entries[i], entries[i].day, columns[entries[i].day] + 64, 1200 - 4, &shift); - columns[entries[i].day] += shift; // If it overflowed, set column to clogged and add one event as not shown @@ -489,6 +537,7 @@ void drawData() ++cloggedCount[entries[i].day]; clogged[entries[i].day] = 1; } + delay(100); } // Display not shown events info