Skip to content

Commit 36fb9b3

Browse files
authored
feature: Now we can set the background and foreground from webscreen.json (#15)
1 parent b76b750 commit 36fb9b3

File tree

7 files changed

+37554
-37601
lines changed

7 files changed

+37554
-37601
lines changed
-1.19 KB
Binary file not shown.
-35.1 KB
Binary file not shown.

webscreen/build/esp32.esp32.esp32s3/webscreen.ino.map

Lines changed: 37519 additions & 37584 deletions
Large diffs are not rendered by default.
0 Bytes
Binary file not shown.

webscreen/globals.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#pragma once
22
#include <Arduino.h>
33
#include "log.h"
4+
#include <stdint.h>
45

56
// Declare the global script filename variable
67
extern String g_script_filename;
78

89
// Add this new global flag
9-
extern bool g_mqtt_enabled;
10+
extern bool g_mqtt_enabled;
11+
12+
// ADD these new global variables for screen colors
13+
extern uint32_t g_bg_color;
14+
extern uint32_t g_fg_color;

webscreen/lvgl_elk.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ void init_lvgl_display() {
281281
disp_drv.draw_buf = &draw_buf;
282282
lv_disp_drv_register(&disp_drv);
283283

284+
lv_obj_t * scr = lv_scr_act();
285+
286+
// Set the screen background color directly on the screen object
287+
lv_obj_set_style_bg_color(scr, lv_color_hex(g_bg_color), 0);
288+
289+
// Set the default text color for any labels created on the screen
290+
// (This will be inherited by children unless they have their own color set)
291+
lv_obj_set_style_text_color(scr, lv_color_hex(g_fg_color), 0);
292+
284293
LOG("LVGL + Display initialized.");
285294
}
286295

webscreen/webscreen.ino

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ bool g_mqtt_enabled = false;
1717
// Global flag to decide fallback vs dynamic
1818
static bool useFallback = false;
1919

20-
static bool readConfigJSON(const char* path, String &outSSID, String &outPASS, String &outScript, bool &outMqttEnabled) {
20+
// Define global color variables with default values
21+
uint32_t g_bg_color = 0x000000; // Default: black
22+
uint32_t g_fg_color = 0xFFFFFF; // Default: white
23+
24+
static bool readConfigJSON(
25+
const char* path,
26+
String &outSSID,
27+
String &outPASS,
28+
String &outScript,
29+
bool &outMqttEnabled,
30+
uint32_t &outBgColor,
31+
uint32_t &outFgColor
32+
) {
2133
File f = SD_MMC.open(path);
2234
if (!f) {
2335
LOG("No JSON file");
@@ -42,21 +54,13 @@ static bool readConfigJSON(const char* path, String &outSSID, String &outPASS, S
4254
// Extract the script filename
4355
outScript = doc["script"] | "app.js";
4456

45-
// Update 'last_read' if desired
46-
time_t now;
47-
time(&now);
48-
doc["last_read"] = (unsigned long)now;
57+
const char* bgColorStr = doc["screen"]["background"] | "#000000";
58+
const char* fgColorStr = doc["screen"]["foreground"] | "#FFFFFF";
4959

50-
// Optionally write back the updated JSON
51-
String updated;
52-
serializeJson(doc, updated);
53-
f = SD_MMC.open(path, FILE_WRITE);
54-
if (!f) {
55-
LOG("Failed to open JSON for writing");
56-
return false;
57-
}
58-
f.print(updated);
59-
f.close();
60+
// Convert hex color string (e.g., "#RRGGBB") to a 32-bit integer
61+
// We use strtol with base 16 and skip the leading '#' character.
62+
outBgColor = strtol(bgColorStr + 1, NULL, 16);
63+
outFgColor = strtol(fgColorStr + 1, NULL, 16);
6064

6165
return true;
6266
}
@@ -80,7 +84,7 @@ void setup() {
8084

8185
// Optionally read /webscreen.json for Wi-Fi
8286
String s, p, scriptFile;
83-
if(!readConfigJSON("/webscreen.json", s, p, scriptFile, g_mqtt_enabled)) {
87+
if(!readConfigJSON("/webscreen.json", s, p, scriptFile, g_mqtt_enabled, g_bg_color, g_fg_color)) {
8488
LOG("Failed to read /webscreen.json => fallback");
8589
useFallback = true;
8690
fallback_setup();

0 commit comments

Comments
 (0)