fix: handle critical bug

Avoid making it get stuck in the main loop
This commit is contained in:
2025-08-14 20:07:06 +02:00
committed by GitHub
parent 32b136d4cc
commit 48f7065a9a

View File

@@ -23,7 +23,6 @@ uint64_t sleepDuration = 30e6; // Default 30 seconds in microseconds
#define EPD_HEIGHT EPD_7IN5B_V2_HEIGHT
// =========== IMAGE TUNING PARAMETERS ===========
// These will be updated from the configuration
uint8_t blackTextThreshold = 190; // Default (0-255)
bool enableDithering = true; // Default
uint8_t ditherStrength = 8; // Default (8-32)
@@ -42,11 +41,49 @@ int16_t *errorB = NULL;
// Create an instance of the JPEG decoder
JPEGDEC jpeg;
// Forward declarations
bool fetchConnectionInformation();
void fetchAndDisplayImage();
void clearDisplay();
// Centralized cleanup + deep sleep
void finishAndSleep(const char* reason) {
Serial.println();
Serial.println("========== Going to deep sleep ==========");
if (reason && reason[0]) {
Serial.print("Reason: ");
Serial.println(reason);
}
// Free dithering buffers if allocated
if (errorR) { free(errorR); errorR = NULL; }
if (errorG) { free(errorG); errorG = NULL; }
if (errorB) { free(errorB); errorB = NULL; }
// Put display to sleep (safe to call even if already sleeping)
EPD_7IN5B_V2_Sleep();
// Free framebuffers
if (BlackImage) { free(BlackImage); BlackImage = NULL; }
if (RYImage) { free(RYImage); RYImage = NULL; }
// Tidy up WiFi to save power before deep sleep
WiFi.disconnect(true, true);
WiFi.mode(WIFI_OFF);
delay(50);
Serial.print("Sleeping for ");
Serial.print(sleepDuration / 60000000);
Serial.println(" minutes");
esp_sleep_enable_timer_wakeup(sleepDuration);
esp_deep_sleep_start();
}
// Apply contrast adjustment to RGB values
void adjustContrast(uint8_t *r, uint8_t *g, uint8_t *b) {
if (!enhanceContrast) return;
float contrast = (contrastLevel / 100.0) + 1.0; // Convert to decimal & shift range: [0..2]
float contrast = (contrastLevel / 100.0) + 1.0; // [1..2]
float intercept = 128 * (1 - contrast);
*r = constrain((*r * contrast) + intercept, 0, 255);
@@ -240,7 +277,8 @@ void setup() {
if ((BlackImage == NULL) || (RYImage == NULL)) {
Serial.println("Failed to allocate memory for framebuffers!");
while(1); // Halt if memory allocation fails
// Even on failure, deep-sleep to allow recovery on next boot
finishAndSleep("Framebuffer allocation failed");
}
// Initialize e-ink display exactly as in Waveshare example
@@ -256,6 +294,7 @@ void setup() {
Serial.println("Buffers allocated and cleared");
// Connect to WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
int wifiAttempts = 0;
@@ -273,7 +312,8 @@ void setup() {
} else {
Serial.println();
Serial.println("WiFi connection failed!");
return;
// IMPORTANT: Do not just return; always deep sleep so we retry later
finishAndSleep("WiFi connection failed");
}
// Test connectivity to server and get configuration
@@ -285,27 +325,8 @@ void setup() {
Serial.println("Server connectivity test failed - skipping image fetch");
}
// Free dithering buffers if allocated
if (errorR) free(errorR);
if (errorG) free(errorG);
if (errorB) free(errorB);
errorR = errorG = errorB = NULL;
// Put display to sleep
EPD_7IN5B_V2_Sleep();
// Free framebuffers
free(BlackImage);
free(RYImage);
BlackImage = NULL;
RYImage = NULL;
// Enter deep sleep
Serial.print("Going to sleep for ");
Serial.print(sleepDuration / 60000000);
Serial.println(" minutes");
esp_sleep_enable_timer_wakeup(sleepDuration);
esp_deep_sleep_start();
// Always finish with cleanup and deep sleep (success or failure)
finishAndSleep("Normal cycle complete");
}
bool fetchConnectionInformation() {
@@ -491,6 +512,7 @@ void fetchAndDisplayImage() {
} else {
totalBytesRead += bytesRead;
}
yield(); // keep WiFi stack happy
}
Serial.print("Total bytes read: ");
Serial.println(totalBytesRead);