Update INFOSCREEN_WITH_INTERVAL.ino

This commit is contained in:
2025-07-19 18:17:10 +02:00
committed by GitHub
parent c987a0bd58
commit e9d145455e

View File

@@ -7,7 +7,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <JPEGDEC.h> #include <JPEGDEC.h>
// WiFi credentials // WiFi credentials (only 2.4ghz)
const char* ssid = "x"; const char* ssid = "x";
const char* password = "x"; const char* password = "x";
@@ -23,16 +23,12 @@ uint64_t sleepDuration = 30e6; // Default 30 seconds in microseconds
#define EPD_HEIGHT EPD_7IN5B_V2_HEIGHT #define EPD_HEIGHT EPD_7IN5B_V2_HEIGHT
// =========== IMAGE TUNING PARAMETERS =========== // =========== IMAGE TUNING PARAMETERS ===========
// Basic color thresholds // These will be updated from the configuration
#define BLACK_TEXT_THRESHOLD 190 // rgb(51,51,51) should be detected as black uint8_t blackTextThreshold = 190; // Default (0-255)
bool enableDithering = true; // Default
// Dithering settings - NEW uint8_t ditherStrength = 8; // Default (8-32)
#define ENABLE_DITHERING true // Set to false to disable dithering bool enhanceContrast = true; // Default
#define DITHER_STRENGTH 8 // Lower values = stronger dithering (8-32) uint8_t contrastLevel = 30; // Default (0-100)
// Contrast settings - NEW
#define ENHANCE_CONTRAST true // Set to false to disable contrast enhancement
#define CONTRAST_LEVEL 30 // Contrast adjustment level (0-100)
// =============================================== // ===============================================
// Framebuffers for black and red layers // Framebuffers for black and red layers
@@ -48,9 +44,9 @@ JPEGDEC jpeg;
// Apply contrast adjustment to RGB values // Apply contrast adjustment to RGB values
void adjustContrast(uint8_t *r, uint8_t *g, uint8_t *b) { void adjustContrast(uint8_t *r, uint8_t *g, uint8_t *b) {
if (!ENHANCE_CONTRAST) return; if (!enhanceContrast) return;
float contrast = (CONTRAST_LEVEL / 100.0) + 1.0; // Convert to decimal & shift range: [0..2] float contrast = (contrastLevel / 100.0) + 1.0; // Convert to decimal & shift range: [0..2]
float intercept = 128 * (1 - contrast); float intercept = 128 * (1 - contrast);
*r = constrain((*r * contrast) + intercept, 0, 255); *r = constrain((*r * contrast) + intercept, 0, 255);
@@ -68,7 +64,7 @@ int jpegDrawCallback(JPEGDRAW *pDraw) {
int height = pDraw->iHeight; int height = pDraw->iHeight;
// Initialize error buffers for dithering if needed // Initialize error buffers for dithering if needed
if (ENABLE_DITHERING && errorR == NULL) { if (enableDithering && errorR == NULL) {
errorR = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t)); errorR = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t));
errorG = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t)); errorG = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t));
errorB = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t)); errorB = (int16_t*)malloc(EPD_WIDTH * sizeof(int16_t));
@@ -89,7 +85,7 @@ int jpegDrawCallback(JPEGDRAW *pDraw) {
// Process each row in this MCU block // Process each row in this MCU block
for (int iy = 0; iy < height; iy++) { for (int iy = 0; iy < height; iy++) {
// Reset error buffers for each row // Reset error buffers for each row
if (ENABLE_DITHERING && errorR != NULL) { if (enableDithering && errorR != NULL) {
memset(errorR, 0, EPD_WIDTH * sizeof(int16_t)); memset(errorR, 0, EPD_WIDTH * sizeof(int16_t));
memset(errorG, 0, EPD_WIDTH * sizeof(int16_t)); memset(errorG, 0, EPD_WIDTH * sizeof(int16_t));
memset(errorB, 0, EPD_WIDTH * sizeof(int16_t)); memset(errorB, 0, EPD_WIDTH * sizeof(int16_t));
@@ -112,15 +108,15 @@ int jpegDrawCallback(JPEGDRAW *pDraw) {
uint8_t b = (pixel & 0x1F) << 3; uint8_t b = (pixel & 0x1F) << 3;
// Apply contrast adjustment if enabled // Apply contrast adjustment if enabled
if (ENHANCE_CONTRAST) { if (enhanceContrast) {
adjustContrast(&r, &g, &b); adjustContrast(&r, &g, &b);
} }
// Apply dithering errors if enabled // Apply dithering errors if enabled
if (ENABLE_DITHERING && errorR != NULL) { if (enableDithering && errorR != NULL) {
r = constrain(r + (errorR[pos_x] / DITHER_STRENGTH), 0, 255); r = constrain(r + (errorR[pos_x] / ditherStrength), 0, 255);
g = constrain(g + (errorG[pos_x] / DITHER_STRENGTH), 0, 255); g = constrain(g + (errorG[pos_x] / ditherStrength), 0, 255);
b = constrain(b + (errorB[pos_x] / DITHER_STRENGTH), 0, 255); b = constrain(b + (errorB[pos_x] / ditherStrength), 0, 255);
} }
// Calculate grayscale value // Calculate grayscale value
@@ -138,7 +134,7 @@ int jpegDrawCallback(JPEGDRAW *pDraw) {
finalColor = 2; // Red finalColor = 2; // Red
} }
// If not red, determine if it's black or white based on grayscale // If not red, determine if it's black or white based on grayscale
else if (gray < BLACK_TEXT_THRESHOLD) { else if (gray < blackTextThreshold) {
finalColor = 0; // Black finalColor = 0; // Black
} }
else { else {
@@ -162,7 +158,7 @@ int jpegDrawCallback(JPEGDRAW *pDraw) {
} }
// Calculate and distribute dithering errors // Calculate and distribute dithering errors
if (ENABLE_DITHERING && errorR != NULL) { if (enableDithering && errorR != NULL) {
int16_t err_r = r - targetR; int16_t err_r = r - targetR;
int16_t err_g = g - targetG; int16_t err_g = g - targetG;
int16_t err_b = b - targetB; int16_t err_b = b - targetB;
@@ -235,6 +231,12 @@ void setup() {
while(1); // Halt if memory allocation fails while(1); // Halt if memory allocation fails
} }
// Initialize e-ink display exactly as in Waveshare example
DEV_Module_Init();
EPD_7IN5B_V2_Init();
// EPD_7IN5B_V2_Clear();
DEV_Delay_ms(500);
// Initialize the Paint library with the buffers // Initialize the Paint library with the buffers
Paint_NewImage(BlackImage, EPD_WIDTH, EPD_HEIGHT, 0, WHITE); Paint_NewImage(BlackImage, EPD_WIDTH, EPD_HEIGHT, 0, WHITE);
Paint_NewImage(RYImage, EPD_WIDTH, EPD_HEIGHT, 0, WHITE); Paint_NewImage(RYImage, EPD_WIDTH, EPD_HEIGHT, 0, WHITE);
@@ -277,7 +279,7 @@ void setup() {
Serial.println("Server connectivity test failed - skipping image fetch"); Serial.println("Server connectivity test failed - skipping image fetch");
} }
// Free dithering buffers if allocated - NEW // Free dithering buffers if allocated
if (errorR) free(errorR); if (errorR) free(errorR);
if (errorG) free(errorG); if (errorG) free(errorG);
if (errorB) free(errorB); if (errorB) free(errorB);
@@ -338,8 +340,8 @@ bool fetchConnectionInformation() {
Serial.println(jsonPayload); Serial.println(jsonPayload);
Serial.println("-----EXTRACTED JSON END-----"); Serial.println("-----EXTRACTED JSON END-----");
// Deserialize the JSON document // Deserialize the JSON document - Increased buffer size for more parameters
StaticJsonDocument<512> doc; StaticJsonDocument<768> doc;
DeserializationError error = deserializeJson(doc, jsonPayload); DeserializationError error = deserializeJson(doc, jsonPayload);
if (error) { if (error) {
Serial.print("JSON parsing failed: "); Serial.print("JSON parsing failed: ");
@@ -370,6 +372,37 @@ bool fetchConnectionInformation() {
// Keep default sleep duration // Keep default sleep duration
} }
// Extract new image processing parameters
if (doc.containsKey("blackTextThreshold")) {
blackTextThreshold = doc["blackTextThreshold"].as<uint8_t>();
Serial.print("Black text threshold set to: ");
Serial.println(blackTextThreshold);
}
if (doc.containsKey("enableDithering")) {
enableDithering = doc["enableDithering"].as<bool>();
Serial.print("Dithering enabled: ");
Serial.println(enableDithering ? "true" : "false");
}
if (doc.containsKey("ditheringStrength")) {
ditherStrength = doc["ditheringStrength"].as<uint8_t>();
Serial.print("Dithering strength set to: ");
Serial.println(ditherStrength);
}
if (doc.containsKey("enhanceContrast")) {
enhanceContrast = doc["enhanceContrast"].as<bool>();
Serial.print("Contrast enhancement enabled: ");
Serial.println(enhanceContrast ? "true" : "false");
}
if (doc.containsKey("contrastStrength")) {
contrastLevel = doc["contrastStrength"].as<uint8_t>();
Serial.print("Contrast level set to: ");
Serial.println(contrastLevel);
}
http.end(); http.end();
return true; return true;
} else { } else {
@@ -479,12 +512,6 @@ void fetchAndDisplayImage() {
// Close the file // Close the file
jpeg.close(); jpeg.close();
// Initialize e-ink display exactly as in Waveshare example
DEV_Module_Init();
EPD_7IN5B_V2_Init();
EPD_7IN5B_V2_Clear();
DEV_Delay_ms(500);
// Display the processed image - using Waveshare's function // Display the processed image - using Waveshare's function
Serial.println("Sending image to display..."); Serial.println("Sending image to display...");