From c9ce5c6317ee99c181e2f234f71f2879bc71b8e6 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sat, 8 Aug 2026 10:04:58 +0100 Subject: [PATCH] Encapsulate util.cpp's own runtime state as static, not global 2 WLED_GLOBAL variables were referenced only in util.cpp: jsonBufferLock, jsonBufferLockMutex. Converted both to file-local `static`, preserving the #if defined(ARDUINO_ARCH_ESP32) guard around jsonBufferLockMutex. No behavior change - purely a storage-class change. Verified: esp32dev builds and links cleanly via `pio run -e esp32dev` (1,320,319 bytes flash, no warnings from either changed file). Co-Authored-By: Claude Sonnet 5 --- wled00/util.cpp | 7 +++++++ wled00/wled.h | 5 +---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/wled00/util.cpp b/wled00/util.cpp index 8ac46c77df..5063c1180b 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -262,6 +262,13 @@ size_t utf8_strlen(const char *s) return len; } +// Runtime state private to this file - previously WLED_GLOBAL, a leftover from +// when all state lived in one big extern block regardless of who used it. +#if defined(ARDUINO_ARCH_ESP32) +static SemaphoreHandle_t jsonBufferLockMutex = xSemaphoreCreateRecursiveMutex(); +#endif +static volatile uint8_t jsonBufferLock = 0; + //threading/network callback details: https://github.com/wled-dev/WLED/pull/2336#discussion_r762276994 bool requestJSONBufferLock(uint8_t moduleID) { diff --git a/wled00/wled.h b/wled00/wled.h index 9bafb49196..bee7dbf9c2 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -854,9 +854,7 @@ WLED_GLOBAL int8_t spi_sclk _INIT(SPISCLKPIN); #endif // global ArduinoJson buffer -#if defined(ARDUINO_ARCH_ESP32) -WLED_GLOBAL SemaphoreHandle_t jsonBufferLockMutex _INIT(xSemaphoreCreateRecursiveMutex()); -#endif +// jsonBufferLockMutex/jsonBufferLock are private to util.cpp - see there. #ifdef BOARD_HAS_PSRAM // if board has PSRAM, use it for JSON document (allocated in setup()) WLED_GLOBAL JsonDocument *pDoc _INIT(nullptr); @@ -864,7 +862,6 @@ WLED_GLOBAL JsonDocument *pDoc _INIT(nullptr); WLED_GLOBAL StaticJsonDocument gDoc; WLED_GLOBAL JsonDocument *pDoc _INIT(&gDoc); #endif -WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); // enable additional debug output #if defined(WLED_DEBUG_HOST)