From 4df51db9ad1f4ed081745f0ce3886fd0f508cc2b Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sat, 5 Sep 2026 15:39:48 +0900 Subject: [PATCH 01/35] Add M5Stack CoreS3 support --- docs/M5Stack_CoreS3.md | 20 + pio-scripts/cores3_upload_watchdog_reset.py | 51 + pio-scripts/cores3_v17_neopixelbus_patch.py | 519 ++ usermods/CoreS3_Audio/CoreS3_Audio.cpp | 599 ++ usermods/CoreS3_Audio/library.json | 11 + usermods/CoreS3_Display/CoreS3_Display.cpp | 7865 +++++++++++++++++ usermods/CoreS3_Display/CoreS3_WLED_Logo.h | 164 + .../CoreS3_WLED_Logo_304x95.png | Bin 0 -> 2382 bytes .../M5StackDisplayHardwareBackend.cpp | 595 ++ .../M5StackDisplayHardwareBackend.h | 124 + .../M5StackDisplayTouchContext.h | 20 + .../M5StackDisplayTouchHelpers.h | 80 + .../CoreS3_Display/M5StackDisplayTouchState.h | 191 + .../M5StackDisplayTouchStateMachine.inc | 1720 ++++ usermods/CoreS3_Display/M5StackDisplayUI.h | 68 + usermods/CoreS3_Display/library.json | 11 + .../platformio_override.ini.example | 79 + usermods/CoreS3_Display/readme.md | 622 ++ usermods/CoreS3_Display/readme_jp.md | 624 ++ .../screenshots/color-c1-c2.png | Bin 0 -> 2308 bytes .../CoreS3_Display/screenshots/color-c1.png | Bin 0 -> 2173 bytes .../screenshots/color-unused.png | Bin 0 -> 1377 bytes .../screenshots/effect-rocktaves.png | Bin 0 -> 1244 bytes .../screenshots/effect-solid.png | Bin 0 -> 1892 bytes usermods/CoreS3_Display/screenshots/main.png | Bin 0 -> 2339 bytes .../screenshots/preset-boot.png | Bin 0 -> 2199 bytes .../screenshots/preset-delete.png | Bin 0 -> 2096 bytes .../screenshots/preset-manage.png | Bin 0 -> 1766 bytes .../screenshots/preset-save.png | Bin 0 -> 1806 bytes .../CoreS3_Display/screenshots/preset.png | Bin 0 -> 1802 bytes usermods/CoreS3_Power/CoreS3_Power.cpp | 1293 +++ usermods/CoreS3_Power/library.json | 11 + usermods/audioreactive/audio_reactive.cpp | 294 +- usermods/audioreactive/audio_source.h | 169 +- wled00/wled.cpp | 28 +- 35 files changed, 15102 insertions(+), 56 deletions(-) create mode 100644 docs/M5Stack_CoreS3.md create mode 100644 pio-scripts/cores3_upload_watchdog_reset.py create mode 100644 pio-scripts/cores3_v17_neopixelbus_patch.py create mode 100644 usermods/CoreS3_Audio/CoreS3_Audio.cpp create mode 100644 usermods/CoreS3_Audio/library.json create mode 100644 usermods/CoreS3_Display/CoreS3_Display.cpp create mode 100644 usermods/CoreS3_Display/CoreS3_WLED_Logo.h create mode 100644 usermods/CoreS3_Display/CoreS3_WLED_Logo_304x95.png create mode 100644 usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp create mode 100644 usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h create mode 100644 usermods/CoreS3_Display/M5StackDisplayTouchContext.h create mode 100644 usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h create mode 100644 usermods/CoreS3_Display/M5StackDisplayTouchState.h create mode 100644 usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc create mode 100644 usermods/CoreS3_Display/M5StackDisplayUI.h create mode 100644 usermods/CoreS3_Display/library.json create mode 100644 usermods/CoreS3_Display/platformio_override.ini.example create mode 100644 usermods/CoreS3_Display/readme.md create mode 100644 usermods/CoreS3_Display/readme_jp.md create mode 100644 usermods/CoreS3_Display/screenshots/color-c1-c2.png create mode 100644 usermods/CoreS3_Display/screenshots/color-c1.png create mode 100644 usermods/CoreS3_Display/screenshots/color-unused.png create mode 100644 usermods/CoreS3_Display/screenshots/effect-rocktaves.png create mode 100644 usermods/CoreS3_Display/screenshots/effect-solid.png create mode 100644 usermods/CoreS3_Display/screenshots/main.png create mode 100644 usermods/CoreS3_Display/screenshots/preset-boot.png create mode 100644 usermods/CoreS3_Display/screenshots/preset-delete.png create mode 100644 usermods/CoreS3_Display/screenshots/preset-manage.png create mode 100644 usermods/CoreS3_Display/screenshots/preset-save.png create mode 100644 usermods/CoreS3_Display/screenshots/preset.png create mode 100644 usermods/CoreS3_Power/CoreS3_Power.cpp create mode 100644 usermods/CoreS3_Power/library.json diff --git a/docs/M5Stack_CoreS3.md b/docs/M5Stack_CoreS3.md new file mode 100644 index 0000000000..5609d139ae --- /dev/null +++ b/docs/M5Stack_CoreS3.md @@ -0,0 +1,20 @@ +# M5Stack CoreS3 Support for WLED + +The M5Stack CoreS3 integration combines multiple usermods and CoreS3-specific build support, including the local touch UI, power management, built-in microphone Audio Reactive support, and ESP32-S3 RMT stabilization. + +The complete documentation is maintained with the CoreS3 Display usermod: + +- **English:** [M5Stack CoreS3 Documentation](../usermods/CoreS3_Display/readme.md) +- **日本語:** [M5Stack CoreS3 日本語ドキュメント](../usermods/CoreS3_Display/readme_jp.md) + +## Main Components + +- `usermods/CoreS3_Power/` +- `usermods/CoreS3_Display/` +- `usermods/CoreS3_Audio/` +- `usermods/audioreactive/` +- `pio-scripts/cores3_upload_watchdog_reset.py` +- `pio-scripts/cores3_v17_neopixelbus_patch.py` +- `usermods/CoreS3_Display/platformio_override.ini.example` + +The CoreS3-specific implementation is kept outside the WLED core as much as practical, with only a minimal integration hook in `wled00/wled.cpp`, to make future upstream synchronization easier. diff --git a/pio-scripts/cores3_upload_watchdog_reset.py b/pio-scripts/cores3_upload_watchdog_reset.py new file mode 100644 index 0000000000..57c955fa63 --- /dev/null +++ b/pio-scripts/cores3_upload_watchdog_reset.py @@ -0,0 +1,51 @@ +# M5Stack CoreS3 upload reset stabilization +# +# Purpose: +# ESP32-S3 native USB / USB-Serial-JTAG uploads can occasionally remain +# in download mode after PlatformIO's default RTS hard reset. +# +# PlatformIO's ESP32 builder constructs esptool's --after argument from: +# board.get("upload.after_reset", "hard_reset") +# +# Set that board option early, before the platform builder creates +# UPLOADERFLAGS. This changes only the post-upload reset method. +# +# Expected esptool behavior: +# --after watchdog-reset +# +# No firmware source, build flag, partition, PSRAM, flash, or runtime +# behavior is modified by this script. + +Import("env") + +TARGET_RESET_METHOD = "watchdog-reset" + + +def configure_upload_reset(): + board = env.BoardConfig() + previous = board.get("upload.after_reset", "hard_reset") + + if previous == TARGET_RESET_METHOD: + print( + "[CoreS3 Upload] post-upload reset already set to " + f"{TARGET_RESET_METHOD}" + ) + return + + board.update("upload.after_reset", TARGET_RESET_METHOD) + + effective = board.get("upload.after_reset", "") + if effective != TARGET_RESET_METHOD: + raise RuntimeError( + "CoreS3 upload reset configuration failed: " + f"expected {TARGET_RESET_METHOD}, got {effective}" + ) + + print( + "[CoreS3 Upload] post-upload reset: " + f"{previous} -> {TARGET_RESET_METHOD}" + ) + + +if not env.IsIntegrationDump(): + configure_upload_reset() diff --git a/pio-scripts/cores3_v17_neopixelbus_patch.py b/pio-scripts/cores3_v17_neopixelbus_patch.py new file mode 100644 index 0000000000..52de00242f --- /dev/null +++ b/pio-scripts/cores3_v17_neopixelbus_patch.py @@ -0,0 +1,519 @@ +# M5Stack CoreS3 / WLED V17 NeoPixelBus Production Integration +# +# PlatformIO PRE script for the m5stack_cores3 environment. +# +# This script makes the two hardware-validated NeoPixelBus fixes reproducible: +# +# 1) ESP32-S3 RMT output: +# - DMA enabled +# - 1024-symbol TX buffer +# - non-blocking readiness check without ESP-IDF timeout=0 log noise +# - guarded RMT handle lifecycle +# +# 2) ESP32-S3 LCD/GDMA output: +# - fully stop/reset/disconnect/delete the GDMA channel when the last +# LCD mux bus is destroyed +# - prevents stale LCD peripheral ownership across runtime bus rebuilds +# +# The script patches the NeoPixelBus source downloaded by PlatformIO under +# .pio/libdeps. It does not require modified library files to be committed. +# +# NeoPixelBus itself is licensed under LGPL-3.0-or-later. This script keeps +# the upstream license header in the patched files untouched. +# +# The patch is idempotent and is safe to run on every PlatformIO build. + +from pathlib import Path + +Import("env") + +RMT_MARKER = "CoreS3 NeoPixelBus RMT DMA1024 production patch" +LCD_MARKER = "CoreS3 NeoPixelBus LCD GDMA teardown production patch" + +LCD_TEST_PRAGMAS = ( + '#pragma message("=== CORES3 TEST: ACTIVE NeoPixelBus NeoEsp32LcdXMethod.h IS COMPILED ===")', + '#pragma message("=== CORES3 TEST: NeoEsp32LcdXMethod.h IS COMPILED ===")', +) + + +def _replace_function(source: str, signature: str, replacement: str) -> str: + '''Replace one C++ function body by matching balanced braces.''' + sig_pos = source.find(signature) + if sig_pos < 0: + raise RuntimeError(f"signature not found: {signature}") + + brace_start = source.find("{", sig_pos + len(signature)) + if brace_start < 0: + raise RuntimeError(f"opening brace not found: {signature}") + + depth = 0 + in_string = False + in_char = False + escape = False + line_comment = False + block_comment = False + i = brace_start + + while i < len(source): + ch = source[i] + nxt = source[i + 1] if i + 1 < len(source) else "" + + if line_comment: + if ch == "\n": + line_comment = False + i += 1 + continue + + if block_comment: + if ch == "*" and nxt == "/": + block_comment = False + i += 2 + continue + i += 1 + continue + + if escape: + escape = False + i += 1 + continue + + if (in_string or in_char) and ch == "\\": + escape = True + i += 1 + continue + + if not in_string and not in_char: + if ch == "/" and nxt == "/": + line_comment = True + i += 2 + continue + if ch == "/" and nxt == "*": + block_comment = True + i += 2 + continue + + if not in_char and ch == '"': + in_string = not in_string + i += 1 + continue + + if not in_string and ch == "'": + in_char = not in_char + i += 1 + continue + + if not in_string and not in_char: + if ch == "{": + depth += 1 + elif ch == "}": + depth -= 1 + if depth == 0: + return source[:sig_pos] + replacement + source[i + 1:] + + i += 1 + + raise RuntimeError(f"matching closing brace not found: {signature}") + + +def _find_rmt_target() -> Path: + '''Locate the active NeoPixelBus RMT header used by this PIO environment.''' + libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") + + candidates = [] + for path in libdeps_root.rglob("NeoEsp32RmtXMethod.h"): + try: + text = path.read_text(encoding="utf-8") + except Exception: + continue + + if "NeoEsp32RmtMethodBase" in text and "rmt_new_tx_channel" in text: + candidates.append(path) + + # WLED's source-pinned NeoPixelBus package is installed as NeoPixelBus@src-... + # Prefer that copy when PlatformIO also leaves another NeoPixelBus directory. + preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] + + if len(preferred) == 1: + return preferred[0] + if len(candidates) == 1: + return candidates[0] + + detail = "\n".join(f" {p}" for p in candidates) or " " + raise RuntimeError( + "CoreS3 NeoPixelBus patch could not uniquely locate " + "NeoEsp32RmtXMethod.h:\n" + detail + ) + + +def _find_lcd_target(rmt_target: Path) -> Path: + ''' + Locate NeoEsp32LcdXMethod.h from the same active NeoPixelBus package. + + The sibling lookup is intentionally preferred so RMT and LCD patches can + never be applied to different NeoPixelBus copies in .pio/libdeps. + ''' + sibling = rmt_target.with_name("NeoEsp32LcdXMethod.h") + if sibling.is_file(): + try: + text = sibling.read_text(encoding="utf-8") + except Exception as exc: + raise RuntimeError( + f"CoreS3 LCD patch could not read {sibling}: {exc}" + ) from exc + + if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: + return sibling + + libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") + candidates = [] + + for path in libdeps_root.rglob("NeoEsp32LcdXMethod.h"): + try: + text = path.read_text(encoding="utf-8") + except Exception: + continue + + if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: + candidates.append(path) + + preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] + + if len(preferred) == 1: + return preferred[0] + if len(candidates) == 1: + return candidates[0] + + detail = "\n".join(f" {p}" for p in candidates) or " " + raise RuntimeError( + "CoreS3 NeoPixelBus patch could not uniquely locate " + "NeoEsp32LcdXMethod.h:\n" + detail + ) + + +def _patch_rmt(target: Path) -> None: + '''Apply the already validated ESP32-S3 RMT DMA1024 production patch.''' + text = target.read_text(encoding="utf-8") + + if RMT_MARKER in text: + required = [ + "config.mem_block_symbols = 1024;", + "config.flags.with_dma = true;", + "return (_channel != nullptr && _led_encoder != nullptr);", + ] + missing = [item for item in required if item not in text] + if missing: + raise RuntimeError( + "CoreS3 RMT patch marker exists but verification failed; " + f"missing: {missing}" + ) + + print(f"[CoreS3 RMT DMA1024] patch already present: {target.name}") + return + + destructor = f''' ~NeoEsp32RmtMethodBase() + {{ + // {RMT_MARKER} + if (_channel != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT( + rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS) + ); + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_disable(_channel)); + }} + + if (_led_encoder != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); + _led_encoder = nullptr; + }} + + if (_channel != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + }} + + gpio_matrix_out(_pin, 0x100, false, false); + pinMode(_pin, INPUT); + + free(_dataEditing); + free(_dataSending); + }}''' + + ready = f''' bool IsReadyToUpdate() const + {{ + // {RMT_MARKER} + // ESP-IDF 5.5 emits an error log when timeout=0 is used as a busy poll. + // Update() performs the actual completion wait before transmitting. + return (_channel != nullptr && _led_encoder != nullptr); + }}''' + + initialize = f''' void Initialize() + {{ + // {RMT_MARKER} + rmt_tx_channel_config_t config = {{}}; + config.clk_src = RMT_CLK_SRC_DEFAULT; + config.gpio_num = static_cast(_pin); + +#if defined(CONFIG_IDF_TARGET_ESP32S3) + // CoreS3 / ESP32-S3 uses DMA with a 1024-symbol buffer. + // A 32-pixel RGB frame is 768 RMT symbols, so the complete frame + // fits in one DMA buffer and avoids the refill boundary implicated + // in the previously observed tail-pixel anomaly. + config.mem_block_symbols = 1024; +#else + config.mem_block_symbols = 192; +#endif + + config.resolution_hz = T_SPEED::RmtTicksPerSecond; + config.trans_queue_depth = 4; + config.flags.invert_out = T_INVERTED::Inverted; +#if defined(CONFIG_IDF_TARGET_ESP32S3) + config.flags.with_dma = true; +#else + config.flags.with_dma = false; +#endif + + esp_err_t ret = rmt_new_tx_channel(&config, &_channel); + if (ret != ESP_OK || _channel == nullptr) + {{ + _channel = nullptr; + return; + }} + + led_strip_encoder_config_t encoder_config = {{}}; + encoder_config.resolution = T_SPEED::RmtTicksPerSecond; + _tx_config.loop_count = 0; + + ret = rmt_new_led_strip_encoder( + &encoder_config, + &_led_encoder, + T_SPEED::RmtBit0, + T_SPEED::RmtBit1 + ); + + if (ret != ESP_OK || _led_encoder == nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + _led_encoder = nullptr; + return; + }} + + ret = rmt_enable(_channel); + if (ret != ESP_OK) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); + _led_encoder = nullptr; + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + return; + }} + }}''' + + update = f''' void Update(bool maintainBufferConsistency) + {{ + // {RMT_MARKER} + if (_channel == nullptr || _led_encoder == nullptr) + {{ + return; + }} + + // Serialize writes: wait for the previous asynchronous RMT transfer + // to finish before starting the next frame. + if (ESP_OK == ESP_ERROR_CHECK_WITHOUT_ABORT( + rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS))) + {{ + const esp_err_t ret = rmt_transmit( + _channel, + _led_encoder, + _dataEditing, + _sizeData, + &_tx_config + ); + + if (ret != ESP_OK) + {{ + return; + }} + + if (maintainBufferConsistency) + {{ + memcpy(_dataSending, _dataEditing, _sizeData); + }} + + std::swap(_dataSending, _dataEditing); + }} + }}''' + + try: + text = _replace_function( + text, "~NeoEsp32RmtMethodBase()", destructor + ) + text = _replace_function( + text, "bool IsReadyToUpdate() const", ready + ) + text = _replace_function( + text, "void Initialize()", initialize + ) + text = _replace_function( + text, "void Update(bool maintainBufferConsistency)", update + ) + except RuntimeError as exc: + raise RuntimeError( + f"CoreS3 RMT DMA1024 patch failed for {target}: {exc}" + ) from exc + + required = [ + RMT_MARKER, + "config.mem_block_symbols = 1024;", + "config.flags.with_dma = true;", + "return (_channel != nullptr && _led_encoder != nullptr);", + ] + for item in required: + if item not in text: + raise RuntimeError( + f"CoreS3 RMT DMA1024 verification failed: missing {item}" + ) + + target.write_text(text, encoding="utf-8", newline="\n") + print( + "[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / " + f"1024-symbol patch: {target}" + ) + + +def _patch_lcd_gdma(target: Path) -> None: + '''Apply the validated LCD/GDMA full teardown and remove TEST-only markers.''' + text = target.read_text(encoding="utf-8") + original = text + + # Remove the compile-time TEST banner used during hardware diagnosis. + for pragma in LCD_TEST_PRAGMAS: + text = text.replace(pragma + "\n\n", "") + text = text.replace(pragma + "\r\n\r\n", "") + text = text.replace(pragma + "\n", "") + text = text.replace(pragma + "\r\n", "") + text = text.replace(pragma, "") + + if LCD_MARKER not in text: + destruct = f''' void Destruct() + {{ + if (_dmaItems == nullptr) + {{ + return; + }} + + // {LCD_MARKER} + // + // gdma_reset() alone resets the channel state but does not release + // the LCD peripheral ownership held by the GDMA driver. A runtime + // WLED bus rebuild can therefore fail on the next gdma_connect(). + // + // NeoEsp32LcdXMethodBase waits for the active LCD transfer to finish + // before DeregisterMuxBus() reaches this teardown. + if (_dmaChannel != nullptr) + {{ + esp_err_t dmaResult; + + dmaResult = gdma_stop(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA stop during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_reset(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA reset during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_disconnect(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA disconnect during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_del_channel(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA delete during teardown failed: %d", + (int)dmaResult + ); + }} + + _dmaChannel = nullptr; + }} + + periph_module_disable(PERIPH_LCD_CAM_MODULE); + periph_module_reset(PERIPH_LCD_CAM_MODULE); + + heap_caps_free(LcdBuffer); + heap_caps_free(_dmaItems); + + LcdBufferSize = 0; + _dmaItems = nullptr; + LcdBuffer = nullptr; + + MuxMap.Reset(); + }}''' + + try: + text = _replace_function(text, "void Destruct()", destruct) + except RuntimeError as exc: + raise RuntimeError( + f"CoreS3 LCD GDMA teardown patch failed for {target}: {exc}" + ) from exc + + required = [ + LCD_MARKER, + "gdma_stop(_dmaChannel);", + "gdma_reset(_dmaChannel);", + "gdma_disconnect(_dmaChannel);", + "gdma_del_channel(_dmaChannel);", + "_dmaChannel = nullptr;", + ] + for item in required: + if item not in text: + raise RuntimeError( + f"CoreS3 LCD GDMA verification failed: missing {item}" + ) + + for pragma in LCD_TEST_PRAGMAS: + if pragma in text: + raise RuntimeError( + "CoreS3 LCD GDMA production cleanup failed: " + f"TEST pragma still present: {pragma}" + ) + + if text == original: + print(f"[CoreS3 LCD GDMA] patch already present: {target.name}") + return + + target.write_text(text, encoding="utf-8", newline="\n") + print( + "[CoreS3 LCD GDMA] applied full GDMA teardown production patch: " + f"{target}" + ) + + +def _apply_patches() -> None: + rmt_target = _find_rmt_target() + lcd_target = _find_lcd_target(rmt_target) + + _patch_rmt(rmt_target) + _patch_lcd_gdma(lcd_target) + + +if not env.IsIntegrationDump(): + _apply_patches() diff --git a/usermods/CoreS3_Audio/CoreS3_Audio.cpp b/usermods/CoreS3_Audio/CoreS3_Audio.cpp new file mode 100644 index 0000000000..2a9817b82b --- /dev/null +++ b/usermods/CoreS3_Audio/CoreS3_Audio.cpp @@ -0,0 +1,599 @@ +#include "wled.h" +#include +#include + +// =========================================================== +// M5Stack CoreS3 Audio Usermod +// +// CoreS3 Audio Production Baseline +// +// Responsibilities +// - Access the CoreS3 internal I2C bus through the M5GFX +// I2C_NUM_1 owner shared with Display / Touch. +// - Detect the AXP2101 PMU and ES7210 audio codec. +// - Configure and verify the built-in ES7210 dual microphones. +// - Reserve the fixed internal audio pins used by WLED. +// - Publish codec readiness to the AudioReactive usermod. +// - Report hardware and integration status to WLED Info. +// +// Ownership +// CoreS3_Audio +// - GPIO0 / GPIO14 reservation +// - ES7210 configuration +// - shared internal I2C access +// - codec-ready signal +// +// AudioReactive +// - I2S_NUM_1 +// - PCM sampling +// - AGC / FFT +// - audio-reactive effects +// +// This usermod intentionally does not install or read I2S, run FFT, +// change LED state, modify Display / Touch behavior, change power rails, +// or reinitialize the CoreS3 internal I2C bus. +// +// CoreS3 internal audio hardware +// ES7210 I2C : 0x40 +// I2C SDA : GPIO12 +// I2C SCL : GPIO11 +// I2S MCLK : GPIO0 +// I2S BCLK : GPIO34 +// I2S WS : GPIO33 +// I2S DATA : GPIO14 +// I2S Port : I2S_NUM_1 +// Channels : Stereo (MIC1 / MIC2) +// +// M5GFX owns the CoreS3 internal I2C bus after Display initialization. +// Audio therefore uses lgfx::i2c transactions on I2C_NUM_1 and never +// calls Wire.begin(), i2c_driver_install(), lgfx::i2c::init(), or +// release() on that shared bus. +// +// The ES7210 register configuration and pin mapping follow the +// M5Stack/M5Unified CoreS3 microphone implementation. +// =========================================================== + +static volatile bool coreS3AudioCodecReadyState = false; +static volatile bool coreS3AudioInitializationFinishedState = false; + +extern "C" bool coreS3AudioCodecReady() +{ + return coreS3AudioCodecReadyState; +} + +extern "C" bool coreS3AudioInitializationFinished() +{ + return coreS3AudioInitializationFinishedState; +} + +#if defined(WLED_M5STACK_CORES3_AUDIO) +extern "C" bool coreS3AudioReactiveSourceReady(); +#endif + +class CoreS3AudioUsermod : public Usermod +{ +private: + // --------------------------------------------------------- + // CoreS3 hardware constants + // --------------------------------------------------------- + + static constexpr uint8_t ES7210_ADDR = 0x40; + static constexpr uint8_t AXP2101_ADDR = 0x34; + static constexpr int CORES3_I2C_SDA = 12; + static constexpr int CORES3_I2C_SCL = 11; + static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; + static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; + + static constexpr int AUDIO_MCLK_PIN = 0; + static constexpr int AUDIO_BCLK_PIN = 34; + static constexpr int AUDIO_WS_PIN = 33; + static constexpr int AUDIO_DATA_IN_PIN = 14; + static constexpr uint32_t AUDIO_SAMPLE_RATE = 16000; + static constexpr unsigned long AUDIO_INIT_DELAY_MS = 2500; + static constexpr unsigned long AUDIO_INIT_RETRY_MS = 1000; + static constexpr uint8_t AUDIO_INIT_MAX_ATTEMPTS = 5; + + // --------------------------------------------------------- + // Runtime state + // --------------------------------------------------------- + + bool coreS3PinsValid = false; + bool audioPinsReserved = false; + bool es7210Found = false; + bool es7210Configured = false; + bool initializationFinished = false; + uint8_t initAttemptCount = 0; + + unsigned long setupStartMs = 0; + unsigned long lastInitAttemptMs = 0; + + bool axp2101Found = false; + bool axp2101RegistersRead = false; + uint8_t axp2101Reg90 = 0; + uint8_t axp2101Reg93 = 0; + uint8_t es7210ProbeReg00 = 0; + + void finishInitialization() + { + initializationFinished = true; + coreS3AudioInitializationFinishedState = true; + } + + // --------------------------------------------------------- + // CoreS3 internal audio pin reservation + // + // GPIO0 = ES7210 MCLK (output) + // GPIO14 = ES7210 DIN (input) + // + // GPIO33/34 are already unavailable to WLED on this CoreS3 build + // and appear as "System" in Pin Info, so only GPIO0/14 need an + // explicit PinManager reservation. + // + // PinOwner::UM_Audioreactive is used intentionally so WLED Pin Info + // reports these as Usermod-owned and all normal pin selectors treat + // them as unavailable. + // --------------------------------------------------------- + + void neutralizePersistedGpio0Button() + { + if (PinManager::getPinOwner(AUDIO_MCLK_PIN) == PinOwner::Button) { + PinManager::deallocatePin(AUDIO_MCLK_PIN, PinOwner::Button); + } + + for (auto& button : buttons) { + if (button.pin == AUDIO_MCLK_PIN) { + button.pin = -1; + button.type = BTN_TYPE_NONE; + button.pressedBefore = false; + button.longPressed = false; + button.pressedTime = 0; + button.waitTime = 0; + } + } + } + + bool reserveInternalAudioPins() + { + neutralizePersistedGpio0Button(); + + if (PinManager::isPinAllocated(AUDIO_MCLK_PIN, PinOwner::UM_Audioreactive) && + PinManager::isPinAllocated(AUDIO_DATA_IN_PIN, PinOwner::UM_Audioreactive)) { + return true; + } + + if (PinManager::isPinAllocated(AUDIO_MCLK_PIN) || + PinManager::isPinAllocated(AUDIO_DATA_IN_PIN)) { + Serial.printf( + "[CoreS3_Audio] ERROR: internal audio pin conflict MCLK0=%s DIN14=%s\n", + PinManager::getPinOwnerName(AUDIO_MCLK_PIN), + PinManager::getPinOwnerName(AUDIO_DATA_IN_PIN) + ); + return false; + } + + const managed_pin_type audioPins[] = { + { AUDIO_MCLK_PIN, true }, // ES7210 master clock output + { AUDIO_DATA_IN_PIN, false } // ES7210 PCM data input + }; + + if (!PinManager::allocateMultiplePins( + audioPins, + sizeof(audioPins) / sizeof(audioPins[0]), + PinOwner::UM_Audioreactive + )) { + Serial.println(F("[CoreS3_Audio] ERROR: failed to reserve GPIO0/GPIO14 for internal audio")); + return false; + } + + return true; + } + + // --------------------------------------------------------- + // Shared CoreS3 internal I2C helpers + // + // IMPORTANT: + // M5GFX owns the CoreS3 internal bus as I2C_NUM_1 on GPIO12/11. + // Audio must use that same owner after Display initialization. + // Do not call Wire.begin(), i2c_driver_install(), lgfx::i2c::init(), + // release(), or otherwise reinitialize the bus here. + // --------------------------------------------------------- + + bool readRegister(uint8_t address, uint8_t reg, uint8_t& value) + { + auto result = lgfx::i2c::transactionWriteRead( + CORES3_INTERNAL_I2C_PORT, + address, + ®, + 1, + &value, + 1, + CORES3_INTERNAL_I2C_FREQUENCY + ); + + return result.has_value(); + } + + bool writeRegister(uint8_t address, uint8_t reg, uint8_t value) + { + const uint8_t data[2] = { reg, value }; + + auto result = lgfx::i2c::transactionWrite( + CORES3_INTERNAL_I2C_PORT, + address, + data, + sizeof(data), + CORES3_INTERNAL_I2C_FREQUENCY + ); + + return result.has_value(); + } + + // --------------------------------------------------------- + // ES7210 microphone configuration + // + // MIC1 / MIC2 are enabled for the two built-in microphones. + // MIC3 / MIC4 remain powered down. + // --------------------------------------------------------- + + bool configureES7210() + { + struct RegisterValue { + uint8_t reg; + uint8_t value; + }; + + // Reset before applying the CoreS3 microphone profile. + if (!writeRegister(ES7210_ADDR, 0x00, 0xFF)) { + return false; + } + + delay(1); + + static constexpr RegisterValue registers[] = { + { 0x00, 0x41 }, + { 0x01, 0x1F }, + { 0x06, 0x00 }, + { 0x07, 0x20 }, + { 0x08, 0x10 }, + { 0x09, 0x30 }, + { 0x0A, 0x30 }, + { 0x20, 0x0A }, + { 0x21, 0x2A }, + { 0x22, 0x0A }, + { 0x23, 0x2A }, + { 0x02, 0xC1 }, + { 0x04, 0x01 }, + { 0x05, 0x00 }, + { 0x11, 0x60 }, + { 0x40, 0x42 }, + { 0x41, 0x70 }, + { 0x42, 0x70 }, + { 0x43, 0x1B }, + { 0x44, 0x1B }, + { 0x45, 0x00 }, + { 0x46, 0x00 }, + { 0x47, 0x00 }, + { 0x48, 0x00 }, + { 0x49, 0x00 }, + { 0x4A, 0x00 }, + { 0x4B, 0x00 }, + { 0x4C, 0xFF }, + { 0x01, 0x14 } + }; + + for (const auto& item : registers) { + if (!writeRegister(ES7210_ADDR, item.reg, item.value)) { + return false; + } + } + + // Verify a few stable key values instead of assuming the writes succeeded. + uint8_t clockControl = 0; + uint8_t mic1Gain = 0; + uint8_t mic2Gain = 0; + uint8_t mic12Power = 0; + uint8_t mic34Power = 0; + + if (!readRegister(ES7210_ADDR, 0x01, clockControl) || + !readRegister(ES7210_ADDR, 0x43, mic1Gain) || + !readRegister(ES7210_ADDR, 0x44, mic2Gain) || + !readRegister(ES7210_ADDR, 0x4B, mic12Power) || + !readRegister(ES7210_ADDR, 0x4C, mic34Power)) { + return false; + } + + return clockControl == 0x14 && + mic1Gain == 0x1B && + mic2Gain == 0x1B && + mic12Power == 0x00 && + mic34Power == 0xFF; + } + + // --------------------------------------------------------- + // Deferred initialization + // + // Audio initialization is delayed until all WLED usermods have + // completed setup. This avoids depending on usermod registration + // order while the existing CoreS3 Display / Power startup remains + // unchanged. + // --------------------------------------------------------- + + void attemptInitialization() + { + if (!audioPinsReserved) { + Serial.println(F("[CoreS3_Audio] ERROR: audio pin reservation unavailable; codec initialization blocked")); + finishInitialization(); + return; + } + + initAttemptCount++; + + Serial.printf( + "[CoreS3_Audio] Initialization attempt %u/%u\n", + initAttemptCount, + AUDIO_INIT_MAX_ATTEMPTS + ); + + if (i2c_sda != CORES3_I2C_SDA || i2c_scl != CORES3_I2C_SCL) { + Serial.printf( + "[CoreS3_Audio] ERROR: invalid CoreS3 I2C pins SDA=%d SCL=%d\n", + i2c_sda, + i2c_scl + ); + + coreS3PinsValid = false; + finishInitialization(); + return; + } + + coreS3PinsValid = true; + + // Read-only PMU diagnostics. Power-rail ownership remains + // separate from this usermod. Reading AXP2101 register 0x90 also + // verifies that the shared M5GFX I2C_NUM_1 bus is reachable. + axp2101RegistersRead = false; + axp2101Found = readRegister(AXP2101_ADDR, 0x90, axp2101Reg90); + + if (axp2101Found) { + axp2101RegistersRead = + readRegister(AXP2101_ADDR, 0x93, axp2101Reg93); + + if (!axp2101RegistersRead) { + Serial.println(F("[CoreS3_Audio] WARNING: AXP2101 REG93 unavailable on M5GFX I2C1")); + } + } + else { + Serial.println(F("[CoreS3_Audio] WARNING: AXP2101 unavailable on M5GFX I2C1")); + } + + // ES7210 register 0x00 is RESET_CTL and is safe to read. + // This probe does not modify the codec or its power rail. + es7210Found = readRegister(ES7210_ADDR, 0x00, es7210ProbeReg00); + + Serial.printf( + "[CoreS3_Audio] ES7210: %s\n", + es7210Found ? "FOUND" : "NOT FOUND" + ); + + if (!es7210Found) { + if (initAttemptCount >= AUDIO_INIT_MAX_ATTEMPTS) { + Serial.println( + F("[CoreS3_Audio] ES7210 unavailable after retries; microphone initialization stopped") + ); + finishInitialization(); + } + + return; + } + + es7210Configured = configureES7210(); + + Serial.printf( + "[CoreS3_Audio] ES7210 configuration: %s\n", + es7210Configured ? "VERIFIED" : "FAILED" + ); + + if (!es7210Configured) { + finishInitialization(); + return; + } + + coreS3AudioCodecReadyState = true; + finishInitialization(); + + Serial.println(F("[CoreS3_Audio] READY - waiting for AudioReactive I2S1")); + } + + const char* getAudioStatusName() const + { + if (!initializationFinished) { + return "INITIALIZING"; + } + + if (!audioPinsReserved) { + return "AUDIO PIN RESERVATION ERROR"; + } + + if (!coreS3PinsValid) { + return "I2C PIN ERROR"; + } + + if (!es7210Found) { + return "ES7210 NOT FOUND"; + } + + if (!es7210Configured) { + return "ES7210 CONFIG FAILED"; + } + +#if defined(WLED_M5STACK_CORES3_AUDIO) + if (coreS3AudioReactiveSourceReady()) { + return "READY - AudioReactive"; + } + + return "CODEC READY - waiting AudioReactive"; +#else + return "AUDIOREACTIVE BUILD FLAG MISSING"; +#endif + } + +public: + // --------------------------------------------------------- + // WLED Usermod setup + // --------------------------------------------------------- + + void setup() override + { + coreS3AudioCodecReadyState = false; + coreS3AudioInitializationFinishedState = false; + initializationFinished = false; + + Serial.println(); + Serial.println(F("[CoreS3_Audio][BUILD] CoreS3 Audio v0.1.5")); + Serial.println(F("[CoreS3_Audio] Initialization start")); + + audioPinsReserved = reserveInternalAudioPins(); + + Serial.printf( + "[CoreS3_Audio] Internal audio pins: %s (MCLK=GPIO0 DIN=GPIO14)\n", + audioPinsReserved ? "READY" : "FAILED" + ); + + Serial.println(F("[CoreS3_Audio] Codec initialization deferred")); + + setupStartMs = millis(); + lastInitAttemptMs = setupStartMs; + } + + // --------------------------------------------------------- + // WLED Usermod loop + // --------------------------------------------------------- + + void loop() override + { + const unsigned long now = millis(); + + if (!initializationFinished) { + if (now - setupStartMs < AUDIO_INIT_DELAY_MS) { + return; + } + + if (initAttemptCount == 0 || + now - lastInitAttemptMs >= AUDIO_INIT_RETRY_MS) { + lastInitAttemptMs = now; + attemptInitialization(); + } + + return; + } + + // No periodic PCM work here. + // Audio Reactive owns I2S1 sampling and FFT processing. + } + + // --------------------------------------------------------- + // WLED Info status + // --------------------------------------------------------- + + void addToJsonInfo(JsonObject& root) override + { + JsonObject user = root["u"]; + + if (user.isNull()) { + user = root.createNestedObject("u"); + } + + JsonArray statusInfo = user.createNestedArray("CoreS3 Audio"); + statusInfo.add(getAudioStatusName()); + + JsonArray i2cInfo = user.createNestedArray("CoreS3 Audio I2C"); + i2cInfo.add("M5GFX I2C1 GPIO12/GPIO11 400kHz"); + + JsonArray codecInfo = user.createNestedArray("CoreS3 ES7210"); + + if (!initializationFinished && !es7210Found) { + codecInfo.add("Waiting for probe"); + } + else if (!es7210Found) { + codecInfo.add("Not found on M5GFX I2C1 (0x40)"); + } + else if (!es7210Configured) { + codecInfo.add("Found - config failed"); + } + else { + codecInfo.add("Found / configured / verified on I2C1"); + } + + JsonArray integrationInfo = user.createNestedArray("CoreS3 Audio Integration"); + +#if defined(WLED_M5STACK_CORES3_AUDIO) + integrationInfo.add( + coreS3AudioReactiveSourceReady() + ? "AudioReactive source READY" + : (coreS3AudioCodecReadyState + ? "Codec ready / waiting I2S1 source" + : "Waiting for ES7210 codec") + ); +#else + integrationInfo.add("AudioReactive CoreS3 build flag missing"); +#endif + + JsonArray i2sInfo = user.createNestedArray("CoreS3 Audio I2S"); + i2sInfo.add("AudioReactive owner: I2S1 Stereo 16bit 16000Hz"); + + JsonArray pmuInfo = user.createNestedArray("CoreS3 Audio PMU"); + + if (!initializationFinished && !axp2101Found) { + pmuInfo.add("Waiting for probe"); + } + else if (!axp2101Found) { + pmuInfo.add("AXP2101 not found on M5GFX I2C1"); + } + else if (!axp2101RegistersRead) { + pmuInfo.add("AXP2101 found / registers unavailable"); + } + else { + char pmuText[48]; + + snprintf( + pmuText, + sizeof(pmuText), + "REG90=0x%02X REG93=0x%02X", + axp2101Reg90, + axp2101Reg93 + ); + + pmuInfo.add(pmuText); + } + + JsonArray pinReservationInfo = + user.createNestedArray("CoreS3 Audio Pin Reservation"); + + if (audioPinsReserved) { + pinReservationInfo.add("GPIO0 MCLK / GPIO14 DIN RESERVED (Usermod)"); + } + else { + char reservationText[80]; + + snprintf( + reservationText, + sizeof(reservationText), + "FAILED: GPIO0=%s GPIO14=%s", + PinManager::getPinOwnerName(AUDIO_MCLK_PIN), + PinManager::getPinOwnerName(AUDIO_DATA_IN_PIN) + ); + + pinReservationInfo.add(reservationText); + } + + JsonArray pinInfo = user.createNestedArray("CoreS3 Audio Pins"); + pinInfo.add("FIXED: MCLK0 BCLK34 WS33 DIN14"); + } +}; + +// ----------------------------------------------------------- +// Register CoreS3 Audio Usermod with WLED +// ----------------------------------------------------------- + +static CoreS3AudioUsermod coreS3AudioUsermod; +REGISTER_USERMOD(coreS3AudioUsermod); diff --git a/usermods/CoreS3_Audio/library.json b/usermods/CoreS3_Audio/library.json new file mode 100644 index 0000000000..14aa812370 --- /dev/null +++ b/usermods/CoreS3_Audio/library.json @@ -0,0 +1,11 @@ +{ + "name": "CoreS3_Audio", + "version": "0.1.5", + "description": "M5Stack CoreS3 built-in microphone Audio Reactive integration usermod for WLED", + "build": { + "libArchive": false + }, + "dependencies": { + "M5GFX": "https://github.com/m5stack/M5GFX.git#0.2.26" + } +} diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp new file mode 100644 index 0000000000..5ef2a60b29 --- /dev/null +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -0,0 +1,7865 @@ +#include "wled.h" +#include +#include +#include +#include + +#include "M5StackDisplayHardwareBackend.h" +#include "M5StackDisplayUI.h" +#include "M5StackDisplayTouchState.h" +#include "M5StackDisplayTouchHelpers.h" +#include "M5StackDisplayTouchContext.h" + +#include "CoreS3_WLED_Logo.h" + +// =========================================================== +// M5Stack Display Controller Usermod +// +// Current verified runtime +// - M5Stack CoreS3 +// - 320 x 240 display +// - Touch input +// - LCD brightness control +// +// Prepared hardware profiles +// - M5Stack Core2 +// - M5Stack Core2 for AWS +// +// The Core2-family profiles remain diagnostic-only until their +// Display / Touch / Power paths are implemented and verified on +// real hardware. +// +// Responsibilities +// - WLED power / brightness control +// - Effect and palette navigation +// - Color hue / saturation control +// - Preset navigation and management +// - Boot-preset selection +// - Startup animation +// - Display sleep / wake +// - Runtime synchronization with WLED state +// - Browser screenshot endpoint (/cores3/screenshot.bmp) +// +// Architecture +// UI and WLED-state logic are kept separate from the thin +// Display / Touch / brightness hardware-access boundary so the +// same controller behavior can later be reused by Core2-family +// hardware profiles. +// +// Compatibility +// The existing "CoreS3_Display" configuration key and usermod +// class identity are intentionally retained so existing CoreS3 +// settings continue to load without migration. +// =========================================================== + +static const char CORES3_DISPLAY_CONFIG_NAME[] PROGMEM = "CoreS3_Display"; + +// CoreS3_Power publishes read-only runtime health state. +// Display consumes these signals only for user-facing warning UX; it does +// not own or modify the power-control implementation. +extern "C" bool coreS3PowerInitializationComplete(); +extern "C" bool coreS3PowerExternal5VReady(); +extern "C" bool coreS3PowerSafeShutdownMonitorReady(); + +#if defined(WLED_M5STACK_CORES3_AUDIO) +// CoreS3_Audio publishes terminal initialization and codec-ready state. +// Display consumes these signals only to annotate Audio Reactive effects when +// the built-in microphone is definitively unavailable. +extern "C" bool coreS3AudioInitializationFinished(); +extern "C" bool coreS3AudioCodecReady(); +#endif + +class CoreS3DisplayUsermod : public Usermod { + private: + + // ========================================================= + // Display + // ========================================================= + + M5GFX display; + M5StackDisplayHardwareBackend hardwareBackend; + + bool displayReady = false; + bool touchReady = false; + bool initDone = false; + + int16_t screenWidth = 0; + int16_t screenHeight = 0; + + unsigned long lastUpdate = 0; + + // ========================================================= + // Browser Screenshot + // ========================================================= + // + // A GET request to /cores3/screenshot.bmp returns a one-shot + // 24-bit BMP capture of the current 320 x 240 LCD contents. + // + // The frame and BMP buffers are allocated only while a request + // is active and are placed in PSRAM. The finished BMP buffer is + // retained by the asynchronous HTTP response until transmission + // completes, then released automatically. + // + // This is intentionally a still-image endpoint. It does not + // continuously stream frames and therefore does not add normal + // runtime Display/Wi-Fi load when unused. + // ========================================================= + + bool screenshotCaptureInProgress = false; + + static constexpr size_t SCREENSHOT_BMP_HEADER_SIZE = 54; + + // ========================================================= + // Network access state + // ========================================================= + // + // CoreS3 is a local controller first. STA connectivity must not be + // treated as a prerequisite for the touch UI because WLED can also be + // reached through its SoftAP, and local LED control must remain usable + // even when no network interface is currently available. + // ========================================================= + + enum NetworkAccessMode : uint8_t { + NETWORK_ACCESS_NONE = 0, + NETWORK_ACCESS_STA, + NETWORK_ACCESS_AP + }; + + NetworkAccessMode lastNetworkAccessMode = NETWORK_ACCESS_NONE; + String lastNetworkDisplayText = ""; + + // Session-only Recovery AP state. This never changes the persisted + // WLED AP behavior or the user's configured AP credentials. + bool recoveryApSessionActive = false; + String recoveryApSSID = ""; + unsigned long recoveryApLastStartAttempt = 0; + + bool readyScreenShown = false; + bool connectingScreenShown = false; + + // ========================================================= + // Battery status + // ========================================================= + // + // MAIN-only status display. Battery is intentionally informational: + // it has no touch target and does not alter WLED behavior. + // ========================================================= + + M5StackBatteryStatus batteryStatus; + bool batteryStatusInitialized = false; + + unsigned long lastBatteryStatusRead = 0; + + static constexpr unsigned long BATTERY_STATUS_UPDATE_MS = 10000; + + // ========================================================= + // Runtime Health / Error UX + // ========================================================= + // + // Normal operation stays visually unchanged. Only actionable failures are + // surfaced, once per boot, and only while MAIN is idle. Warnings are + // temporary so the controller remains usable even when a subsystem fails. + // ========================================================= + + enum RuntimeHealthWarning : uint8_t { + RUNTIME_HEALTH_WARNING_NONE = 0, + RUNTIME_HEALTH_WARNING_LED_POWER, + RUNTIME_HEALTH_WARNING_POWER_SAFETY, + RUNTIME_HEALTH_WARNING_TOUCH + }; + + RuntimeHealthWarning activeRuntimeHealthWarning = + RUNTIME_HEALTH_WARNING_NONE; + + uint8_t runtimeHealthWarningsShownMask = 0; + unsigned long runtimeHealthStartMs = 0; + unsigned long runtimeHealthWarningStartMs = 0; + + static constexpr uint8_t RUNTIME_HEALTH_SHOWN_LED_POWER = 0x01; + static constexpr uint8_t RUNTIME_HEALTH_SHOWN_POWER_SAFETY = 0x02; + static constexpr uint8_t RUNTIME_HEALTH_SHOWN_TOUCH = 0x04; + + // Give the deferred M5GFX I2C1 power-key monitor time to ARM before + // declaring Safe Shutdown unavailable. + static constexpr unsigned long POWER_SAFETY_WARNING_GRACE_MS = 5000; + static constexpr unsigned long RUNTIME_HEALTH_WARNING_HOLD_MS = 2200; + + // Audio health is contextual rather than a global warning: only an effect + // that actually depends on audio is annotated when codec initialization has + // definitively failed. During initialization the normal capability text is + // kept unchanged. + bool lastAudioUnavailable = false; + + // ========================================================= + // Cached WLED state + // ========================================================= + + int8_t lastLedState = -1; + + int lastBrightnessValue = -1; + int lastEffectMode = -1; + + int lastSpeedValue = -1; + int lastIntensityValue = -1; + + int lastPaletteValue = -1; + + int lastHueValue = -1; + int lastSaturationValue = -1; + + uint32_t lastPrimaryColor = 0; + bool lastPrimaryColorValid = false; + + // COLOR page multi-slot editor state. + // + // selectedColorSlot is runtime-only UI state: + // 0 = C1, 1 = C2, 2 = C3 + // + // WLED Effect metadata decides which slots are selectable. + uint8_t selectedColorSlot = 0; + + uint32_t lastSelectedColor = 0; + bool lastSelectedColorValid = false; + + uint32_t lastColorSlots[3] = { 0, 0, 0 }; + bool lastColorSlotsValid = false; + + // Runtime-only BLACK toggle history. + // + // A long-press on C1/C2/C3 toggles that WLED color slot between black + // (#000000) and its last observed non-black value. This history is never + // written to Flash/config and is refreshed by both CoreS3 and Web UI color + // changes. + uint32_t lastNonBlackColorSlots[3] = { 0, 0, 0 }; + bool lastNonBlackColorSlotValid[3] = { false, false, false }; + + // ========================================================= + // Preset state + // ========================================================= + + int lastPresetValue = -1; + int lastBootPresetValue = -1; + + // CoreS3 UI navigation position. + // + // This intentionally differs from WLED currentPreset: + // - currentPreset = WLED's currently active Preset + // - presetNavigationCursorId = last Preset selected/browsed by this UI + // + // When currentPreset becomes 0 (Custom State) after Color/Effect changes, + // the cursor remains on the last useful Preset position. + uint8_t presetNavigationCursorId = 0; + + // Last non/zero WLED currentPreset value observed by the cursor sync logic. + // This is runtime-only UI state and is not persisted to Flash. + uint8_t lastObservedCurrentPreset = 0; + + uint8_t pendingPresetId = 0; + String pendingPresetName = ""; + + unsigned long pendingPresetRequestMs = 0; + + unsigned long lastPresetsModifiedTime = 0; + + bool presetNoEntries = false; + + static constexpr unsigned long PRESET_APPLY_PENDING_MS = 1500; + + // ========================================================= + // Preset RAM cache + // + // WLED Preset names are limited to 32 characters. + // + // Cache is intentionally a fixed array: + // - no repeated heap allocation during navigation + // - predictable memory use + // - maximum WLED persistent Presets = 250 + // + // Approximate RAM: + // 250 x 34 bytes = about 8.5KB + // ========================================================= + + struct PresetCacheEntry { + uint8_t id; + char name[33]; + }; + + PresetCacheEntry presetCache[250]; + + uint16_t presetCacheCount = 0; + + bool presetCacheReady = false; + bool presetCacheBuilding = false; + + uint16_t presetCacheScanId = 1; + + unsigned long presetCacheLastScanMs = 0; + + unsigned long presetCacheSourceModifiedTime = 0; + unsigned long presetCacheBuildSourceModifiedTime = 0; + + static constexpr unsigned long PRESET_CACHE_SCAN_INTERVAL_MS = 5; + + // ========================================================= + // Persistent Display Settings + // ========================================================= + + uint16_t lcdBrightness = 128; + + // 0 = Never + uint16_t sleepTimeoutSec = 30; + + bool fadeEnabled = true; + + uint16_t fadeDurationMs = 250; + + // ========================================================= + // Current physical LCD brightness + // ========================================================= + + uint8_t currentDisplayBrightness = 0; + + // ========================================================= + // Display suspend + // ========================================================= + + static constexpr uint8_t DISPLAY_FADE_STEP = 4; + + enum DisplayPowerState : uint8_t { + DISPLAY_POWER_ACTIVE = 0, DISPLAY_POWER_SLEEP_FADE_OUT, DISPLAY_POWER_SLEEPING, DISPLAY_POWER_WAKE_FADE_IN, DISPLAY_POWER_WAKE_WAIT_RELEASE }; + + DisplayPowerState displayPowerState = DISPLAY_POWER_ACTIVE; + + unsigned long lastUserActivityMs = 0; + unsigned long displayFadeLastStep = 0; + + // ========================================================= + // Startup animation + // ========================================================= + + enum StartupState : uint8_t { + STARTUP_FADE_IN = 0, STARTUP_WAIT_WIFI, STARTUP_READY_HOLD, STARTUP_FADE_OUT, STARTUP_MAIN_FADE_IN, STARTUP_DONE }; + + StartupState startupState = STARTUP_FADE_IN; + + unsigned long startupStateStart = 0; + unsigned long startupLastFadeStep = 0; + unsigned long startupLastDotsUpdate = 0; + + uint8_t startupDotCount = 0; + + NetworkAccessMode startupNetworkAccessMode = NETWORK_ACCESS_NONE; + String startupIPAddress = ""; + + static constexpr unsigned long STARTUP_READY_HOLD_MS = 700; + static constexpr unsigned long STARTUP_DOTS_INTERVAL_MS = 350; + + // Display/UI fail-safe only. WLED itself continues its normal Wi-Fi + // connection/reconnection behavior after the local UI becomes available. + static constexpr unsigned long STARTUP_NETWORK_WAIT_TIMEOUT_MS = 10000; + + // ========================================================= + // Persistent logical HSV + // ========================================================= + + CHSV32 logicalColorHsv; + + bool logicalColorHsvValid = false; + + uint8_t logicalHueValue = 0; + uint8_t logicalSaturationValue = 0; + uint8_t logicalWhiteValue = 0; + + // ========================================================= + // Pages + // ========================================================= + + enum ScreenPage : uint8_t { + SCREEN_MAIN = 0, SCREEN_COLOR, SCREEN_EFFECT, SCREEN_PRESET }; + + ScreenPage currentPage = SCREEN_MAIN; + + // ========================================================= + // Preset sub pages + // ========================================================= + + enum PresetSubPage : uint8_t { + PRESET_SUBPAGE_NAV = 0, PRESET_SUBPAGE_MANAGE, PRESET_SUBPAGE_SAVE, PRESET_SUBPAGE_OVERWRITE, PRESET_SUBPAGE_DELETE, PRESET_SUBPAGE_BOOT }; + + PresetSubPage presetSubPage = PRESET_SUBPAGE_NAV; + + // ========================================================= + // New Preset save operation + // ========================================================= + + enum PresetSaveOperationState : uint8_t { + PRESET_SAVE_OP_IDLE = 0, PRESET_SAVE_OP_WAIT_WLED, PRESET_SAVE_OP_WAIT_CACHE, PRESET_SAVE_OP_SUCCESS, PRESET_SAVE_OP_FAILED }; + + PresetSaveOperationState presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + uint8_t presetSaveCandidateId = 0; + String presetSaveCandidateName = ""; + + unsigned long presetSaveHoldStartTime = 0; + bool presetSaveHoldTriggered = false; + + unsigned long presetSaveResultStartMs = 0; + + static constexpr unsigned long PRESET_SAVE_HOLD_MS = 1000; + static constexpr unsigned long PRESET_SAVE_RESULT_HOLD_MS = 900; + + // ========================================================= + // Existing Preset overwrite selection + // ========================================================= + + uint8_t presetOverwriteTargetId = 0; + String presetOverwriteTargetName = ""; + + bool presetSaveOperationIsOverwrite = false; + + // ========================================================= + // Existing Preset delete selection / operation + // ========================================================= + + enum PresetDeleteOperationState : uint8_t { + PRESET_DELETE_OP_IDLE = 0, PRESET_DELETE_OP_WAIT_CACHE, PRESET_DELETE_OP_SUCCESS, PRESET_DELETE_OP_FAILED }; + + PresetDeleteOperationState presetDeleteOperationState = PRESET_DELETE_OP_IDLE; + + uint8_t presetDeleteTargetId = 0; + String presetDeleteTargetName = ""; + + bool presetDeleteWasCurrentPreset = false; + bool presetDeleteWasBootPreset = false; + + unsigned long presetDeleteResultStartMs = 0; + + // ========================================================= + // Boot Preset selection / operation + // ========================================================= + + enum PresetBootOperationState : uint8_t { + PRESET_BOOT_OP_IDLE = 0, PRESET_BOOT_OP_WAIT_CONFIG, PRESET_BOOT_OP_SUCCESS, PRESET_BOOT_OP_FAILED }; + + PresetBootOperationState presetBootOperationState = PRESET_BOOT_OP_IDLE; + + uint8_t presetBootTargetId = 0; + String presetBootTargetName = "NONE"; + + unsigned long presetBootResultStartMs = 0; + + // ========================================================= + // Touch state + // ========================================================= + + M5StackTouchRuntimeState touchState; + + // ========================================================= + // Hue gesture + // ========================================================= + + CHSV32 hueEditHsv; + + bool hueEditValid = false; + + uint8_t hueEditValue = 0; + uint8_t hueEditWhite = 0; + + // ========================================================= + // Saturation gesture + // ========================================================= + + CHSV32 saturationEditHsv; + + bool saturationEditValid = false; + + uint8_t saturationEditValue = 0; + uint8_t saturationEditWhite = 0; + + // ========================================================= + // Layout + // ========================================================= + + static constexpr int16_t POWER_BUTTON_X = 8; + static constexpr int16_t POWER_BUTTON_Y = 8; + static constexpr int16_t POWER_BUTTON_W = 44; + static constexpr int16_t POWER_BUTTON_H = 44; + + static constexpr int16_t HEADER_CONTENT_LEFT = 60; + static constexpr int16_t HEADER_CONTENT_RIGHT = 312; + + static constexpr int16_t HEADER_CENTER_X = ( HEADER_CONTENT_LEFT + HEADER_CONTENT_RIGHT ) / 2; + + static constexpr int16_t HEADER_TITLE_Y = 18; + static constexpr int16_t HEADER_IP_Y = 41; + + // MAIN status row is split into: + // left = network / Recovery AP status + // right = display-only battery icon + percentage + static constexpr int16_t HEADER_NETWORK_LEFT = 60; + static constexpr int16_t HEADER_NETWORK_RIGHT = 238; + static constexpr int16_t HEADER_NETWORK_CENTER_X = + ( HEADER_NETWORK_LEFT + HEADER_NETWORK_RIGHT ) / 2; + + static constexpr int16_t BATTERY_STATUS_LEFT = 244; + static constexpr int16_t BATTERY_STATUS_RIGHT = 312; + + static constexpr int16_t BATTERY_ICON_X = 246; + static constexpr int16_t BATTERY_ICON_Y = 36; + static constexpr int16_t BATTERY_ICON_W = 18; + static constexpr int16_t BATTERY_ICON_H = 10; + + static constexpr int16_t BATTERY_PERCENT_X = 289; + static constexpr int16_t BATTERY_PERCENT_Y = 41; + + static constexpr int16_t CONTROL_LEFT_X = 16; + static constexpr int16_t CONTROL_RIGHT_X = 240; + + static constexpr int16_t CONTROL_BUTTON_W = 64; + static constexpr int16_t CONTROL_BUTTON_H = 34; + + // ========================================================= + // MAIN layout + // ========================================================= + + static constexpr int16_t BRI_BUTTON_Y = 82; + static constexpr int16_t FX_BUTTON_Y = 138; + + static constexpr int16_t EFFECT_DETAIL_X = 88; + static constexpr int16_t EFFECT_DETAIL_Y = 138; + static constexpr int16_t EFFECT_DETAIL_W = 144; + static constexpr int16_t EFFECT_DETAIL_H = 34; + + // ========================================================= + // MAIN bottom visible buttons + // ========================================================= + + static constexpr int16_t MAIN_BOTTOM_BUTTON_Y = 188; + static constexpr int16_t MAIN_BOTTOM_BUTTON_H = 40; + + static constexpr int16_t COLOR_BUTTON_X = 16; + static constexpr int16_t COLOR_BUTTON_W = 140; + + static constexpr int16_t PRESET_OPEN_BUTTON_X = 164; + static constexpr int16_t PRESET_OPEN_BUTTON_W = 140; + + // ========================================================= + // Back button + // ========================================================= + + static constexpr int16_t BACK_BUTTON_X = 268; + static constexpr int16_t BACK_BUTTON_Y = 8; + static constexpr int16_t BACK_BUTTON_W = 44; + static constexpr int16_t BACK_BUTTON_H = 44; + + // ========================================================= + // COLOR layout + // ========================================================= + + static constexpr int16_t COLOR_SLOT_1_X = 32; + static constexpr int16_t COLOR_SLOT_2_X = 124; + static constexpr int16_t COLOR_SLOT_3_X = 216; + + static constexpr int16_t COLOR_SLOT_Y = 76; + static constexpr int16_t COLOR_SLOT_W = 72; + static constexpr int16_t COLOR_SLOT_H = 28; + + static constexpr int16_t COLOR_SLOT_LABEL_Y = 68; + static constexpr int16_t COLOR_SELECTED_INFO_Y = 121; + + static constexpr int16_t HUE_BUTTON_Y = 151; + + static constexpr int16_t SATURATION_LABEL_Y = 198; + static constexpr int16_t SATURATION_BUTTON_Y = 204; + + // ========================================================= + // EFFECT detail layout + // ========================================================= + + static constexpr int16_t SPEED_BUTTON_Y = 82; + + static constexpr int16_t INTENSITY_BUTTON_Y = 140; + + // ========================================================= + // Palette visible layout + // ========================================================= + + static constexpr int16_t PALETTE_LABEL_Y = 188; + static constexpr int16_t PALETTE_BUTTON_Y = 198; + + // ========================================================= + // PRESET screen layout + // ========================================================= + + static constexpr int16_t PRESET_NAME_Y = 92; + static constexpr int16_t PRESET_ID_Y = 128; + static constexpr int16_t PRESET_STATUS_Y = 154; + + static constexpr int16_t PRESET_NAV_LABEL_Y = 188; + static constexpr int16_t PRESET_NAV_BUTTON_Y = 198; + + // ========================================================= + // PRESET MANAGE button on navigation screen + // ========================================================= + + static constexpr int16_t PRESET_MANAGE_BUTTON_X = 88; + static constexpr int16_t PRESET_MANAGE_BUTTON_Y = 198; + static constexpr int16_t PRESET_MANAGE_BUTTON_W = 144; + static constexpr int16_t PRESET_MANAGE_BUTTON_H = 34; + + // ========================================================= + // PRESET MANAGE screen + // ========================================================= + + static constexpr int16_t PRESET_SAVE_NEW_BUTTON_X = 60; + static constexpr int16_t PRESET_SAVE_NEW_BUTTON_Y = 64; + static constexpr int16_t PRESET_SAVE_NEW_BUTTON_W = 200; + static constexpr int16_t PRESET_SAVE_NEW_BUTTON_H = 34; + + // ========================================================= + // PRESET MANAGE OVERWRITE button + // ========================================================= + + static constexpr int16_t PRESET_OVERWRITE_BUTTON_X = 60; + static constexpr int16_t PRESET_OVERWRITE_BUTTON_Y = 106; + static constexpr int16_t PRESET_OVERWRITE_BUTTON_W = 200; + static constexpr int16_t PRESET_OVERWRITE_BUTTON_H = 34; + + // ========================================================= + // PRESET MANAGE DELETE button + // ========================================================= + + static constexpr int16_t PRESET_DELETE_BUTTON_X = 60; + static constexpr int16_t PRESET_DELETE_BUTTON_Y = 148; + static constexpr int16_t PRESET_DELETE_BUTTON_W = 200; + static constexpr int16_t PRESET_DELETE_BUTTON_H = 34; + + // ========================================================= + // PRESET MANAGE BOOT PRESET button + // ========================================================= + + static constexpr int16_t PRESET_BOOT_BUTTON_X = 60; + static constexpr int16_t PRESET_BOOT_BUTTON_Y = 190; + static constexpr int16_t PRESET_BOOT_BUTTON_W = 200; + static constexpr int16_t PRESET_BOOT_BUTTON_H = 34; + + // ========================================================= + // PRESET SAVE confirmation screen + // ========================================================= + + static constexpr int16_t PRESET_SAVE_HOLD_BUTTON_X = 60; + static constexpr int16_t PRESET_SAVE_HOLD_BUTTON_Y = 180; + static constexpr int16_t PRESET_SAVE_HOLD_BUTTON_W = 200; + static constexpr int16_t PRESET_SAVE_HOLD_BUTTON_H = 44; + + // ========================================================= + // PRESET OVERWRITE selection / confirmation screen + // ========================================================= + + static constexpr int16_t PRESET_OVERWRITE_NAV_BUTTON_Y = 124; + + static constexpr int16_t PRESET_OVERWRITE_HOLD_BUTTON_X = 60; + static constexpr int16_t PRESET_OVERWRITE_HOLD_BUTTON_Y = 194; + static constexpr int16_t PRESET_OVERWRITE_HOLD_BUTTON_W = 200; + static constexpr int16_t PRESET_OVERWRITE_HOLD_BUTTON_H = 40; + + // ========================================================= + // PRESET DELETE selection / confirmation screen + // ========================================================= + + static constexpr int16_t PRESET_DELETE_NAV_BUTTON_Y = 124; + + static constexpr int16_t PRESET_DELETE_HOLD_BUTTON_X = 60; + static constexpr int16_t PRESET_DELETE_HOLD_BUTTON_Y = 194; + static constexpr int16_t PRESET_DELETE_HOLD_BUTTON_W = 200; + static constexpr int16_t PRESET_DELETE_HOLD_BUTTON_H = 40; + + // ========================================================= + // PRESET BOOT selection / confirmation screen + // ========================================================= + + static constexpr int16_t PRESET_BOOT_NAV_BUTTON_Y = 124; + + static constexpr int16_t PRESET_BOOT_HOLD_BUTTON_X = 60; + static constexpr int16_t PRESET_BOOT_HOLD_BUTTON_Y = 194; + static constexpr int16_t PRESET_BOOT_HOLD_BUTTON_W = 200; + static constexpr int16_t PRESET_BOOT_HOLD_BUTTON_H = 40; + + // ========================================================= + // Touch timing + // ========================================================= + + static constexpr unsigned long TOUCH_POLL_MS = 15; + + static constexpr unsigned long TOUCH_RELEASE_CONFIRM_MS = 70; + + static constexpr unsigned long TOUCH_ACTION_COOLDOWN_MS = 250; + + // Recovery AP is intentionally harder to trigger than ordinary UI + // actions because it temporarily exposes a Wi-Fi access point. + static constexpr unsigned long WIFI_RECOVERY_HOLD_MS = 1500; + static constexpr unsigned long WIFI_RECOVERY_REOPEN_MS = 1000; + + // ========================================================= + // COLOR slot behavior + // ========================================================= + + // Deliberately longer than the repeat-control threshold. BLACK is + // reversible, but should still require an intentional hold. + static constexpr unsigned long COLOR_SLOT_LONG_PRESS_MS = 600; + + // ========================================================= + // Brightness behavior + // ========================================================= + + static constexpr unsigned long BRI_LONG_PRESS_MS = 400; + static constexpr unsigned long BRI_REPEAT_MS = 80; + + static constexpr int BRI_SHORT_STEP = 1; + static constexpr int BRI_LONG_STEP = 5; + + // ========================================================= + // Effect behavior + // ========================================================= + + static constexpr unsigned long EFFECT_LONG_PRESS_MS = 400; + static constexpr unsigned long EFFECT_REPEAT_MS = 250; + + // ========================================================= + // Hue behavior + // ========================================================= + + static constexpr unsigned long HUE_LONG_PRESS_MS = 400; + static constexpr unsigned long HUE_REPEAT_MS = 80; + + static constexpr int HUE_SHORT_STEP = 1; + static constexpr int HUE_LONG_STEP = 5; + + // ========================================================= + // Saturation behavior + // ========================================================= + + static constexpr unsigned long SATURATION_LONG_PRESS_MS = 400; + static constexpr unsigned long SATURATION_REPEAT_MS = 80; + + static constexpr int SATURATION_SHORT_STEP = 1; + static constexpr int SATURATION_LONG_STEP = 5; + + // ========================================================= + // Speed behavior + // ========================================================= + + static constexpr unsigned long SPEED_LONG_PRESS_MS = 400; + static constexpr unsigned long SPEED_REPEAT_MS = 80; + + static constexpr int SPEED_SHORT_STEP = 1; + static constexpr int SPEED_LONG_STEP = 5; + + // ========================================================= + // Intensity behavior + // ========================================================= + + static constexpr unsigned long INTENSITY_LONG_PRESS_MS = 400; + static constexpr unsigned long INTENSITY_REPEAT_MS = 80; + + static constexpr int INTENSITY_SHORT_STEP = 1; + static constexpr int INTENSITY_LONG_STEP = 5; + + // ========================================================= + // Palette behavior + // ========================================================= + + static constexpr unsigned long PALETTE_LONG_PRESS_MS = 400; + static constexpr unsigned long PALETTE_REPEAT_MS = 250; + + // ========================================================= + // Preset behavior + // ========================================================= + + static constexpr unsigned long PRESET_LONG_PRESS_MS = 400; + static constexpr unsigned long PRESET_REPEAT_MS = 600; + + // ========================================================= + // Shared long press / repeat timing helpers + // ========================================================= + + + + + // ========================================================= + // Normal LCD brightness + // ========================================================= + + uint8_t getNormalDisplayBrightness() { + return (uint8_t)constrain( (int)lcdBrightness, 1, 255 ); + } + + // ========================================================= + // Sleep timeout + // ========================================================= + + unsigned long getSleepTimeoutMs() { + return (unsigned long)sleepTimeoutSec * 1000UL; + } + + // ========================================================= + // Fade interval calculation + // ========================================================= + + unsigned long getFadeIntervalMs() { + uint16_t normalBrightness = getNormalDisplayBrightness(); + + uint16_t steps = ( normalBrightness + DISPLAY_FADE_STEP - 1 ) / DISPLAY_FADE_STEP; + + if ( steps == 0 ) { + steps = 1; + } + + unsigned long interval = (unsigned long)fadeDurationMs / steps; + + if ( interval < 1 ) { + interval = 1; + } + + return interval; + } + + // ========================================================= + // Hardware backend facade + // + // Keep board-specific implementation behind one backend while + // preserving the existing usermod call sites. This minimizes the + // regression surface for the hardware-verified CoreS3 UI. + // ========================================================= + + const char* getHardwareProbeStateName() { + return hardwareBackend.probeStateName(); + } + + const char* getDetectedPmuName() { + return hardwareBackend.detectedPmuName(); + } + + const char* getDetectedImuName() { + return hardwareBackend.detectedImuName(); + } + + const char* getDetectedVariantName() { + return hardwareBackend.detectedVariantName(); + } + + bool isCore2FamilyProfile() { + return hardwareBackend.isCore2FamilyProfile(); + } + + bool isCore2DiagnosticOnlyMode() { + return hardwareBackend.isCore2DiagnosticOnlyMode(); + } + + const char* getHardwareRuntimeModeName() { + return hardwareBackend.runtimeModeName(); + } + + const char* getHardwarePortStatusName() { + return hardwareBackend.portStatusName(); + } + + const char* getDetectedRevisionName() { + return hardwareBackend.detectedRevisionName(); + } + + void runHardwareDiagnostics() { + hardwareBackend.runDiagnostics(); + } + + const char* getHardwareProfileName() { + return hardwareBackend.profileName(); + } + + const char* getHardwareRevisionName() { + return hardwareBackend.revisionName(); + } + + bool isHardwareDisplayRuntimeEnabled() { + return hardwareBackend.isDisplayRuntimeEnabled(); + } + + bool initializeDisplayHardware() { + return hardwareBackend.initializeDisplay( screenWidth, screenHeight, touchReady ); + } + + bool readDisplayTouch( int16_t& touchX, int16_t& touchY ) { + return hardwareBackend.readTouch( touchX, touchY ); + } + + void writeDisplayBrightness( uint8_t value ) { + hardwareBackend.writeBrightness( value ); + } + + // ========================================================= + // Display brightness + // ========================================================= + + void setDisplayBrightness( uint8_t value ) { + currentDisplayBrightness = value; + + writeDisplayBrightness( value ); + } + + // ========================================================= + // Generic Fade + // ========================================================= + + bool updateFade( uint8_t targetBrightness, unsigned long now, unsigned long& lastFadeStep ) { + if ( currentDisplayBrightness == targetBrightness ) { + return true; + } + + if (!fadeEnabled) { + setDisplayBrightness( targetBrightness ); + + return true; + } + + unsigned long fadeInterval = getFadeIntervalMs(); + + if ( now - lastFadeStep < fadeInterval ) { + return false; + } + + lastFadeStep = now; + + if ( currentDisplayBrightness < targetBrightness ) { + int nextValue = currentDisplayBrightness + DISPLAY_FADE_STEP; + + if ( nextValue > targetBrightness ) { + nextValue = targetBrightness; + } + + setDisplayBrightness( (uint8_t)nextValue ); + } + else { + int nextValue = currentDisplayBrightness - DISPLAY_FADE_STEP; + + if ( nextValue < targetBrightness ) { + nextValue = targetBrightness; + } + + setDisplayBrightness( (uint8_t)nextValue ); + } + + return ( currentDisplayBrightness == targetBrightness ); + } + + // ========================================================= + // Preset cache rebuild start + // ========================================================= + + void startPresetCacheRebuild() { + presetCacheCount = 0; + + presetCacheScanId = 1; + + presetCacheLastScanMs = 0; + + presetCacheReady = false; + + presetCacheBuilding = true; + + presetNoEntries = false; + + presetCacheBuildSourceModifiedTime = presetsModifiedTime; + + Serial.printf( "[CoreS3_Display] " "Preset cache rebuild start " "(modified=%lu)\n", presetCacheBuildSourceModifiedTime ); + } + + // ========================================================= + // Preset cache rebuild complete + // ========================================================= + + void finishPresetCacheRebuild() { + presetCacheBuilding = false; + + presetCacheReady = true; + + presetCacheSourceModifiedTime = presetCacheBuildSourceModifiedTime; + + presetNoEntries = ( presetCacheCount == 0 ); + + normalizePresetNavigationCursor(); + + syncPresetNavigationCursorFromCurrentPreset(); + + Serial.printf( "[CoreS3_Display] " "Preset cache ready: %u preset(s)\n", (unsigned)presetCacheCount ); + + if ( presetsModifiedTime != presetCacheSourceModifiedTime ) { + Serial.println( F( "[CoreS3_Display] " "Preset changed during cache build. Rebuilding." ) ); + + startPresetCacheRebuild(); + + return; + } + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_NAV && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + drawPresetDetails( getDisplayedPresetId(), pendingPresetId > 0 ); + + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + + lastPresetValue = getDisplayedPresetId(); + } + else if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_MANAGE && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + drawPresetManageScreen(); + } + else if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_SAVE && presetSaveOperationState == PRESET_SAVE_OP_IDLE && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + preparePresetSaveCandidate(); + drawPresetSaveScreen(); + } + else if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_OVERWRITE && presetSaveOperationState == PRESET_SAVE_OP_IDLE && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + String refreshedName; + + if ( presetOverwriteTargetId == 0 || !getCachedPresetName( presetOverwriteTargetId, refreshedName ) ) { + preparePresetOverwriteTarget(); + } + else { + presetOverwriteTargetName = refreshedName; + } + + drawPresetOverwriteScreen(); + } + else if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_DELETE && presetDeleteOperationState == PRESET_DELETE_OP_IDLE && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + String refreshedName; + + if ( presetDeleteTargetId == 0 || !getCachedPresetName( presetDeleteTargetId, refreshedName ) ) { + preparePresetDeleteTarget(); + } + else { + presetDeleteTargetName = refreshedName; + } + + drawPresetDeleteScreen(); + } + else if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_BOOT && presetBootOperationState == PRESET_BOOT_OP_IDLE && displayPowerState == DISPLAY_POWER_ACTIVE && touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + if ( presetBootTargetId > 0 ) { + String refreshedName; + + if ( getCachedPresetName( presetBootTargetId, refreshedName ) ) { + presetBootTargetName = refreshedName; + } + else { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + } + } + + drawPresetBootScreen(); + } + } + + // ========================================================= + // Background Preset cache service + // ========================================================= + + void servicePresetCache() { + if ( presetCacheReady && !presetCacheBuilding && presetsModifiedTime != presetCacheSourceModifiedTime ) { + startPresetCacheRebuild(); + } + + if (!presetCacheBuilding) { + return; + } + + if ( pendingPresetId > 0 ) { + return; + } + + if ( presetNeedsSaving() ) { + return; + } + + unsigned long now = millis(); + + if ( now - presetCacheLastScanMs < PRESET_CACHE_SCAN_INTERVAL_MS ) { + return; + } + + presetCacheLastScanMs = now; + + if ( presetCacheScanId > 250 ) { + finishPresetCacheRebuild(); + + return; + } + + String presetName; + + uint8_t scanId = (uint8_t)presetCacheScanId; + + if ( getPresetName( scanId, presetName ) ) { + if ( presetCacheCount < 250 ) { + presetCache[ presetCacheCount ].id = scanId; + + strlcpy( presetCache[ presetCacheCount ].name, presetName.c_str(), sizeof( presetCache[ presetCacheCount ].name ) ); + + presetCacheCount++; + } + } + + presetCacheScanId++; + + if ( presetCacheScanId > 250 ) { + finishPresetCacheRebuild(); + } + } + + // ========================================================= + // Find Preset in RAM cache + // ========================================================= + + int findPresetCacheIndex( uint8_t presetId ) { + for ( uint16_t i = 0; i < presetCacheCount; i++ ) { + if ( presetCache[i].id == presetId ) { + return (int)i; + } + } + + return -1; + } + + bool getCachedPresetName( uint8_t presetId, String& name ) { + int index = findPresetCacheIndex( presetId ); + + if ( index < 0 ) { + return false; + } + + name = presetCache[ index ].name; + + return true; + } + + uint8_t findFirstFreePresetId() { + if ( !presetCacheReady || presetCacheBuilding ) { + return 0; + } + + uint16_t expectedId = 1; + + for ( uint16_t i = 0; i < presetCacheCount; i++ ) { + uint8_t cachedId = presetCache[i].id; + + if ( cachedId < expectedId ) { + continue; + } + + if ( cachedId == expectedId ) { + expectedId++; + + if ( expectedId > 250 ) { + return 0; + } + + continue; + } + + break; + } + + if ( expectedId >= 1 && expectedId <= 250 ) { + return (uint8_t)expectedId; + } + + return 0; + } + + bool preparePresetSaveCandidate() { + presetSaveCandidateId = findFirstFreePresetId(); + + presetSaveCandidateName = ""; + + if ( presetSaveCandidateId == 0 ) { + return false; + } + + char presetName[33]; + + snprintf( presetName, sizeof(presetName), "CoreS3 Preset %u", presetSaveCandidateId ); + + presetSaveCandidateName = presetName; + + return true; + } + bool preparePresetOverwriteTarget() { + presetOverwriteTargetId = 0; + + presetOverwriteTargetName = ""; + + if ( !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 ) { + return false; + } + + uint8_t basePresetId = getPresetManagementBaseId(); + + int targetIndex = findPresetCacheIndex( basePresetId ); + + if ( targetIndex < 0 ) { + targetIndex = 0; + } + + presetOverwriteTargetId = presetCache[ targetIndex ].id; + + presetOverwriteTargetName = presetCache[ targetIndex ].name; + + return true; + } + + bool stepPresetOverwriteTarget( int direction ) { + if ( direction == 0 || !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 ) { + return false; + } + + String targetName; + + uint8_t targetId = findAdjacentPreset( presetOverwriteTargetId, direction, &targetName ); + + if ( targetId == 0 ) { + return false; + } + + presetOverwriteTargetId = targetId; + + presetOverwriteTargetName = targetName; + + return true; + } + bool preparePresetDeleteTarget() { + presetDeleteTargetId = 0; + + presetDeleteTargetName = ""; + + if ( !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 ) { + return false; + } + + uint8_t basePresetId = getPresetManagementBaseId(); + + int targetIndex = findPresetCacheIndex( basePresetId ); + + if ( targetIndex < 0 ) { + targetIndex = 0; + } + + presetDeleteTargetId = presetCache[ targetIndex ].id; + + presetDeleteTargetName = presetCache[ targetIndex ].name; + + return true; + } + + bool stepPresetDeleteTarget( int direction ) { + if ( direction == 0 || !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 ) { + return false; + } + + String targetName; + + uint8_t targetId = findAdjacentPreset( presetDeleteTargetId, direction, &targetName ); + + if ( targetId == 0 ) { + return false; + } + + presetDeleteTargetId = targetId; + + presetDeleteTargetName = targetName; + + return true; + } + bool preparePresetBootTarget() { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + + if ( !presetCacheReady || presetCacheBuilding ) { + return false; + } + + if ( isPresetNavigationCursorValid() ) { + String cursorName; + + if ( getCachedPresetName( presetNavigationCursorId, cursorName ) ) { + presetBootTargetId = presetNavigationCursorId; + presetBootTargetName = cursorName; + + return true; + } + } + + if ( currentPreset > 0 ) { + String currentName; + + if ( getCachedPresetName( currentPreset, currentName ) ) { + presetBootTargetId = currentPreset; + presetBootTargetName = currentName; + + return true; + } + } + + if ( bootPreset > 0 ) { + String bootName; + + if ( getCachedPresetName( bootPreset, bootName ) ) { + presetBootTargetId = bootPreset; + presetBootTargetName = bootName; + } + } + + return true; + } + + bool stepPresetBootTarget( int direction ) { + if ( direction == 0 || !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 ) { + return false; + } + + if ( presetBootTargetId == 0 ) { + uint16_t targetIndex = ( direction > 0 ) ? 0 : ( presetCacheCount - 1 ); + + presetBootTargetId = presetCache[targetIndex].id; + presetBootTargetName = presetCache[targetIndex].name; + + return true; + } + + int currentIndex = findPresetCacheIndex( presetBootTargetId ); + + if ( currentIndex < 0 ) { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + + return true; + } + + if ( direction > 0 ) { + if ( currentIndex >= (int)presetCacheCount - 1 ) { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + } + else { + presetBootTargetId = presetCache[currentIndex + 1].id; + presetBootTargetName = presetCache[currentIndex + 1].name; + } + } + else { + if ( currentIndex <= 0 ) { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + } + else { + presetBootTargetId = presetCache[currentIndex - 1].id; + presetBootTargetName = presetCache[currentIndex - 1].name; + } + } + + return true; + } + + bool isPresetSaveBusy() { + return ( presetSaveOperationState != PRESET_SAVE_OP_IDLE ); + } + + bool isPresetDeleteBusy() { + return ( presetDeleteOperationState != PRESET_DELETE_OP_IDLE ); + } + + bool isPresetBootBusy() { + return ( presetBootOperationState != PRESET_BOOT_OP_IDLE ); + } + + bool requestNewPresetSave() { + presetSaveOperationIsOverwrite = false; + + if ( presetSaveOperationState != PRESET_SAVE_OP_IDLE || !presetCacheReady || presetCacheBuilding || pendingPresetId > 0 || presetSaveCandidateId == 0 || findPresetCacheIndex( presetSaveCandidateId ) >= 0 || presetNeedsSaving() ) { + presetSaveOperationState = PRESET_SAVE_OP_FAILED; + + presetSaveResultStartMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Preset save request rejected" ) ); + + return false; + } + + savePreset( presetSaveCandidateId, presetSaveCandidateName.c_str() ); + + if ( !presetNeedsSaving() ) { + presetSaveOperationState = PRESET_SAVE_OP_FAILED; + + presetSaveResultStartMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Preset save could not be queued" ) ); + + return false; + } + + presetSaveOperationState = PRESET_SAVE_OP_WAIT_WLED; + + presetSaveResultStartMs = 0; + + lastUserActivityMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.printf( "[CoreS3_Display] " "Preset save request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + + return true; + } + + bool requestPresetOverwrite() { + presetSaveOperationIsOverwrite = true; + + presetSaveCandidateId = presetOverwriteTargetId; + + presetSaveCandidateName = presetOverwriteTargetName; + + if ( presetSaveOperationState != PRESET_SAVE_OP_IDLE || !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 || pendingPresetId > 0 || presetSaveCandidateId == 0 || findPresetCacheIndex( presetSaveCandidateId ) < 0 || presetSaveCandidateName.length() == 0 || presetNeedsSaving() ) { + presetSaveOperationState = PRESET_SAVE_OP_FAILED; + + presetSaveResultStartMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Preset overwrite request rejected" ) ); + + return false; + } + + savePreset( presetSaveCandidateId, presetSaveCandidateName.c_str() ); + + if ( !presetNeedsSaving() ) { + presetSaveOperationState = PRESET_SAVE_OP_FAILED; + + presetSaveResultStartMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Preset overwrite could not be queued" ) ); + + return false; + } + + presetSaveOperationState = PRESET_SAVE_OP_WAIT_WLED; + + presetSaveResultStartMs = 0; + + lastUserActivityMs = millis(); + + drawPresetSaveOperationStatus(); + + Serial.printf( "[CoreS3_Display] " "Preset overwrite request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + + return true; + } + + bool requestPresetDelete() { + if ( presetDeleteOperationState != PRESET_DELETE_OP_IDLE || !presetCacheReady || presetCacheBuilding || presetCacheCount == 0 || pendingPresetId > 0 || presetDeleteTargetId == 0 || findPresetCacheIndex( presetDeleteTargetId ) < 0 || presetDeleteTargetName.length() == 0 || presetNeedsSaving() ) { + presetDeleteOperationState = PRESET_DELETE_OP_FAILED; + + presetDeleteResultStartMs = millis(); + + drawPresetDeleteOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Preset delete request rejected" ) ); + + return false; + } + + presetDeleteWasCurrentPreset = ( currentPreset == presetDeleteTargetId ); + presetDeleteWasBootPreset = ( bootPreset == presetDeleteTargetId ); + + deletePreset( presetDeleteTargetId ); + + if ( !presetCacheBuilding ) { + startPresetCacheRebuild(); + } + + presetDeleteOperationState = PRESET_DELETE_OP_WAIT_CACHE; + + presetDeleteResultStartMs = 0; + + lastUserActivityMs = millis(); + + drawPresetDeleteOperationStatus(); + + Serial.printf( "[CoreS3_Display] " "Preset delete request: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); + + return true; + } + + bool requestPresetBootSetting() { + bool validTarget = + presetBootTargetId == 0 || + findPresetCacheIndex( presetBootTargetId ) >= 0; + + if ( presetBootOperationState != PRESET_BOOT_OP_IDLE || + !presetCacheReady || + presetCacheBuilding || + pendingPresetId > 0 || + presetNeedsSaving() || + !validTarget || + presetBootTargetId == bootPreset ) { + presetBootOperationState = PRESET_BOOT_OP_FAILED; + presetBootResultStartMs = millis(); + + drawPresetBootOperationStatus(); + + Serial.println( F( "[CoreS3_Display] " "Boot Preset request rejected" ) ); + + return false; + } + + bootPreset = presetBootTargetId; + configNeedsWrite = true; + + presetBootOperationState = PRESET_BOOT_OP_WAIT_CONFIG; + presetBootResultStartMs = 0; + lastUserActivityMs = millis(); + + drawPresetBootOperationStatus(); + + if ( presetBootTargetId == 0 ) { + Serial.println( F( "[CoreS3_Display] " "Boot Preset clear request" ) ); + } + else { + Serial.printf( "[CoreS3_Display] " "Boot Preset request: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); + } + + return true; + } + + void servicePresetSaveOperation() { + if ( presetSaveOperationState == PRESET_SAVE_OP_IDLE ) { + return; + } + + unsigned long now = millis(); + + if ( presetSaveOperationState == PRESET_SAVE_OP_WAIT_WLED ) { + if ( presetNeedsSaving() ) { + return; + } + + if ( !presetCacheBuilding ) { + startPresetCacheRebuild(); + } + + presetSaveOperationState = PRESET_SAVE_OP_WAIT_CACHE; + + if ( currentPage == SCREEN_PRESET && ( presetSubPage == PRESET_SUBPAGE_SAVE || presetSubPage == PRESET_SUBPAGE_OVERWRITE ) && displayPowerState == DISPLAY_POWER_ACTIVE ) { + drawPresetSaveOperationStatus(); + } + + return; + } + + if ( presetSaveOperationState == PRESET_SAVE_OP_WAIT_CACHE ) { + if ( !presetCacheReady || presetCacheBuilding ) { + return; + } + + bool presetVerified = ( findPresetCacheIndex( presetSaveCandidateId ) >= 0 ); + + if ( presetVerified && presetSaveOperationIsOverwrite ) { + String verifiedName; + + presetVerified = getCachedPresetName( presetSaveCandidateId, verifiedName ) && verifiedName == presetSaveCandidateName; + } + + if (presetVerified) { + presetSaveOperationState = PRESET_SAVE_OP_SUCCESS; + + presetNavigationCursorId = presetSaveCandidateId; + + // The save operation intentionally changes the CoreS3 navigation + // position without necessarily changing WLED's active Preset. + lastObservedCurrentPreset = currentPreset; + + Serial.printf( "[CoreS3_Display] " "%s verified: %u (%s)\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + } + else { + presetSaveOperationState = PRESET_SAVE_OP_FAILED; + + Serial.printf( "[CoreS3_Display] " "%s verification failed: %u\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId ); + } + + presetSaveResultStartMs = now; + + if ( currentPage == SCREEN_PRESET && ( presetSubPage == PRESET_SUBPAGE_SAVE || presetSubPage == PRESET_SUBPAGE_OVERWRITE ) && displayPowerState == DISPLAY_POWER_ACTIVE ) { + drawPresetSaveOperationStatus(); + } + + return; + } + + if ( presetSaveOperationState == PRESET_SAVE_OP_SUCCESS || presetSaveOperationState == PRESET_SAVE_OP_FAILED ) { + if ( now - presetSaveResultStartMs < PRESET_SAVE_RESULT_HOLD_MS ) { + return; + } + + int16_t touchX = -1; + int16_t touchY = -1; + + if ( readDisplayTouch( touchX, touchY ) ) { + return; + } + + bool saveSucceeded = ( presetSaveOperationState == PRESET_SAVE_OP_SUCCESS ); + + bool completedOverwrite = presetSaveOperationIsOverwrite; + + presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + presetSaveOperationIsOverwrite = false; + + presetSaveResultStartMs = 0; + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + + resetTouchGesture(); + + if (completedOverwrite) { + presetOverwriteTargetId = 0; + + presetOverwriteTargetName = ""; + } + + if (saveSucceeded) { + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + drawPresetScreen(); + } + else { + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + drawPresetManageScreen(); + } + } + } + + void servicePresetDeleteOperation() { + if ( presetDeleteOperationState == PRESET_DELETE_OP_IDLE ) { + return; + } + + unsigned long now = millis(); + + if ( presetDeleteOperationState == PRESET_DELETE_OP_WAIT_CACHE ) { + if ( !presetCacheReady || presetCacheBuilding ) { + return; + } + + bool deleteVerified = ( findPresetCacheIndex( presetDeleteTargetId ) < 0 ); + + if (deleteVerified) { + if ( presetDeleteWasCurrentPreset && currentPreset == presetDeleteTargetId ) { + currentPreset = 0; + + lastPresetValue = -1; + } + + if ( presetDeleteWasBootPreset && bootPreset == presetDeleteTargetId ) { + bootPreset = 0; + configNeedsWrite = true; + lastBootPresetValue = -1; + + Serial.println( F( "[CoreS3_Display] " "Deleted Boot Preset cleared" ) ); + } + + presetDeleteOperationState = PRESET_DELETE_OP_SUCCESS; + + Serial.printf( "[CoreS3_Display] " "Preset delete verified: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); + } + else { + presetDeleteOperationState = PRESET_DELETE_OP_FAILED; + + Serial.printf( "[CoreS3_Display] " "Preset delete verification failed: %u\n", presetDeleteTargetId ); + } + + presetDeleteResultStartMs = now; + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_DELETE && displayPowerState == DISPLAY_POWER_ACTIVE ) { + drawPresetDeleteOperationStatus(); + } + + return; + } + + if ( presetDeleteOperationState == PRESET_DELETE_OP_SUCCESS || presetDeleteOperationState == PRESET_DELETE_OP_FAILED ) { + if ( now - presetDeleteResultStartMs < PRESET_SAVE_RESULT_HOLD_MS ) { + return; + } + + int16_t touchX = -1; + int16_t touchY = -1; + + if ( readDisplayTouch( touchX, touchY ) ) { + return; + } + + bool deleteSucceeded = ( presetDeleteOperationState == PRESET_DELETE_OP_SUCCESS ); + + presetDeleteOperationState = PRESET_DELETE_OP_IDLE; + + presetDeleteResultStartMs = 0; + + presetDeleteWasCurrentPreset = false; + presetDeleteWasBootPreset = false; + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + + resetTouchGesture(); + + presetDeleteTargetId = 0; + + presetDeleteTargetName = ""; + + if (deleteSucceeded) { + drawPresetScreen(); + } + else { + drawPresetManageScreen(); + } + } + } + + void servicePresetBootOperation() { + if ( presetBootOperationState == PRESET_BOOT_OP_IDLE ) { + return; + } + + unsigned long now = millis(); + + if ( presetBootOperationState == PRESET_BOOT_OP_WAIT_CONFIG ) { + if ( configNeedsWrite ) { + return; + } + + if ( bootPreset == presetBootTargetId ) { + presetBootOperationState = PRESET_BOOT_OP_SUCCESS; + + if ( presetBootTargetId == 0 ) { + Serial.println( F( "[CoreS3_Display] " "Boot Preset clear verified" ) ); + } + else { + Serial.printf( "[CoreS3_Display] " "Boot Preset verified: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); + } + } + else { + presetBootOperationState = PRESET_BOOT_OP_FAILED; + + Serial.printf( "[CoreS3_Display] " "Boot Preset verification failed: target=%u actual=%u\n", presetBootTargetId, bootPreset ); + } + + presetBootResultStartMs = now; + lastBootPresetValue = bootPreset; + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_BOOT && displayPowerState == DISPLAY_POWER_ACTIVE ) { + drawPresetBootOperationStatus(); + } + + return; + } + + if ( presetBootOperationState == PRESET_BOOT_OP_SUCCESS || presetBootOperationState == PRESET_BOOT_OP_FAILED ) { + if ( now - presetBootResultStartMs < PRESET_SAVE_RESULT_HOLD_MS ) { + return; + } + + int16_t touchX = -1; + int16_t touchY = -1; + + if ( readDisplayTouch( touchX, touchY ) ) { + return; + } + + presetBootOperationState = PRESET_BOOT_OP_IDLE; + presetBootResultStartMs = 0; + presetSaveHoldStartTime = 0; + presetSaveHoldTriggered = false; + + resetTouchGesture(); + + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + + drawPresetManageScreen(); + } + } + + // ========================================================= + // Network access helpers + // ========================================================= + + NetworkAccessMode getNetworkAccessMode() { + // Prefer the normal station connection when both STA and SoftAP are active. + if ( WiFi.status() == WL_CONNECTED ) { + return NETWORK_ACCESS_STA; + } + + // WLED owns SoftAP lifecycle and exposes apActive through wled.h. + // A running SoftAP is a valid network-ready state even though + // WiFi.status() is not WL_CONNECTED. + if ( apActive ) { + return NETWORK_ACCESS_AP; + } + + return NETWORK_ACCESS_NONE; + } + + String getNetworkIPAddress( NetworkAccessMode mode ) { + if ( mode == NETWORK_ACCESS_STA ) { + return WiFi.localIP().toString(); + } + + if ( mode == NETWORK_ACCESS_AP ) { + return WiFi.softAPIP().toString(); + } + + return ""; + } + + String getNetworkDisplayText( NetworkAccessMode mode ) { + if ( mode == NETWORK_ACCESS_STA ) { + return WiFi.localIP().toString(); + } + + if ( mode == NETWORK_ACCESS_AP ) { + if ( recoveryApSessionActive && recoveryApSSID.length() > 0 ) { + String displaySSID = recoveryApSSID; + + if ( displaySSID.length() > 18 ) { + displaySSID = displaySSID.substring( 0, 18 ); + } + + return displaySSID + " " + WiFi.softAPIP().toString(); + } + + return String( "AP: " ) + WiFi.softAPIP().toString(); + } + + return "Offline - Hold Recovery AP"; + } + + String getCurrentNetworkDisplayText() { + return getNetworkDisplayText( getNetworkAccessMode() ); + } + + bool startWiFiRecoveryAP() { + if ( getNetworkAccessMode() != NETWORK_ACCESS_NONE ) { + return false; + } + + recoveryApLastStartAttempt = millis(); + + // WLED::initAP(true) deliberately uses WLED's compiled recovery/default + // AP credentials and bypasses AP_BEHAVIOR_BUTTON_ONLY. Back up the live + // config variables first, then restore them immediately so this emergency + // session does not modify the user's stored AP configuration. + char savedApSSID[ sizeof(apSSID) ]; + char savedApPass[ sizeof(apPass) ]; + + strlcpy( savedApSSID, apSSID, sizeof(savedApSSID) ); + strlcpy( savedApPass, apPass, sizeof(savedApPass) ); + + WLED::instance().initAP( true ); + + String startedSSID = apSSID; + + strlcpy( apSSID, savedApSSID, sizeof(apSSID) ); + strlcpy( apPass, savedApPass, sizeof(apPass) ); + + if ( !apActive ) { + recoveryApSessionActive = false; + recoveryApSSID = ""; + + return false; + } + + recoveryApSessionActive = true; + recoveryApSSID = startedSSID; + + return true; + } + + void serviceWiFiRecoveryAP() { + if ( !recoveryApSessionActive ) { + return; + } + + if ( WiFi.status() == WL_CONNECTED ) { + recoveryApSessionActive = false; + recoveryApSSID = ""; + + return; + } + + if ( apActive ) { + return; + } + + const unsigned long now = millis(); + + if ( now - recoveryApLastStartAttempt < WIFI_RECOVERY_REOPEN_MS ) { + return; + } + + // WLED's normal reconnect path may temporarily tear down SoftAP while it + // retries STA. During an explicit recovery session, reopen the emergency + // AP so the user retains a path back into the Web UI. + startWiFiRecoveryAP(); + } + + // ========================================================= + // Runtime Health / Error UX + // ========================================================= + + uint8_t getRuntimeHealthWarningMask( RuntimeHealthWarning warning ) const { + switch ( warning ) { + case RUNTIME_HEALTH_WARNING_LED_POWER: + return RUNTIME_HEALTH_SHOWN_LED_POWER; + + case RUNTIME_HEALTH_WARNING_POWER_SAFETY: + return RUNTIME_HEALTH_SHOWN_POWER_SAFETY; + + case RUNTIME_HEALTH_WARNING_TOUCH: + return RUNTIME_HEALTH_SHOWN_TOUCH; + + case RUNTIME_HEALTH_WARNING_NONE: + default: + return 0; + } + } + + RuntimeHealthWarning getNextRuntimeHealthWarning( unsigned long now ) { + const bool graceExpired = + now - runtimeHealthStartMs >= POWER_SAFETY_WARNING_GRACE_MS; + + if ( + !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_LED_POWER ) && + ( + ( coreS3PowerInitializationComplete() && !coreS3PowerExternal5VReady() ) || + ( graceExpired && !coreS3PowerInitializationComplete() ) + ) + ) { + return RUNTIME_HEALTH_WARNING_LED_POWER; + } + + if ( + !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_POWER_SAFETY ) && + graceExpired && + !coreS3PowerSafeShutdownMonitorReady() + ) { + return RUNTIME_HEALTH_WARNING_POWER_SAFETY; + } + + if ( + !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_TOUCH ) && + !touchReady + ) { + return RUNTIME_HEALTH_WARNING_TOUCH; + } + + return RUNTIME_HEALTH_WARNING_NONE; + } + + void drawRuntimeHealthWarning( RuntimeHealthWarning warning ) { + display.fillScreen( TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + if ( warning == RUNTIME_HEALTH_WARNING_LED_POWER ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "LED POWER ERROR", screenWidth / 2, 82 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( "External 5V unavailable", screenWidth / 2, 122 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "Check power path and reboot", screenWidth / 2, 151 ); + + return; + } + + if ( warning == RUNTIME_HEALTH_WARNING_POWER_SAFETY ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "POWER SAFETY WARNING", screenWidth / 2, 82 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( "Safe shutdown unavailable", screenWidth / 2, 122 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "Use WLED power control", screenWidth / 2, 151 ); + + return; + } + + if ( warning == RUNTIME_HEALTH_WARNING_TOUCH ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "TOUCH ERROR", screenWidth / 2, 82 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( "Touch input unavailable", screenWidth / 2, 122 ); + + display.setTextColor( TFT_CYAN, TFT_BLACK ); + display.drawString( "Use WLED Web UI", screenWidth / 2, 151 ); + } + } + + bool serviceRuntimeHealthWarnings() { + if ( + startupState != STARTUP_DONE || + displayPowerState != DISPLAY_POWER_ACTIVE || + currentPage != SCREEN_MAIN + ) { + return false; + } + + const unsigned long now = millis(); + + if ( activeRuntimeHealthWarning != RUNTIME_HEALTH_WARNING_NONE ) { + if ( now - runtimeHealthWarningStartMs < RUNTIME_HEALTH_WARNING_HOLD_MS ) { + return true; + } + + runtimeHealthWarningsShownMask |= + getRuntimeHealthWarningMask( activeRuntimeHealthWarning ); + + activeRuntimeHealthWarning = RUNTIME_HEALTH_WARNING_NONE; + runtimeHealthWarningStartMs = 0; + + drawMainScreen( getCurrentNetworkDisplayText() ); + + lastNetworkAccessMode = getNetworkAccessMode(); + lastNetworkDisplayText = getCurrentNetworkDisplayText(); + lastUserActivityMs = now; + + return true; + } + + if ( touchState.touchTarget != M5STACK_TOUCH_TARGET_NONE ) { + return false; + } + + const RuntimeHealthWarning nextWarning = + getNextRuntimeHealthWarning( now ); + + if ( nextWarning == RUNTIME_HEALTH_WARNING_NONE ) { + return false; + } + + activeRuntimeHealthWarning = nextWarning; + runtimeHealthWarningStartMs = now; + + resetTouchGesture(); + drawRuntimeHealthWarning( nextWarning ); + + Serial.printf( + "[CoreS3_Display] Runtime health warning: %u\n", + (unsigned)nextWarning + ); + + return true; + } + + // ========================================================= + // Startup logo + // ========================================================= + + void drawStartupBase() { + display.fillScreen( TFT_BLACK ); + + bool logoResult = display.drawPng( CORES3_WLED_LOGO_PNG, CORES3_WLED_LOGO_PNG_LEN, 8, 20 ); + + if (!logoResult) { + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "WLED M5Stack CoreS3", screenWidth / 2, 68 ); + + Serial.println( F( "[CoreS3_Display] " "WARNING: startup PNG draw failed" ) ); + } + } + + void drawStartupConnectingStatus() { + display.fillRect( 0, 125, screenWidth, 100, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 2 ); + + // Reserve the width of all three dots from the beginning so the + // "Wi-Fi Connecting" label never shifts while the dots animate. + const char* label = "Wi-Fi Connecting"; + const int16_t labelWidth = display.textWidth( label ); + const int16_t dotWidth = display.textWidth( "." ); + const int16_t reservedDotsWidth = dotWidth * 3; + + const int16_t labelCenterX = + ( screenWidth / 2 ) - ( reservedDotsWidth / 2 ); + + display.drawString( label, labelCenterX, 153 ); + + const int16_t labelRight = + labelCenterX + ( labelWidth / 2 ); + + for ( uint8_t i = 0; i < startupDotCount && i < 3; i++ ) { + const int16_t dotCenterX = + labelRight + ( dotWidth / 2 ) + ( i * dotWidth ); + + display.drawString( ".", dotCenterX, 153 ); + } + + display.setTextSize( 1 ); + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Starting WLED...", screenWidth / 2, 185 ); + } + + + void drawStartupConnectedStatus( const String& ipAddress ) { + display.fillRect( 0, 125, screenWidth, 100, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_GREEN, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "Wi-Fi Connected", screenWidth / 2, 150 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( ipAddress, screenWidth / 2, 181 ); + } + + void drawStartupAccessPointStatus( const String& ipAddress ) { + display.fillRect( 0, 125, screenWidth, 100, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( TFT_GREEN, TFT_BLACK ); + display.setTextSize( 2 ); + + display.drawString( "Wi-Fi AP Ready", screenWidth / 2, 150 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + + display.drawString( ipAddress, screenWidth / 2, 181 ); + } + + void drawStartupLocalControlStatus() { + display.fillRect( 0, 125, screenWidth, 100, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + display.setTextSize( 2 ); + + display.drawString( "Local Control Ready", screenWidth / 2, 150 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.setTextSize( 1 ); + + display.drawString( "Wi-Fi unavailable", screenWidth / 2, 181 ); + } + + void handleStartupSequence() { + if ( startupState == STARTUP_DONE ) { + return; + } + + unsigned long now = millis(); + + if ( startupState == STARTUP_FADE_IN ) { + if ( updateFade( getNormalDisplayBrightness(), now, startupLastFadeStep ) ) { + startupState = STARTUP_WAIT_WIFI; + + startupStateStart = now; + + startupLastDotsUpdate = now; + } + + return; + } + + if ( startupState == STARTUP_WAIT_WIFI ) { + if ( now - startupLastDotsUpdate >= STARTUP_DOTS_INTERVAL_MS ) { + startupLastDotsUpdate = now; + + startupDotCount++; + + if ( startupDotCount > 3 ) { + startupDotCount = 0; + } + + drawStartupConnectingStatus(); + } + + NetworkAccessMode currentNetworkMode = getNetworkAccessMode(); + + if ( currentNetworkMode != NETWORK_ACCESS_NONE ) { + startupNetworkAccessMode = currentNetworkMode; + startupIPAddress = getNetworkIPAddress( currentNetworkMode ); + + if ( currentNetworkMode == NETWORK_ACCESS_STA ) { + drawStartupConnectedStatus( startupIPAddress ); + } + else { + drawStartupAccessPointStatus( startupIPAddress ); + } + + startupState = STARTUP_READY_HOLD; + startupStateStart = now; + + return; + } + + if ( now - startupStateStart >= STARTUP_NETWORK_WAIT_TIMEOUT_MS ) { + startupNetworkAccessMode = NETWORK_ACCESS_NONE; + startupIPAddress = ""; + + drawStartupLocalControlStatus(); + + startupState = STARTUP_READY_HOLD; + startupStateStart = now; + } + + return; + } + + if ( startupState == STARTUP_READY_HOLD ) { + if ( now - startupStateStart >= STARTUP_READY_HOLD_MS ) { + startupState = STARTUP_FADE_OUT; + + startupLastFadeStep = now; + } + + return; + } + + if ( startupState == STARTUP_FADE_OUT ) { + if ( updateFade( 0, now, startupLastFadeStep ) ) { + drawMainScreen( getNetworkDisplayText( startupNetworkAccessMode ) ); + + setDisplayBrightness( 0 ); + + startupState = STARTUP_MAIN_FADE_IN; + + startupLastFadeStep = now; + } + + return; + } + + if ( startupState == STARTUP_MAIN_FADE_IN ) { + if ( updateFade( getNormalDisplayBrightness(), now, startupLastFadeStep ) ) { + startupState = STARTUP_DONE; + + lastNetworkAccessMode = getNetworkAccessMode(); + lastNetworkDisplayText = getNetworkDisplayText( lastNetworkAccessMode ); + + readyScreenShown = true; + + connectingScreenShown = false; + + lastUserActivityMs = now; + + displayPowerState = DISPLAY_POWER_ACTIVE; + + Serial.println( F( "[CoreS3_Display] " "Startup animation complete" ) ); + } + + return; + } + } + + bool pollWakeTouch( unsigned long now ) { + if ( now - touchState.wakeTouchLastPoll < TOUCH_POLL_MS ) { + return touchState.wakeTouchState; + } + + touchState.wakeTouchLastPoll = now; + + int16_t x = -1; + int16_t y = -1; + + touchState.wakeTouchState = ( readDisplayTouch( x, y ) ); + + return touchState.wakeTouchState; + } + + bool settlePendingPreset() { + if ( pendingPresetId == 0 ) { + return false; + } + + bool completed = ( currentPreset == pendingPresetId ); + + bool timedOut = ( millis() - pendingPresetRequestMs >= PRESET_APPLY_PENDING_MS ); + + if ( !completed && !timedOut ) { + return false; + } + + pendingPresetId = 0; + + pendingPresetName = ""; + + pendingPresetRequestMs = 0; + + return true; + } + uint8_t getDisplayedPresetId() { + if ( pendingPresetId > 0 ) { + if ( millis() - pendingPresetRequestMs < PRESET_APPLY_PENDING_MS ) { + return pendingPresetId; + } + } + + if ( isPresetNavigationCursorValid() ) { + return presetNavigationCursorId; + } + + if ( + currentPreset > 0 && + presetCacheReady && + !presetCacheBuilding && + findPresetCacheIndex( currentPreset ) >= 0 + ) { + return currentPreset; + } + + return 0; + } + + void redrawCurrentPageForWake() { + settlePendingPreset(); + + if ( currentPage == SCREEN_COLOR ) { + drawColorScreen(); + + return; + } + + if ( currentPage == SCREEN_EFFECT ) { + drawEffectDetailScreen(); + + return; + } + + if ( currentPage == SCREEN_PRESET ) { + if ( presetSubPage == PRESET_SUBPAGE_MANAGE ) { + drawPresetManageScreen(); + } + else if ( presetSubPage == PRESET_SUBPAGE_SAVE ) { + drawPresetSaveScreen(); + } + else if ( presetSubPage == PRESET_SUBPAGE_OVERWRITE ) { + drawPresetOverwriteScreen(); + } + else if ( presetSubPage == PRESET_SUBPAGE_DELETE ) { + drawPresetDeleteScreen(); + } + else if ( presetSubPage == PRESET_SUBPAGE_BOOT ) { + drawPresetBootScreen(); + } + else { + drawPresetScreen(); + } + + return; + } + + drawMainScreen( getCurrentNetworkDisplayText() ); + } + + void beginDisplaySleep( unsigned long now ) { + resetTouchGesture(); + + displayPowerState = DISPLAY_POWER_SLEEP_FADE_OUT; + + displayFadeLastStep = now; + + touchState.wakeTouchLastPoll = 0; + + touchState.wakeTouchState = false; + + touchState.wakeReleaseCandidate = 0; + } + + void beginDisplayWake( unsigned long now ) { + resetTouchGesture(); + + setDisplayBrightness( 0 ); + + redrawCurrentPageForWake(); + + setDisplayBrightness( 0 ); + + displayPowerState = DISPLAY_POWER_WAKE_FADE_IN; + + displayFadeLastStep = now; + + touchState.wakeReleaseCandidate = 0; + + lastUserActivityMs = now; + } + + bool handleDisplayPowerManagement() { + unsigned long now = millis(); + + if ( displayPowerState == DISPLAY_POWER_ACTIVE ) { + if ( sleepTimeoutSec > 0 && !touchState.touchActive && now - lastUserActivityMs >= getSleepTimeoutMs() ) { + beginDisplaySleep( now ); + + return true; + } + + return false; + } + + if ( displayPowerState == DISPLAY_POWER_SLEEP_FADE_OUT ) { + if ( pollWakeTouch( now ) ) { + beginDisplayWake( now ); + + return true; + } + + if ( updateFade( 0, now, displayFadeLastStep ) ) { + displayPowerState = DISPLAY_POWER_SLEEPING; + + setDisplayBrightness( 0 ); + + Serial.println( F( "[CoreS3_Display] " "Display sleeping" ) ); + } + + return true; + } + + if ( displayPowerState == DISPLAY_POWER_SLEEPING ) { + if ( pollWakeTouch( now ) ) { + beginDisplayWake( now ); + } + + return true; + } + + if ( displayPowerState == DISPLAY_POWER_WAKE_FADE_IN ) { + pollWakeTouch( now ); + + if ( updateFade( getNormalDisplayBrightness(), now, displayFadeLastStep ) ) { + displayPowerState = DISPLAY_POWER_WAKE_WAIT_RELEASE; + + touchState.wakeReleaseCandidate = 0; + + } + + return true; + } + + if ( displayPowerState == DISPLAY_POWER_WAKE_WAIT_RELEASE ) { + bool touching = pollWakeTouch( now ); + + if (touching) { + touchState.wakeReleaseCandidate = 0; + + return true; + } + + if ( touchState.wakeReleaseCandidate == 0 ) { + touchState.wakeReleaseCandidate = now; + + return true; + } + + if ( now - touchState.wakeReleaseCandidate >= TOUCH_RELEASE_CONFIRM_MS ) { + displayPowerState = DISPLAY_POWER_ACTIVE; + + lastUserActivityMs = now; + + touchState.wakeReleaseCandidate = 0; + + touchState.wakeTouchState = false; + + resetTouchGesture(); + + Serial.println( F( "[CoreS3_Display] " "Display active" ) ); + } + + return true; + } + + return false; + } + + uint16_t rgbTo565( uint8_t r, uint8_t g, uint8_t b ) { + return ( ((uint16_t)(r & 0xF8) << 8) | ((uint16_t)(g & 0xFC) << 3) | ((uint16_t)b >> 3) ); + } + + struct M5StackEffectColorCapabilities { + bool color1 = true; + bool color2 = true; + bool color3 = true; + }; + + uint32_t getPrimaryColor() { + if ( strip.getSegmentsNum() > 0 ) { + return strip.getMainSegment().colors[0]; + } + + return 0; + } + + uint32_t getColorSlotValue( uint8_t colorSlot ) { + if ( strip.getSegmentsNum() == 0 || colorSlot > 2 ) { + return 0; + } + + return strip.getMainSegment().colors[ colorSlot ]; + } + + uint32_t getSelectedColor() { + return getColorSlotValue( selectedColorSlot ); + } + + bool isEffectColorSlotEnabled( + const M5StackEffectColorCapabilities& capability, + uint8_t colorSlot + ) { + if ( colorSlot == 0 ) { + return capability.color1; + } + + if ( colorSlot == 1 ) { + return capability.color2; + } + + if ( colorSlot == 2 ) { + return capability.color3; + } + + return false; + } + + uint8_t getFirstEnabledColorSlot( + const M5StackEffectColorCapabilities& capability + ) { + if ( capability.color1 ) { + return 0; + } + + if ( capability.color2 ) { + return 1; + } + + if ( capability.color3 ) { + return 2; + } + + return 0; + } + + bool normalizeSelectedColorSlot( uint8_t effectMode ) { + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( effectMode ); + + if ( !effectUsesAnyColor( capability ) ) { + selectedColorSlot = 0; + return false; + } + + if ( !isEffectColorSlotEnabled( capability, selectedColorSlot ) ) { + selectedColorSlot = getFirstEnabledColorSlot( capability ); + } + + return true; + } + + void cacheCurrentColorSlots() { + if ( strip.getSegmentsNum() == 0 ) { + lastColorSlotsValid = false; + return; + } + + Segment& mainSegment = strip.getMainSegment(); + + for ( uint8_t colorSlot = 0; colorSlot < 3; colorSlot++ ) { + const uint32_t slotColor = mainSegment.colors[ colorSlot ]; + + lastColorSlots[ colorSlot ] = slotColor; + + if ( slotColor != 0 ) { + lastNonBlackColorSlots[ colorSlot ] = slotColor; + lastNonBlackColorSlotValid[ colorSlot ] = true; + } + } + + lastColorSlotsValid = true; + } + + bool currentColorSlotsChanged() { + if ( strip.getSegmentsNum() == 0 ) { + return lastColorSlotsValid; + } + + if ( !lastColorSlotsValid ) { + return true; + } + + Segment& mainSegment = strip.getMainSegment(); + + for ( uint8_t colorSlot = 0; colorSlot < 3; colorSlot++ ) { + if ( mainSegment.colors[ colorSlot ] != lastColorSlots[ colorSlot ] ) { + return true; + } + } + + return false; + } + + void selectColorSlot( uint8_t colorSlot ) { + const uint8_t effectMode = getCurrentEffectMode(); + + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( effectMode ); + + if ( !isEffectColorSlotEnabled( capability, colorSlot ) ) { + return; + } + + selectedColorSlot = colorSlot; + + hueEditValid = false; + saturationEditValid = false; + + const uint32_t selectedColor = getSelectedColor(); + + syncLogicalColorFromRgb( selectedColor ); + + lastSelectedColor = selectedColor; + lastSelectedColorValid = true; + + cacheCurrentColorSlots(); + + if ( currentPage == SCREEN_COLOR ) { + drawColorDetails( selectedColor ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + } + } + + bool toggleColorSlotBlack( uint8_t colorSlot ) { + if ( strip.getSegmentsNum() == 0 || colorSlot > 2 ) { + return false; + } + + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( getCurrentEffectMode() ); + + if ( !isEffectColorSlotEnabled( capability, colorSlot ) ) { + return false; + } + + Segment& mainSegment = strip.getMainSegment(); + + const uint32_t currentColor = mainSegment.colors[ colorSlot ]; + + uint32_t newColor = 0; + + if ( currentColor != 0 ) { + lastNonBlackColorSlots[ colorSlot ] = currentColor; + lastNonBlackColorSlotValid[ colorSlot ] = true; + + newColor = 0; + } + else { + // If this slot has never had a non-black color during this runtime, + // restore to RGB white. The user can then immediately tune Hue/Sat. + newColor = + lastNonBlackColorSlotValid[ colorSlot ] + ? lastNonBlackColorSlots[ colorSlot ] + : 0x00FFFFFF; + } + + selectedColorSlot = colorSlot; + + hueEditValid = false; + saturationEditValid = false; + + if ( newColor != currentColor ) { + mainSegment.setColor( colorSlot, newColor ); + + stateUpdated( CALL_MODE_BUTTON ); + } + + syncLogicalColorFromRgb( newColor ); + + lastSelectedColor = newColor; + lastSelectedColorValid = true; + + if ( colorSlot == 0 ) { + lastPrimaryColor = newColor; + lastPrimaryColorValid = true; + } + + cacheCurrentColorSlots(); + + lastHueValue = logicalHueValue; + lastSaturationValue = logicalSaturationValue; + + if ( currentPage == SCREEN_COLOR ) { + drawColorDetails( newColor ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + } + + return true; + } + + uint8_t getCurrentEffectMode() { + if ( strip.getSegmentsNum() > 0 ) { + return strip.getMainSegment().mode; + } + + return 0; + } + + uint8_t getCurrentSpeed() { + if ( strip.getSegmentsNum() > 0 ) { + return strip.getMainSegment().speed; + } + + return 0; + } + + uint8_t getCurrentIntensity() { + if ( strip.getSegmentsNum() > 0 ) { + return strip.getMainSegment().intensity; + } + + return 0; + } + + uint8_t getCurrentPalette() { + if ( strip.getSegmentsNum() > 0 ) { + return strip.getMainSegment().palette; + } + + return 0; + } + + size_t getSelectablePaletteCount() { + return FIXED_PALETTE_COUNT + customPalettes.size() + usermodPalettes.size(); + } + + uint8_t paletteIdFromSequenceIndex( size_t sequenceIndex ) { + if ( sequenceIndex < FIXED_PALETTE_COUNT ) { + return (uint8_t)sequenceIndex; + } + + sequenceIndex -= FIXED_PALETTE_COUNT; + + if ( sequenceIndex < customPalettes.size() ) { + return (uint8_t)( WLED_CUSTOM_PALETTE_ID_BASE - sequenceIndex ); + } + + sequenceIndex -= customPalettes.size(); + + if ( sequenceIndex < usermodPalettes.size() ) { + return (uint8_t)( WLED_USERMOD_PALETTE_ID_BASE - sequenceIndex ); + } + + return 0; + } + + int findPaletteSequenceIndex( uint8_t paletteId ) { + if ( paletteId < FIXED_PALETTE_COUNT ) { + return paletteId; + } + + if ( paletteId > WLED_CUSTOM_PALETTE_ID_BASE ) { + size_t usermodIndex = WLED_USERMOD_PALETTE_ID_BASE - paletteId; + + if ( usermodIndex < usermodPalettes.size() ) { + return (int)( FIXED_PALETTE_COUNT + customPalettes.size() + usermodIndex ); + } + + return -1; + } + + if ( paletteId >= FIXED_PALETTE_COUNT && paletteId <= WLED_CUSTOM_PALETTE_ID_BASE ) { + size_t customIndex = WLED_CUSTOM_PALETTE_ID_BASE - paletteId; + + if ( customIndex < customPalettes.size() ) { + return (int)( FIXED_PALETTE_COUNT + customIndex ); + } + + return -1; + } + + return -1; + } + + void getPaletteName( uint8_t paletteId, char* paletteName, size_t paletteNameSize ) { + if ( paletteName == nullptr || paletteNameSize == 0 ) { + return; + } + + paletteName[0] = '\0'; + + extractModeName( paletteId, JSON_palette_names, paletteName, paletteNameSize - 1 ); + + if ( strlen(paletteName) == 0 ) { + snprintf( paletteName, paletteNameSize, "Palette %u", paletteId ); + } + } + + uint8_t findAdjacentPreset( uint8_t startPreset, int direction, String* foundName = nullptr ) { + if ( direction == 0 ) { + return 0; + } + + if ( !presetCacheReady || presetCacheCount == 0 ) { + return 0; + } + + int currentIndex = findPresetCacheIndex( startPreset ); + + int newIndex; + + if ( currentIndex < 0 ) { + if ( direction > 0 ) { + newIndex = 0; + } + else { + newIndex = presetCacheCount - 1; + } + } + else { + newIndex = currentIndex + ( direction > 0 ? 1 : -1 ); + + if ( newIndex >= presetCacheCount ) { + newIndex = 0; + } + + if ( newIndex < 0 ) { + newIndex = presetCacheCount - 1; + } + } + + if ( foundName != nullptr ) { + *foundName = presetCache[ newIndex ].name; + } + + return presetCache[ newIndex ].id; + } + + bool isPresetNavigationCursorValid() { + return ( + presetNavigationCursorId > 0 && + presetCacheReady && + !presetCacheBuilding && + findPresetCacheIndex( presetNavigationCursorId ) >= 0 + ); + } + + void normalizePresetNavigationCursor() { + if ( !presetCacheReady || presetCacheBuilding ) { + return; + } + + if ( presetCacheCount == 0 ) { + presetNavigationCursorId = 0; + return; + } + + if ( isPresetNavigationCursorValid() ) { + return; + } + + if ( presetNavigationCursorId == 0 ) { + if ( currentPreset > 0 && findPresetCacheIndex( currentPreset ) >= 0 ) { + presetNavigationCursorId = currentPreset; + } + + return; + } + + const uint8_t previousCursor = presetNavigationCursorId; + + // If the cursor Preset was deleted, prefer the next higher saved ID. + // If there is no higher ID, fall back to the final saved Preset. + for ( uint16_t index = 0; index < presetCacheCount; index++ ) { + if ( presetCache[ index ].id > previousCursor ) { + presetNavigationCursorId = presetCache[ index ].id; + return; + } + } + + presetNavigationCursorId = presetCache[ presetCacheCount - 1 ].id; + } + + void syncPresetNavigationCursorFromCurrentPreset() { + if ( currentPreset == lastObservedCurrentPreset ) { + return; + } + + if ( currentPreset == 0 ) { + // WLED entered Custom State. Keep the CoreS3 navigation cursor where + // the user last selected a Preset. + lastObservedCurrentPreset = 0; + return; + } + + if ( !presetCacheReady || presetCacheBuilding ) { + // Retry once the cache is available. Do not mark this value observed yet. + return; + } + + if ( findPresetCacheIndex( currentPreset ) < 0 ) { + // Retry if the Preset cache is in transition. + return; + } + + presetNavigationCursorId = currentPreset; + lastObservedCurrentPreset = currentPreset; + } + + uint8_t getPresetManagementBaseId() { + if ( isPresetNavigationCursorValid() ) { + return presetNavigationCursorId; + } + + if ( + currentPreset > 0 && + presetCacheReady && + !presetCacheBuilding && + findPresetCacheIndex( currentPreset ) >= 0 + ) { + return currentPreset; + } + + if ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 ) { + return presetCache[ 0 ].id; + } + + return 0; + } + uint8_t getPresetNavigationBaseId() { + if ( pendingPresetId > 0 && millis() - pendingPresetRequestMs < PRESET_APPLY_PENDING_MS ) { + return pendingPresetId; + } + + if ( isPresetNavigationCursorValid() ) { + return presetNavigationCursorId; + } + + if ( + currentPreset > 0 && + presetCacheReady && + !presetCacheBuilding && + findPresetCacheIndex( currentPreset ) >= 0 + ) { + return currentPreset; + } + + return 0; + } + + uint8_t getHueFromColor( uint32_t color ) { + CRGBW rgb( color ); + + CHSV32 hsv; + + rgb2hsv( rgb, hsv ); + + return (uint8_t)( hsv.h >> 8 ); + } + + uint8_t getSaturationFromColor( uint32_t color ) { + CRGBW rgb( color ); + + CHSV32 hsv; + + rgb2hsv( rgb, hsv ); + + return hsv.s; + } + + void syncLogicalColorFromRgb( uint32_t color ) { + CRGBW rgb( color ); + + rgb2hsv( rgb, logicalColorHsv ); + + logicalHueValue = (uint8_t)( logicalColorHsv.h >> 8 ); + + logicalSaturationValue = logicalColorHsv.s; + + logicalWhiteValue = rgb.w; + + logicalColorHsvValid = true; + + lastHueValue = logicalHueValue; + + lastSaturationValue = logicalSaturationValue; + } + + uint8_t getDisplayedHue() { + uint32_t currentColor = getSelectedColor(); + + if ( logicalColorHsvValid && lastSelectedColorValid && currentColor == lastSelectedColor ) { + return logicalHueValue; + } + + return getHueFromColor( currentColor ); + } + + uint8_t getDisplayedSaturation() { + uint32_t currentColor = getSelectedColor(); + + if ( logicalColorHsvValid && lastSelectedColorValid && currentColor == lastSelectedColor ) { + return logicalSaturationValue; + } + + return getSaturationFromColor( currentColor ); + } + + // ========================================================= + // Common centered text / standard page header helpers + // ========================================================= + + void setCenteredTextStyle( uint16_t color, uint8_t size, uint16_t background = TFT_BLACK ) { + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( color, background ); + display.setTextSize( size ); + } + + void drawStandardPageHeader( const char* title, const char* subtitle, uint16_t titleColor = TFT_WHITE ) { + setCenteredTextStyle( titleColor, 2 ); + display.drawString( title, screenWidth / 2, 18 ); + + setCenteredTextStyle( TFT_WHITE, 1 ); + display.drawString( subtitle, screenWidth / 2, 41 ); + + display.drawFastHLine( 8, 58, screenWidth - 16, TFT_DARKGREY ); + } + + void drawPowerIcon( int16_t centerX, int16_t centerY, uint16_t iconColor, uint16_t backgroundColor ) { + display.drawCircle( centerX, centerY + 2, 11, iconColor ); + + display.drawCircle( centerX, centerY + 2, 10, iconColor ); + + display.fillRect( centerX - 4, centerY - 11, 9, 8, backgroundColor ); + + display.drawFastVLine( centerX - 1, centerY - 14, 12, iconColor ); + + display.drawFastVLine( centerX, centerY - 14, 12, iconColor ); + + display.drawFastVLine( centerX + 1, centerY - 14, 12, iconColor ); + } + + void drawPowerButton( bool ledOn, bool pressed ) { + uint16_t stateColor = ledOn ? TFT_GREEN : TFT_RED; + + uint16_t backgroundColor = pressed ? stateColor : TFT_BLACK; + + uint16_t iconColor = pressed ? TFT_BLACK : stateColor; + + display.fillRect( POWER_BUTTON_X - 2, POWER_BUTTON_Y - 2, POWER_BUTTON_W + 4, POWER_BUTTON_H + 4, TFT_BLACK ); + + display.fillRect( POWER_BUTTON_X, POWER_BUTTON_Y, POWER_BUTTON_W, POWER_BUTTON_H, backgroundColor ); + + display.drawRect( POWER_BUTTON_X, POWER_BUTTON_Y, POWER_BUTTON_W, POWER_BUTTON_H, stateColor ); + + display.drawRect( POWER_BUTTON_X + 1, POWER_BUTTON_Y + 1, POWER_BUTTON_W - 2, POWER_BUTTON_H - 2, stateColor ); + + int16_t centerX = POWER_BUTTON_X + (POWER_BUTTON_W / 2); + + int16_t centerY = POWER_BUTTON_Y + (POWER_BUTTON_H / 2); + + drawPowerIcon( centerX, centerY, iconColor, backgroundColor ); + + touchState.powerButtonVisualPressed = pressed; + } + + void drawTriangleButton( int16_t x, int16_t y, bool pointRight, bool pressed ) { + const uint16_t buttonColor = TFT_CYAN; + + const int16_t w = CONTROL_BUTTON_W; + + const int16_t h = CONTROL_BUTTON_H; + + display.fillRect( x - 2, y - 2, w + 4, h + 4, TFT_BLACK ); + + if (pressed) { + display.fillRect( x, y, w, h, buttonColor ); + } + else { + display.fillRect( x, y, w, h, TFT_BLACK ); + + display.drawRect( x, y, w, h, buttonColor ); + + display.drawRect( x + 1, y + 1, w - 2, h - 2, buttonColor ); + } + + int16_t centerX = x + (w / 2); + + int16_t centerY = y + (h / 2); + + uint16_t triangleColor = pressed ? TFT_BLACK : buttonColor; + + if (pointRight) { + display.fillTriangle( centerX + 10, centerY, centerX - 7, centerY - 9, centerX - 7, centerY + 9, triangleColor ); + } + else { + display.fillTriangle( centerX - 10, centerY, centerX + 7, centerY - 9, centerX + 7, centerY + 9, triangleColor ); + } + } + + // ========================================================= + // Shared numeric control drawing + // ========================================================= + + void drawNumericControl( int16_t clearY, int16_t clearH, const char* label, int16_t labelY, int16_t buttonY, int16_t valueY, const char* valueText, M5StackTouchTarget pressedTarget, M5StackTouchTarget downTarget, M5StackTouchTarget upTarget ) { + display.fillRect( 0, clearY, screenWidth, clearH, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( label, screenWidth / 2, labelY ); + + drawTriangleButton( CONTROL_LEFT_X, buttonY, false, pressedTarget == downTarget ); + + drawTriangleButton( CONTROL_RIGHT_X, buttonY, true, pressedTarget == upTarget ); + + display.setTextSize( 2 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.drawString( valueText, screenWidth / 2, valueY ); + } + + void drawBrightness( int brightnessValue, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + brightnessValue = constrain( brightnessValue, 0, 255 ); + + int brightnessPercent = 0; + + if ( brightnessValue >= 255 ) { + brightnessPercent = 100; + } + else if ( brightnessValue > 0 ) { + brightnessPercent = max( 1, ( brightnessValue * 100 ) / 255 ); + } + + char valueText[16]; + + snprintf( + valueText, + sizeof(valueText), + "%d %d%%", + brightnessValue, + brightnessPercent + ); + + drawNumericControl( + 62, + 58, + "LED Brightness", + 70, + BRI_BUTTON_Y, + 99, + valueText, + pressedTarget, + M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN, + M5STACK_TOUCH_TARGET_BRIGHTNESS_UP + ); + } + + struct M5StackEffectCapabilities { + bool supports1D = true; + bool supports2D = false; + bool supports3D = false; + bool audioVolume = false; + bool audioFrequency = false; + }; + + M5StackEffectCapabilities getEffectCapabilities( uint8_t effectMode ) { + M5StackEffectCapabilities capability; + + const char* modeData = strip.getModeData( effectMode ); + + if ( modeData == nullptr ) { + return capability; + } + + const char* metadataStart = strchr( modeData, '@' ); + + if ( metadataStart == nullptr ) { + // WLED metadata specification defaults missing flags to 1D. + return capability; + } + + const char* flagsStart = metadataStart + 1; + + // Metadata sections: + // parameters ; colors ; palette ; flags ; defaults + for ( uint8_t section = 0; section < 3; section++ ) { + flagsStart = strchr( flagsStart, ';' ); + + if ( flagsStart == nullptr ) { + return capability; + } + + flagsStart++; + } + + const char* flagsEnd = strchr( flagsStart, ';' ); + + if ( flagsEnd == nullptr ) { + flagsEnd = flagsStart + strlen( flagsStart ); + } + + if ( flagsStart == flagsEnd ) { + return capability; + } + + bool dimensionFlagFound = false; + + capability.supports1D = false; + + for ( const char* flag = flagsStart; flag < flagsEnd; flag++ ) { + switch ( *flag ) { + case '0': + // Flag 0 means the effect also works well on a single LED. + // Treat it as 1D-capable for the compact CoreS3 display. + capability.supports1D = true; + dimensionFlagFound = true; + break; + + case '1': + capability.supports1D = true; + dimensionFlagFound = true; + break; + + case '2': + capability.supports2D = true; + dimensionFlagFound = true; + break; + + case '3': + capability.supports3D = true; + dimensionFlagFound = true; + break; + + case 'v': + capability.audioVolume = true; + break; + + case 'f': + capability.audioFrequency = true; + break; + + default: + break; + } + } + + if ( !dimensionFlagFound ) { + // WLED metadata specification: missing dimension flags fall back to 1D. + capability.supports1D = true; + } + + return capability; + } + + bool effectUsesAudioReactive( + const M5StackEffectCapabilities& capability + ) const { + return capability.audioVolume || capability.audioFrequency; + } + + bool isCoreS3AudioUnavailable() const { +#if defined(WLED_M5STACK_CORES3_AUDIO) + return + coreS3AudioInitializationFinished() && + !coreS3AudioCodecReady(); +#else + return false; +#endif + } + + bool isAudioUnavailableForEffect( uint8_t effectMode ) { + const M5StackEffectCapabilities capability = + getEffectCapabilities( effectMode ); + + return + effectUsesAudioReactive( capability ) && + isCoreS3AudioUnavailable(); + } + + bool effectRequires2D( const M5StackEffectCapabilities& capability ) { + return capability.supports2D && !capability.supports1D; + } + + bool currentMainSegmentIs2D() { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + return strip.getMainSegment().is2D(); + } + + void getEffectDimensionText( + const M5StackEffectCapabilities& capability, + char* text, + size_t textSize + ) { + if ( text == nullptr || textSize == 0 ) { + return; + } + + text[0] = '\0'; + + if ( capability.supports1D && capability.supports2D && capability.supports3D ) { + strncpy( text, "1D/2D/3D", textSize - 1 ); + } + else if ( capability.supports1D && capability.supports2D ) { + strncpy( text, "1D/2D", textSize - 1 ); + } + else if ( capability.supports1D && capability.supports3D ) { + strncpy( text, "1D/3D", textSize - 1 ); + } + else if ( capability.supports2D && capability.supports3D ) { + strncpy( text, "2D/3D", textSize - 1 ); + } + else if ( capability.supports2D ) { + strncpy( text, "2D", textSize - 1 ); + } + else if ( capability.supports3D ) { + strncpy( text, "3D", textSize - 1 ); + } + else { + strncpy( text, "1D", textSize - 1 ); + } + + text[ textSize - 1 ] = '\0'; + } + + void getEffectAudioText( + const M5StackEffectCapabilities& capability, + char* text, + size_t textSize + ) { + if ( text == nullptr || textSize == 0 ) { + return; + } + + text[0] = '\0'; + + if ( capability.audioVolume && capability.audioFrequency ) { + strncpy( text, "AUDIO", textSize - 1 ); + } + else if ( capability.audioFrequency ) { + strncpy( text, "FFT", textSize - 1 ); + } + else if ( capability.audioVolume ) { + strncpy( text, "VOL", textSize - 1 ); + } + + text[ textSize - 1 ] = '\0'; + } + + void getEffectCapabilityText( + uint8_t effectMode, + char* text, + size_t textSize, + bool& incompatibleWithCurrentSegment + ) { + if ( text == nullptr || textSize == 0 ) { + incompatibleWithCurrentSegment = false; + return; + } + + const M5StackEffectCapabilities capability = getEffectCapabilities( effectMode ); + + incompatibleWithCurrentSegment = + effectRequires2D( capability ) && !currentMainSegmentIs2D(); + + const bool audioUnavailable = + effectUsesAudioReactive( capability ) && + isCoreS3AudioUnavailable(); + + char dimensionText[16]; + char audioText[8]; + + getEffectDimensionText( capability, dimensionText, sizeof(dimensionText) ); + getEffectAudioText( capability, audioText, sizeof(audioText) ); + + if ( incompatibleWithCurrentSegment ) { + if ( audioUnavailable ) { + snprintf( text, textSize, "2D REQ | AUDIO UNAVAILABLE" ); + } + else if ( audioText[0] != '\0' ) { + snprintf( text, textSize, "2D REQUIRED | %s", audioText ); + } + else { + snprintf( text, textSize, "2D REQUIRED" ); + } + + return; + } + + if ( audioUnavailable ) { + snprintf( text, textSize, "AUDIO UNAVAILABLE" ); + return; + } + + if ( audioText[0] != '\0' ) { + snprintf( text, textSize, "%s | %s", dimensionText, audioText ); + } + else { + snprintf( text, textSize, "%s", dimensionText ); + } + } + + bool getEffectMetadataSection( + uint8_t effectMode, + uint8_t sectionIndex, + const char*& sectionStart, + const char*& sectionEnd + ) { + sectionStart = nullptr; + sectionEnd = nullptr; + + const char* modeData = strip.getModeData( effectMode ); + + if ( modeData == nullptr ) { + return false; + } + + const char* metadataStart = strchr( modeData, '@' ); + + if ( metadataStart == nullptr ) { + return false; + } + + sectionStart = metadataStart + 1; + + for ( uint8_t section = 0; section < sectionIndex; section++ ) { + const char* separator = strchr( sectionStart, ';' ); + + if ( separator == nullptr ) { + sectionStart = nullptr; + return false; + } + + sectionStart = separator + 1; + } + + sectionEnd = strchr( sectionStart, ';' ); + + if ( sectionEnd == nullptr ) { + sectionEnd = sectionStart + strlen( sectionStart ); + } + + return true; + } + + void copyEffectMetadataLabel( + const char* fieldStart, + const char* fieldEnd, + const char* defaultLabel, + char* label, + size_t labelSize + ) { + if ( label == nullptr || labelSize == 0 ) { + return; + } + + label[0] = '\0'; + + if ( fieldStart == nullptr || fieldEnd == nullptr || fieldStart >= fieldEnd ) { + return; + } + + if ( fieldEnd - fieldStart == 1 && *fieldStart == '!' ) { + strncpy( label, defaultLabel, labelSize - 1 ); + label[ labelSize - 1 ] = '\0'; + return; + } + + size_t copyLength = (size_t)( fieldEnd - fieldStart ); + + if ( copyLength >= labelSize ) { + copyLength = labelSize - 1; + } + + memcpy( label, fieldStart, copyLength ); + label[ copyLength ] = '\0'; + } + + bool getEffectSliderMetadata( + uint8_t effectMode, + uint8_t sliderIndex, + const char* defaultLabel, + char* label, + size_t labelSize + ) { + if ( label == nullptr || labelSize == 0 ) { + return false; + } + + label[0] = '\0'; + + const char* sectionStart = nullptr; + const char* sectionEnd = nullptr; + + if ( !getEffectMetadataSection( effectMode, 0, sectionStart, sectionEnd ) ) { + // WLED metadata fallback: missing parameter section means the + // standard Speed + Intensity sliders are available. + if ( sliderIndex <= 1 ) { + strncpy( label, defaultLabel, labelSize - 1 ); + label[ labelSize - 1 ] = '\0'; + return true; + } + + return false; + } + + const char* fieldStart = sectionStart; + + for ( uint8_t fieldIndex = 0; fieldIndex < sliderIndex; fieldIndex++ ) { + const char* comma = nullptr; + + for ( const char* cursor = fieldStart; cursor < sectionEnd; cursor++ ) { + if ( *cursor == ',' ) { + comma = cursor; + break; + } + } + + if ( comma == nullptr ) { + // Explicit metadata section exists but this field is missing. + // WLED treats a missing/empty label as a disabled control. + return false; + } + + fieldStart = comma + 1; + } + + const char* fieldEnd = sectionEnd; + + for ( const char* cursor = fieldStart; cursor < sectionEnd; cursor++ ) { + if ( *cursor == ',' ) { + fieldEnd = cursor; + break; + } + } + + if ( fieldStart >= fieldEnd ) { + return false; + } + + copyEffectMetadataLabel( + fieldStart, + fieldEnd, + defaultLabel, + label, + labelSize + ); + + // Keep labels compact for the 320 px CoreS3 display. + if ( strlen(label) > 24 ) { + label[24] = '\0'; + } + + return label[0] != '\0'; + } + + bool getEffectSpeedControlMetadata( + uint8_t effectMode, + char* label, + size_t labelSize + ) { + return getEffectSliderMetadata( + effectMode, + 0, + "Speed", + label, + labelSize + ); + } + + bool getEffectIntensityControlMetadata( + uint8_t effectMode, + char* label, + size_t labelSize + ) { + return getEffectSliderMetadata( + effectMode, + 1, + "Intensity", + label, + labelSize + ); + } + + bool isEffectSpeedControlVisible( uint8_t effectMode ) { + char label[32]; + + return getEffectSpeedControlMetadata( + effectMode, + label, + sizeof(label) + ); + } + + bool isEffectIntensityControlVisible( uint8_t effectMode ) { + char label[32]; + + return getEffectIntensityControlMetadata( + effectMode, + label, + sizeof(label) + ); + } + + bool getEffectColorSlotCustomLabel( + uint8_t effectMode, + uint8_t colorSlot, + char* label, + size_t labelSize + ) { + if ( label == nullptr || labelSize == 0 || colorSlot > 2 ) { + return false; + } + + label[0] = '\0'; + + const char* sectionStart = nullptr; + const char* sectionEnd = nullptr; + + if ( !getEffectMetadataSection( effectMode, 1, sectionStart, sectionEnd ) ) { + // Missing Colors metadata uses WLED's default Fx/Bg/Cs labels. + // Keep CoreS3's stable C1/C2/C3 naming without an extra comment. + return false; + } + + const char* fieldStart = sectionStart; + + for ( uint8_t fieldIndex = 0; fieldIndex < colorSlot; fieldIndex++ ) { + const char* comma = nullptr; + + for ( const char* cursor = fieldStart; cursor < sectionEnd; cursor++ ) { + if ( *cursor == ',' ) { + comma = cursor; + break; + } + } + + if ( comma == nullptr ) { + return false; + } + + fieldStart = comma + 1; + } + + const char* fieldEnd = sectionEnd; + + for ( const char* cursor = fieldStart; cursor < sectionEnd; cursor++ ) { + if ( *cursor == ',' ) { + fieldEnd = cursor; + break; + } + } + + if ( fieldStart >= fieldEnd ) { + return false; + } + + // "!" means WLED's default Fx/Bg/Cs label. It is intentionally not + // repeated because CoreS3 keeps C1/C2/C3 as the primary slot names. + if ( fieldEnd - fieldStart == 1 && *fieldStart == '!' ) { + return false; + } + + size_t copyLength = (size_t)( fieldEnd - fieldStart ); + + // Keep the supplemental line compact on the 320 px display. + if ( copyLength > 24 ) { + copyLength = 24; + } + + if ( copyLength >= labelSize ) { + copyLength = labelSize - 1; + } + + memcpy( label, fieldStart, copyLength ); + label[ copyLength ] = '\0'; + + return label[0] != '\0'; + } + + M5StackEffectColorCapabilities getEffectColorCapabilities( uint8_t effectMode ) { + M5StackEffectColorCapabilities capability; + + const char* sectionStart = nullptr; + const char* sectionEnd = nullptr; + + if ( !getEffectMetadataSection( effectMode, 1, sectionStart, sectionEnd ) ) { + // WLED metadata fallback: missing Colors section means all three + // color slots (Fx/Bg/Cs) are available. + return capability; + } + + capability.color1 = false; + capability.color2 = false; + capability.color3 = false; + + if ( sectionStart >= sectionEnd ) { + // Explicit empty Colors section means the Effect uses no color slots. + return capability; + } + + const char* fieldStart = sectionStart; + + for ( uint8_t colorIndex = 0; colorIndex < 3; colorIndex++ ) { + const char* fieldEnd = sectionEnd; + + for ( const char* cursor = fieldStart; cursor < sectionEnd; cursor++ ) { + if ( *cursor == ',' ) { + fieldEnd = cursor; + break; + } + } + + const bool enabled = fieldStart < fieldEnd; + + if ( colorIndex == 0 ) { + capability.color1 = enabled; + } + else if ( colorIndex == 1 ) { + capability.color2 = enabled; + } + else { + capability.color3 = enabled; + } + + if ( fieldEnd >= sectionEnd ) { + break; + } + + fieldStart = fieldEnd + 1; + } + + return capability; + } + + bool effectUsesPrimaryColor( uint8_t effectMode ) { + return getEffectColorCapabilities( effectMode ).color1; + } + + bool effectUsesAnyColor( const M5StackEffectColorCapabilities& capability ) { + return capability.color1 || capability.color2 || capability.color3; + } + + void getEffectColorCapabilityText( + uint8_t effectMode, + char* text, + size_t textSize + ) { + if ( text == nullptr || textSize == 0 ) { + return; + } + + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( effectMode ); + + const char* capabilityText = "NONE"; + + if ( capability.color1 && capability.color2 && capability.color3 ) { + capabilityText = "C1/C2/C3"; + } + else if ( capability.color1 && capability.color2 ) { + capabilityText = "C1/C2"; + } + else if ( capability.color1 && capability.color3 ) { + capabilityText = "C1/C3"; + } + else if ( capability.color2 && capability.color3 ) { + capabilityText = "C2/C3"; + } + else if ( capability.color1 ) { + capabilityText = "C1"; + } + else if ( capability.color2 ) { + capabilityText = "C2"; + } + else if ( capability.color3 ) { + capabilityText = "C3"; + } + + strncpy( text, capabilityText, textSize - 1 ); + text[ textSize - 1 ] = '\0'; + } + + bool isEffectPaletteControlVisible( uint8_t effectMode ) { + const char* sectionStart = nullptr; + const char* sectionEnd = nullptr; + + if ( !getEffectMetadataSection( effectMode, 2, sectionStart, sectionEnd ) ) { + // WLED metadata fallback: missing Palette section means enabled. + return true; + } + + // Explicit empty Palette section means this Effect does not use palettes. + return sectionStart < sectionEnd; + } + + void getEffectName( uint8_t effectMode, char* effectName, size_t effectNameSize ) { + if ( effectName == nullptr || effectNameSize == 0 ) { + return; + } + + effectName[0] = '\0'; + + extractModeName( effectMode, nullptr, effectName, effectNameSize - 1 ); + + if ( strlen(effectName) == 0 ) { + strncpy( effectName, "Unknown", effectNameSize - 1 ); + + effectName[ effectNameSize - 1 ] = '\0'; + } + + if ( strlen(effectName) > 22 ) { + effectName[22] = '\0'; + } + } + + void drawEffectDetailButton( uint8_t effectMode, bool pressed ) { + const uint16_t buttonColor = TFT_CYAN; + + uint16_t backgroundColor = pressed ? buttonColor : TFT_BLACK; + + uint16_t textColor = pressed ? TFT_BLACK : TFT_WHITE; + + display.fillRect( EFFECT_DETAIL_X - 2, EFFECT_DETAIL_Y - 2, EFFECT_DETAIL_W + 4, EFFECT_DETAIL_H + 4, TFT_BLACK ); + + display.fillRect( EFFECT_DETAIL_X, EFFECT_DETAIL_Y, EFFECT_DETAIL_W, EFFECT_DETAIL_H, backgroundColor ); + + display.drawRect( EFFECT_DETAIL_X, EFFECT_DETAIL_Y, EFFECT_DETAIL_W, EFFECT_DETAIL_H, buttonColor ); + + display.drawRect( EFFECT_DETAIL_X + 1, EFFECT_DETAIL_Y + 1, EFFECT_DETAIL_W - 2, EFFECT_DETAIL_H - 2, buttonColor ); + + char effectName[64]; + + getEffectName( effectMode, effectName, sizeof(effectName) ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( textColor, backgroundColor ); + + if ( strlen(effectName) <= 10 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( effectName, EFFECT_DETAIL_X + (EFFECT_DETAIL_W / 2), EFFECT_DETAIL_Y + (EFFECT_DETAIL_H / 2) ); + + touchState.effectDetailVisualPressed = pressed; + } + + void drawEffect( uint8_t effectMode, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 120, screenWidth, 58, TFT_BLACK ); + + char capabilityText[40]; + char effectLabel[56]; + bool incompatibleWithCurrentSegment = false; + + getEffectCapabilityText( + effectMode, + capabilityText, + sizeof(capabilityText), + incompatibleWithCurrentSegment + ); + + snprintf( effectLabel, sizeof(effectLabel), "Effect %s", capabilityText ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( + ( incompatibleWithCurrentSegment || isAudioUnavailableForEffect( effectMode ) ) + ? TFT_YELLOW + : TFT_WHITE, + TFT_BLACK + ); + + display.setTextSize( 1 ); + + display.drawString( effectLabel, screenWidth / 2, 128 ); + + drawTriangleButton( CONTROL_LEFT_X, FX_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_EFFECT_PREV ); + + drawEffectDetailButton( effectMode, pressedTarget == M5STACK_TOUCH_TARGET_EFFECT_DETAIL ); + + drawTriangleButton( CONTROL_RIGHT_X, FX_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_EFFECT_NEXT ); + } + + void drawColorButton( uint32_t color, bool pressed ) { + const uint16_t buttonColor = TFT_CYAN; + + const uint8_t effectMode = getCurrentEffectMode(); + + const M5StackEffectColorCapabilities colorCapability = + getEffectColorCapabilities( effectMode ); + + const bool primaryColorUsed = colorCapability.color1; + + const bool anyColorUsed = effectUsesAnyColor( colorCapability ); + + uint16_t backgroundColor = pressed ? buttonColor : TFT_BLACK; + + uint16_t titleColor = pressed ? TFT_BLACK : TFT_WHITE; + + uint16_t capabilityColor = + pressed ? TFT_BLACK : + ( primaryColorUsed ? TFT_CYAN : ( anyColorUsed ? TFT_YELLOW : TFT_DARKGREY ) ); + + uint16_t previewColor = rgbTo565( R(color), G(color), B(color) ); + + display.fillRect( COLOR_BUTTON_X - 2, MAIN_BOTTOM_BUTTON_Y - 2, COLOR_BUTTON_W + 4, MAIN_BOTTOM_BUTTON_H + 4, TFT_BLACK ); + + display.fillRect( COLOR_BUTTON_X, MAIN_BOTTOM_BUTTON_Y, COLOR_BUTTON_W, MAIN_BOTTOM_BUTTON_H, backgroundColor ); + + display.drawRect( COLOR_BUTTON_X, MAIN_BOTTOM_BUTTON_Y, COLOR_BUTTON_W, MAIN_BOTTOM_BUTTON_H, buttonColor ); + + display.drawRect( COLOR_BUTTON_X + 1, MAIN_BOTTOM_BUTTON_Y + 1, COLOR_BUTTON_W - 2, MAIN_BOTTOM_BUTTON_H - 2, buttonColor ); + + static constexpr int16_t SWATCH_X = 28; + static constexpr int16_t SWATCH_W = 24; + static constexpr int16_t SWATCH_H = 24; + + int16_t swatchY = MAIN_BOTTOM_BUTTON_Y + ( ( MAIN_BOTTOM_BUTTON_H - SWATCH_H ) / 2 ); + + if ( primaryColorUsed ) { + display.fillRect( SWATCH_X, swatchY, SWATCH_W, SWATCH_H, previewColor ); + + display.drawRect( SWATCH_X, swatchY, SWATCH_W, SWATCH_H, TFT_WHITE ); + } + else { + display.fillRect( SWATCH_X, swatchY, SWATCH_W, SWATCH_H, TFT_BLACK ); + + display.drawRect( SWATCH_X, swatchY, SWATCH_W, SWATCH_H, TFT_DARKGREY ); + + display.drawLine( SWATCH_X + 4, swatchY + 4, SWATCH_X + SWATCH_W - 5, swatchY + SWATCH_H - 5, TFT_DARKGREY ); + + display.drawLine( SWATCH_X + SWATCH_W - 5, swatchY + 4, SWATCH_X + 4, swatchY + SWATCH_H - 5, TFT_DARKGREY ); + } + + char capabilityText[16]; + + getEffectColorCapabilityText( + effectMode, + capabilityText, + sizeof(capabilityText) + ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( titleColor, backgroundColor ); + + display.setTextSize( 2 ); + + display.drawString( "COLOR", 105, MAIN_BOTTOM_BUTTON_Y + 12 ); + + display.setTextColor( capabilityColor, backgroundColor ); + + display.setTextSize( 1 ); + + display.drawString( capabilityText, 105, MAIN_BOTTOM_BUTTON_Y + 29 ); + + touchState.colorButtonVisualPressed = pressed; + } + + void drawPresetOpenButton( bool pressed ) { + const uint16_t buttonColor = TFT_CYAN; + + uint16_t backgroundColor = pressed ? buttonColor : TFT_BLACK; + + uint16_t textColor = pressed ? TFT_BLACK : TFT_WHITE; + + display.fillRect( PRESET_OPEN_BUTTON_X - 2, MAIN_BOTTOM_BUTTON_Y - 2, PRESET_OPEN_BUTTON_W + 4, MAIN_BOTTOM_BUTTON_H + 4, TFT_BLACK ); + + display.fillRect( PRESET_OPEN_BUTTON_X, MAIN_BOTTOM_BUTTON_Y, PRESET_OPEN_BUTTON_W, MAIN_BOTTOM_BUTTON_H, backgroundColor ); + + display.drawRect( PRESET_OPEN_BUTTON_X, MAIN_BOTTOM_BUTTON_Y, PRESET_OPEN_BUTTON_W, MAIN_BOTTOM_BUTTON_H, buttonColor ); + + display.drawRect( PRESET_OPEN_BUTTON_X + 1, MAIN_BOTTOM_BUTTON_Y + 1, PRESET_OPEN_BUTTON_W - 2, MAIN_BOTTOM_BUTTON_H - 2, buttonColor ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( textColor, backgroundColor ); + + display.setTextSize( 2 ); + + display.drawString( "PRESET", PRESET_OPEN_BUTTON_X + (PRESET_OPEN_BUTTON_W / 2), MAIN_BOTTOM_BUTTON_Y + (MAIN_BOTTOM_BUTTON_H / 2) ); + + touchState.presetOpenButtonVisualPressed = pressed; + } + + void drawBackButton( bool pressed ) { + const uint16_t buttonColor = TFT_CYAN; + + uint16_t backgroundColor = pressed ? buttonColor : TFT_BLACK; + + uint16_t iconColor = pressed ? TFT_BLACK : buttonColor; + + display.fillRect( BACK_BUTTON_X - 2, BACK_BUTTON_Y - 2, BACK_BUTTON_W + 4, BACK_BUTTON_H + 4, TFT_BLACK ); + + display.fillRect( BACK_BUTTON_X, BACK_BUTTON_Y, BACK_BUTTON_W, BACK_BUTTON_H, backgroundColor ); + + display.drawRect( BACK_BUTTON_X, BACK_BUTTON_Y, BACK_BUTTON_W, BACK_BUTTON_H, buttonColor ); + + display.drawRect( BACK_BUTTON_X + 1, BACK_BUTTON_Y + 1, BACK_BUTTON_W - 2, BACK_BUTTON_H - 2, buttonColor ); + + int16_t centerX = BACK_BUTTON_X + (BACK_BUTTON_W / 2); + + int16_t centerY = BACK_BUTTON_Y + (BACK_BUTTON_H / 2); + + display.fillTriangle( centerX - 11, centerY, centerX - 1, centerY - 9, centerX - 1, centerY + 9, iconColor ); + + display.fillRect( centerX - 1, centerY - 2, 13, 5, iconColor ); + + touchState.backButtonVisualPressed = pressed; + } + + void drawColorDetails( uint32_t color ) { + display.fillRect( 0, 60, screenWidth, 78, TFT_BLACK ); + + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( getCurrentEffectMode() ); + + const int16_t slotX[3] = { + COLOR_SLOT_1_X, + COLOR_SLOT_2_X, + COLOR_SLOT_3_X + }; + + Segment& mainSegment = strip.getMainSegment(); + + for ( uint8_t colorSlot = 0; colorSlot < 3; colorSlot++ ) { + const bool enabled = + isEffectColorSlotEnabled( capability, colorSlot ); + + const bool selected = + enabled && colorSlot == selectedColorSlot; + + char slotLabel[4]; + + snprintf( slotLabel, sizeof(slotLabel), "C%u", colorSlot + 1 ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( + selected ? TFT_CYAN : ( enabled ? TFT_WHITE : TFT_DARKGREY ), + TFT_BLACK + ); + + display.setTextSize( 1 ); + + display.drawString( + slotLabel, + slotX[ colorSlot ] + ( COLOR_SLOT_W / 2 ), + COLOR_SLOT_LABEL_Y + ); + + if ( enabled ) { + const uint32_t slotColor = mainSegment.colors[ colorSlot ]; + + const uint16_t previewColor = + rgbTo565( R(slotColor), G(slotColor), B(slotColor) ); + + display.fillRect( + slotX[ colorSlot ], + COLOR_SLOT_Y, + COLOR_SLOT_W, + COLOR_SLOT_H, + previewColor + ); + + display.drawRect( + slotX[ colorSlot ], + COLOR_SLOT_Y, + COLOR_SLOT_W, + COLOR_SLOT_H, + selected ? TFT_CYAN : TFT_DARKGREY + ); + + if ( selected ) { + display.drawRect( + slotX[ colorSlot ] + 1, + COLOR_SLOT_Y + 1, + COLOR_SLOT_W - 2, + COLOR_SLOT_H - 2, + TFT_WHITE + ); + } + } + else { + display.fillRect( + slotX[ colorSlot ], + COLOR_SLOT_Y, + COLOR_SLOT_W, + COLOR_SLOT_H, + TFT_BLACK + ); + + display.drawRect( + slotX[ colorSlot ], + COLOR_SLOT_Y, + COLOR_SLOT_W, + COLOR_SLOT_H, + TFT_DARKGREY + ); + + display.drawLine( + slotX[ colorSlot ] + 8, + COLOR_SLOT_Y + 5, + slotX[ colorSlot ] + COLOR_SLOT_W - 9, + COLOR_SLOT_Y + COLOR_SLOT_H - 6, + TFT_DARKGREY + ); + + display.drawLine( + slotX[ colorSlot ] + COLOR_SLOT_W - 9, + COLOR_SLOT_Y + 5, + slotX[ colorSlot ] + 8, + COLOR_SLOT_Y + COLOR_SLOT_H - 6, + TFT_DARKGREY + ); + } + } + + char selectedText[24]; + char customColorLabel[32]; + + const bool hasCustomColorLabel = + getEffectColorSlotCustomLabel( + getCurrentEffectMode(), + selectedColorSlot, + customColorLabel, + sizeof(customColorLabel) + ); + + snprintf( + selectedText, + sizeof(selectedText), + "C%u #%02X%02X%02X", + selectedColorSlot + 1, + R(color), + G(color), + B(color) + ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( + selectedText, + screenWidth / 2, + 116 + ); + + char slotHintText[56]; + + if ( hasCustomColorLabel ) { + snprintf( + slotHintText, + sizeof(slotHintText), + "Role: %s Hold: %s", + customColorLabel, + color == 0 ? "RESTORE" : "BLACK" + ); + + display.setTextColor( TFT_CYAN, TFT_BLACK ); + } + else { + snprintf( + slotHintText, + sizeof(slotHintText), + "Hold: %s", + color == 0 ? "RESTORE" : "BLACK" + ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + } + + display.setTextSize( 1 ); + + display.drawString( + slotHintText, + screenWidth / 2, + 130 + ); + + display.drawFastHLine( 32, 136, screenWidth - 64, TFT_DARKGREY ); + } + + void drawHue( uint8_t hueValue, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + char valueText[8]; + + snprintf( valueText, sizeof(valueText), "%u", hueValue ); + + drawNumericControl( 138, 56, "Hue", 144, HUE_BUTTON_Y, HUE_BUTTON_Y + (CONTROL_BUTTON_H / 2), valueText, pressedTarget, M5STACK_TOUCH_TARGET_HUE_DOWN, M5STACK_TOUCH_TARGET_HUE_UP ); + } + + void drawSaturation( uint8_t saturationValue, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + char valueText[8]; + + snprintf( valueText, sizeof(valueText), "%u", saturationValue ); + + drawNumericControl( 194, 46, "Saturation", SATURATION_LABEL_Y, SATURATION_BUTTON_Y, SATURATION_BUTTON_Y + (CONTROL_BUTTON_H / 2), valueText, pressedTarget, M5STACK_TOUCH_TARGET_SATURATION_DOWN, M5STACK_TOUCH_TARGET_SATURATION_UP ); + } + + void drawSpeed( uint8_t speedValue, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + char label[32]; + + if ( !getEffectSpeedControlMetadata( getCurrentEffectMode(), label, sizeof(label) ) ) { + display.fillRect( 0, 62, screenWidth, 58, TFT_BLACK ); + return; + } + + char valueText[8]; + + snprintf( valueText, sizeof(valueText), "%u", speedValue ); + + drawNumericControl( 62, 58, label, 70, SPEED_BUTTON_Y, 99, valueText, pressedTarget, M5STACK_TOUCH_TARGET_SPEED_DOWN, M5STACK_TOUCH_TARGET_SPEED_UP ); + } + + void drawIntensity( uint8_t intensityValue, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + char label[32]; + + if ( !getEffectIntensityControlMetadata( getCurrentEffectMode(), label, sizeof(label) ) ) { + display.fillRect( 0, 120, screenWidth, 60, TFT_BLACK ); + return; + } + + char valueText[8]; + + snprintf( valueText, sizeof(valueText), "%u", intensityValue ); + + drawNumericControl( 120, 60, label, 128, INTENSITY_BUTTON_Y, 157, valueText, pressedTarget, M5STACK_TOUCH_TARGET_INTENSITY_DOWN, M5STACK_TOUCH_TARGET_INTENSITY_UP ); + } + + void drawPalette( uint8_t paletteId, M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 180, screenWidth, 60, TFT_BLACK ); + + if ( !isEffectPaletteControlVisible( getCurrentEffectMode() ) ) { + return; + } + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Palette", screenWidth / 2, PALETTE_LABEL_Y ); + + drawTriangleButton( CONTROL_LEFT_X, PALETTE_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_PALETTE_PREV ); + + drawTriangleButton( CONTROL_RIGHT_X, PALETTE_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_PALETTE_NEXT ); + + char paletteName[64]; + + getPaletteName( paletteId, paletteName, sizeof(paletteName) ); + + if ( strlen(paletteName) > 22 ) { + paletteName[22] = '\0'; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + if ( strlen(paletteName) <= 10 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( paletteName, screenWidth / 2, PALETTE_BUTTON_Y + (CONTROL_BUTTON_H / 2) ); + } + + void drawEffectPageName( uint8_t effectMode ) { + display.fillRect( 40, 30, 240, 26, TFT_BLACK ); + + char effectName[64]; + char capabilityText[40]; + bool incompatibleWithCurrentSegment = false; + + getEffectName( effectMode, effectName, sizeof(effectName) ); + + getEffectCapabilityText( + effectMode, + capabilityText, + sizeof(capabilityText), + incompatibleWithCurrentSegment + ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( effectName, screenWidth / 2, 36 ); + + display.setTextColor( + ( incompatibleWithCurrentSegment || isAudioUnavailableForEffect( effectMode ) ) + ? TFT_YELLOW + : TFT_CYAN, + TFT_BLACK + ); + + display.drawString( capabilityText, screenWidth / 2, 49 ); + } + + String getPresetDisplayName( uint8_t presetId ) { + if ( presetId == 0 ) { + return "Custom State"; + } + + if ( pendingPresetId == presetId && pendingPresetName.length() > 0 ) { + return pendingPresetName; + } + + String name; + + if ( getCachedPresetName( presetId, name ) ) { + return name; + } + + char fallback[24]; + + snprintf( fallback, sizeof(fallback), "Preset %u", presetId ); + + return String(fallback); + } + + void drawPresetDetails( uint8_t presetId, bool applying = false ) { + display.fillRect( 0, 60, screenWidth, 118, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + // ------------------------------------------------------- + // Internal Preset ID scan number is intentionally hidden. + // ------------------------------------------------------- + + if ( !presetCacheReady ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "Loading Presets", screenWidth / 2, PRESET_NAME_Y ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Updating Preset List", screenWidth / 2, PRESET_ID_Y ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "WLED remains active", screenWidth / 2, PRESET_STATUS_Y ); + + return; + } + + if ( presetNoEntries ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "No Presets", screenWidth / 2, PRESET_NAME_Y ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "No saved preset found", screenWidth / 2, PRESET_ID_Y ); + + display.drawString( "Use MANAGE to create", screenWidth / 2, PRESET_STATUS_Y ); + + return; + } + + if ( presetId == 0 ) { + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "Custom State", screenWidth / 2, PRESET_NAME_Y ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "No active preset", screenWidth / 2, PRESET_ID_Y ); + + display.drawString( "Use arrows to apply", screenWidth / 2, PRESET_STATUS_Y ); + + return; + } + + String presetName = getPresetDisplayName( presetId ); + + if ( presetName.length() > 28 ) { + presetName = presetName.substring( 0, 25 ) + "..."; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + if ( presetName.length() <= 12 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( presetName, screenWidth / 2, PRESET_NAME_Y ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetId ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( idText, screenWidth / 2, PRESET_ID_Y ); + + if (applying) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.drawString( "Applying...", screenWidth / 2, PRESET_STATUS_Y ); + } + else if ( currentPreset == presetId ) { + display.setTextColor( TFT_GREEN, TFT_BLACK ); + + display.drawString( "Active Preset", screenWidth / 2, PRESET_STATUS_Y ); + } + else { + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Saved WLED Preset", screenWidth / 2, PRESET_STATUS_Y ); + } + } + + void drawPresetTextButton( int16_t x, int16_t y, int16_t w, int16_t h, const char* label, bool enabled, bool pressed, uint8_t textSize ) { + uint16_t buttonColor = enabled ? TFT_CYAN : TFT_DARKGREY; + + uint16_t backgroundColor = ( enabled && pressed ) ? buttonColor : TFT_BLACK; + + uint16_t textColor = ( enabled && pressed ) ? TFT_BLACK : ( enabled ? TFT_WHITE : TFT_DARKGREY ); + + display.fillRect( x - 2, y - 2, w + 4, h + 4, TFT_BLACK ); + + display.fillRect( x, y, w, h, backgroundColor ); + + display.drawRect( x, y, w, h, buttonColor ); + + display.drawRect( x + 1, y + 1, w - 2, h - 2, buttonColor ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( textColor, backgroundColor ); + + display.setTextSize( textSize ); + + display.drawString( label, x + (w / 2), y + (h / 2) ); + } + + void drawPresetDangerTextButton( int16_t x, int16_t y, int16_t w, int16_t h, const char* label, bool enabled, bool pressed, uint8_t textSize ) { + uint16_t buttonColor = enabled ? TFT_RED : TFT_DARKGREY; + + uint16_t backgroundColor = ( enabled && pressed ) ? buttonColor : TFT_BLACK; + + uint16_t textColor = ( enabled && pressed ) ? TFT_BLACK : ( enabled ? TFT_RED : TFT_DARKGREY ); + + display.fillRect( x - 2, y - 2, w + 4, h + 4, TFT_BLACK ); + + display.fillRect( x, y, w, h, backgroundColor ); + + display.drawRect( x, y, w, h, buttonColor ); + + display.drawRect( x + 1, y + 1, w - 2, h - 2, buttonColor ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( textColor, backgroundColor ); + + display.setTextSize( textSize ); + + display.drawString( label, x + (w / 2), y + (h / 2) ); + } + + void drawPresetManageButton( bool pressed ) { + bool enabled = ( presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 ); + + const char* label = presetCacheReady ? "MANAGE" : "LOADING"; + + drawPresetTextButton( PRESET_MANAGE_BUTTON_X, PRESET_MANAGE_BUTTON_Y, PRESET_MANAGE_BUTTON_W, PRESET_MANAGE_BUTTON_H, label, enabled, pressed && enabled, 1 ); + + touchState.presetManageButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetNavigation( M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 178, screenWidth, 62, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Preset", screenWidth / 2, PRESET_NAV_LABEL_Y ); + + drawTriangleButton( CONTROL_LEFT_X, PRESET_NAV_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_PREV ); + + drawTriangleButton( CONTROL_RIGHT_X, PRESET_NAV_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_NEXT ); + + drawPresetManageButton( pressedTarget == M5STACK_TOUCH_TARGET_PRESET_MANAGE ); + } + + uint16_t getBatteryStatusColor() const { + if ( !batteryStatus.available || !batteryStatus.present ) { + return TFT_DARKGREY; + } + + if ( batteryStatus.level <= 15 ) { + return TFT_RED; + } + + if ( batteryStatus.level <= 35 ) { + return TFT_YELLOW; + } + + return TFT_GREEN; + } + + bool sampleBatteryStatus( bool forceRead = false ) { + const unsigned long now = millis(); + + if ( + !forceRead && + batteryStatusInitialized && + now - lastBatteryStatusRead < BATTERY_STATUS_UPDATE_MS + ) { + return false; + } + + lastBatteryStatusRead = now; + + M5StackBatteryStatus newStatus; + hardwareBackend.readBatteryStatus( newStatus ); + + const bool changed = + !batteryStatusInitialized || + newStatus.available != batteryStatus.available || + newStatus.present != batteryStatus.present || + newStatus.charging != batteryStatus.charging || + newStatus.level != batteryStatus.level; + + batteryStatus = newStatus; + batteryStatusInitialized = true; + + return changed; + } + + void drawMainBatteryStatus() { + display.fillRect( + BATTERY_STATUS_LEFT, + 28, + BATTERY_STATUS_RIGHT - BATTERY_STATUS_LEFT, + 28, + TFT_BLACK + ); + + const uint16_t batteryColor = getBatteryStatusColor(); + + // Battery body + positive terminal. + display.drawRect( + BATTERY_ICON_X, + BATTERY_ICON_Y, + BATTERY_ICON_W, + BATTERY_ICON_H, + batteryColor + ); + + display.fillRect( + BATTERY_ICON_X + BATTERY_ICON_W, + BATTERY_ICON_Y + 3, + 2, + 4, + batteryColor + ); + + if ( batteryStatus.available && batteryStatus.present ) { + const int16_t interiorW = BATTERY_ICON_W - 4; + int16_t fillW = + ( interiorW * batteryStatus.level + 99 ) / 100; + + fillW = constrain( fillW, 0, interiorW ); + + if ( fillW > 0 ) { + display.fillRect( + BATTERY_ICON_X + 2, + BATTERY_ICON_Y + 2, + fillW, + BATTERY_ICON_H - 4, + batteryColor + ); + } + } + + char batteryText[8]; + + if ( batteryStatus.available && batteryStatus.present ) { + snprintf( + batteryText, + sizeof(batteryText), + "%u%%", + batteryStatus.level + ); + } + else { + strlcpy( + batteryText, + "--%", + sizeof(batteryText) + ); + } + + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( batteryColor, TFT_BLACK ); + display.setTextSize( 1 ); + + display.drawString( + batteryText, + BATTERY_PERCENT_X, + BATTERY_PERCENT_Y + ); + } + + void drawMainNetworkStatusLine( + const String& networkStatusText, + uint16_t textColor = TFT_WHITE + ) { + display.fillRect( + HEADER_NETWORK_LEFT, + 28, + HEADER_NETWORK_RIGHT - HEADER_NETWORK_LEFT, + 28, + TFT_BLACK + ); + + display.setTextDatum( textdatum_t::middle_center ); + display.setTextColor( textColor, TFT_BLACK ); + display.setTextSize( 1 ); + + display.drawString( + networkStatusText, + HEADER_NETWORK_CENTER_X, + HEADER_IP_Y + ); + } + + void drawMainScreen( const String& networkStatusText ) { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_MAIN; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "WLED M5Stack CoreS3", HEADER_CENTER_X, HEADER_TITLE_Y ); + + drawMainNetworkStatusLine( networkStatusText ); + + sampleBatteryStatus( true ); + drawMainBatteryStatus(); + + display.drawFastHLine( 8, 58, screenWidth - 16, TFT_DARKGREY ); + + drawPowerButton( bri > 0, false ); + + drawBrightness( bri, M5STACK_TOUCH_TARGET_NONE ); + + uint8_t effectMode = getCurrentEffectMode(); + + drawEffect( effectMode, M5STACK_TOUCH_TARGET_NONE ); + + uint32_t primaryColor = getPrimaryColor(); + + drawColorButton( primaryColor, false ); + + drawPresetOpenButton( false ); + + syncLogicalColorFromRgb( primaryColor ); + + lastLedState = bri > 0 ? 1 : 0; + + lastBrightnessValue = bri; + + lastEffectMode = effectMode; + + lastSpeedValue = getCurrentSpeed(); + + lastIntensityValue = getCurrentIntensity(); + + lastPaletteValue = getCurrentPalette(); + + lastPresetValue = currentPreset; + + lastPrimaryColor = primaryColor; + + lastPrimaryColorValid = true; + } + + void drawColorScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_COLOR; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + const uint8_t effectMode = getCurrentEffectMode(); + + const M5StackEffectColorCapabilities colorCapability = + getEffectColorCapabilities( effectMode ); + + char capabilityText[16]; + char subtitleText[32]; + + getEffectColorCapabilityText( + effectMode, + capabilityText, + sizeof(capabilityText) + ); + + snprintf( + subtitleText, + sizeof(subtitleText), + "Effect uses %s", + capabilityText + ); + + const bool anyColorUsed = + effectUsesAnyColor( colorCapability ); + + drawStandardPageHeader( + "COLOR", + subtitleText, + anyColorUsed ? TFT_WHITE : TFT_YELLOW + ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + uint32_t primaryColor = getPrimaryColor(); + + if ( !anyColorUsed ) { + selectedColorSlot = 0; + + lastSelectedColorValid = false; + + display.fillRect( 0, 60, screenWidth, 180, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "COLOR NOT USED", screenWidth / 2, 104 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( + "This effect does not use color slots.", + screenWidth / 2, + 139 + ); + } + else { + normalizeSelectedColorSlot( effectMode ); + + const uint32_t selectedColor = getSelectedColor(); + + syncLogicalColorFromRgb( selectedColor ); + + lastSelectedColor = selectedColor; + lastSelectedColorValid = true; + + drawColorDetails( selectedColor ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + } + + cacheCurrentColorSlots(); + + lastLedState = bri > 0 ? 1 : 0; + + lastEffectMode = effectMode; + + lastPrimaryColor = primaryColor; + + lastPrimaryColorValid = true; + + lastHueValue = logicalHueValue; + + lastSaturationValue = logicalSaturationValue; + } + + void drawEffectDetailScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_EFFECT; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "EFFECT", screenWidth / 2, 18 ); + + uint8_t effectMode = getCurrentEffectMode(); + + drawEffectPageName( effectMode ); + + display.drawFastHLine( 8, 58, screenWidth - 16, TFT_DARKGREY ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + uint8_t speedValue = getCurrentSpeed(); + + uint8_t intensityValue = getCurrentIntensity(); + + uint8_t paletteValue = getCurrentPalette(); + + drawSpeed( speedValue, M5STACK_TOUCH_TARGET_NONE ); + + drawIntensity( intensityValue, M5STACK_TOUCH_TARGET_NONE ); + + drawPalette( paletteValue, M5STACK_TOUCH_TARGET_NONE ); + + lastLedState = bri > 0 ? 1 : 0; + + lastEffectMode = effectMode; + + lastSpeedValue = speedValue; + + lastIntensityValue = intensityValue; + + lastPaletteValue = paletteValue; + } + + void drawPresetScreen() { + syncPresetNavigationCursorFromCurrentPreset(); + + normalizePresetNavigationCursor(); + + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + + presetSubPage = PRESET_SUBPAGE_NAV; + + presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + presetSaveOperationIsOverwrite = false; + + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + presetOverwriteTargetId = 0; + + presetOverwriteTargetName = ""; + + presetDeleteOperationState = PRESET_DELETE_OP_IDLE; + + presetDeleteTargetId = 0; + + presetDeleteTargetName = ""; + + presetDeleteWasCurrentPreset = false; + presetDeleteWasBootPreset = false; + + presetDeleteResultStartMs = 0; + + presetBootOperationState = PRESET_BOOT_OP_IDLE; + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + presetBootResultStartMs = 0; + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + + presetSaveResultStartMs = 0; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + drawStandardPageHeader( "PRESET", "Saved WLED Preset" ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + uint8_t presetId = getDisplayedPresetId(); + + bool applying = ( pendingPresetId > 0 ); + + drawPresetDetails( presetId, applying ); + + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + + lastLedState = bri > 0 ? 1 : 0; + + lastPresetValue = presetId; + lastBootPresetValue = bootPreset; + + lastPresetsModifiedTime = presetsModifiedTime; + } + + void drawPresetSaveNewButton( bool pressed ) { + uint8_t freePresetId = findFirstFreePresetId(); + + bool enabled = ( presetCacheReady && !presetCacheBuilding && freePresetId > 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + char buttonLabel[24]; + + if ( !presetCacheReady || presetCacheBuilding ) { + strlcpy( buttonLabel, "SAVE NEW", sizeof(buttonLabel) ); + } + else if ( freePresetId == 0 ) { + strlcpy( buttonLabel, "SAVE NEW FULL", sizeof(buttonLabel) ); + } + else { + snprintf( + buttonLabel, + sizeof(buttonLabel), + "SAVE NEW #%u", + freePresetId + ); + } + + drawPresetTextButton( + PRESET_SAVE_NEW_BUTTON_X, + PRESET_SAVE_NEW_BUTTON_Y, + PRESET_SAVE_NEW_BUTTON_W, + PRESET_SAVE_NEW_BUTTON_H, + buttonLabel, + enabled, + pressed && enabled, + 2 + ); + + touchState.presetSaveNewButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetOverwriteOpenButton( bool pressed ) { + bool enabled = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetTextButton( PRESET_OVERWRITE_BUTTON_X, PRESET_OVERWRITE_BUTTON_Y, PRESET_OVERWRITE_BUTTON_W, PRESET_OVERWRITE_BUTTON_H, "OVERWRITE", enabled, pressed && enabled, 2 ); + + touchState.presetOverwriteOpenButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetDeleteOpenButton( bool pressed ) { + bool enabled = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetDangerTextButton( PRESET_DELETE_BUTTON_X, PRESET_DELETE_BUTTON_Y, PRESET_DELETE_BUTTON_W, PRESET_DELETE_BUTTON_H, "DELETE", enabled, pressed && enabled, 2 ); + + touchState.presetDeleteOpenButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetBootOpenButton( bool pressed ) { + bool enabled = ( presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetTextButton( PRESET_BOOT_BUTTON_X, PRESET_BOOT_BUTTON_Y, PRESET_BOOT_BUTTON_W, PRESET_BOOT_BUTTON_H, "BOOT PRESET", enabled, pressed && enabled, 2 ); + + touchState.presetBootOpenButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetSaveHoldButton( bool pressed ) { + bool enabled = ( presetSaveOperationState == PRESET_SAVE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetSaveCandidateId > 0 && findPresetCacheIndex( presetSaveCandidateId ) < 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetTextButton( PRESET_SAVE_HOLD_BUTTON_X, PRESET_SAVE_HOLD_BUTTON_Y, PRESET_SAVE_HOLD_BUTTON_W, PRESET_SAVE_HOLD_BUTTON_H, "HOLD TO SAVE", enabled, pressed && enabled, 2 ); + + touchState.presetSaveHoldButtonVisualPressed = ( pressed && enabled ); + } + + void getPresetManageCurrentStateText( + char* text, + size_t textSize + ) { + if ( text == nullptr || textSize == 0 ) { + return; + } + + text[0] = '\0'; + + if ( pendingPresetId > 0 ) { + snprintf( + text, + textSize, + "Current: Applying Preset #%u", + pendingPresetId + ); + + return; + } + + if ( currentPreset == 0 ) { + strlcpy( + text, + "Current: Custom State", + textSize + ); + + return; + } + + snprintf( + text, + textSize, + "Current: Active Preset #%u", + currentPreset + ); + } + + void drawPresetManageScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + + presetSubPage = PRESET_SUBPAGE_MANAGE; + + presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + presetSaveOperationIsOverwrite = false; + + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + presetOverwriteTargetId = 0; + + presetOverwriteTargetName = ""; + + presetDeleteOperationState = PRESET_DELETE_OP_IDLE; + + presetDeleteTargetId = 0; + + presetDeleteTargetName = ""; + + presetDeleteWasCurrentPreset = false; + presetDeleteWasBootPreset = false; + + presetDeleteResultStartMs = 0; + + presetBootOperationState = PRESET_BOOT_OP_IDLE; + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + presetBootResultStartMs = 0; + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + + presetSaveResultStartMs = 0; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + char manageSubtitle[40]; + + getPresetManageCurrentStateText( + manageSubtitle, + sizeof(manageSubtitle) + ); + + drawStandardPageHeader( "PRESET MANAGE", manageSubtitle ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + drawPresetSaveNewButton( false ); + + drawPresetOverwriteOpenButton( false ); + + drawPresetDeleteOpenButton( false ); + + drawPresetBootOpenButton( false ); + + lastLedState = bri > 0 ? 1 : 0; + + lastPresetValue = currentPreset; + + lastPresetsModifiedTime = presetsModifiedTime; + } + + // ========================================================= + // SAVE NEW / OVERWRITE operation status + // + // Internal 1..250 cache scan position is intentionally + // hidden from the user. + // ========================================================= + + void drawPresetSaveOperationStatus() { + if ( currentPage != SCREEN_PRESET || ( presetSubPage != PRESET_SUBPAGE_SAVE && presetSubPage != PRESET_SUBPAGE_OVERWRITE ) ) { + return; + } + + display.fillRect( 0, 60, screenWidth, 180, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + if ( presetSaveOperationState == PRESET_SAVE_OP_WAIT_WLED ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( presetSaveOperationIsOverwrite ? "Overwriting..." : "Saving...", screenWidth / 2, 110 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( presetSaveCandidateName, screenWidth / 2, 142 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetSaveCandidateId ); + + display.drawString( idText, screenWidth / 2, 163 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Writing WLED Preset", screenWidth / 2, 188 ); + + return; + } + + if ( presetSaveOperationState == PRESET_SAVE_OP_WAIT_CACHE ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "Updating Preset List", screenWidth / 2, 110 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( presetSaveOperationIsOverwrite ? "Verifying Overwrite..." : "Verifying New Preset...", screenWidth / 2, 158 ); + + return; + } + + if ( presetSaveOperationState == PRESET_SAVE_OP_SUCCESS ) { + display.setTextColor( TFT_GREEN, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( presetSaveOperationIsOverwrite ? "OVERWRITTEN" : "SAVED", screenWidth / 2, 104 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( presetSaveCandidateName, screenWidth / 2, 140 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetSaveCandidateId ); + + display.drawString( idText, screenWidth / 2, 165 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Preset cache verified", screenWidth / 2, 192 ); + + return; + } + + if ( presetSaveOperationState == PRESET_SAVE_OP_FAILED ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( presetSaveOperationIsOverwrite ? "OVERWRITE FAILED" : "SAVE FAILED", screenWidth / 2, 108 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Preset was not verified", screenWidth / 2, 150 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Returning to PRESET MANAGE", screenWidth / 2, 180 ); + } + } + + void drawPresetSaveScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + + presetSubPage = PRESET_SUBPAGE_SAVE; + + if ( presetSaveOperationState == PRESET_SAVE_OP_IDLE ) { + presetSaveOperationIsOverwrite = false; + } + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + drawStandardPageHeader( "SAVE PRESET", "Current WLED State" ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + if ( presetSaveOperationState != PRESET_SAVE_OP_IDLE ) { + drawPresetSaveOperationStatus(); + return; + } + + if ( presetSaveCandidateId == 0 ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "No Free ID", screenWidth / 2, 110 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Preset IDs 1-250 are unavailable", screenWidth / 2, 150 ); + + return; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + if ( presetSaveCandidateName.length() <= 18 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( presetSaveCandidateName, screenWidth / 2, 88 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetSaveCandidateId ); + + display.setTextSize( 1 ); + + display.drawString( idText, screenWidth / 2, 120 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Hold 1 second to save", screenWidth / 2, 151 ); + + drawPresetSaveHoldButton( false ); + + lastLedState = bri > 0 ? 1 : 0; + } + + void drawPresetOverwriteNavigation( M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 112, screenWidth, 58, TFT_BLACK ); + + drawTriangleButton( CONTROL_LEFT_X, PRESET_OVERWRITE_NAV_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV ); + + drawTriangleButton( CONTROL_RIGHT_X, PRESET_OVERWRITE_NAV_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "TARGET", screenWidth / 2, PRESET_OVERWRITE_NAV_BUTTON_Y + (CONTROL_BUTTON_H / 2) ); + } + + void drawPresetOverwriteHoldButton( bool pressed ) { + bool enabled = ( presetSaveOperationState == PRESET_SAVE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetOverwriteTargetId > 0 && findPresetCacheIndex( presetOverwriteTargetId ) >= 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetTextButton( PRESET_OVERWRITE_HOLD_BUTTON_X, PRESET_OVERWRITE_HOLD_BUTTON_Y, PRESET_OVERWRITE_HOLD_BUTTON_W, PRESET_OVERWRITE_HOLD_BUTTON_H, "HOLD TO OVERWRITE", enabled, pressed && enabled, 1 ); + + touchState.presetOverwriteHoldButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetOverwriteScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + + presetSubPage = PRESET_SUBPAGE_OVERWRITE; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + drawStandardPageHeader( "OVERWRITE PRESET", "Select destination only" ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + if ( presetSaveOperationState != PRESET_SAVE_OP_IDLE ) { + drawPresetSaveOperationStatus(); + return; + } + + if ( presetOverwriteTargetId == 0 || findPresetCacheIndex( presetOverwriteTargetId ) < 0 ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "No Preset", screenWidth / 2, 104 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Nothing can be overwritten", screenWidth / 2, 145 ); + + return; + } + + String displayName = presetOverwriteTargetName; + + if ( displayName.length() > 28 ) { + displayName = displayName.substring( 0, 25 ) + "..."; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + if ( displayName.length() <= 12 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( displayName, screenWidth / 2, 82 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetOverwriteTargetId ); + + display.setTextSize( 1 ); + + display.drawString( idText, screenWidth / 2, 105 ); + + drawPresetOverwriteNavigation( M5STACK_TOUCH_TARGET_NONE ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Current WLED State", screenWidth / 2, 174 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "will replace this Preset", screenWidth / 2, 187 ); + + drawPresetOverwriteHoldButton( false ); + + lastLedState = bri > 0 ? 1 : 0; + } + + void drawPresetDeleteNavigation( M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 112, screenWidth, 58, TFT_BLACK ); + + drawTriangleButton( CONTROL_LEFT_X, PRESET_DELETE_NAV_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV ); + + drawTriangleButton( CONTROL_RIGHT_X, PRESET_DELETE_NAV_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "TARGET", screenWidth / 2, PRESET_DELETE_NAV_BUTTON_Y + (CONTROL_BUTTON_H / 2) ); + } + + void drawPresetDeleteHoldButton( bool pressed ) { + bool enabled = ( presetDeleteOperationState == PRESET_DELETE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetDeleteTargetId > 0 && findPresetCacheIndex( presetDeleteTargetId ) >= 0 && pendingPresetId == 0 && !presetNeedsSaving() ); + + drawPresetDangerTextButton( PRESET_DELETE_HOLD_BUTTON_X, PRESET_DELETE_HOLD_BUTTON_Y, PRESET_DELETE_HOLD_BUTTON_W, PRESET_DELETE_HOLD_BUTTON_H, "HOLD TO DELETE", enabled, pressed && enabled, 1 ); + + touchState.presetDeleteHoldButtonVisualPressed = ( pressed && enabled ); + } + + // ========================================================= + // DELETE operation status + // + // Internal cache scan position is not displayed. + // ========================================================= + + void drawPresetDeleteOperationStatus() { + if ( currentPage != SCREEN_PRESET || presetSubPage != PRESET_SUBPAGE_DELETE ) { + return; + } + + display.fillRect( 0, 60, screenWidth, 180, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + if ( presetDeleteOperationState == PRESET_DELETE_OP_WAIT_CACHE ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "Updating Preset List", screenWidth / 2, 105 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( presetDeleteTargetName, screenWidth / 2, 145 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Verifying Deletion...", screenWidth / 2, 175 ); + + return; + } + + if ( presetDeleteOperationState == PRESET_DELETE_OP_SUCCESS ) { + display.setTextColor( TFT_GREEN, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "DELETED", screenWidth / 2, 104 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( presetDeleteTargetName, screenWidth / 2, 140 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetDeleteTargetId ); + + display.drawString( idText, screenWidth / 2, 165 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Preset cache verified", screenWidth / 2, 192 ); + + return; + } + + if ( presetDeleteOperationState == PRESET_DELETE_OP_FAILED ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "DELETE FAILED", screenWidth / 2, 108 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Preset still exists", screenWidth / 2, 150 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Returning to PRESET MANAGE", screenWidth / 2, 180 ); + } + } + + void drawPresetDeleteScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + + presetSubPage = PRESET_SUBPAGE_DELETE; + + readyScreenShown = true; + + connectingScreenShown = false; + + resetTouchGesture(); + + drawStandardPageHeader( "DELETE PRESET", "Select target only", TFT_RED ); + + drawPowerButton( bri > 0, false ); + + drawBackButton( false ); + + if ( presetDeleteOperationState != PRESET_DELETE_OP_IDLE ) { + drawPresetDeleteOperationStatus(); + return; + } + + if ( presetDeleteTargetId == 0 || findPresetCacheIndex( presetDeleteTargetId ) < 0 ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 2 ); + + display.drawString( "No Preset", screenWidth / 2, 104 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Nothing can be deleted", screenWidth / 2, 145 ); + + return; + } + + String displayName = presetDeleteTargetName; + + if ( displayName.length() > 28 ) { + displayName = displayName.substring( 0, 25 ) + "..."; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + + if ( displayName.length() <= 12 ) { + display.setTextSize( 2 ); + } + else { + display.setTextSize( 1 ); + } + + display.drawString( displayName, screenWidth / 2, 82 ); + + char idText[24]; + + snprintf( idText, sizeof(idText), "Preset ID: %u", presetDeleteTargetId ); + + display.setTextSize( 1 ); + + display.drawString( idText, screenWidth / 2, 105 ); + + drawPresetDeleteNavigation( M5STACK_TOUCH_TARGET_NONE ); + + display.setTextColor( TFT_RED, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "Delete this Preset", screenWidth / 2, 174 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + + display.drawString( "Current LED state is kept", screenWidth / 2, 187 ); + + drawPresetDeleteHoldButton( false ); + + lastLedState = bri > 0 ? 1 : 0; + } + + void drawPresetBootNavigation( M5StackTouchTarget pressedTarget = M5STACK_TOUCH_TARGET_NONE ) { + display.fillRect( 0, 112, screenWidth, 58, TFT_BLACK ); + + drawTriangleButton( CONTROL_LEFT_X, PRESET_BOOT_NAV_BUTTON_Y, false, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV ); + + drawTriangleButton( CONTROL_RIGHT_X, PRESET_BOOT_NAV_BUTTON_Y, true, pressedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT ); + + display.setTextDatum( textdatum_t::middle_center ); + + display.setTextColor( TFT_CYAN, TFT_BLACK ); + + display.setTextSize( 1 ); + + display.drawString( "BOOT TARGET", screenWidth / 2, PRESET_BOOT_NAV_BUTTON_Y + (CONTROL_BUTTON_H / 2) ); + } + + bool isPresetBootTargetValid() { + return presetBootTargetId == 0 || findPresetCacheIndex( presetBootTargetId ) >= 0; + } + + bool isPresetBootTargetCurrent() { + return isPresetBootTargetValid() && presetBootTargetId == bootPreset; + } + + void drawPresetBootHoldButton( bool pressed ) { + bool enabled = ( presetBootOperationState == PRESET_BOOT_OP_IDLE && presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 && !presetNeedsSaving() && isPresetBootTargetValid() && !isPresetBootTargetCurrent() ); + + const char* label; + + if ( isPresetBootTargetCurrent() ) { + label = ( presetBootTargetId == 0 ) ? "BOOT ALREADY NONE" : "CURRENT BOOT PRESET"; + } + else { + label = ( presetBootTargetId == 0 ) ? "HOLD TO CLEAR BOOT" : "HOLD TO SET BOOT"; + } + + drawPresetTextButton( PRESET_BOOT_HOLD_BUTTON_X, PRESET_BOOT_HOLD_BUTTON_Y, PRESET_BOOT_HOLD_BUTTON_W, PRESET_BOOT_HOLD_BUTTON_H, label, enabled, pressed && enabled, 1 ); + + touchState.presetBootHoldButtonVisualPressed = ( pressed && enabled ); + } + + void drawPresetBootOperationStatus() { + if ( currentPage != SCREEN_PRESET || presetSubPage != PRESET_SUBPAGE_BOOT ) { + return; + } + + display.fillRect( 0, 60, screenWidth, 180, TFT_BLACK ); + + display.setTextDatum( textdatum_t::middle_center ); + + if ( presetBootOperationState == PRESET_BOOT_OP_WAIT_CONFIG ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "Saving Boot Setting", screenWidth / 2, 105 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( presetBootTargetId == 0 ? "NONE" : presetBootTargetName, screenWidth / 2, 145 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "Writing WLED Config...", screenWidth / 2, 175 ); + + return; + } + + if ( presetBootOperationState == PRESET_BOOT_OP_SUCCESS ) { + display.setTextColor( TFT_GREEN, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( presetBootTargetId == 0 ? "BOOT CLEARED" : "BOOT PRESET SET", screenWidth / 2, 104 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( presetBootTargetId == 0 ? "NONE" : presetBootTargetName, screenWidth / 2, 140 ); + + if ( presetBootTargetId > 0 ) { + char idText[24]; + snprintf( idText, sizeof(idText), "Preset ID: %u", presetBootTargetId ); + display.drawString( idText, screenWidth / 2, 165 ); + } + else { + display.drawString( "No startup Preset", screenWidth / 2, 165 ); + } + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "WLED config saved", screenWidth / 2, 192 ); + + return; + } + + if ( presetBootOperationState == PRESET_BOOT_OP_FAILED ) { + display.setTextColor( TFT_RED, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "BOOT SET FAILED", screenWidth / 2, 108 ); + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( "Setting was not verified", screenWidth / 2, 150 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "Returning to PRESET MANAGE", screenWidth / 2, 180 ); + } + } + + void drawPresetBootScreen() { + display.fillScreen( TFT_BLACK ); + + currentPage = SCREEN_PRESET; + presetSubPage = PRESET_SUBPAGE_BOOT; + readyScreenShown = true; + connectingScreenShown = false; + + resetTouchGesture(); + + drawStandardPageHeader( "BOOT PRESET", "Startup Preset" ); + + drawPowerButton( bri > 0, false ); + drawBackButton( false ); + + if ( presetBootOperationState != PRESET_BOOT_OP_IDLE ) { + drawPresetBootOperationStatus(); + return; + } + + if ( !presetCacheReady || presetCacheBuilding ) { + display.setTextColor( TFT_YELLOW, TFT_BLACK ); + display.setTextSize( 2 ); + display.drawString( "Loading Presets", screenWidth / 2, 104 ); + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.setTextSize( 1 ); + display.drawString( "Updating Preset List", screenWidth / 2, 145 ); + return; + } + + if ( presetBootTargetId > 0 && findPresetCacheIndex( presetBootTargetId ) < 0 ) { + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + } + + String displayName = ( presetBootTargetId == 0 ) ? String("NONE") : presetBootTargetName; + + if ( displayName.length() > 28 ) { + displayName = displayName.substring( 0, 25 ) + "..."; + } + + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.setTextSize( displayName.length() <= 12 ? 2 : 1 ); + display.drawString( displayName, screenWidth / 2, 82 ); + + display.setTextSize( 1 ); + + if ( presetBootTargetId == 0 ) { + display.drawString( "No startup Preset", screenWidth / 2, 105 ); + } + else { + char idText[24]; + snprintf( idText, sizeof(idText), "Preset ID: %u", presetBootTargetId ); + display.drawString( idText, screenWidth / 2, 105 ); + } + + drawPresetBootNavigation( M5STACK_TOUCH_TARGET_NONE ); + + if ( presetBootTargetId == bootPreset && ( bootPreset == 0 || findPresetCacheIndex( bootPreset ) >= 0 ) ) { + display.setTextColor( TFT_GREEN, TFT_BLACK ); + display.drawString( bootPreset == 0 ? "Boot Preset Disabled" : "Current Boot Preset", screenWidth / 2, 174 ); + } + else if ( bootPreset > 0 && findPresetCacheIndex( bootPreset ) < 0 && presetBootTargetId == 0 ) { + char missingText[40]; + snprintf( missingText, sizeof(missingText), "Configured Boot ID %u missing", bootPreset ); + display.setTextColor( TFT_RED, TFT_BLACK ); + display.drawString( missingText, screenWidth / 2, 174 ); + } + else { + display.setTextColor( TFT_WHITE, TFT_BLACK ); + display.drawString( "Set this at startup", screenWidth / 2, 174 ); + } + + display.setTextColor( TFT_DARKGREY, TFT_BLACK ); + display.drawString( "Current LED state is kept", screenWidth / 2, 187 ); + + drawPresetBootHoldButton( false ); + + lastBootPresetValue = bootPreset; + lastLedState = bri > 0 ? 1 : 0; + } + + + void beginHueEdit() { + uint32_t currentColor = getSelectedColor(); + + if ( !logicalColorHsvValid || !lastSelectedColorValid || currentColor != lastSelectedColor ) { + syncLogicalColorFromRgb( currentColor ); + + lastSelectedColor = currentColor; + + lastSelectedColorValid = true; + } + + hueEditHsv = logicalColorHsv; + + // A true black WLED color converts to HSV with V=0. CoreS3 intentionally + // has no per-color Value control, so changing Hue alone could never leave + // black. Once the user starts editing a black slot, bootstrap the edit + // state to a visible fully-saturated color. The stored WLED color is not + // changed until the first actual Hue step is applied. + if ( currentColor == 0 ) { + hueEditHsv.s = 255; + hueEditHsv.v = 255; + } + + hueEditValue = logicalHueValue; + + hueEditWhite = logicalWhiteValue; + + hueEditValid = true; + } + + void beginSaturationEdit() { + uint32_t currentColor = getSelectedColor(); + + if ( !logicalColorHsvValid || !lastSelectedColorValid || currentColor != lastSelectedColor ) { + syncLogicalColorFromRgb( currentColor ); + + lastSelectedColor = currentColor; + + lastSelectedColorValid = true; + } + + saturationEditHsv = logicalColorHsv; + + // Saturation also cannot make a true black color visible while HSV V is + // zero. Bootstrap only the edit Value; the requested Saturation remains + // under direct user control. + if ( currentColor == 0 ) { + saturationEditHsv.v = 255; + } + + saturationEditValue = logicalSaturationValue; + + saturationEditWhite = logicalWhiteValue; + + saturationEditValid = true; + } + + void resetTouchGesture() { + touchState.touchActive = false; + + touchState.touchTarget = M5STACK_TOUCH_TARGET_NONE; + + touchState.lastTouchInsidePower = false; + + touchState.lastTouchInsideWiFiRecovery = false; + + touchState.lastTouchInsideBrightness = false; + + touchState.lastTouchInsideEffect = false; + + touchState.lastTouchInsideEffectDetail = false; + + touchState.lastTouchInsideColor = false; + + touchState.lastTouchInsidePresetOpen = false; + + touchState.lastTouchInsideBack = false; + + touchState.lastTouchInsideColorSlot = false; + + touchState.lastTouchInsideHue = false; + + touchState.lastTouchInsideSaturation = false; + + touchState.lastTouchInsideSpeed = false; + + touchState.lastTouchInsideIntensity = false; + + touchState.lastTouchInsidePalette = false; + + touchState.lastTouchInsidePresetNav = false; + + touchState.lastTouchInsidePresetManage = false; + + touchState.lastTouchInsidePresetSaveNew = false; + + touchState.lastTouchInsidePresetSaveHold = false; + + touchState.lastTouchInsidePresetOverwriteOpen = false; + + touchState.lastTouchInsidePresetOverwriteNav = false; + + touchState.lastTouchInsidePresetOverwriteHold = false; + + touchState.lastTouchInsidePresetDeleteOpen = false; + + touchState.lastTouchInsidePresetDeleteNav = false; + + touchState.lastTouchInsidePresetDeleteHold = false; + + touchState.lastTouchInsidePresetBootOpen = false; + touchState.lastTouchInsidePresetBootNav = false; + touchState.lastTouchInsidePresetBootHold = false; + + touchState.powerButtonVisualPressed = false; + + touchState.wifiRecoveryVisualPressed = false; + + touchState.brightnessButtonVisualPressed = false; + + touchState.effectButtonVisualPressed = false; + + touchState.effectDetailVisualPressed = false; + + touchState.colorButtonVisualPressed = false; + + touchState.presetOpenButtonVisualPressed = false; + + touchState.backButtonVisualPressed = false; + + touchState.hueButtonVisualPressed = false; + + touchState.saturationButtonVisualPressed = false; + + touchState.speedButtonVisualPressed = false; + + touchState.intensityButtonVisualPressed = false; + + touchState.paletteButtonVisualPressed = false; + + touchState.presetNavButtonVisualPressed = false; + + touchState.presetManageButtonVisualPressed = false; + + touchState.presetSaveNewButtonVisualPressed = false; + + touchState.presetSaveHoldButtonVisualPressed = false; + + touchState.presetOverwriteOpenButtonVisualPressed = false; + + touchState.presetOverwriteNavButtonVisualPressed = false; + + touchState.presetOverwriteHoldButtonVisualPressed = false; + + touchState.presetDeleteOpenButtonVisualPressed = false; + + touchState.presetDeleteNavButtonVisualPressed = false; + + touchState.presetDeleteHoldButtonVisualPressed = false; + + touchState.presetBootOpenButtonVisualPressed = false; + touchState.presetBootNavButtonVisualPressed = false; + touchState.presetBootHoldButtonVisualPressed = false; + + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.wifiRecoveryHoldState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.brightnessRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.effectRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.colorSlotHoldState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.hueRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.saturationRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.speedRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.intensityRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.paletteRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.presetRepeatState ); + + hueEditValid = false; + + saturationEditValid = false; + + touchState.touchReleaseCandidate = 0; + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + + touchState.lastTouchX = -1; + + touchState.lastTouchY = -1; + } + + void toggleLedPowerFromTouch() { + toggleOnOff(); + + stateUpdated( CALL_MODE_BUTTON ); + + lastLedState = -1; + + lastBrightnessValue = -1; + } + + bool applyBrightnessValue( int newValue ) { + newValue = constrain( newValue, 0, 255 ); + + if ( newValue == bri ) { + return false; + } + + if ( newValue == 0 ) { + if ( bri > 0 ) { + briLast = bri; + + bri = 0; + } + } + else { + if ( bri == 0 ) { + strip.restartRuntime(); + } + + bri = (uint8_t)newValue; + } + + stateUpdated( CALL_MODE_BUTTON ); + + lastLedState = -1; + + return true; + } + + void applyBrightnessStep( int step ) { + int newValue = constrain( (int)bri + step, 0, 255 ); + + if ( applyBrightnessValue( newValue ) ) { + if ( currentPage == SCREEN_MAIN ) { + drawBrightness( bri, touchState.touchTarget ); + } + + lastBrightnessValue = bri; + } + } + + void brightnessShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN ) { + applyBrightnessStep( -BRI_SHORT_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_BRIGHTNESS_UP ) { + applyBrightnessStep( BRI_SHORT_STEP ); + } + } + + void brightnessLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN ) { + applyBrightnessStep( -BRI_LONG_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_BRIGHTNESS_UP ) { + applyBrightnessStep( BRI_LONG_STEP ); + } + } + + void applyEffectStep( int step ) { + uint8_t modeCount = strip.getModeCount(); + + if ( modeCount == 0 || step == 0 ) { + return; + } + + Segment& mainSegment = strip.getMainSegment(); + + const int direction = ( step < 0 ) ? -1 : 1; + + int newMode = mainSegment.mode; + + bool validModeFound = false; + + for ( uint16_t attempt = 0; attempt < modeCount; attempt++ ) { + newMode += direction; + + if ( newMode < 0 ) { + newMode = modeCount - 1; + } + + if ( newMode >= modeCount ) { + newMode = 0; + } + + const char* modeData = strip.getModeData( (uint8_t)newMode ); + + if ( modeData != nullptr && strncmp_P( "RSVD", modeData, 4 ) != 0 ) { + validModeFound = true; + break; + } + } + + if ( !validModeFound || newMode == mainSegment.mode ) { + return; + } + + mainSegment.setMode( (uint8_t)newMode ); + + stateUpdated( CALL_MODE_BUTTON ); + + if ( currentPage == SCREEN_MAIN ) { + drawEffect( mainSegment.mode, touchState.touchTarget ); + + drawColorButton( getPrimaryColor(), false ); + } + + lastEffectMode = mainSegment.mode; + + lastSpeedValue = mainSegment.speed; + + lastIntensityValue = mainSegment.intensity; + + lastPaletteValue = mainSegment.palette; + } + + void effectLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_EFFECT_PREV ) { + applyEffectStep( -1 ); + } + else if ( target == M5STACK_TOUCH_TARGET_EFFECT_NEXT ) { + applyEffectStep( 1 ); + } + } + + bool applySpeedValue( int newValue ) { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + newValue = constrain( newValue, 0, 255 ); + + Segment& mainSegment = strip.getMainSegment(); + + if ( newValue == mainSegment.speed ) { + return false; + } + + mainSegment.speed = (uint8_t)newValue; + + stateUpdated( CALL_MODE_BUTTON ); + + if ( currentPage == SCREEN_EFFECT ) { + drawSpeed( mainSegment.speed, touchState.touchTarget ); + } + + lastSpeedValue = mainSegment.speed; + + return true; + } + + void applySpeedStep( int step ) { + int newValue = constrain( (int)getCurrentSpeed() + step, 0, 255 ); + + applySpeedValue( newValue ); + } + + void speedShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_SPEED_DOWN ) { + applySpeedStep( -SPEED_SHORT_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_SPEED_UP ) { + applySpeedStep( SPEED_SHORT_STEP ); + } + } + + void speedLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_SPEED_DOWN ) { + applySpeedStep( -SPEED_LONG_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_SPEED_UP ) { + applySpeedStep( SPEED_LONG_STEP ); + } + } + + bool applyIntensityValue( int newValue ) { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + newValue = constrain( newValue, 0, 255 ); + + Segment& mainSegment = strip.getMainSegment(); + + if ( newValue == mainSegment.intensity ) { + return false; + } + + mainSegment.intensity = (uint8_t)newValue; + + stateUpdated( CALL_MODE_BUTTON ); + + if ( currentPage == SCREEN_EFFECT ) { + drawIntensity( mainSegment.intensity, touchState.touchTarget ); + } + + lastIntensityValue = mainSegment.intensity; + + return true; + } + + void applyIntensityStep( int step ) { + int newValue = constrain( (int)getCurrentIntensity() + step, 0, 255 ); + + applyIntensityValue( newValue ); + } + + void intensityShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_INTENSITY_DOWN ) { + applyIntensityStep( -INTENSITY_SHORT_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_INTENSITY_UP ) { + applyIntensityStep( INTENSITY_SHORT_STEP ); + } + } + + void intensityLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_INTENSITY_DOWN ) { + applyIntensityStep( -INTENSITY_LONG_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_INTENSITY_UP ) { + applyIntensityStep( INTENSITY_LONG_STEP ); + } + } + + bool applyPaletteValue( uint8_t newPalette ) { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + Segment& mainSegment = strip.getMainSegment(); + + if ( newPalette == mainSegment.palette ) { + return false; + } + + mainSegment.setPalette( newPalette ); + + stateUpdated( CALL_MODE_BUTTON ); + + if ( currentPage == SCREEN_EFFECT ) { + drawPalette( mainSegment.palette, touchState.touchTarget ); + } + + lastPaletteValue = mainSegment.palette; + + return true; + } + + void applyPaletteStep( int step ) { + size_t paletteCount = getSelectablePaletteCount(); + + if ( paletteCount == 0 ) { + return; + } + + uint8_t currentPalette = getCurrentPalette(); + + int currentIndex = findPaletteSequenceIndex( currentPalette ); + + if ( currentIndex < 0 ) { + currentIndex = 0; + } + + int newIndex = currentIndex + step; + + while ( newIndex < 0 ) { + newIndex += (int)paletteCount; + } + + while ( newIndex >= (int)paletteCount ) { + newIndex -= (int)paletteCount; + } + + uint8_t newPalette = paletteIdFromSequenceIndex( (size_t)newIndex ); + + applyPaletteValue( newPalette ); + } + + void paletteShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_PALETTE_PREV ) { + applyPaletteStep( -1 ); + } + else if ( target == M5STACK_TOUCH_TARGET_PALETTE_NEXT ) { + applyPaletteStep( 1 ); + } + } + + void paletteLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_PALETTE_PREV ) { + applyPaletteStep( -1 ); + } + else if ( target == M5STACK_TOUCH_TARGET_PALETTE_NEXT ) { + applyPaletteStep( 1 ); + } + } + + bool applyPresetStep( int direction ) { + if ( direction == 0 ) { + return false; + } + + if ( !presetCacheReady ) { + if ( currentPage == SCREEN_PRESET ) { + drawPresetDetails( getDisplayedPresetId(), false ); + + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + } + + Serial.println( F( "[CoreS3_Display] " "Preset cache not ready" ) ); + + return false; + } + + uint8_t basePreset = getPresetNavigationBaseId(); + + String presetName; + + uint8_t newPreset = findAdjacentPreset( basePreset, direction, &presetName ); + + if ( newPreset == 0 ) { + presetNoEntries = true; + + pendingPresetId = 0; + + pendingPresetName = ""; + + pendingPresetRequestMs = 0; + + if ( currentPage == SCREEN_PRESET ) { + drawPresetDetails( 0, false ); + + drawPresetNavigation( touchState.touchTarget ); + } + + lastPresetValue = 0; + + return false; + } + + presetNoEntries = false; + + // Remember the CoreS3 navigation position immediately. This keeps the + // selected Preset as the UI starting point even if WLED later enters + // Custom State after a Color/Effect adjustment. + presetNavigationCursorId = newPreset; + + pendingPresetId = newPreset; + + pendingPresetName = presetName; + + pendingPresetRequestMs = millis(); + + applyPreset( newPreset, CALL_MODE_BUTTON_PRESET ); + + if ( currentPage == SCREEN_PRESET ) { + drawPresetDetails( newPreset, true ); + + drawPresetNavigation( touchState.touchTarget ); + } + + lastPresetValue = newPreset; + + Serial.printf( "[CoreS3_Display] " "Preset cache request: %u (%s)\n", newPreset, presetName.c_str() ); + + return true; + } + + void presetShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_PRESET_PREV ) { + applyPresetStep( -1 ); + } + else if ( target == M5STACK_TOUCH_TARGET_PRESET_NEXT ) { + applyPresetStep( 1 ); + } + } + + void presetLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_PRESET_PREV ) { + applyPresetStep( -1 ); + } + else if ( target == M5STACK_TOUCH_TARGET_PRESET_NEXT ) { + applyPresetStep( 1 ); + } + } + + bool applyHueValue( uint8_t newHue ) { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + if (!hueEditValid) { + beginHueEdit(); + } + + if (!hueEditValid) { + return false; + } + + hueEditValue = newHue; + + hueEditHsv.h = ((uint16_t)newHue) << 8; + + logicalHueValue = newHue; + + logicalColorHsv = hueEditHsv; + + logicalSaturationValue = logicalColorHsv.s; + + logicalWhiteValue = hueEditWhite; + + logicalColorHsvValid = true; + + CRGBW newRgb; + + hsv2rgb_spectrum( logicalColorHsv, newRgb ); + + newRgb.w = logicalWhiteValue; + + uint32_t newColor = newRgb.color32; + + Segment& mainSegment = strip.getMainSegment(); + + if ( newColor != mainSegment.colors[ selectedColorSlot ] ) { + mainSegment.setColor( selectedColorSlot, newColor ); + + stateUpdated( CALL_MODE_BUTTON ); + } + + if ( currentPage == SCREEN_COLOR ) { + drawColorDetails( newColor ); + + drawHue( logicalHueValue, touchState.touchTarget ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + } + + lastSelectedColor = newColor; + lastSelectedColorValid = true; + + if ( selectedColorSlot == 0 ) { + lastPrimaryColor = newColor; + lastPrimaryColorValid = true; + } + + cacheCurrentColorSlots(); + + lastHueValue = logicalHueValue; + + lastSaturationValue = logicalSaturationValue; + + return true; + } + + void applyHueStep( int step ) { + if (!hueEditValid) { + beginHueEdit(); + } + + if (!hueEditValid) { + return; + } + + int newValue = (int)logicalHueValue + step; + + while ( newValue < 0 ) { + newValue += 256; + } + + while ( newValue > 255 ) { + newValue -= 256; + } + + applyHueValue( (uint8_t)newValue ); + } + + void hueShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_HUE_DOWN ) { + applyHueStep( -HUE_SHORT_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_HUE_UP ) { + applyHueStep( HUE_SHORT_STEP ); + } + } + + void hueLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_HUE_DOWN ) { + applyHueStep( -HUE_LONG_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_HUE_UP ) { + applyHueStep( HUE_LONG_STEP ); + } + } + + bool applySaturationValue( uint8_t newSaturation ) { + if ( strip.getSegmentsNum() == 0 ) { + return false; + } + + if (!saturationEditValid) { + beginSaturationEdit(); + } + + if (!saturationEditValid) { + return false; + } + + saturationEditValue = newSaturation; + + saturationEditHsv.s = newSaturation; + + logicalSaturationValue = newSaturation; + + logicalColorHsv = saturationEditHsv; + + logicalHueValue = (uint8_t)( logicalColorHsv.h >> 8 ); + + logicalWhiteValue = saturationEditWhite; + + logicalColorHsvValid = true; + + CRGBW newRgb; + + hsv2rgb_spectrum( logicalColorHsv, newRgb ); + + newRgb.w = logicalWhiteValue; + + uint32_t newColor = newRgb.color32; + + Segment& mainSegment = strip.getMainSegment(); + + if ( newColor != mainSegment.colors[ selectedColorSlot ] ) { + mainSegment.setColor( selectedColorSlot, newColor ); + + stateUpdated( CALL_MODE_BUTTON ); + } + + if ( currentPage == SCREEN_COLOR ) { + drawColorDetails( newColor ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, touchState.touchTarget ); + } + + lastSelectedColor = newColor; + lastSelectedColorValid = true; + + if ( selectedColorSlot == 0 ) { + lastPrimaryColor = newColor; + lastPrimaryColorValid = true; + } + + cacheCurrentColorSlots(); + + lastHueValue = logicalHueValue; + + lastSaturationValue = logicalSaturationValue; + + return true; + } + + void applySaturationStep( int step ) { + if (!saturationEditValid) { + beginSaturationEdit(); + } + + if (!saturationEditValid) { + return; + } + + int newValue = constrain( (int)logicalSaturationValue + step, 0, 255 ); + + if ( newValue == logicalSaturationValue ) { + return; + } + + applySaturationValue( (uint8_t)newValue ); + } + + void saturationShortPress( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_SATURATION_DOWN ) { + applySaturationStep( -SATURATION_SHORT_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_SATURATION_UP ) { + applySaturationStep( SATURATION_SHORT_STEP ); + } + } + + void saturationLongPressStep( M5StackTouchTarget target ) { + if ( target == M5STACK_TOUCH_TARGET_SATURATION_DOWN ) { + applySaturationStep( -SATURATION_LONG_STEP ); + } + else if ( target == M5STACK_TOUCH_TARGET_SATURATION_UP ) { + applySaturationStep( SATURATION_LONG_STEP ); + } + } + + #include "M5StackDisplayTouchStateMachine.inc" + + void serviceAudioHealthPresentation( uint8_t effectMode ) { + const bool audioUnavailable = isCoreS3AudioUnavailable(); + + if ( audioUnavailable == lastAudioUnavailable ) { + return; + } + + const bool relevantVisiblePage = + currentPage == SCREEN_MAIN || currentPage == SCREEN_EFFECT; + + const bool currentEffectUsesAudio = + effectUsesAudioReactive( getEffectCapabilities( effectMode ) ); + + // Avoid changing effect visuals in the middle of a touch gesture. Keep the + // previous cached state so the change is retried after release. + if ( + relevantVisiblePage && + currentEffectUsesAudio && + touchState.touchTarget != M5STACK_TOUCH_TARGET_NONE + ) { + return; + } + + lastAudioUnavailable = audioUnavailable; + + if ( !currentEffectUsesAudio ) { + return; + } + + if ( currentPage == SCREEN_MAIN ) { + drawEffect( effectMode, M5STACK_TOUCH_TARGET_NONE ); + return; + } + + if ( currentPage == SCREEN_EFFECT ) { + drawEffectPageName( effectMode ); + } + } + + // ========================================================= + // Page-specific runtime display synchronization + // + // These helpers contain only UI/WLED state synchronization. + // Hardware-specific display/touch access remains outside this layer + // so the same page logic can be reused for Core2 variants later. + // ========================================================= + + bool updateMainPageState( uint8_t effectMode, uint8_t currentSpeed, uint8_t currentIntensity, uint8_t currentPalette, uint32_t primaryColor, bool primaryColorChanged ) { + bool brightnessTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN || touchState.touchTarget == M5STACK_TOUCH_TARGET_BRIGHTNESS_UP ); + + if ( !brightnessTouchActive && (int)bri != lastBrightnessValue ) { + drawBrightness( bri, M5STACK_TOUCH_TARGET_NONE ); + + lastBrightnessValue = bri; + } + + bool effectTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_EFFECT_PREV || touchState.touchTarget == M5STACK_TOUCH_TARGET_EFFECT_DETAIL || touchState.touchTarget == M5STACK_TOUCH_TARGET_EFFECT_NEXT ); + + if ( !effectTouchActive && (int)effectMode != lastEffectMode ) { + drawEffect( effectMode, M5STACK_TOUCH_TARGET_NONE ); + + drawColorButton( primaryColor, false ); + + lastEffectMode = effectMode; + + lastSpeedValue = currentSpeed; + + lastIntensityValue = currentIntensity; + + lastPaletteValue = currentPalette; + } + + if ( (int)currentPalette != lastPaletteValue ) { + lastPaletteValue = currentPalette; + } + + if ( (int)currentPreset != lastPresetValue ) { + lastPresetValue = currentPreset; + } + + if ( primaryColorChanged && touchState.touchTarget != M5STACK_TOUCH_TARGET_COLOR_OPEN ) { + syncLogicalColorFromRgb( primaryColor ); + + drawColorButton( primaryColor, false ); + + return true; + } + + return false; + } + + bool updateColorPageState( + uint8_t effectMode, + uint32_t primaryColor, + bool primaryColorChanged, + bool colorControlTouchActive + ) { + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE && (int)effectMode != lastEffectMode ) { + drawColorScreen(); + + return true; + } + + const M5StackEffectColorCapabilities capability = + getEffectColorCapabilities( effectMode ); + + if ( !effectUsesAnyColor( capability ) ) { + if ( currentColorSlotsChanged() ) { + cacheCurrentColorSlots(); + } + + return primaryColorChanged; + } + + normalizeSelectedColorSlot( effectMode ); + + if ( currentColorSlotsChanged() && !colorControlTouchActive ) { + const uint32_t selectedColor = getSelectedColor(); + + syncLogicalColorFromRgb( selectedColor ); + + lastSelectedColor = selectedColor; + lastSelectedColorValid = true; + + drawColorDetails( selectedColor ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + + cacheCurrentColorSlots(); + + lastHueValue = logicalHueValue; + + lastSaturationValue = logicalSaturationValue; + + return primaryColorChanged; + } + + return false; + } + + bool updateEffectPageState( uint8_t effectMode, uint8_t currentSpeed, uint8_t currentIntensity, uint8_t currentPalette ) { + bool speedTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_SPEED_DOWN || touchState.touchTarget == M5STACK_TOUCH_TARGET_SPEED_UP ); + + bool intensityTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_INTENSITY_DOWN || touchState.touchTarget == M5STACK_TOUCH_TARGET_INTENSITY_UP ); + + bool paletteTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PALETTE_PREV || touchState.touchTarget == M5STACK_TOUCH_TARGET_PALETTE_NEXT ); + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE && (int)effectMode != lastEffectMode ) { + drawEffectDetailScreen(); + + return true; + } + + if ( !speedTouchActive && (int)currentSpeed != lastSpeedValue ) { + drawSpeed( currentSpeed, M5STACK_TOUCH_TARGET_NONE ); + + lastSpeedValue = currentSpeed; + } + + if ( !intensityTouchActive && (int)currentIntensity != lastIntensityValue ) { + drawIntensity( currentIntensity, M5STACK_TOUCH_TARGET_NONE ); + + lastIntensityValue = currentIntensity; + } + + if ( !paletteTouchActive && (int)currentPalette != lastPaletteValue ) { + drawPalette( currentPalette, M5STACK_TOUCH_TARGET_NONE ); + + lastPaletteValue = currentPalette; + } + + return false; + } + + void updatePresetPageState( uint8_t displayedPreset, bool presetPendingSettled ) { + if ( presetSubPage == PRESET_SUBPAGE_MANAGE ) { + const bool presetFileChanged = + ( presetsModifiedTime != lastPresetsModifiedTime ); + + const bool activePresetChanged = + ( (int)currentPreset != lastPresetValue ); + + if ( + touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE && + ( activePresetChanged || presetFileChanged || presetPendingSettled ) + ) { + drawPresetManageScreen(); + } + + return; + } + + if ( presetSubPage == PRESET_SUBPAGE_NAV ) { + bool presetTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_PREV || touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_NEXT ); + + bool presetFileChanged = ( presetsModifiedTime != lastPresetsModifiedTime ); + + if (presetFileChanged) { + lastPresetsModifiedTime = presetsModifiedTime; + + presetNoEntries = false; + } + + if ( !presetTouchActive && ( (int)displayedPreset != lastPresetValue || presetPendingSettled || presetFileChanged ) ) { + drawPresetDetails( displayedPreset, pendingPresetId > 0 ); + + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + + lastPresetValue = displayedPreset; + } + + return; + } + + if ( presetSubPage == PRESET_SUBPAGE_BOOT ) { + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE && (int)bootPreset != lastBootPresetValue ) { + drawPresetBootScreen(); + lastBootPresetValue = bootPreset; + } + } + } + + static void writeBmp16( uint8_t* destination, uint16_t value ) { + destination[0] = (uint8_t)( value & 0xFF ); + destination[1] = (uint8_t)( ( value >> 8 ) & 0xFF ); + } + + static void writeBmp32( uint8_t* destination, uint32_t value ) { + destination[0] = (uint8_t)( value & 0xFF ); + destination[1] = (uint8_t)( ( value >> 8 ) & 0xFF ); + destination[2] = (uint8_t)( ( value >> 16 ) & 0xFF ); + destination[3] = (uint8_t)( ( value >> 24 ) & 0xFF ); + } + + bool buildScreenshotBmp( + uint8_t*& bmpData, + size_t& bmpSize + ) { + bmpData = nullptr; + bmpSize = 0; + + if ( !displayReady || screenWidth <= 0 || screenHeight <= 0 ) { + return false; + } + + const size_t width = (size_t)screenWidth; + const size_t height = (size_t)screenHeight; + const size_t pixelCount = width * height; + const size_t frameBytes = pixelCount * sizeof(uint16_t); + + // 24-bit BMP rows are aligned to 4-byte boundaries. + const size_t rowBytes = width * 3; + const size_t rowStride = ( rowBytes + 3 ) & ~((size_t)3); + const size_t imageBytes = rowStride * height; + const size_t totalBytes = SCREENSHOT_BMP_HEADER_SIZE + imageBytes; + + uint16_t* frameBuffer = static_cast( + heap_caps_malloc( + frameBytes, + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT + ) + ); + + if ( frameBuffer == nullptr ) { + return false; + } + + uint8_t* outputBuffer = static_cast( + heap_caps_malloc( + totalBytes, + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT + ) + ); + + if ( outputBuffer == nullptr ) { + heap_caps_free( frameBuffer ); + return false; + } + + if ( + !hardwareBackend.readDisplayRgb565( + frameBuffer, + screenWidth, + screenHeight + ) + ) { + heap_caps_free( outputBuffer ); + heap_caps_free( frameBuffer ); + + return false; + } + + memset( outputBuffer, 0, totalBytes ); + + // ------------------------------------------------------- + // BITMAPFILEHEADER (14 bytes) + // ------------------------------------------------------- + + outputBuffer[0] = 'B'; + outputBuffer[1] = 'M'; + + writeBmp32( outputBuffer + 2, (uint32_t)totalBytes ); + writeBmp32( outputBuffer + 10, (uint32_t)SCREENSHOT_BMP_HEADER_SIZE ); + + // ------------------------------------------------------- + // BITMAPINFOHEADER (40 bytes) + // + // Negative height selects top-down row order. This lets us + // preserve the LCD's natural 0..height-1 scan order without + // reversing the captured frame. + // ------------------------------------------------------- + + writeBmp32( outputBuffer + 14, 40 ); + writeBmp32( outputBuffer + 18, (uint32_t)screenWidth ); + writeBmp32( + outputBuffer + 22, + (uint32_t)(int32_t)( -screenHeight ) + ); + + writeBmp16( outputBuffer + 26, 1 ); + writeBmp16( outputBuffer + 28, 24 ); + + writeBmp32( outputBuffer + 30, 0 ); + writeBmp32( outputBuffer + 34, (uint32_t)imageBytes ); + + // 72 DPI, expressed as pixels per metre. + writeBmp32( outputBuffer + 38, 2835 ); + writeBmp32( outputBuffer + 42, 2835 ); + + for ( size_t y = 0; y < height; y++ ) { + const uint16_t* sourceRow = frameBuffer + ( y * width ); + uint8_t* destinationRow = + outputBuffer + SCREENSHOT_BMP_HEADER_SIZE + ( y * rowStride ); + + for ( size_t x = 0; x < width; x++ ) { + const uint16_t rawPixel = sourceRow[x]; + + // M5GFX readRect() returns the CoreS3 panel RGB565 word with + // the two bytes swapped relative to the logical TFT color value. + // + // Example: + // TFT_CYAN = 0x07FF + // readRect = 0xFF07 + // + // Restore the logical RGB565 word before expanding to RGB888. + const uint16_t pixel = (uint16_t)( + ( rawPixel >> 8 ) | + ( rawPixel << 8 ) + ); + + const uint8_t red5 = (uint8_t)( ( pixel >> 11 ) & 0x1F ); + const uint8_t green6 = (uint8_t)( ( pixel >> 5 ) & 0x3F ); + const uint8_t blue5 = (uint8_t)( pixel & 0x1F ); + + const uint8_t red8 = (uint8_t)( ( red5 << 3 ) | ( red5 >> 2 ) ); + const uint8_t green8 = (uint8_t)( ( green6 << 2 ) | ( green6 >> 4 ) ); + const uint8_t blue8 = (uint8_t)( ( blue5 << 3 ) | ( blue5 >> 2 ) ); + + uint8_t* destinationPixel = destinationRow + ( x * 3 ); + + // BMP 24-bit pixel byte order is B, G, R. + destinationPixel[0] = blue8; + destinationPixel[1] = green8; + destinationPixel[2] = red8; + } + } + + heap_caps_free( frameBuffer ); + + bmpData = outputBuffer; + bmpSize = totalBytes; + + return true; + } + + void handleScreenshotRequest( AsyncWebServerRequest* request ) { + if ( request == nullptr ) { + return; + } + + if ( + isCore2DiagnosticOnlyMode() || + !displayReady || + screenWidth <= 0 || + screenHeight <= 0 + ) { + request->send( + 503, + F("text/plain"), + F("CoreS3 display screenshot is unavailable.\n") + ); + + return; + } + + // Keep screenshot memory use bounded and avoid overlapping LCD reads. + if ( screenshotCaptureInProgress ) { + request->send( + 429, + F("text/plain"), + F("A CoreS3 screenshot request is already in progress.\n") + ); + + return; + } + + screenshotCaptureInProgress = true; + + uint8_t* bmpData = nullptr; + size_t bmpSize = 0; + + if ( !buildScreenshotBmp( bmpData, bmpSize ) ) { + screenshotCaptureInProgress = false; + + Serial.println( + F( "[CoreS3_Display] Screenshot: capture/buffer allocation failed" ) + ); + + request->send( + 503, + F("text/plain"), + F("CoreS3 screenshot capture failed. PSRAM may be unavailable.\n") + ); + + return; + } + + // The async response outlives this request handler. Keep the BMP buffer + // alive with shared ownership until the response itself is destroyed. + std::shared_ptr bmpOwner( + bmpData, + [this]( uint8_t* pointer ) { + if ( pointer != nullptr ) { + heap_caps_free( pointer ); + } + + screenshotCaptureInProgress = false; + } + ); + + const size_t responseSize = bmpSize; + + AsyncWebServerResponse* response = request->beginResponse( + F("image/bmp"), + responseSize, + [bmpOwner, responseSize]( + uint8_t* buffer, + size_t maxLength, + size_t index + ) -> size_t { + if ( buffer == nullptr || index >= responseSize ) { + return 0; + } + + const size_t remaining = responseSize - index; + const size_t copyLength = + ( maxLength < remaining ) ? maxLength : remaining; + + memcpy( + buffer, + bmpOwner.get() + index, + copyLength + ); + + return copyLength; + } + ); + + if ( response == nullptr ) { + screenshotCaptureInProgress = false; + + request->send( + 503, + F("text/plain"), + F("CoreS3 screenshot response could not be created.\n") + ); + + return; + } + + response->addHeader( + F("Cache-Control"), + F("no-store, no-cache, must-revalidate, max-age=0") + ); + + response->addHeader( F("Pragma"), F("no-cache") ); + response->addHeader( F("Expires"), F("0") ); + + response->addHeader( + F("Content-Disposition"), + F("inline; filename=\"cores3-screen.bmp\"") + ); + + request->send( response ); + + Serial.printf( + "[CoreS3_Display] Screenshot: %d x %d BMP (%u bytes)\n", + screenWidth, + screenHeight, + (unsigned)responseSize + ); + } + + void registerScreenshotEndpoint() { + server.on( + F("/cores3/screenshot.bmp"), + HTTP_GET, + [this]( AsyncWebServerRequest* request ) { + handleScreenshotRequest( request ); + } + ); + + Serial.println( + F( "[CoreS3_Display] Screenshot endpoint: /cores3/screenshot.bmp" ) + ); + } + + public: + + CoreS3DisplayUsermod() + : hardwareBackend( display ) { + } + + void addToConfig( JsonObject& root ) override { + JsonObject top = root.createNestedObject( FPSTR( CORES3_DISPLAY_CONFIG_NAME ) ); + + top[ F("sleep-timeout") ] = sleepTimeoutSec; + + top[ F("lcd-brightness") ] = lcdBrightness; + + top[ F("fade") ] = fadeEnabled; + + top[ F("fade-duration") ] = fadeDurationMs; + } + + bool readFromConfig( JsonObject& root ) override { + JsonObject top = root[ FPSTR( CORES3_DISPLAY_CONFIG_NAME ) ]; + + if ( top.isNull() ) { + Serial.println( F( "[CoreS3_Display] " "No display config found. Using defaults." ) ); + + return false; + } + + bool configComplete = true; + + int newSleepTimeout = sleepTimeoutSec; + + int newLcdBrightness = lcdBrightness; + + bool newFadeEnabled = fadeEnabled; + + int newFadeDuration = fadeDurationMs; + + configComplete &= getJsonValue( top[ F("sleep-timeout") ], newSleepTimeout, 30 ); + + configComplete &= getJsonValue( top[ F("lcd-brightness") ], newLcdBrightness, 128 ); + + configComplete &= getJsonValue( top[ F("fade") ], newFadeEnabled, true ); + + configComplete &= getJsonValue( top[ F("fade-duration") ], newFadeDuration, 250 ); + + newSleepTimeout = constrain( newSleepTimeout, 0, 3600 ); + + newLcdBrightness = constrain( newLcdBrightness, 1, 255 ); + + newFadeDuration = constrain( newFadeDuration, 50, 2000 ); + + sleepTimeoutSec = (uint16_t)newSleepTimeout; + + lcdBrightness = (uint16_t)newLcdBrightness; + + fadeEnabled = newFadeEnabled; + + fadeDurationMs = (uint16_t)newFadeDuration; + + if ( initDone ) { + Serial.printf( + "[CoreS3_Display] " + "Config updated: " + "Sleep=%u sec, " + "LCD=%u, " + "Fade=%s, " + "FadeDuration=%u ms\n", + sleepTimeoutSec, + lcdBrightness, + fadeEnabled ? "ON" : "OFF", + fadeDurationMs + ); + } + + if ( initDone && displayReady && displayPowerState == DISPLAY_POWER_ACTIVE ) { + setDisplayBrightness( getNormalDisplayBrightness() ); + } + + return configComplete; + } + + void appendConfigData( Print& settingsScript ) override { + settingsScript.print( F( "cs3st=addDropdown(" "'CoreS3_Display'," "'sleep-timeout'" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3st," "'Never'," "0" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3st," "'15 sec'," "15" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3st," "'30 sec'," "30" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3st," "'60 sec'," "60" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3st," "'120 sec'," "120" ");" ) ); + + settingsScript.print( F( "addInfo(" "'CoreS3_Display:lcd-brightness'," "1," "'1-255'" ");" ) ); + + settingsScript.print( F( "cs3fd=addDropdown(" "'CoreS3_Display'," "'fade-duration'" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3fd," "'Fast - 150 ms'," "150" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3fd," "'Normal - 250 ms'," "250" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3fd," "'Slow - 400 ms'," "400" ");" ) ); + + settingsScript.print( F( "addOption(" "cs3fd," "'Very Slow - 600 ms'," "600" ");" ) ); + } + + void setup() override { + Serial.println(); + + Serial.println( F( "[CoreS3_Display][BUILD] CoreS3 Display v0.1.0" ) ); + Serial.println( F( "[CoreS3_Display] Initialization start" ) ); + + registerScreenshotEndpoint(); + + Serial.printf( + "[CoreS3_Display] " "Hardware: %s, Revision=%s, Runtime=%s\n", + getHardwareProfileName(), + getHardwareRevisionName(), + getHardwareRuntimeModeName() + ); + + Serial.printf( + "[CoreS3_Display] " "Port status: %s\n", + getHardwarePortStatusName() + ); + + Serial.printf( "[CoreS3_Display] " "Settings: " "Sleep=%u sec, " "LCD=%u, " "Fade=%s, " "FadeDuration=%u ms\n", sleepTimeoutSec, lcdBrightness, fadeEnabled ? "ON" : "OFF", fadeDurationMs ); + + runHardwareDiagnostics(); + + if ( isCore2DiagnosticOnlyMode() ) { + initDone = true; + + Serial.println( F( "[CoreS3_Display] Core2 diagnostic-only runtime complete" ) ); + Serial.println( F( "[CoreS3_Display] Display/Touch/LCD brightness initialization intentionally skipped" ) ); + Serial.println( F( "[CoreS3_Display] Capture the hardware probe result before enabling Core2 UI/Power support" ) ); + Serial.println(); + + return; + } + + if ( !initializeDisplayHardware() ) { + return; + } + + setDisplayBrightness( 0 ); + + drawStartupBase(); + + startupDotCount = 3; + + drawStartupConnectingStatus(); + + startupState = STARTUP_FADE_IN; + + startupStateStart = millis(); + + startupLastFadeStep = startupStateStart; + + startupLastDotsUpdate = startupStateStart; + + runtimeHealthStartMs = startupStateStart; + + displayPowerState = DISPLAY_POWER_ACTIVE; + + lastUserActivityMs = startupStateStart; + + lastPresetsModifiedTime = presetsModifiedTime; + + presetCacheCount = 0; + + presetCacheReady = false; + + presetCacheBuilding = false; + + presetCacheScanId = 1; + + presetCacheSourceModifiedTime = presetsModifiedTime; + + displayReady = true; + + initDone = true; + + Serial.println( F( "[CoreS3_Display] Initialization complete" ) ); + + Serial.println(); + } + + void loop() override { + if (!displayReady) { + return; + } + + if ( startupState != STARTUP_DONE ) { + handleStartupSequence(); + + return; + } + + serviceWiFiRecoveryAP(); + + if ( !presetCacheReady && !presetCacheBuilding ) { + startPresetCacheRebuild(); + } + + servicePresetCache(); + + servicePresetSaveOperation(); + + servicePresetDeleteOperation(); + + servicePresetBootOperation(); + + if ( serviceRuntimeHealthWarnings() ) { + return; + } + + // Follow a Preset explicitly selected from the WLED Web UI. + // A transition to currentPreset == 0 intentionally does not erase the + // CoreS3 navigation cursor. + syncPresetNavigationCursorFromCurrentPreset(); + + if ( displayPowerState == DISPLAY_POWER_ACTIVE ) { + handleTouch(); + } + + if ( handleDisplayPowerManagement() ) { + return; + } + + unsigned long now = millis(); + + if ( now - lastUpdate < 250 ) { + return; + } + + lastUpdate = now; + + bool presetPendingSettled = settlePendingPreset(); + + NetworkAccessMode currentNetworkMode = getNetworkAccessMode(); + String currentNetworkDisplayText = + getNetworkDisplayText( currentNetworkMode ); + + if ( !readyScreenShown ) { + drawMainScreen( currentNetworkDisplayText ); + + lastNetworkAccessMode = currentNetworkMode; + lastNetworkDisplayText = currentNetworkDisplayText; + + return; + } + + const bool networkPresentationChanged = + ( currentNetworkMode != lastNetworkAccessMode ) || + ( currentNetworkDisplayText != lastNetworkDisplayText ); + + if ( networkPresentationChanged ) { + // Do not redraw/reset MAIN in the middle of a touch gesture. This is + // especially important when a Recovery AP starts during a long press. + if ( + currentPage == SCREEN_MAIN && + touchState.touchTarget != M5STACK_TOUCH_TARGET_NONE + ) { + // Keep the old cache so the transition is still observed after the + // finger is released, unless the Recovery handler updates it itself. + } + else { + lastNetworkAccessMode = currentNetworkMode; + lastNetworkDisplayText = currentNetworkDisplayText; + + // Network changes are informational only. Do not interrupt COLOR, + // EFFECT or PRESET operation. MAIN is the page that owns the + // network-status line, so redraw it only when it is currently visible. + if ( currentPage == SCREEN_MAIN ) { + drawMainScreen( currentNetworkDisplayText ); + + return; + } + } + } + + if ( + currentPage == SCREEN_MAIN && + sampleBatteryStatus() + ) { + drawMainBatteryStatus(); + } + + bool ledOn = (bri > 0); + + if ( (int8_t)ledOn != lastLedState ) { + if ( touchState.touchTarget != M5STACK_TOUCH_TARGET_POWER ) { + drawPowerButton( ledOn, false ); + } + + lastLedState = ledOn ? 1 : 0; + } + + uint8_t effectMode = getCurrentEffectMode(); + + serviceAudioHealthPresentation( effectMode ); + + uint8_t currentSpeed = getCurrentSpeed(); + + uint8_t currentIntensity = getCurrentIntensity(); + + uint8_t currentPalette = getCurrentPalette(); + + uint8_t displayedPreset = getDisplayedPresetId(); + + uint32_t primaryColor = getPrimaryColor(); + + bool primaryColorChanged = ( !lastPrimaryColorValid || primaryColor != lastPrimaryColor ); + + bool hueTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_HUE_DOWN || touchState.touchTarget == M5STACK_TOUCH_TARGET_HUE_UP ); + + bool saturationTouchActive = ( touchState.touchTarget == M5STACK_TOUCH_TARGET_SATURATION_DOWN || touchState.touchTarget == M5STACK_TOUCH_TARGET_SATURATION_UP ); + + bool colorSlotTouchActive = ( + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_1 || + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_2 || + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_3 + ); + + bool colorControlTouchActive = ( hueTouchActive || saturationTouchActive || colorSlotTouchActive ); + + bool primaryColorChangeHandled = false; + + if ( currentPage == SCREEN_MAIN ) { + primaryColorChangeHandled = updateMainPageState( effectMode, currentSpeed, currentIntensity, currentPalette, primaryColor, primaryColorChanged ); + } + else if ( currentPage == SCREEN_COLOR ) { + primaryColorChangeHandled = updateColorPageState( effectMode, primaryColor, primaryColorChanged, colorControlTouchActive ); + } + else if ( currentPage == SCREEN_EFFECT ) { + if ( updateEffectPageState( effectMode, currentSpeed, currentIntensity, currentPalette ) ) { + return; + } + } + else if ( currentPage == SCREEN_PRESET ) { + updatePresetPageState( displayedPreset, presetPendingSettled ); + } + + if ( primaryColorChanged && primaryColorChangeHandled ) { + lastPrimaryColor = primaryColor; + + lastPrimaryColorValid = true; + } + } + + void addToJsonInfo( JsonObject& root ) override { + JsonObject user = root["u"]; + + if ( user.isNull() ) { + user = root.createNestedObject( "u" ); + } + + JsonArray hardwareProfileInfo = user.createNestedArray( "M5Stack Hardware Profile" ); + + hardwareProfileInfo.add( getHardwareProfileName() ); + + JsonArray hardwareRevisionInfo = user.createNestedArray( "M5Stack Hardware Revision" ); + + hardwareRevisionInfo.add( getHardwareRevisionName() ); + + JsonArray hardwareRuntimeInfo = user.createNestedArray( "M5Stack Runtime Mode" ); + + hardwareRuntimeInfo.add( getHardwareRuntimeModeName() ); + + JsonArray hardwarePortStatusInfo = user.createNestedArray( "M5Stack Porting Status" ); + + hardwarePortStatusInfo.add( getHardwarePortStatusName() ); + + JsonArray hardwareProbeInfo = user.createNestedArray( "M5Stack Hardware Probe" ); + + hardwareProbeInfo.add( getHardwareProbeStateName() ); + + JsonArray hardwareVariantInfo = user.createNestedArray( "M5Stack Detected Variant" ); + + hardwareVariantInfo.add( getDetectedVariantName() ); + + JsonArray hardwareDetectedRevisionInfo = user.createNestedArray( "M5Stack Detected Revision" ); + + hardwareDetectedRevisionInfo.add( getDetectedRevisionName() ); + + JsonArray hardwarePmuInfo = user.createNestedArray( "M5Stack Detected PMU" ); + + hardwarePmuInfo.add( getDetectedPmuName() ); + + JsonArray hardwareImuInfo = user.createNestedArray( "M5Stack Detected IMU" ); + + hardwareImuInfo.add( getDetectedImuName() ); + + JsonArray hardwareCore2I2cInfo = user.createNestedArray( "M5Stack Core2 I2C Signature" ); + + if ( hardwareBackend.isProbeComplete() ) { + char signature[64]; + + snprintf( + signature, + sizeof(signature), + "34:%c 35:%c 38:%c 40:%c 51:%c 68:%c", + hardwareBackend.hasI2CAddress( 0x34 ) ? 'Y' : 'N', + hardwareBackend.hasI2CAddress( 0x35 ) ? 'Y' : 'N', + hardwareBackend.hasI2CAddress( 0x38 ) ? 'Y' : 'N', + hardwareBackend.hasI2CAddress( 0x40 ) ? 'Y' : 'N', + hardwareBackend.hasI2CAddress( 0x51 ) ? 'Y' : 'N', + hardwareBackend.hasI2CAddress( 0x68 ) ? 'Y' : 'N' + ); + + hardwareCore2I2cInfo.add( signature ); + } + else { + hardwareCore2I2cInfo.add( "Not probed" ); + } + + JsonArray displayInfo = user.createNestedArray( "CoreS3 Display" ); + + if ( isCore2DiagnosticOnlyMode() ) { + displayInfo.add( "DIAGNOSTIC ONLY - NOT INITIALIZED" ); + } + else if (displayReady) { + char text[32]; + + snprintf( text, sizeof(text), "READY (%d x %d)", screenWidth, screenHeight ); + + displayInfo.add( text ); + } + else { + displayInfo.add( "FAILED" ); + } + + JsonArray screenshotInfo = user.createNestedArray( "CoreS3 Display Screenshot" ); + + if ( isCore2DiagnosticOnlyMode() ) { + screenshotInfo.add( "UNAVAILABLE - DIAGNOSTIC ONLY" ); + } + else if ( displayReady ) { + screenshotInfo.add( "/cores3/screenshot.bmp" ); + } + else { + screenshotInfo.add( "UNAVAILABLE" ); + } + + JsonArray touchInfo = user.createNestedArray( "CoreS3 Display Touch" ); + + if ( isCore2DiagnosticOnlyMode() ) { + if ( hardwareBackend.isProbeComplete() ) { + touchInfo.add( + hardwareBackend.hasI2CAddress( 0x38 ) + ? "I2C 0x38 DETECTED - NOT INITIALIZED" + : "I2C 0x38 NOT DETECTED" + ); + } + else { + touchInfo.add( "DIAGNOSTIC PROBE UNAVAILABLE" ); + } + } + else { + touchInfo.add( touchReady ? "READY" : "NOT FOUND" ); + } + + JsonArray wifiInfo = user.createNestedArray( "CoreS3 Display WiFi" ); + + NetworkAccessMode networkMode = getNetworkAccessMode(); + + if ( networkMode == NETWORK_ACCESS_STA ) { + wifiInfo.add( String( "STA: " ) + WiFi.localIP().toString() ); + } + else if ( networkMode == NETWORK_ACCESS_AP ) { + if ( recoveryApSessionActive && recoveryApSSID.length() > 0 ) { + wifiInfo.add( + String( "Recovery AP: " ) + + recoveryApSSID + + " @ " + + WiFi.softAPIP().toString() + ); + } + else { + wifiInfo.add( String( "AP: " ) + WiFi.softAPIP().toString() ); + } + } + else { + wifiInfo.add( "Offline - local control available" ); + } + + JsonArray ledInfo = user.createNestedArray( "CoreS3 Display LED" ); + + ledInfo.add( bri > 0 ? "ON" : "OFF" ); + + JsonArray brightnessInfo = user.createNestedArray( "CoreS3 Display Brightness" ); + + brightnessInfo.add( bri ); + + JsonArray effectInfo = user.createNestedArray( "CoreS3 Display Effect" ); + + if ( strip.getSegmentsNum() > 0 ) { + char effectName[64]; + + getEffectName( strip.getMainSegment().mode, effectName, sizeof(effectName) ); + + effectInfo.add( effectName ); + } + else { + effectInfo.add( "No segment" ); + } + + JsonArray speedInfo = user.createNestedArray( "CoreS3 Effect Speed" ); + + if ( strip.getSegmentsNum() > 0 ) { + speedInfo.add( getCurrentSpeed() ); + } + else { + speedInfo.add( "No segment" ); + } + + JsonArray intensityInfo = user.createNestedArray( "CoreS3 Effect Intensity" ); + + if ( strip.getSegmentsNum() > 0 ) { + intensityInfo.add( getCurrentIntensity() ); + } + else { + intensityInfo.add( "No segment" ); + } + + JsonArray paletteInfo = user.createNestedArray( "CoreS3 Effect Palette" ); + + if ( strip.getSegmentsNum() > 0 ) { + char paletteName[64]; + + getPaletteName( getCurrentPalette(), paletteName, sizeof(paletteName) ); + + paletteInfo.add( paletteName ); + } + else { + paletteInfo.add( "No segment" ); + } + + JsonArray paletteIdInfo = user.createNestedArray( "CoreS3 Effect Palette ID" ); + + if ( strip.getSegmentsNum() > 0 ) { + paletteIdInfo.add( getCurrentPalette() ); + } + else { + paletteIdInfo.add( "No segment" ); + } + + JsonArray paletteTouchInfo = user.createNestedArray( "CoreS3 Palette Touch Area" ); + + paletteTouchInfo.add( "Expanded" ); + + JsonArray presetInfo = user.createNestedArray( "CoreS3 Display Preset" ); + + presetInfo.add( currentPreset > 0 ? "Active Preset" : "Custom State" ); + + JsonArray presetIdInfo = user.createNestedArray( "CoreS3 Display Preset ID" ); + + presetIdInfo.add( currentPreset ); + + JsonArray bootPresetInfo = user.createNestedArray( "CoreS3 Boot Preset" ); + + if ( bootPreset == 0 ) { + bootPresetInfo.add( "NONE" ); + } + else { + bootPresetInfo.add( bootPreset ); + } + + JsonArray presetTouchInfo = user.createNestedArray( "CoreS3 Preset Touch Area" ); + + presetTouchInfo.add( "Expanded" ); + + JsonArray presetCacheInfo = user.createNestedArray( "CoreS3 Preset Cache" ); + + if ( presetCacheReady ) { + char cacheText[32]; + + snprintf( cacheText, sizeof(cacheText), "READY (%u)", (unsigned)presetCacheCount ); + + presetCacheInfo.add( cacheText ); + } + else if ( presetCacheBuilding ) { + // ----------------------------------------------------- + // Do not expose the internal 1..250 scan position. + // ----------------------------------------------------- + + presetCacheInfo.add( "LOADING" ); + } + else { + presetCacheInfo.add( "NOT READY" ); + } + + JsonArray colorInfo = user.createNestedArray( "CoreS3 Display Color" ); + + if ( strip.getSegmentsNum() > 0 ) { + uint32_t color = getPrimaryColor(); + + char colorText[16]; + + snprintf( colorText, sizeof(colorText), "#%02X%02X%02X", R(color), G(color), B(color) ); + + colorInfo.add( colorText ); + } + else { + colorInfo.add( "No segment" ); + } + + JsonArray hueInfo = user.createNestedArray( "CoreS3 Display Hue" ); + + if ( strip.getSegmentsNum() > 0 ) { + hueInfo.add( getDisplayedHue() ); + } + else { + hueInfo.add( "No segment" ); + } + + JsonArray saturationInfo = user.createNestedArray( "CoreS3 Display Saturation" ); + + if ( strip.getSegmentsNum() > 0 ) { + saturationInfo.add( getDisplayedSaturation() ); + } + else { + saturationInfo.add( "No segment" ); + } + + JsonArray pageInfo = user.createNestedArray( "CoreS3 Display Page" ); + + if ( currentPage == SCREEN_MAIN ) { + pageInfo.add( "MAIN" ); + } + else if ( currentPage == SCREEN_COLOR ) { + pageInfo.add( "COLOR" ); + } + else if ( currentPage == SCREEN_EFFECT ) { + pageInfo.add( "EFFECT" ); + } + else { + pageInfo.add( "PRESET" ); + } + + JsonArray powerStateInfo = user.createNestedArray( "CoreS3 Display Power State" ); + + switch ( displayPowerState ) { + case DISPLAY_POWER_ACTIVE: + powerStateInfo.add( "ACTIVE" ); + break; + + case DISPLAY_POWER_SLEEP_FADE_OUT: + powerStateInfo.add( "SLEEP FADE OUT" ); + break; + + case DISPLAY_POWER_SLEEPING: + powerStateInfo.add( "SLEEPING" ); + break; + + case DISPLAY_POWER_WAKE_FADE_IN: + powerStateInfo.add( "WAKE FADE IN" ); + break; + + case DISPLAY_POWER_WAKE_WAIT_RELEASE: + powerStateInfo.add( "WAKE WAIT RELEASE" ); + break; + } + + JsonArray lcdBrightnessInfo = user.createNestedArray( "CoreS3 LCD Brightness" ); + + lcdBrightnessInfo.add( lcdBrightness ); + + JsonArray sleepInfo = user.createNestedArray( "CoreS3 Display Sleep" ); + + if ( sleepTimeoutSec == 0 ) { + sleepInfo.add( "Never" ); + } + else { + char sleepText[24]; + + snprintf( sleepText, sizeof(sleepText), "%u sec", sleepTimeoutSec ); + + sleepInfo.add( sleepText ); + } + + JsonArray fadeInfo = user.createNestedArray( "CoreS3 Display Fade" ); + + if (!fadeEnabled) { + fadeInfo.add( "OFF" ); + } + else { + char fadeText[24]; + + snprintf( fadeText, sizeof(fadeText), "ON (%u ms)", fadeDurationMs ); + + fadeInfo.add( fadeText ); + } + + } +}; + +static CoreS3DisplayUsermod coreS3DisplayUsermod; + +REGISTER_USERMOD(coreS3DisplayUsermod); diff --git a/usermods/CoreS3_Display/CoreS3_WLED_Logo.h b/usermods/CoreS3_Display/CoreS3_WLED_Logo.h new file mode 100644 index 0000000000..1acff82c74 --- /dev/null +++ b/usermods/CoreS3_Display/CoreS3_WLED_Logo.h @@ -0,0 +1,164 @@ +#pragma once + +#include + +// WLED startup logo for M5Stack CoreS3 +// Source image resized to 304 x 95 pixels using nearest-neighbor scaling. +// Embedded as PNG bytes for M5GFX::drawPng(). + +static constexpr uint16_t CORES3_WLED_LOGO_WIDTH = 304; +static constexpr uint16_t CORES3_WLED_LOGO_HEIGHT = 95; + +static const uint8_t CORES3_WLED_LOGO_PNG[] PROGMEM = { + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, + 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x5F, 0x08, 0x06, 0x00, 0x00, 0x00, 0x6F, 0x2E, 0x9B, + 0xC8, 0x00, 0x00, 0x09, 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0xDA, 0xED, 0xDD, 0x51, 0x6C, 0x14, + 0xC7, 0x19, 0xC0, 0xF1, 0x6F, 0x2F, 0x1C, 0x01, 0x07, 0xD1, 0x9C, 0xF0, 0x19, 0x0B, 0xDB, 0x08, + 0x2A, 0x44, 0x64, 0x70, 0x94, 0xB3, 0x03, 0xED, 0x43, 0x22, 0xD1, 0x52, 0xA5, 0xAD, 0x14, 0xD4, + 0xA6, 0x55, 0x05, 0x55, 0x14, 0xA9, 0x6D, 0x12, 0x30, 0xA9, 0xFA, 0x90, 0xA0, 0x3C, 0x10, 0x25, + 0x12, 0x8B, 0x54, 0xB5, 0x79, 0x88, 0x5C, 0x5E, 0xDA, 0xDA, 0x86, 0x22, 0x1A, 0xE5, 0xA1, 0x58, + 0x15, 0xB4, 0x15, 0x48, 0x51, 0x23, 0x45, 0xC9, 0x43, 0xFB, 0xD0, 0x34, 0xF6, 0x49, 0x05, 0x47, + 0x44, 0x51, 0x8D, 0x94, 0x60, 0x39, 0x47, 0x24, 0x2C, 0x44, 0x28, 0xB6, 0xE1, 0xAE, 0x0F, 0xBE, + 0xBD, 0x1B, 0xDF, 0xCE, 0xDD, 0xED, 0xDE, 0xED, 0xDE, 0xCE, 0xDA, 0xFF, 0xDF, 0x0B, 0xA7, 0xF3, + 0xDC, 0xEE, 0xCC, 0xCE, 0x32, 0xF7, 0x7D, 0xB3, 0x3B, 0x7B, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x24, 0xCB, 0xA8, 0xDA, 0xBC, 0x36, 0x59, 0x28, 0xBD, 0xEE, + 0xEC, 0x8D, 0xB4, 0x2A, 0x03, 0x1D, 0xE5, 0xD7, 0xE3, 0xFB, 0x2D, 0x8B, 0x53, 0x05, 0x30, 0x4F, + 0x82, 0x43, 0x00, 0x20, 0xAE, 0x56, 0x99, 0x1A, 0x75, 0x15, 0x7E, 0xF8, 0x96, 0xFF, 0x70, 0xF2, + 0xDC, 0x33, 0x9E, 0x3F, 0xEF, 0x94, 0xCD, 0x7C, 0xE5, 0x86, 0x4C, 0x7C, 0xEB, 0x62, 0xCD, 0xB2, + 0x8F, 0x8E, 0x15, 0x0A, 0x44, 0x62, 0x00, 0x11, 0x18, 0x00, 0x30, 0x80, 0x01, 0x40, 0x74, 0x29, + 0xA4, 0x93, 0x3A, 0xAA, 0x93, 0xF5, 0x33, 0x1F, 0xB9, 0x53, 0xBD, 0xDF, 0x3D, 0x5A, 0x4E, 0x0B, + 0x7F, 0xFE, 0xA1, 0xEB, 0x7D, 0xF5, 0x3D, 0xDD, 0xE7, 0x53, 0x7F, 0xC8, 0xC8, 0xEC, 0xDC, 0x7D, + 0xD5, 0xCB, 0xDE, 0xBD, 0x5D, 0x77, 0x5F, 0xE3, 0xB9, 0xC5, 0x7F, 0x07, 0x8A, 0xA9, 0x24, 0xE9, + 0x24, 0x40, 0x04, 0x06, 0x00, 0x0C, 0x60, 0x00, 0x48, 0x21, 0x63, 0x6F, 0x49, 0x8A, 0x18, 0x60, + 0xD9, 0x28, 0xBC, 0xD3, 0x91, 0x29, 0xA5, 0xAB, 0xDB, 0x92, 0x0F, 0x94, 0xDE, 0xBF, 0xFF, 0xE1, + 0x87, 0x5C, 0x65, 0x37, 0xBD, 0x7D, 0xDA, 0xC8, 0x74, 0x76, 0xF2, 0x69, 0x29, 0x88, 0x88, 0xF4, + 0xA6, 0x94, 0x37, 0xBB, 0x07, 0xDC, 0xD3, 0x04, 0xAF, 0x8C, 0x1B, 0x59, 0xFF, 0xF7, 0xDE, 0xEC, + 0x2A, 0xF7, 0xC1, 0xE6, 0xA4, 0x88, 0x88, 0x24, 0xD7, 0xED, 0xD4, 0x96, 0xDD, 0xB8, 0xEB, 0xA2, + 0x71, 0x6D, 0xD8, 0x3B, 0xB6, 0xBB, 0x54, 0xFF, 0x75, 0x5B, 0xD6, 0x96, 0xDE, 0xDF, 0xD6, 0xB6, + 0xDD, 0x55, 0x76, 0xA8, 0xEF, 0x54, 0x2C, 0xA7, 0x44, 0x88, 0xC0, 0x00, 0x10, 0x81, 0x45, 0x45, + 0x9D, 0x78, 0x0F, 0xB2, 0x6C, 0x54, 0xD1, 0x96, 0x2E, 0xEA, 0x52, 0xCD, 0xFD, 0xE7, 0x8A, 0xEB, + 0xBD, 0xE9, 0xEF, 0x3E, 0x5B, 0x88, 0x32, 0x2A, 0x73, 0x22, 0xAD, 0x4A, 0x4B, 0x22, 0x2F, 0xC7, + 0x67, 0xE3, 0xEE, 0x68, 0xF8, 0xD7, 0x03, 0xDA, 0xCF, 0xB7, 0x32, 0x32, 0x53, 0xA3, 0xAD, 0xCA, + 0xA8, 0x4B, 0xB5, 0x70, 0xEB, 0xB2, 0xF6, 0xF3, 0x9F, 0xFF, 0xFB, 0xC9, 0x42, 0x94, 0x51, 0x99, + 0x1A, 0x6D, 0xE9, 0xA2, 0x2E, 0xD5, 0x27, 0xB7, 0x3F, 0x76, 0xBD, 0x77, 0xE4, 0xD2, 0xF3, 0xDA, + 0x3E, 0x30, 0x3D, 0x32, 0x23, 0x02, 0x03, 0x10, 0x5B, 0x0C, 0x60, 0x00, 0xE2, 0x9B, 0x81, 0x45, + 0xB6, 0x67, 0x8F, 0xF7, 0x81, 0x55, 0x15, 0xC6, 0x62, 0xEF, 0x6A, 0xFB, 0x2F, 0xEE, 0x2B, 0xC8, + 0x05, 0xDE, 0xD5, 0x26, 0xE9, 0x83, 0xA2, 0x4E, 0xF6, 0x87, 0x95, 0x4E, 0x6A, 0x27, 0xE9, 0x83, + 0xE4, 0x4C, 0xF8, 0xE7, 0xF3, 0xFD, 0xD6, 0xAB, 0xD9, 0x6C, 0x98, 0x69, 0xA3, 0x2E, 0x5D, 0x6C, + 0x96, 0x3A, 0xE1, 0x1F, 0x46, 0x3A, 0x59, 0x6D, 0x92, 0x3E, 0x48, 0xCE, 0x84, 0xBF, 0xA9, 0xA9, + 0x24, 0x11, 0x18, 0x00, 0x52, 0x48, 0x00, 0x58, 0xD9, 0x29, 0xA4, 0xE1, 0x9A, 0x4D, 0x21, 0xC3, + 0x4E, 0x1B, 0x5B, 0x91, 0x4E, 0xAA, 0x57, 0x1C, 0x43, 0x4B, 0x1D, 0x2B, 0x6D, 0xEC, 0x15, 0x49, + 0x2E, 0xA6, 0x48, 0xCD, 0x5E, 0x99, 0x0C, 0x3B, 0x6D, 0x6C, 0x45, 0x3A, 0xE9, 0xA4, 0x8E, 0x61, + 0xA5, 0x8D, 0xB5, 0x52, 0x49, 0xD3, 0xD2, 0x49, 0x22, 0x30, 0x00, 0xB1, 0xB5, 0x6A, 0x25, 0x35, + 0xF6, 0xDD, 0x6F, 0xDF, 0x28, 0xBD, 0x4E, 0xA5, 0x6A, 0x87, 0x0F, 0xFD, 0x67, 0x5B, 0x5F, 0xBF, + 0xD5, 0x2F, 0x3F, 0x2B, 0xF7, 0xED, 0xEA, 0x73, 0xD5, 0x6F, 0xFA, 0x3B, 0x3F, 0x73, 0x95, 0x5D, + 0xFB, 0xA7, 0x21, 0x6D, 0x5B, 0x74, 0x65, 0xA3, 0x72, 0x63, 0xF0, 0x5D, 0xF7, 0xB1, 0x7E, 0xBD, + 0x3F, 0x36, 0xE7, 0x4B, 0xF2, 0xAB, 0x6F, 0x79, 0x3E, 0x5F, 0x74, 0x72, 0x1F, 0x3E, 0x19, 0x49, + 0xBD, 0x8F, 0x6D, 0x7A, 0xA3, 0xE1, 0x3A, 0x8B, 0x88, 0xBC, 0x78, 0xF9, 0x39, 0x06, 0x30, 0x13, + 0xF5, 0x3D, 0x78, 0xB7, 0xF4, 0x3A, 0xDD, 0x6E, 0x5E, 0xFD, 0x12, 0x9B, 0x3A, 0x24, 0xF1, 0xD0, + 0xD6, 0xC5, 0xC1, 0x2C, 0x9D, 0xAE, 0x5D, 0xB6, 0x58, 0xCE, 0x4B, 0xD9, 0xA8, 0xDC, 0xED, 0xEC, + 0x2B, 0x1E, 0xEC, 0x74, 0x2C, 0xCF, 0x17, 0x6B, 0xCD, 0x76, 0x25, 0x05, 0x8C, 0x4F, 0x1B, 0x7A, + 0x56, 0x6F, 0x59, 0x3C, 0xEC, 0x6D, 0xE9, 0x65, 0xFF, 0x7F, 0x9A, 0x14, 0x12, 0x00, 0x29, 0x64, + 0xDC, 0x5C, 0xBF, 0x7E, 0xBD, 0x4E, 0x89, 0xE0, 0xBE, 0xBD, 0x9C, 0xC9, 0x7B, 0x3F, 0x13, 0xF7, + 0xF5, 0xEB, 0xE7, 0xBD, 0xAC, 0xBA, 0xFC, 0xC8, 0x59, 0x76, 0xE4, 0x67, 0x32, 0x3F, 0x92, 0x89, + 0x7B, 0xD5, 0xE7, 0xE5, 0xFB, 0xF3, 0xD4, 0x65, 0x47, 0x7E, 0x26, 0xF4, 0x9D, 0xC9, 0xFB, 0x46, + 0x27, 0xEE, 0xFD, 0xF4, 0x87, 0x8E, 0xBA, 0x04, 0xC9, 0x59, 0x76, 0xE4, 0x67, 0x32, 0xBF, 0x91, + 0x7B, 0xBE, 0xAA, 0xD5, 0x39, 0x5D, 0x8C, 0x88, 0xE7, 0xE6, 0xE6, 0xE4, 0xE6, 0xCD, 0x9B, 0x9E, + 0xB6, 0xA5, 0x2E, 0x3F, 0x52, 0x97, 0x1D, 0x45, 0x3D, 0xA1, 0x4F, 0x04, 0x06, 0x80, 0x14, 0x12, + 0x00, 0x48, 0x21, 0x03, 0x92, 0xDB, 0xBF, 0x18, 0x3E, 0xB7, 0xB7, 0xB7, 0x8B, 0x55, 0xBC, 0x65, + 0xCB, 0xFA, 0xAD, 0x39, 0x93, 0x9A, 0x0F, 0x4C, 0x9C, 0x77, 0x85, 0xF4, 0xB9, 0xE7, 0x5E, 0x91, + 0x2F, 0x8F, 0xBC, 0xEE, 0xE9, 0xF3, 0x5F, 0xF6, 0xFF, 0xA0, 0xE6, 0x76, 0xD3, 0xCA, 0xC4, 0xF9, + 0xD5, 0xEE, 0xC7, 0x39, 0xD3, 0xEB, 0x58, 0xBD, 0xE3, 0x5F, 0xAE, 0xE3, 0x76, 0xED, 0xFD, 0xAD, + 0x46, 0xD7, 0xF9, 0xD4, 0x96, 0x3F, 0xBB, 0xCE, 0x21, 0x11, 0x91, 0xEF, 0x7F, 0xB0, 0xA7, 0x66, + 0x59, 0x47, 0xF6, 0xF6, 0x07, 0xF2, 0xAB, 0xAB, 0xAF, 0x7A, 0xDA, 0xAE, 0x6E, 0x9B, 0x44, 0x60, + 0x00, 0xC0, 0x00, 0x06, 0x80, 0x14, 0x12, 0x08, 0x81, 0x7A, 0x35, 0x2C, 0xCD, 0xE1, 0x08, 0xFD, + 0x18, 0x13, 0x81, 0x01, 0x00, 0x11, 0x58, 0xF8, 0x9C, 0xC9, 0x7A, 0x91, 0xA5, 0x13, 0x99, 0xCB, + 0xE9, 0xBB, 0x5E, 0x37, 0xE1, 0x6F, 0xD2, 0xC4, 0xFC, 0xF5, 0x63, 0x39, 0x6D, 0x1F, 0xA4, 0x7F, + 0x11, 0xEF, 0x9F, 0xCE, 0xEC, 0xDA, 0x33, 0xE5, 0xB9, 0x6C, 0x14, 0x13, 0xFE, 0xCF, 0x5F, 0xFD, + 0xD1, 0x8A, 0x1F, 0xC0, 0x88, 0xC0, 0x00, 0x30, 0x80, 0x01, 0x80, 0x71, 0x29, 0xE4, 0x54, 0xD7, + 0x63, 0xA5, 0x65, 0x03, 0x5B, 0xAF, 0xFD, 0xC3, 0xAA, 0xF6, 0x9E, 0x69, 0xF4, 0x93, 0x9A, 0x4C, + 0x21, 0xA3, 0xD9, 0x73, 0x08, 0x41, 0x2A, 0x8F, 0x25, 0x85, 0xFE, 0xAD, 0xD7, 0xFE, 0x99, 0xF5, + 0x3B, 0xBE, 0x10, 0x81, 0x01, 0x20, 0x85, 0x04, 0x00, 0x23, 0x52, 0x48, 0xDD, 0x0F, 0xAD, 0xAA, + 0xA1, 0x9D, 0xFA, 0x88, 0xE2, 0x29, 0x11, 0x23, 0xD3, 0xC9, 0x8E, 0x31, 0xD2, 0x45, 0x34, 0x67, + 0x7E, 0xF2, 0x6B, 0x1C, 0x84, 0x10, 0xE8, 0xC7, 0x17, 0x6B, 0x62, 0xAA, 0xEB, 0x31, 0xF1, 0x3B, + 0xBE, 0x10, 0x81, 0x01, 0x58, 0x5E, 0x11, 0xD8, 0x13, 0xB9, 0xAC, 0xA5, 0x1B, 0x2D, 0x9D, 0xE7, + 0x59, 0xA9, 0xCF, 0x97, 0x32, 0x29, 0xEA, 0x5A, 0x3A, 0xE9, 0x1A, 0x9F, 0x08, 0xCC, 0xA9, 0xF7, + 0xDC, 0xFC, 0xBC, 0xCF, 0x36, 0x36, 0x5F, 0x2E, 0xCC, 0x3E, 0x88, 0x63, 0x0C, 0xCC, 0xC4, 0x7D, + 0xF8, 0xC7, 0x43, 0x37, 0xBE, 0xF4, 0xAC, 0x5A, 0x2B, 0xF7, 0x5B, 0x09, 0xDF, 0xE3, 0x0B, 0x11, + 0x18, 0x80, 0xD8, 0x62, 0x00, 0x03, 0xB0, 0xBC, 0x52, 0xC8, 0x7A, 0xE1, 0x9E, 0xFA, 0x5E, 0x58, + 0x9C, 0x25, 0x42, 0xE9, 0x3A, 0x3F, 0x08, 0x61, 0xD2, 0x33, 0xBE, 0xFC, 0xD0, 0x3D, 0xCF, 0x6B, + 0xCD, 0xD0, 0x51, 0x59, 0x33, 0x74, 0xD4, 0xD5, 0x6E, 0xDD, 0xB2, 0x21, 0xDD, 0xF2, 0xA2, 0x6A, + 0x65, 0xC3, 0x96, 0x3E, 0xDE, 0xA1, 0x4F, 0x3F, 0x8A, 0x4B, 0x8C, 0xD2, 0x8D, 0xFE, 0xA8, 0x47, + 0x0B, 0x97, 0x22, 0xE9, 0x26, 0xEC, 0x9D, 0x67, 0x84, 0x35, 0xDA, 0x86, 0xA8, 0x9E, 0x27, 0xE6, + 0x3C, 0xCF, 0xAB, 0x5E, 0x9D, 0xBF, 0x9E, 0x7A, 0x5C, 0xFE, 0xBA, 0xFB, 0x7D, 0xD7, 0xFB, 0xAD, + 0x7C, 0xF6, 0x97, 0x33, 0x96, 0xBC, 0xD3, 0x91, 0x99, 0x10, 0x91, 0x8C, 0xDF, 0xF1, 0x85, 0x08, + 0x0C, 0x00, 0x29, 0x24, 0x00, 0x18, 0x91, 0x42, 0xDA, 0xB6, 0x5D, 0x50, 0x5E, 0x5B, 0xBA, 0x74, + 0xB2, 0x55, 0x5A, 0xF9, 0xEB, 0x41, 0x26, 0x09, 0xF2, 0x57, 0x89, 0xE2, 0xD4, 0x16, 0x53, 0x7B, + 0x36, 0x8E, 0x57, 0x27, 0xE3, 0x54, 0xE7, 0x27, 0x72, 0xD9, 0x7E, 0x3F, 0x63, 0x11, 0x11, 0x18, + 0x80, 0xE5, 0x19, 0x81, 0x89, 0x48, 0xD6, 0x79, 0x71, 0xF0, 0xE0, 0xC1, 0x8C, 0xF3, 0xBA, 0xAB, + 0xAB, 0xAB, 0xE6, 0xC6, 0x6C, 0xDB, 0xCE, 0x06, 0x55, 0xB1, 0x4B, 0xB3, 0x2B, 0xEF, 0x61, 0xB1, + 0xF9, 0xE9, 0x9C, 0x58, 0x57, 0xBC, 0x3D, 0x83, 0x2A, 0x7F, 0x65, 0xCA, 0xFC, 0x93, 0x6B, 0xE6, + 0x52, 0xAC, 0xFB, 0xA3, 0x70, 0xE7, 0xE3, 0x58, 0xD6, 0xFB, 0xD3, 0xF9, 0xAB, 0x46, 0xD7, 0xCF, + 0xB6, 0xED, 0x8C, 0x8F, 0xE2, 0xD9, 0x46, 0x52, 0xC8, 0x52, 0x38, 0x77, 0xF8, 0xF0, 0xE1, 0x82, + 0x8F, 0x9D, 0x05, 0x96, 0x62, 0xEE, 0xFD, 0x7B, 0x4A, 0x56, 0x9A, 0xF9, 0x37, 0x4E, 0x7B, 0x2E, + 0xFB, 0xBF, 0x1F, 0x1F, 0x31, 0xBE, 0x3D, 0xA9, 0x91, 0xBD, 0xB1, 0xEE, 0x8F, 0x85, 0xFF, 0x3E, + 0x13, 0xCB, 0x7A, 0x1F, 0x9F, 0x7E, 0xD9, 0xF4, 0x2A, 0x4E, 0xF8, 0x18, 0xEC, 0xB8, 0x91, 0x15, + 0xC0, 0xF2, 0xD4, 0x50, 0xC4, 0xA4, 0x46, 0x65, 0x9D, 0x9D, 0x9D, 0xBA, 0x22, 0x27, 0x6C, 0xDB, + 0x7E, 0xA9, 0xE6, 0x46, 0x5E, 0x9B, 0x5C, 0xDC, 0x46, 0x67, 0x6F, 0x6C, 0x0E, 0xD6, 0x80, 0x72, + 0xBB, 0xD3, 0xF8, 0x7E, 0xAB, 0xE6, 0xB1, 0x53, 0x97, 0x60, 0xED, 0x1C, 0x18, 0x30, 0xBA, 0x5D, + 0x9B, 0xDE, 0x3E, 0xED, 0x6A, 0xCB, 0xE4, 0xD3, 0xE5, 0x45, 0xB4, 0xBD, 0x0F, 0x0F, 0x98, 0x7E, + 0x16, 0xCF, 0x5A, 0x47, 0xC7, 0x5D, 0x21, 0xFB, 0x7B, 0x6F, 0x76, 0x95, 0xDB, 0xB0, 0x23, 0x63, + 0x74, 0x13, 0x36, 0xEE, 0xBA, 0xE8, 0xEA, 0x83, 0xBD, 0x63, 0xBB, 0x4B, 0xF5, 0xCF, 0xEC, 0x78, + 0xC4, 0xE8, 0xFA, 0x0F, 0xF5, 0x9D, 0xB2, 0xEA, 0x44, 0x52, 0xDA, 0x4C, 0x6E, 0x66, 0x66, 0x46, + 0x44, 0x44, 0xEE, 0xDD, 0xBB, 0xD7, 0x7F, 0xF2, 0xE4, 0xC9, 0xAC, 0xDF, 0xFD, 0x12, 0x81, 0x01, + 0x88, 0x2D, 0x06, 0x30, 0x00, 0xB1, 0xD5, 0xD0, 0xA5, 0xBE, 0xE1, 0xE1, 0x61, 0xCB, 0x47, 0x3A, + 0xE9, 0x99, 0xFD, 0xC5, 0xF1, 0xD2, 0xEB, 0x3D, 0x7B, 0x16, 0x97, 0x33, 0x7C, 0xF3, 0xF2, 0x37, + 0x6A, 0x7E, 0xE6, 0xCC, 0xBA, 0x3F, 0xD6, 0xFC, 0x7B, 0x4F, 0x4F, 0x8F, 0x24, 0x12, 0x09, 0x4F, + 0xDB, 0xD2, 0xED, 0xDF, 0xCB, 0xE7, 0xEA, 0x51, 0x57, 0xD7, 0x9B, 0x42, 0x7D, 0xE6, 0x52, 0x5D, + 0x9F, 0x8D, 0x9B, 0x79, 0xF6, 0x76, 0x7B, 0x4F, 0x6D, 0x17, 0x6E, 0x5D, 0x36, 0xAE, 0xFA, 0xC9, + 0x75, 0x3B, 0x3D, 0x97, 0xFD, 0xE4, 0xB6, 0x99, 0x57, 0x44, 0xB7, 0xB5, 0x6D, 0xF7, 0xFD, 0x19, + 0x27, 0x6D, 0xAC, 0x1C, 0x4B, 0x88, 0xC0, 0x00, 0x90, 0x42, 0x02, 0x40, 0x1C, 0x04, 0xBA, 0x34, + 0x48, 0x77, 0xA5, 0xA1, 0xEA, 0x7D, 0x1C, 0x9A, 0xAB, 0x90, 0x87, 0x2F, 0xBD, 0x20, 0x95, 0xE9, + 0xA8, 0xDD, 0x7E, 0xAC, 0x4A, 0x1C, 0xFA, 0xD1, 0xE2, 0xDF, 0x57, 0x8D, 0xD5, 0xAC, 0xD3, 0xE6, + 0xCD, 0x9B, 0x4B, 0x29, 0xE4, 0x4F, 0x6F, 0xFD, 0xA4, 0x66, 0x59, 0xDD, 0xFE, 0xD5, 0x3A, 0xF8, + 0xB9, 0x0A, 0xA9, 0xDD, 0x7E, 0x95, 0x7B, 0xEA, 0x9C, 0x30, 0x7A, 0x70, 0x70, 0xF0, 0xBC, 0x65, + 0x59, 0x4F, 0xD5, 0x4A, 0xD9, 0xBD, 0x6E, 0xAB, 0xDE, 0xDF, 0x1B, 0xA5, 0xDB, 0x6E, 0xB5, 0x29, + 0x85, 0x7A, 0xFB, 0x0C, 0x72, 0x5B, 0x41, 0xF5, 0x81, 0x9F, 0xE3, 0xA6, 0x2B, 0x9B, 0xCF, 0xE7, + 0x5F, 0x1A, 0x1D, 0x1D, 0x3D, 0x11, 0x87, 0x3E, 0x38, 0x74, 0xE8, 0xD0, 0x53, 0x89, 0x44, 0xE2, + 0x7C, 0x98, 0x7D, 0xE0, 0x6B, 0x4C, 0x20, 0x02, 0x03, 0xB0, 0x92, 0x04, 0xBA, 0x5E, 0xC7, 0x99, + 0x9C, 0x6B, 0x6B, 0x6B, 0x93, 0xF5, 0xEB, 0xD7, 0x7B, 0xFB, 0x36, 0xA9, 0x13, 0xF5, 0xB4, 0xD2, + 0x70, 0xDF, 0xEF, 0xB5, 0xF5, 0xB2, 0x65, 0x71, 0x72, 0xFF, 0x6F, 0x1D, 0xCD, 0xD5, 0xC9, 0xCF, + 0x45, 0x8E, 0x20, 0xCB, 0x36, 0x7B, 0x71, 0x25, 0xEC, 0x36, 0xD4, 0x2B, 0xB7, 0x61, 0xC3, 0x06, + 0x49, 0x26, 0x93, 0xC6, 0xD6, 0xDF, 0xD4, 0x3E, 0x30, 0xA9, 0xFE, 0xEA, 0xC4, 0x7D, 0x90, 0x88, + 0xC0, 0x00, 0xC4, 0x16, 0x03, 0x18, 0x00, 0x52, 0x48, 0x91, 0xF2, 0xE4, 0x9E, 0x6D, 0xDB, 0x2F, + 0x8A, 0xC8, 0x6F, 0x8A, 0xAF, 0xF5, 0xCF, 0xF3, 0xF9, 0xE5, 0x0E, 0x4B, 0x44, 0xA4, 0x53, 0xF9, + 0xFB, 0x92, 0x30, 0xD3, 0xF6, 0x36, 0x49, 0x3E, 0x53, 0x67, 0xB1, 0x79, 0x3E, 0x9F, 0x2F, 0x4D, + 0xE2, 0x7B, 0xDD, 0xA6, 0x88, 0xC8, 0xB0, 0x9A, 0xE6, 0x16, 0xF7, 0xF1, 0x3D, 0x29, 0xDF, 0x27, + 0xE6, 0xE7, 0xCE, 0xA8, 0xB0, 0x27, 0x32, 0x6B, 0xF5, 0x85, 0x5A, 0x7F, 0xB5, 0x2E, 0x7E, 0xF6, + 0x5F, 0x6D, 0x19, 0x48, 0xAB, 0xDA, 0x30, 0x38, 0x38, 0x38, 0x61, 0x59, 0x56, 0xA6, 0xE6, 0xF9, + 0xB4, 0x02, 0xFB, 0x20, 0xEC, 0xFA, 0xAB, 0x6D, 0xD0, 0xD5, 0xBF, 0x5A, 0x1D, 0x74, 0x75, 0x6D, + 0xF6, 0xC2, 0x05, 0x11, 0x18, 0x00, 0x52, 0x48, 0x00, 0x58, 0x96, 0x29, 0xA4, 0x63, 0x7A, 0x7A, + 0xBA, 0x94, 0xB6, 0x2D, 0xB9, 0xB2, 0xA8, 0x09, 0x2D, 0x1B, 0x5D, 0x56, 0xB0, 0x6F, 0xDF, 0x3E, + 0x4F, 0xCF, 0x29, 0xCB, 0xE5, 0x72, 0xE2, 0x35, 0xF4, 0x0D, 0x2B, 0x8C, 0x76, 0xDA, 0x18, 0x56, + 0x18, 0xDD, 0x48, 0x2A, 0xD3, 0x48, 0xFD, 0xA3, 0x6A, 0xC3, 0xC8, 0xC8, 0x48, 0x7F, 0xAD, 0x54, + 0x4C, 0x44, 0x66, 0x6D, 0xDB, 0x4E, 0xD1, 0x07, 0xD1, 0xA5, 0xC3, 0xD5, 0x84, 0x75, 0xF5, 0x91, + 0x08, 0x0C, 0x00, 0x11, 0x98, 0x4E, 0xF1, 0x4E, 0xE4, 0x13, 0x95, 0xA3, 0x75, 0xBD, 0x91, 0x3D, + 0x48, 0x07, 0x0E, 0x1C, 0x10, 0x11, 0x91, 0x73, 0xE7, 0xCE, 0xC9, 0xC2, 0xC2, 0x42, 0xE0, 0xDF, + 0x42, 0x15, 0xDF, 0x9E, 0x91, 0xB4, 0xB1, 0x99, 0x36, 0xC4, 0xBD, 0xFE, 0x15, 0xF5, 0x7E, 0x90, + 0x3E, 0x88, 0x3E, 0x1A, 0x8B, 0xA2, 0xDE, 0x44, 0x60, 0x00, 0x62, 0x8B, 0x01, 0x0C, 0x00, 0x29, + 0xA4, 0x09, 0xA1, 0xAF, 0x93, 0x36, 0x8A, 0x88, 0x9C, 0x3D, 0x7B, 0x36, 0xF0, 0xED, 0xFB, 0x99, + 0x54, 0x6D, 0xF6, 0xBE, 0x9F, 0xB0, 0x38, 0x75, 0x69, 0xA4, 0xFE, 0x26, 0xB4, 0x41, 0xD7, 0x07, + 0x85, 0x42, 0x61, 0x76, 0x64, 0x64, 0x24, 0x45, 0x1F, 0x44, 0x9B, 0x4E, 0x12, 0x81, 0x01, 0x00, + 0x03, 0x18, 0x00, 0x52, 0xC8, 0x98, 0x51, 0xD3, 0xC6, 0x30, 0xAE, 0x42, 0x36, 0x72, 0xEF, 0x98, + 0x69, 0xBC, 0xB6, 0x21, 0xEE, 0xF5, 0xA7, 0x0F, 0x88, 0xC0, 0x00, 0x80, 0x08, 0xAC, 0x55, 0xC2, + 0x9E, 0xC4, 0x07, 0x40, 0x04, 0x06, 0x00, 0x0C, 0x60, 0x00, 0x48, 0x21, 0x63, 0xE9, 0xC2, 0x85, + 0x0B, 0x96, 0xC8, 0xD2, 0x45, 0xDD, 0xF5, 0xD2, 0x46, 0x93, 0xEE, 0x9F, 0x01, 0x40, 0x04, 0x06, + 0x80, 0x01, 0x0C, 0x00, 0x48, 0x21, 0x23, 0x4B, 0x25, 0x2B, 0xD3, 0xC9, 0xEE, 0xEE, 0x6E, 0x11, + 0x11, 0xB9, 0x73, 0xE7, 0x4E, 0xEA, 0xCC, 0x99, 0x33, 0xB3, 0xCB, 0xAD, 0xE3, 0x96, 0xC3, 0x3D, + 0x42, 0x71, 0x6F, 0x43, 0xDC, 0xEB, 0x3F, 0x3A, 0x3A, 0xFA, 0x17, 0x09, 0xF8, 0xB7, 0x61, 0x89, + 0xC0, 0x00, 0x80, 0x01, 0x0C, 0x00, 0x03, 0x18, 0x00, 0x30, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0xEC, 0xFF, 0xB1, 0xC1, 0xD0, 0xE0, 0x9F, 0xE6, + 0x52, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 +}; + +static constexpr size_t CORES3_WLED_LOGO_PNG_LEN = sizeof(CORES3_WLED_LOGO_PNG); diff --git a/usermods/CoreS3_Display/CoreS3_WLED_Logo_304x95.png b/usermods/CoreS3_Display/CoreS3_WLED_Logo_304x95.png new file mode 100644 index 0000000000000000000000000000000000000000..a3511c96c8e04f166ba6f69a55ea11116e625ea8 GIT binary patch literal 2382 zcmV-U39EZP+`uyHx*mTh^A|-`2L>Rd%TFR4k`N zh`<04fkj1~cn`8{RMD0$>Qc@9=ZfpS>F+*Uh%zd3BCSeGA}_~h=j8arbx|RnG8E^ zY(+Zhq>f>wjkQuq)6z*IFaWW&Y9%%H-EZG0y-uZShY$T{TVKD`FFz~y?do&YZ~8kw zbCq5Dry*@ybY-J*WAI(~v3|WjHs?$7t8aNeUvzRGJ{d{9k8OIR;W~?mym*u|!y8OslcI0O=Kaz=i)#v5=^VDTW zekSyt$@@kp?)~(Kj=BWNS$5=SGQW{~zN*zZU5tq5=H}$)&6}RKr7k~GmmNhtp-%(` zfD{q{NFf7~+rJn-x3{;8RlH@(`mH&Wnh#8zcZKx1=U$O7kG$^b^I!J=pyqbd%EbBA z?#DUP>sYp4^+B4<3 zY!nmz&tLt~iXXs;zyL6S0f2ESWS_MU0{JhULHeW8xK8Rxs%*1{8$3r*1 z^sHJd_ge|y;|mcOfY@RH1F*bO$a>+P`d(O^B|_;sKL}ycGrs)LJG!6}|e`U)2lWXQutox&I=i^w``_uWWM%T}_?TnA&*+x>xQaE?`@Zo}woduR+?)`g8 zR+{-YPJ1rZ+4a2up?u16E^O;=<0*LW4lZB&+K@_==u-!owDjgQrb*zW@bi2MDE?Y z*MIWl$*kY~5`h7TEe0?EvDMI}zTx3v%SSrtnf=A_((65Y7MC-8^YS-pe&)=&Ki2$v zPfw2|5(!V+((T{N=ju^c*PB9p=2BgsjhlJ)c6(247j9iPuKLM!J3W{E1iCaxccr1~eH@yrkcTh5AeHH;-F~b}9Qx7X-&c**tkdSr zn@cI=PjCICW?9|-)jIgO3aQ6@C-y5adZnY_edixA= zTS4`KpW~)0o6qN88XX-y5r;lFbm-7iiA3Ujn0?@DOdtXSu#yD2UdNf7Yinzh_3PJr z`!=Z)^|aD(-|w~cvSo2{|8E`5)ydCAjv&i6dvPV~Hqe2-_$-Zss8H3 zeUOO20K^sp7ywd8k@Do8ZQC!3$Zv|T_0RM9jN=gz3GCo*gGg<5~LE`Ii3Z5c?b+AugcIAK{B5BzmdMgY-SyH& zlkT4ZKm-PW0So|0AtOsAE7QsD?rsqgxpU`E>dculv&*gHmcupwa=`WFfK zw_%CE0Du7u00S5RFn|F70000000000005Zm|FOZ);GgDFT>t<807*qoM6N<$g5>|V A)c^nh literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp new file mode 100644 index 0000000000..606a188b1b --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -0,0 +1,595 @@ +#include "wled.h" + +#include "M5StackDisplayHardwareBackend.h" + +#include + +// =========================================================== +// Compile-time M5Stack hardware profile +// +// CoreS3 remains the default verified runtime. +// Core2 / Core2 for AWS remain diagnostic-only until their +// Display / Touch / Power paths are verified on real hardware. +// =========================================================== + +#define WLED_M5STACK_DISPLAY_PROFILE_CORES3 0 +#define WLED_M5STACK_DISPLAY_PROFILE_CORE2 1 +#define WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS 2 + +#define WLED_M5STACK_DISPLAY_REVISION_UNKNOWN 0 +#define WLED_M5STACK_DISPLAY_REVISION_V1_0 10 +#define WLED_M5STACK_DISPLAY_REVISION_V1_1 11 +#define WLED_M5STACK_DISPLAY_REVISION_V1_3 13 + +#ifndef WLED_M5STACK_DISPLAY_PROFILE + #define WLED_M5STACK_DISPLAY_PROFILE WLED_M5STACK_DISPLAY_PROFILE_CORES3 +#endif + +#ifndef WLED_M5STACK_DISPLAY_REVISION + #define WLED_M5STACK_DISPLAY_REVISION WLED_M5STACK_DISPLAY_REVISION_UNKNOWN +#endif + +#ifndef WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY + #define WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY 0 +#endif + +static_assert( + WLED_M5STACK_DISPLAY_PROFILE >= WLED_M5STACK_DISPLAY_PROFILE_CORES3 && + WLED_M5STACK_DISPLAY_PROFILE <= WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS, + "Invalid WLED_M5STACK_DISPLAY_PROFILE" +); + +static_assert( + WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_UNKNOWN || + WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_0 || + WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_1 || + WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_3, + "Invalid WLED_M5STACK_DISPLAY_REVISION" +); + +static_assert( + WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 0 || + WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1, + "WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY must be 0 or 1" +); + +static_assert( + WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORES3 || + WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1, + "Core2-family profiles require WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY=1" +); + +static_assert( + WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 0 || + WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORE2 || + WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS, + "Diagnostic-only mode is reserved for Core2-family profiles" +); + +enum M5StackDisplayHardwareProfile : uint8_t { + M5STACK_DISPLAY_HARDWARE_CORES3 = WLED_M5STACK_DISPLAY_PROFILE_CORES3, + M5STACK_DISPLAY_HARDWARE_CORE2 = WLED_M5STACK_DISPLAY_PROFILE_CORE2, + M5STACK_DISPLAY_HARDWARE_CORE2_AWS = WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS +}; + +enum M5StackDisplayHardwareRevision : uint8_t { + M5STACK_DISPLAY_REVISION_UNKNOWN = WLED_M5STACK_DISPLAY_REVISION_UNKNOWN, + M5STACK_DISPLAY_REVISION_V1_0 = WLED_M5STACK_DISPLAY_REVISION_V1_0, + M5STACK_DISPLAY_REVISION_V1_1 = WLED_M5STACK_DISPLAY_REVISION_V1_1, + M5STACK_DISPLAY_REVISION_V1_3 = WLED_M5STACK_DISPLAY_REVISION_V1_3 +}; + +static constexpr M5StackDisplayHardwareProfile ACTIVE_M5STACK_DISPLAY_PROFILE = + static_cast( WLED_M5STACK_DISPLAY_PROFILE ); + +static constexpr M5StackDisplayHardwareRevision ACTIVE_M5STACK_DISPLAY_REVISION = + static_cast( WLED_M5STACK_DISPLAY_REVISION ); + +static constexpr bool ACTIVE_M5STACK_CORE2_DIAGNOSTIC_ONLY = + ( WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1 ); + +struct M5StackDisplayHardwareCapabilities { + const char* name; + uint8_t displayRotation; + bool displayRuntimeEnabled; + bool touchRuntimeEnabled; + bool brightnessRuntimeEnabled; + bool batteryRuntimeEnabled; + bool diagnosticProbeEnabled; +}; + +static constexpr M5StackDisplayHardwareCapabilities M5STACK_HARDWARE_CAPABILITIES[] = { + { + "M5Stack CoreS3", + 1, + true, + true, + true, + true, + false + }, + { + "M5Stack Core2", + 1, + false, + false, + false, + false, + true + }, + { + "M5Stack Core2 for AWS", + 1, + false, + false, + false, + false, + true + } +}; + +static constexpr const M5StackDisplayHardwareCapabilities& + ACTIVE_M5STACK_HARDWARE_CAPABILITIES = + M5STACK_HARDWARE_CAPABILITIES[WLED_M5STACK_DISPLAY_PROFILE]; + +// =========================================================== +// CoreS3 runtime battery telemetry +// +// CoreS3 uses AXP2101 at 0x34. After Display initialization the internal +// GPIO12/GPIO11 bus is owned by M5GFX I2C_NUM_1, matching the verified +// CoreS3 Power/Audio runtime architecture. +// +// AXP2101: +// 0x00 bit3 = battery present +// 0x01 6:5 = battery current direction (01 = charging) +// 0xA4 = fuel-gauge battery percentage +// =========================================================== + +static constexpr i2c_port_t CORES3_BATTERY_I2C_PORT = I2C_NUM_1; +static constexpr uint32_t CORES3_BATTERY_I2C_FREQUENCY = 400000; +static constexpr uint8_t CORES3_AXP2101_ADDR = 0x34; +static constexpr uint8_t AXP2101_REG_PMU_STATUS1 = 0x00; +static constexpr uint8_t AXP2101_REG_PMU_STATUS2 = 0x01; +static constexpr uint8_t AXP2101_REG_BATTERY_PERCENT = 0xA4; + +static bool readCoreS3Axp2101Register( uint8_t reg, uint8_t& value ) { + auto result = lgfx::i2c::transactionWriteRead( + CORES3_BATTERY_I2C_PORT, + CORES3_AXP2101_ADDR, + ®, + 1, + &value, + 1, + CORES3_BATTERY_I2C_FREQUENCY + ); + + return result.has_value(); +} + +bool M5StackDisplayHardwareBackend::probeI2CAddress( TwoWire& wire, uint8_t address ) { + wire.beginTransmission( address ); + + return wire.endTransmission() == 0; + } + +bool M5StackDisplayHardwareBackend::readI2CRegister8( TwoWire& wire, uint8_t address, uint8_t reg, uint8_t& value ) { + wire.beginTransmission( address ); + wire.write( reg ); + + if ( wire.endTransmission( false ) != 0 ) { + return false; + } + + size_t received = wire.requestFrom( address, (size_t)1, true ); + + if ( received != 1 || !wire.available() ) { + return false; + } + + value = (uint8_t)wire.read(); + + return true; + } + +void M5StackDisplayHardwareBackend::classifyCore2HardwareProbe( TwoWire& wire ) { + if ( hardwareProbe.address68 ) { + uint8_t chipId = 0; + + // BMI270 CHIP_ID register 0x00 returns 0x24. + if ( readI2CRegister8( wire, 0x68, 0x00, chipId ) && chipId == 0x24 ) { + hardwareProbe.imu = M5STACK_DETECTED_IMU_BMI270; + hardwareProbe.imuChipId = chipId; + } + else { + // MPU6886 WHO_AM_I register 0x75 returns 0x19. + if ( readI2CRegister8( wire, 0x68, 0x75, chipId ) && chipId == 0x19 ) { + hardwareProbe.imu = M5STACK_DETECTED_IMU_MPU6886; + hardwareProbe.imuChipId = chipId; + } + } + } + + if ( ACTIVE_M5STACK_DISPLAY_PROFILE == M5STACK_DISPLAY_HARDWARE_CORE2_AWS ) { + if ( hardwareProbe.address34 ) { + // Both documented Core2 for AWS generations use AXP192. + hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP192; + } + + if ( hardwareProbe.imu == M5STACK_DETECTED_IMU_BMI270 ) { + hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3; + } + else if ( hardwareProbe.imu == M5STACK_DETECTED_IMU_MPU6886 ) { + // Official legacy Core2 for AWS documentation identifies MPU6886, + // but does not assign the v1.0 / v1.1 name here. Keep it generic. + hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY; + } + } + else if ( ACTIVE_M5STACK_DISPLAY_PROFILE == M5STACK_DISPLAY_HARDWARE_CORE2 ) { + if ( hardwareProbe.address34 && hardwareProbe.address40 ) { + // Core2 v1.1 signature: AXP2101 + INA3221. + hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP2101; + hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_V1_1; + } + else if ( hardwareProbe.address34 ) { + hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP192; + hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_LEGACY; + } + } + } + +void M5StackDisplayHardwareBackend::printCore2HardwareProbeResult() { + Serial.printf( + "[CoreS3_Display] Hardware probe: %s, Variant=%s, PMU=%s, IMU=%s", + probeStateName(), + detectedVariantName(), + detectedPmuName(), + detectedImuName() + ); + + if ( hardwareProbe.imuChipId > 0 ) { + Serial.printf( " (ID=0x%02X)", hardwareProbe.imuChipId ); + } + + Serial.println(); + + Serial.printf( + "[CoreS3_Display] Detected revision: %s\n", + detectedRevisionName() + ); + + Serial.printf( + "[CoreS3_Display] Core2 I2C signature: " + "34=%s 35=%s 38=%s 40=%s 51=%s 68=%s\n", + hardwareProbe.address34 ? "YES" : "NO", + hardwareProbe.address35 ? "YES" : "NO", + hardwareProbe.address38 ? "YES" : "NO", + hardwareProbe.address40 ? "YES" : "NO", + hardwareProbe.address51 ? "YES" : "NO", + hardwareProbe.address68 ? "YES" : "NO" + ); + } + +M5StackDisplayHardwareBackend::M5StackDisplayHardwareBackend( M5GFX& displayRef ) + : display( displayRef ) { + } + +const char* M5StackDisplayHardwareBackend::profileName() const { + return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.name; + } + +const char* M5StackDisplayHardwareBackend::revisionName() const { + switch ( ACTIVE_M5STACK_DISPLAY_REVISION ) { + case M5STACK_DISPLAY_REVISION_V1_0: + return "v1.0"; + + case M5STACK_DISPLAY_REVISION_V1_1: + return "v1.1"; + + case M5STACK_DISPLAY_REVISION_V1_3: + return "v1.3"; + + case M5STACK_DISPLAY_REVISION_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +bool M5StackDisplayHardwareBackend::isDisplayRuntimeEnabled() const { + return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled; + } + +bool M5StackDisplayHardwareBackend::isCore2FamilyProfile() const { + return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.diagnosticProbeEnabled; + } + +bool M5StackDisplayHardwareBackend::isCore2DiagnosticOnlyMode() const { + return isCore2FamilyProfile() && ACTIVE_M5STACK_CORE2_DIAGNOSTIC_ONLY; + } + +const char* M5StackDisplayHardwareBackend::runtimeModeName() const { + return isCore2DiagnosticOnlyMode() ? "CORE2 DIAGNOSTIC ONLY" : "DISPLAY ACTIVE"; + } + +const char* M5StackDisplayHardwareBackend::portStatusName() const { + if ( ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled ) { + return "CORES3 VERIFIED DISPLAY RUNTIME"; + } + + if ( isCore2DiagnosticOnlyMode() ) { + return "CORE2 PORT PREPARED - DIAGNOSTIC ONLY"; + } + + return "HARDWARE RUNTIME BLOCKED"; + } + +const char* M5StackDisplayHardwareBackend::probeStateName() const { + switch ( hardwareProbe.state ) { + case M5STACK_HARDWARE_PROBE_NOT_REQUIRED: + return "NOT REQUIRED"; + + case M5STACK_HARDWARE_PROBE_COMPLETE: + return "COMPLETE"; + + case M5STACK_HARDWARE_PROBE_FAILED: + return "FAILED"; + + case M5STACK_HARDWARE_PROBE_NOT_RUN: + default: + return "NOT RUN"; + } + } + +const char* M5StackDisplayHardwareBackend::detectedPmuName() const { + switch ( hardwareProbe.pmu ) { + case M5STACK_DETECTED_PMU_AXP192: + return "AXP192 signature"; + + case M5STACK_DETECTED_PMU_AXP2101: + return "AXP2101 signature"; + + case M5STACK_DETECTED_PMU_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +const char* M5StackDisplayHardwareBackend::detectedImuName() const { + switch ( hardwareProbe.imu ) { + case M5STACK_DETECTED_IMU_MPU6886: + return "MPU6886"; + + case M5STACK_DETECTED_IMU_BMI270: + return "BMI270"; + + case M5STACK_DETECTED_IMU_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +const char* M5StackDisplayHardwareBackend::detectedVariantName() const { + switch ( hardwareProbe.variant ) { + case M5STACK_DETECTED_VARIANT_CORE2_LEGACY: + return "Core2 legacy signature"; + + case M5STACK_DETECTED_VARIANT_CORE2_V1_1: + return "Core2 v1.1 signature"; + + case M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY: + return "Core2 for AWS MPU6886 generation"; + + case M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3: + return "Core2 for AWS v1.3 signature"; + + case M5STACK_DETECTED_VARIANT_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +const char* M5StackDisplayHardwareBackend::detectedRevisionName() const { + switch ( hardwareProbe.variant ) { + case M5STACK_DETECTED_VARIANT_CORE2_V1_1: + return "v1.1 signature"; + + case M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3: + return "v1.3 signature"; + + case M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY: + return "Legacy MPU6886 generation; exact revision unknown"; + + case M5STACK_DETECTED_VARIANT_CORE2_LEGACY: + return "Legacy generation; exact revision unknown"; + + case M5STACK_DETECTED_VARIANT_UNKNOWN: + default: + return "UNKNOWN"; + } + } + +bool M5StackDisplayHardwareBackend::isProbeComplete() const { + return hardwareProbe.state == M5STACK_HARDWARE_PROBE_COMPLETE; + } + +bool M5StackDisplayHardwareBackend::hasI2CAddress( uint8_t address ) const { + switch ( address ) { + case 0x34: + return hardwareProbe.address34; + + case 0x35: + return hardwareProbe.address35; + + case 0x38: + return hardwareProbe.address38; + + case 0x40: + return hardwareProbe.address40; + + case 0x51: + return hardwareProbe.address51; + + case 0x68: + return hardwareProbe.address68; + + default: + return false; + } + } + +void M5StackDisplayHardwareBackend::runDiagnostics() { + hardwareProbe = HardwareProbeResult(); + + if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.diagnosticProbeEnabled ) { + hardwareProbe.state = M5STACK_HARDWARE_PROBE_NOT_REQUIRED; + + return; + } + + TwoWire probeWire( 1 ); + + if ( !probeWire.begin( CORE2_INTERNAL_I2C_SDA, CORE2_INTERNAL_I2C_SCL, CORE2_INTERNAL_I2C_FREQUENCY ) ) { + hardwareProbe.state = M5STACK_HARDWARE_PROBE_FAILED; + + Serial.println( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C start failed" ) ); + + return; + } + + delay( 10 ); + + hardwareProbe.address34 = probeI2CAddress( probeWire, 0x34 ); + hardwareProbe.address35 = probeI2CAddress( probeWire, 0x35 ); + hardwareProbe.address38 = probeI2CAddress( probeWire, 0x38 ); + hardwareProbe.address40 = probeI2CAddress( probeWire, 0x40 ); + hardwareProbe.address51 = probeI2CAddress( probeWire, 0x51 ); + hardwareProbe.address68 = probeI2CAddress( probeWire, 0x68 ); + + classifyCore2HardwareProbe( probeWire ); + + probeWire.end(); + + hardwareProbe.state = M5STACK_HARDWARE_PROBE_COMPLETE; + + printCore2HardwareProbeResult(); + } + +bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int16_t& screenHeight, bool& touchReady ) { + if ( !isDisplayRuntimeEnabled() ) { + Serial.printf( + "[CoreS3_Display] Hardware profile not enabled for Display runtime: %s (%s)\n", + profileName(), + revisionName() + ); + + return false; + } + + display.begin(); + + display.setRotation( ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRotation ); + + screenWidth = display.width(); + screenHeight = display.height(); + + Serial.printf( "[CoreS3_Display] " "Display size: %d x %d\n", screenWidth, screenHeight ); + + if ( screenWidth <= 0 || screenHeight <= 0 ) { + Serial.println( F( "[CoreS3_Display] " "ERROR: Display not detected" ) ); + + return false; + } + + touchReady = ( display.touch() != nullptr ); + + Serial.printf( "[CoreS3_Display] " "Touch: %s\n", touchReady ? "READY" : "NOT FOUND" ); + + return true; + } + +bool M5StackDisplayHardwareBackend::readTouch( int16_t& touchX, int16_t& touchY ) { + if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.touchRuntimeEnabled ) { + return false; + } + + return ( display.getTouch( &touchX, &touchY ) > 0 ); + } + +void M5StackDisplayHardwareBackend::writeBrightness( uint8_t value ) { + if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.brightnessRuntimeEnabled ) { + return; + } + + display.setBrightness( value ); + } + + +bool M5StackDisplayHardwareBackend::readDisplayRgb565( + uint16_t* pixels, + int16_t width, + int16_t height +) { + if ( + !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled || + pixels == nullptr || + width <= 0 || + height <= 0 + ) { + return false; + } + + if ( width != display.width() || height != display.height() ) { + return false; + } + + display.readRect( + 0, + 0, + width, + height, + pixels + ); + + return true; + } + + +bool M5StackDisplayHardwareBackend::readBatteryStatus( M5StackBatteryStatus& status ) { + status = M5StackBatteryStatus(); + + if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.batteryRuntimeEnabled ) { + return false; + } + + if ( ACTIVE_M5STACK_DISPLAY_PROFILE != M5STACK_DISPLAY_HARDWARE_CORES3 ) { + return false; + } + + uint8_t status1 = 0; + uint8_t status2 = 0; + uint8_t batteryPercent = 0; + + if ( + !readCoreS3Axp2101Register( AXP2101_REG_PMU_STATUS1, status1 ) || + !readCoreS3Axp2101Register( AXP2101_REG_PMU_STATUS2, status2 ) || + !readCoreS3Axp2101Register( AXP2101_REG_BATTERY_PERCENT, batteryPercent ) + ) { + return false; + } + + status.available = true; + status.present = ( status1 & 0x08 ) != 0; + + const uint8_t batteryDirection = ( status2 >> 5 ) & 0x03; + status.charging = ( batteryDirection == 0x01 ); + + // AXP2101 REG A4 is a direct percentage value. Treat values outside + // the documented 0..100 range as invalid telemetry. + if ( batteryPercent > 100 ) { + status.available = false; + return false; + } + + status.level = batteryPercent; + + return true; + } diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h new file mode 100644 index 0000000000..a3ef44a5bf --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h @@ -0,0 +1,124 @@ +#pragma once + +#include +#include + +// M5Stack display hardware boundary. +// +// This class contains only board-dependent Display / Touch / brightness +// access and Core2-family read-only hardware diagnostics. +// UI/WLED behavior remains in CoreS3_Display.cpp. +struct M5StackBatteryStatus { + bool available = false; + bool present = false; + bool charging = false; + uint8_t level = 0; +}; + +class M5StackDisplayHardwareBackend { + private: + + enum M5StackHardwareProbeState : uint8_t { + M5STACK_HARDWARE_PROBE_NOT_REQUIRED = 0, + M5STACK_HARDWARE_PROBE_NOT_RUN, + M5STACK_HARDWARE_PROBE_COMPLETE, + M5STACK_HARDWARE_PROBE_FAILED + }; + + enum M5StackDetectedPmu : uint8_t { + M5STACK_DETECTED_PMU_UNKNOWN = 0, + M5STACK_DETECTED_PMU_AXP192, + M5STACK_DETECTED_PMU_AXP2101 + }; + + enum M5StackDetectedImu : uint8_t { + M5STACK_DETECTED_IMU_UNKNOWN = 0, + M5STACK_DETECTED_IMU_MPU6886, + M5STACK_DETECTED_IMU_BMI270 + }; + + enum M5StackDetectedVariant : uint8_t { + M5STACK_DETECTED_VARIANT_UNKNOWN = 0, + M5STACK_DETECTED_VARIANT_CORE2_LEGACY, + M5STACK_DETECTED_VARIANT_CORE2_V1_1, + M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY, + M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3 + }; + + struct HardwareProbeResult { + M5StackHardwareProbeState state = M5STACK_HARDWARE_PROBE_NOT_RUN; + M5StackDetectedPmu pmu = M5STACK_DETECTED_PMU_UNKNOWN; + M5StackDetectedImu imu = M5STACK_DETECTED_IMU_UNKNOWN; + M5StackDetectedVariant variant = M5STACK_DETECTED_VARIANT_UNKNOWN; + + bool address34 = false; + bool address35 = false; + bool address38 = false; + bool address40 = false; + bool address51 = false; + bool address68 = false; + + uint8_t imuChipId = 0; + }; + + static constexpr int CORE2_INTERNAL_I2C_SDA = 21; + static constexpr int CORE2_INTERNAL_I2C_SCL = 22; + static constexpr uint32_t CORE2_INTERNAL_I2C_FREQUENCY = 400000; + + M5GFX& display; + HardwareProbeResult hardwareProbe; + + bool probeI2CAddress( TwoWire& wire, uint8_t address ); + bool readI2CRegister8( TwoWire& wire, uint8_t address, uint8_t reg, uint8_t& value ); + + void classifyCore2HardwareProbe( TwoWire& wire ); + void printCore2HardwareProbeResult(); + + public: + + explicit M5StackDisplayHardwareBackend( M5GFX& displayRef ); + + const char* profileName() const; + const char* revisionName() const; + + bool isDisplayRuntimeEnabled() const; + bool isCore2FamilyProfile() const; + bool isCore2DiagnosticOnlyMode() const; + + const char* runtimeModeName() const; + const char* portStatusName() const; + + const char* probeStateName() const; + const char* detectedPmuName() const; + const char* detectedImuName() const; + const char* detectedVariantName() const; + const char* detectedRevisionName() const; + + bool isProbeComplete() const; + bool hasI2CAddress( uint8_t address ) const; + + void runDiagnostics(); + + bool initializeDisplay( + int16_t& screenWidth, + int16_t& screenHeight, + bool& touchReady + ); + + bool readTouch( int16_t& touchX, int16_t& touchY ); + void writeBrightness( uint8_t value ); + + // Read the current physical LCD contents as RGB565. + // The caller owns the buffer; this backend keeps panel access out of the + // UI/WLED-state layer. + bool readDisplayRgb565( + uint16_t* pixels, + int16_t width, + int16_t height + ); + + // Read battery state through the active board-specific hardware backend. + // The UI consumes only this generic status object; PMIC/register details + // stay outside CoreS3_Display.cpp. + bool readBatteryStatus( M5StackBatteryStatus& status ); +}; diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchContext.h b/usermods/CoreS3_Display/M5StackDisplayTouchContext.h new file mode 100644 index 0000000000..d9722870f2 --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayTouchContext.h @@ -0,0 +1,20 @@ +#pragma once + +#include "M5StackDisplayTouchState.h" + +// Explicit input boundary for the Touch state-machine entry points. +// +// The contexts do not own data. They only reference the existing +// M5StackTouchRuntimeState and the current hit-test snapshot/time. +// This prepares Press/Hold/Release for later physical extraction +// without changing the state-machine algorithm. +struct M5StackTouchFrameContext { + M5StackTouchRuntimeState& state; + const M5StackTouchHitState& hit; + unsigned long now; +}; + +struct M5StackTouchReleaseContext { + M5StackTouchRuntimeState& state; + unsigned long now; +}; diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h new file mode 100644 index 0000000000..3be4e13bc2 --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h @@ -0,0 +1,80 @@ +#pragma once + +#include "M5StackDisplayUI.h" +#include "M5StackDisplayTouchState.h" + +// Stateless helpers shared by the M5Stack controller Touch layer. +// +// These functions do not own runtime state, access hardware, draw UI, +// or modify WLED state. Their bodies are moved from the hardware-verified +// CoreS3_Display implementation without algorithm changes. +namespace M5StackDisplayTouchHelpers { + +inline void resetRepeatTouch( M5StackRepeatTouchState& state ) { + state.pressStart = 0; + state.lastRepeat = 0; + state.longPressActive = false; +} +inline void beginRepeatTouch( M5StackRepeatTouchState& state, unsigned long now ) { + state.pressStart = now; + state.lastRepeat = now; + state.longPressActive = false; +} +inline bool serviceRepeatTouch( M5StackRepeatTouchState& state, unsigned long now, unsigned long longPressMs, unsigned long repeatMs ) { + if ( !state.longPressActive && now - state.pressStart >= longPressMs ) { + state.longPressActive = true; + state.lastRepeat = now; + return true; + } + + if ( state.longPressActive && now - state.lastRepeat >= repeatMs ) { + state.lastRepeat = now; + return true; + } + + return false; +} +inline bool pointInsideRect( int16_t px, int16_t py, const M5StackTouchRect& rect ) { + return ( px >= rect.x && px < rect.x + rect.w && py >= rect.y && py < rect.y + rect.h ); +} + +bool isPowerButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_POWER ); } +bool isBrightnessDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_DOWN ); } +bool isBrightnessUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_UP ); } +bool isEffectPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_PREV ); } +bool isEffectDetailTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_DETAIL ); } +bool isEffectNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_NEXT ); } +bool isColorButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_COLOR_OPEN ); } +bool isPresetOpenButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_OPEN ); } +bool isBackButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BACK ); } +bool isHueDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_DOWN ); } +bool isHueUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_UP ); } +bool isSaturationDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_DOWN ); } +bool isSaturationUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_UP ); } +bool isSpeedDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_DOWN ); } +bool isSpeedUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_UP ); } +bool isIntensityDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_DOWN ); } +bool isIntensityUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_UP ); } +bool isPalettePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_PREV ); } +bool isPaletteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_NEXT ); } +bool isPresetPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_PREV ); } +bool isPresetNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_NEXT ); } +bool isPresetManageTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_MANAGE ); } +bool isPresetSaveNewTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_NEW ); } +bool isPresetSaveHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_HOLD ); } +bool isPresetOverwriteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_OPEN ); } +bool isPresetOverwritePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_PREV ); } +bool isPresetOverwriteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_NEXT ); } +bool isPresetOverwriteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_HOLD ); } +bool isPresetDeleteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_OPEN ); } +bool isPresetDeletePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_PREV ); } +bool isPresetDeleteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_NEXT ); } +bool isPresetDeleteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_HOLD ); } +bool isPresetBootOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_OPEN ); } +bool isPresetBootPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_PREV ); } +bool isPresetBootNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_NEXT ); } +bool isPresetBootHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_HOLD ); } +inline bool isTouchTargetPair( M5StackTouchTarget target, M5StackTouchTarget firstTarget, M5StackTouchTarget secondTarget ) { + return target == firstTarget || target == secondTarget; +} +} // namespace M5StackDisplayTouchHelpers diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchState.h b/usermods/CoreS3_Display/M5StackDisplayTouchState.h new file mode 100644 index 0000000000..f68daa17ae --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayTouchState.h @@ -0,0 +1,191 @@ +#pragma once + +#include + +// Common M5Stack controller Touch state types. +// +// These types contain no hardware access and no WLED operations. +// Touch geometry is defined separately in M5StackDisplayUI.h. +// The runtime state machine remains in CoreS3_Display.cpp for now. + +// Touch action selected when a press begins. +enum M5StackTouchTarget : uint8_t { + M5STACK_TOUCH_TARGET_NONE = 0, + + M5STACK_TOUCH_TARGET_POWER, + + M5STACK_TOUCH_TARGET_WIFI_RECOVERY, + + M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN, M5STACK_TOUCH_TARGET_BRIGHTNESS_UP, + + M5STACK_TOUCH_TARGET_EFFECT_PREV, M5STACK_TOUCH_TARGET_EFFECT_DETAIL, M5STACK_TOUCH_TARGET_EFFECT_NEXT, + + M5STACK_TOUCH_TARGET_COLOR_OPEN, M5STACK_TOUCH_TARGET_PRESET_OPEN, + + M5STACK_TOUCH_TARGET_BACK, + + M5STACK_TOUCH_TARGET_COLOR_SLOT_1, M5STACK_TOUCH_TARGET_COLOR_SLOT_2, M5STACK_TOUCH_TARGET_COLOR_SLOT_3, + + M5STACK_TOUCH_TARGET_HUE_DOWN, M5STACK_TOUCH_TARGET_HUE_UP, + + M5STACK_TOUCH_TARGET_SATURATION_DOWN, M5STACK_TOUCH_TARGET_SATURATION_UP, + + M5STACK_TOUCH_TARGET_SPEED_DOWN, M5STACK_TOUCH_TARGET_SPEED_UP, + + M5STACK_TOUCH_TARGET_INTENSITY_DOWN, M5STACK_TOUCH_TARGET_INTENSITY_UP, + + M5STACK_TOUCH_TARGET_PALETTE_PREV, M5STACK_TOUCH_TARGET_PALETTE_NEXT, + + M5STACK_TOUCH_TARGET_PRESET_PREV, M5STACK_TOUCH_TARGET_PRESET_NEXT, + + M5STACK_TOUCH_TARGET_PRESET_MANAGE, M5STACK_TOUCH_TARGET_PRESET_SAVE_NEW, M5STACK_TOUCH_TARGET_PRESET_SAVE_HOLD, + + M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_OPEN, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_HOLD, + + M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN, M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV, M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT, M5STACK_TOUCH_TARGET_PRESET_DELETE_HOLD, + + M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN, M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV, M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT, M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD }; + +// One hit-test snapshot built from the current Touch coordinates. +struct M5StackTouchHitState { + bool insidePower = false; + bool insideWiFiRecovery = false; + bool insideBrightnessDown = false; + bool insideBrightnessUp = false; + bool insideEffectPrev = false; + bool insideEffectDetail = false; + bool insideEffectNext = false; + bool insideColor = false; + bool insidePresetOpen = false; + bool insideBack = false; + bool insideColorSlot1 = false; + bool insideColorSlot2 = false; + bool insideColorSlot3 = false; + bool insideHueDown = false; + bool insideHueUp = false; + bool insideSaturationDown = false; + bool insideSaturationUp = false; + bool insideSpeedDown = false; + bool insideSpeedUp = false; + bool insideIntensityDown = false; + bool insideIntensityUp = false; + bool insidePalettePrev = false; + bool insidePaletteNext = false; + bool insidePresetPrev = false; + bool insidePresetNext = false; + bool insidePresetManage = false; + bool insidePresetSaveNew = false; + bool insidePresetSaveHold = false; + bool insidePresetOverwriteOpen = false; + bool insidePresetOverwritePrev = false; + bool insidePresetOverwriteNext = false; + bool insidePresetOverwriteHold = false; + bool insidePresetDeleteOpen = false; + bool insidePresetDeletePrev = false; + bool insidePresetDeleteNext = false; + bool insidePresetDeleteHold = false; + bool insidePresetBootOpen = false; + bool insidePresetBootPrev = false; + bool insidePresetBootNext = false; + bool insidePresetBootHold = false; +}; + +// Shared long-press / repeat timing state. +struct M5StackRepeatTouchState { + unsigned long pressStart = 0; + unsigned long lastRepeat = 0; + bool longPressActive = false; +}; + +// Consolidated runtime state for Touch interaction and feedback. +// +// Field names/defaults intentionally match the previously separate +// CoreS3_Display.cpp members so these phases remain storage-only refactors. +struct M5StackTouchRuntimeState { + unsigned long lastTouchPoll = 0; + unsigned long lastTouchAction = 0; + unsigned long touchReleaseCandidate = 0; + M5StackTouchTarget touchTarget = M5STACK_TOUCH_TARGET_NONE; + bool touchActive = false; + bool lastTouchInsidePower = false; + bool lastTouchInsideWiFiRecovery = false; + bool lastTouchInsideBrightness = false; + bool lastTouchInsideEffect = false; + bool lastTouchInsideEffectDetail = false; + bool lastTouchInsideColor = false; + bool lastTouchInsidePresetOpen = false; + bool lastTouchInsideBack = false; + bool lastTouchInsideColorSlot = false; + bool lastTouchInsideHue = false; + bool lastTouchInsideSaturation = false; + bool lastTouchInsideSpeed = false; + bool lastTouchInsideIntensity = false; + bool lastTouchInsidePalette = false; + bool lastTouchInsidePresetNav = false; + bool lastTouchInsidePresetManage = false; + bool lastTouchInsidePresetSaveNew = false; + bool lastTouchInsidePresetSaveHold = false; + bool lastTouchInsidePresetOverwriteOpen = false; + bool lastTouchInsidePresetOverwriteNav = false; + bool lastTouchInsidePresetOverwriteHold = false; + bool lastTouchInsidePresetDeleteOpen = false; + bool lastTouchInsidePresetDeleteNav = false; + bool lastTouchInsidePresetDeleteHold = false; + bool lastTouchInsidePresetBootOpen = false; + bool lastTouchInsidePresetBootNav = false; + bool lastTouchInsidePresetBootHold = false; + M5StackRepeatTouchState wifiRecoveryHoldState; + M5StackRepeatTouchState brightnessRepeatState; + M5StackRepeatTouchState effectRepeatState; + M5StackRepeatTouchState colorSlotHoldState; + M5StackRepeatTouchState hueRepeatState; + M5StackRepeatTouchState saturationRepeatState; + M5StackRepeatTouchState speedRepeatState; + M5StackRepeatTouchState intensityRepeatState; + M5StackRepeatTouchState paletteRepeatState; + M5StackRepeatTouchState presetRepeatState; + // ESP32 toolchains use a 16-bit signed short here. + // Using the fundamental type also keeps VS Code IntelliSense from + // mis-parsing these final coordinate members in this header. + signed short lastTouchX = -1; + signed short lastTouchY = -1; + + // Wake Touch state used while the LCD is sleeping/waking. + unsigned long wakeTouchLastPoll = 0; + bool wakeTouchState = false; + unsigned long wakeReleaseCandidate = 0; + + // Visual pressed-state flags used only for button feedback drawing. + bool powerButtonVisualPressed = false; + bool wifiRecoveryVisualPressed = false; + bool brightnessButtonVisualPressed = false; + bool effectButtonVisualPressed = false; + bool effectDetailVisualPressed = false; + bool colorButtonVisualPressed = false; + bool presetOpenButtonVisualPressed = false; + bool backButtonVisualPressed = false; + bool hueButtonVisualPressed = false; + bool saturationButtonVisualPressed = false; + bool speedButtonVisualPressed = false; + bool intensityButtonVisualPressed = false; + bool paletteButtonVisualPressed = false; + bool presetNavButtonVisualPressed = false; + bool presetManageButtonVisualPressed = false; + bool presetSaveNewButtonVisualPressed = false; + bool presetSaveHoldButtonVisualPressed = false; + bool presetOverwriteOpenButtonVisualPressed = false; + bool presetOverwriteNavButtonVisualPressed = false; + bool presetOverwriteHoldButtonVisualPressed = false; + bool presetDeleteOpenButtonVisualPressed = false; + bool presetDeleteNavButtonVisualPressed = false; + bool presetDeleteHoldButtonVisualPressed = false; + bool presetBootOpenButtonVisualPressed = false; + bool presetBootNavButtonVisualPressed = false; + bool presetBootHoldButtonVisualPressed = false; + + // VS Code IntelliSense has occasionally failed to expose the final member + // of this large runtime-state struct even though the ESP32 compiler parses + // it correctly. Keep an unused tail guard so all real runtime members sit + // before the parser-sensitive final position. + bool intellisenseTailGuard = false; +}; diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc b/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc new file mode 100644 index 0000000000..5b4bef126d --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc @@ -0,0 +1,1720 @@ + // ========================================================= + // Shared paired-touch helpers + // ========================================================= + + + bool isSelectedTouchPairInside( M5StackTouchTarget firstTarget, bool firstInside, M5StackTouchTarget secondTarget, bool secondInside ) { + if ( touchState.touchTarget == firstTarget ) { + return firstInside; + } + + if ( touchState.touchTarget == secondTarget ) { + return secondInside; + } + + return false; + } + + // ========================================================= + // Touch processing + // ========================================================= + + // ========================================================= + // Build one touch hit-test snapshot + // ========================================================= + + M5StackTouchHitState buildTouchHitState( int16_t touchX, int16_t touchY ) { + M5StackTouchHitState hit; + + hit.insidePower = M5StackDisplayTouchHelpers::isPowerButtonTouched( touchX, touchY ); + + if ( currentPage == SCREEN_MAIN ) { + if ( getNetworkAccessMode() == NETWORK_ACCESS_NONE ) { + hit.insideWiFiRecovery = + M5StackDisplayTouchHelpers::pointInsideRect( + touchX, + touchY, + M5STACK_TOUCH_WIFI_RECOVERY + ); + } + + hit.insideBrightnessDown = M5StackDisplayTouchHelpers::isBrightnessDownTouched( touchX, touchY ); + + hit.insideBrightnessUp = M5StackDisplayTouchHelpers::isBrightnessUpTouched( touchX, touchY ); + + hit.insideEffectPrev = M5StackDisplayTouchHelpers::isEffectPrevTouched( touchX, touchY ); + + hit.insideEffectDetail = M5StackDisplayTouchHelpers::isEffectDetailTouched( touchX, touchY ); + + hit.insideEffectNext = M5StackDisplayTouchHelpers::isEffectNextTouched( touchX, touchY ); + + hit.insideColor = M5StackDisplayTouchHelpers::isColorButtonTouched( touchX, touchY ); + + hit.insidePresetOpen = M5StackDisplayTouchHelpers::isPresetOpenButtonTouched( touchX, touchY ); + } + + if ( currentPage == SCREEN_COLOR ) { + hit.insideBack = M5StackDisplayTouchHelpers::isBackButtonTouched( touchX, touchY ); + + const M5StackEffectColorCapabilities colorCapability = + getEffectColorCapabilities( getCurrentEffectMode() ); + + if ( colorCapability.color1 ) { + hit.insideColorSlot1 = + M5StackDisplayTouchHelpers::pointInsideRect( + touchX, + touchY, + M5STACK_TOUCH_COLOR_SLOT_1 + ); + } + + if ( colorCapability.color2 ) { + hit.insideColorSlot2 = + M5StackDisplayTouchHelpers::pointInsideRect( + touchX, + touchY, + M5STACK_TOUCH_COLOR_SLOT_2 + ); + } + + if ( colorCapability.color3 ) { + hit.insideColorSlot3 = + M5StackDisplayTouchHelpers::pointInsideRect( + touchX, + touchY, + M5STACK_TOUCH_COLOR_SLOT_3 + ); + } + + if ( effectUsesAnyColor( colorCapability ) ) { + hit.insideHueDown = M5StackDisplayTouchHelpers::isHueDownTouched( touchX, touchY ); + + hit.insideHueUp = M5StackDisplayTouchHelpers::isHueUpTouched( touchX, touchY ); + + hit.insideSaturationDown = M5StackDisplayTouchHelpers::isSaturationDownTouched( touchX, touchY ); + + hit.insideSaturationUp = M5StackDisplayTouchHelpers::isSaturationUpTouched( touchX, touchY ); + } + } + + if ( currentPage == SCREEN_EFFECT ) { + hit.insideBack = M5StackDisplayTouchHelpers::isBackButtonTouched( touchX, touchY ); + + const uint8_t effectMode = getCurrentEffectMode(); + + if ( isEffectSpeedControlVisible( effectMode ) ) { + hit.insideSpeedDown = M5StackDisplayTouchHelpers::isSpeedDownTouched( touchX, touchY ); + + hit.insideSpeedUp = M5StackDisplayTouchHelpers::isSpeedUpTouched( touchX, touchY ); + } + + if ( isEffectIntensityControlVisible( effectMode ) ) { + hit.insideIntensityDown = M5StackDisplayTouchHelpers::isIntensityDownTouched( touchX, touchY ); + + hit.insideIntensityUp = M5StackDisplayTouchHelpers::isIntensityUpTouched( touchX, touchY ); + } + + if ( isEffectPaletteControlVisible( effectMode ) ) { + hit.insidePalettePrev = M5StackDisplayTouchHelpers::isPalettePrevTouched( touchX, touchY ); + + hit.insidePaletteNext = M5StackDisplayTouchHelpers::isPaletteNextTouched( touchX, touchY ); + } + } + + if ( currentPage == SCREEN_PRESET ) { + hit.insideBack = M5StackDisplayTouchHelpers::isBackButtonTouched( touchX, touchY ); + + if ( presetSubPage == PRESET_SUBPAGE_NAV ) { + hit.insidePresetPrev = M5StackDisplayTouchHelpers::isPresetPrevTouched( touchX, touchY ); + + hit.insidePresetNext = M5StackDisplayTouchHelpers::isPresetNextTouched( touchX, touchY ); + + hit.insidePresetManage = ( presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 && M5StackDisplayTouchHelpers::isPresetManageTouched( touchX, touchY ) ); + } + else if ( presetSubPage == PRESET_SUBPAGE_MANAGE ) { + hit.insidePresetSaveNew = ( findFirstFreePresetId() > 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetSaveNewTouched( touchX, touchY ) ); + + hit.insidePresetOverwriteOpen = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetOverwriteOpenTouched( touchX, touchY ) ); + + hit.insidePresetDeleteOpen = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetDeleteOpenTouched( touchX, touchY ) ); + + hit.insidePresetBootOpen = ( presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetBootOpenTouched( touchX, touchY ) ); + } + else if ( presetSubPage == PRESET_SUBPAGE_SAVE ) { + hit.insidePresetSaveHold = ( presetSaveOperationState == PRESET_SAVE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetSaveCandidateId > 0 && findPresetCacheIndex( presetSaveCandidateId ) < 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetSaveHoldTouched( touchX, touchY ) ); + } + else if ( presetSubPage == PRESET_SUBPAGE_OVERWRITE ) { + hit.insidePresetOverwritePrev = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetOverwritePrevTouched( touchX, touchY ) ); + + hit.insidePresetOverwriteNext = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetOverwriteNextTouched( touchX, touchY ) ); + + hit.insidePresetOverwriteHold = ( presetSaveOperationState == PRESET_SAVE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetOverwriteTargetId > 0 && findPresetCacheIndex( presetOverwriteTargetId ) >= 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetOverwriteHoldTouched( touchX, touchY ) ); + } + else if ( presetSubPage == PRESET_SUBPAGE_DELETE ) { + hit.insidePresetDeletePrev = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetDeletePrevTouched( touchX, touchY ) ); + + hit.insidePresetDeleteNext = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetDeleteNextTouched( touchX, touchY ) ); + + hit.insidePresetDeleteHold = ( presetDeleteOperationState == PRESET_DELETE_OP_IDLE && presetCacheReady && !presetCacheBuilding && presetDeleteTargetId > 0 && findPresetCacheIndex( presetDeleteTargetId ) >= 0 && pendingPresetId == 0 && !presetNeedsSaving() && M5StackDisplayTouchHelpers::isPresetDeleteHoldTouched( touchX, touchY ) ); + } + else if ( presetSubPage == PRESET_SUBPAGE_BOOT ) { + hit.insidePresetBootPrev = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetBootPrevTouched( touchX, touchY ) ); + + hit.insidePresetBootNext = ( presetCacheReady && !presetCacheBuilding && presetCacheCount > 0 && M5StackDisplayTouchHelpers::isPresetBootNextTouched( touchX, touchY ) ); + + hit.insidePresetBootHold = ( presetBootOperationState == PRESET_BOOT_OP_IDLE && presetCacheReady && !presetCacheBuilding && pendingPresetId == 0 && !presetNeedsSaving() && isPresetBootTargetValid() && !isPresetBootTargetCurrent() && M5StackDisplayTouchHelpers::isPresetBootHoldTouched( touchX, touchY ) ); + } + } + + + return hit; + } + + // ========================================================= + // Touch Press + // + // Starts a new gesture and acquires its M5StackTouchTarget. + // ========================================================= + + void handleTouchPress( const M5StackTouchFrameContext& context ) { + M5StackTouchRuntimeState& touchState = context.state; + const M5StackTouchHitState& hit = context.hit; + const unsigned long now = context.now; + + if (!touchState.touchActive) { + touchState.touchActive = true; + + touchState.touchTarget = M5STACK_TOUCH_TARGET_NONE; + + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.wifiRecoveryHoldState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.brightnessRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.effectRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.colorSlotHoldState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.hueRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.saturationRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.speedRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.intensityRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.paletteRepeatState ); + M5StackDisplayTouchHelpers::resetRepeatTouch( touchState.presetRepeatState ); + + presetSaveHoldStartTime = 0; + + presetSaveHoldTriggered = false; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_NONE ) { + if (hit.insidePower) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_POWER; + + touchState.lastTouchInsidePower = true; + } + + else if ( currentPage == SCREEN_MAIN ) { + if (hit.insideWiFiRecovery) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_WIFI_RECOVERY; + + touchState.lastTouchInsideWiFiRecovery = true; + touchState.wifiRecoveryVisualPressed = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( + touchState.wifiRecoveryHoldState, + now + ); + + drawMainNetworkStatusLine( + "Hold... Recovery AP", + TFT_YELLOW + ); + } + else if (hit.insideBrightnessDown) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN; + + touchState.lastTouchInsideBrightness = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.brightnessRepeatState, now ); + } + else if (hit.insideBrightnessUp) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_BRIGHTNESS_UP; + + touchState.lastTouchInsideBrightness = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.brightnessRepeatState, now ); + } + else if (hit.insideEffectPrev) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_EFFECT_PREV; + + touchState.lastTouchInsideEffect = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.effectRepeatState, now ); + } + else if (hit.insideEffectDetail) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_EFFECT_DETAIL; + + touchState.lastTouchInsideEffectDetail = true; + } + else if (hit.insideEffectNext) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_EFFECT_NEXT; + + touchState.lastTouchInsideEffect = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.effectRepeatState, now ); + } + else if (hit.insideColor) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_COLOR_OPEN; + + touchState.lastTouchInsideColor = true; + } + else if (hit.insidePresetOpen) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_OPEN; + + touchState.lastTouchInsidePresetOpen = true; + } + } + + else if ( currentPage == SCREEN_COLOR ) { + if (hit.insideBack) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_BACK; + + touchState.lastTouchInsideBack = true; + } + else if (hit.insideColorSlot1) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_COLOR_SLOT_1; + + touchState.lastTouchInsideColorSlot = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.colorSlotHoldState, now ); + } + else if (hit.insideColorSlot2) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_COLOR_SLOT_2; + + touchState.lastTouchInsideColorSlot = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.colorSlotHoldState, now ); + } + else if (hit.insideColorSlot3) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_COLOR_SLOT_3; + + touchState.lastTouchInsideColorSlot = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.colorSlotHoldState, now ); + } + else if (hit.insideHueDown) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_HUE_DOWN; + + touchState.lastTouchInsideHue = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.hueRepeatState, now ); + + beginHueEdit(); + } + else if (hit.insideHueUp) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_HUE_UP; + + touchState.lastTouchInsideHue = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.hueRepeatState, now ); + + beginHueEdit(); + } + else if (hit.insideSaturationDown) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_SATURATION_DOWN; + + touchState.lastTouchInsideSaturation = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.saturationRepeatState, now ); + + beginSaturationEdit(); + } + else if (hit.insideSaturationUp) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_SATURATION_UP; + + touchState.lastTouchInsideSaturation = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.saturationRepeatState, now ); + + beginSaturationEdit(); + } + } + + else if ( currentPage == SCREEN_EFFECT ) { + if (hit.insideBack) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_BACK; + + touchState.lastTouchInsideBack = true; + } + else if (hit.insideSpeedDown) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_SPEED_DOWN; + + touchState.lastTouchInsideSpeed = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.speedRepeatState, now ); + } + else if (hit.insideSpeedUp) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_SPEED_UP; + + touchState.lastTouchInsideSpeed = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.speedRepeatState, now ); + } + else if (hit.insideIntensityDown) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_INTENSITY_DOWN; + + touchState.lastTouchInsideIntensity = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.intensityRepeatState, now ); + } + else if (hit.insideIntensityUp) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_INTENSITY_UP; + + touchState.lastTouchInsideIntensity = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.intensityRepeatState, now ); + } + else if (hit.insidePalettePrev) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PALETTE_PREV; + + touchState.lastTouchInsidePalette = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.paletteRepeatState, now ); + } + else if (hit.insidePaletteNext) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PALETTE_NEXT; + + touchState.lastTouchInsidePalette = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.paletteRepeatState, now ); + } + } + + else if ( currentPage == SCREEN_PRESET ) { + if (hit.insideBack) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_BACK; + + touchState.lastTouchInsideBack = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_NAV && hit.insidePresetPrev ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_PREV; + + touchState.lastTouchInsidePresetNav = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.presetRepeatState, now ); + } + else if ( presetSubPage == PRESET_SUBPAGE_NAV && hit.insidePresetNext ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_NEXT; + + touchState.lastTouchInsidePresetNav = true; + + M5StackDisplayTouchHelpers::beginRepeatTouch( touchState.presetRepeatState, now ); + } + else if ( presetSubPage == PRESET_SUBPAGE_NAV && hit.insidePresetManage ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_MANAGE; + + touchState.lastTouchInsidePresetManage = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_MANAGE && hit.insidePresetSaveNew ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_SAVE_NEW; + + touchState.lastTouchInsidePresetSaveNew = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_MANAGE && hit.insidePresetOverwriteOpen ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_OPEN; + + touchState.lastTouchInsidePresetOverwriteOpen = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_MANAGE && hit.insidePresetDeleteOpen ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN; + + touchState.lastTouchInsidePresetDeleteOpen = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_MANAGE && hit.insidePresetBootOpen ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN; + + touchState.lastTouchInsidePresetBootOpen = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_SAVE && hit.insidePresetSaveHold ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_SAVE_HOLD; + + touchState.lastTouchInsidePresetSaveHold = true; + + presetSaveHoldStartTime = now; + + presetSaveHoldTriggered = false; + } + else if ( presetSubPage == PRESET_SUBPAGE_OVERWRITE && hit.insidePresetOverwritePrev ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV; + + touchState.lastTouchInsidePresetOverwriteNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_OVERWRITE && hit.insidePresetOverwriteNext ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT; + + touchState.lastTouchInsidePresetOverwriteNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_OVERWRITE && hit.insidePresetOverwriteHold ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_HOLD; + + touchState.lastTouchInsidePresetOverwriteHold = true; + + presetSaveHoldStartTime = now; + + presetSaveHoldTriggered = false; + } + else if ( presetSubPage == PRESET_SUBPAGE_DELETE && hit.insidePresetDeletePrev ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV; + + touchState.lastTouchInsidePresetDeleteNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_DELETE && hit.insidePresetDeleteNext ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT; + + touchState.lastTouchInsidePresetDeleteNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_DELETE && hit.insidePresetDeleteHold ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_DELETE_HOLD; + + touchState.lastTouchInsidePresetDeleteHold = true; + + presetSaveHoldStartTime = now; + + presetSaveHoldTriggered = false; + } + else if ( presetSubPage == PRESET_SUBPAGE_BOOT && hit.insidePresetBootPrev ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV; + + touchState.lastTouchInsidePresetBootNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_BOOT && hit.insidePresetBootNext ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT; + + touchState.lastTouchInsidePresetBootNav = true; + } + else if ( presetSubPage == PRESET_SUBPAGE_BOOT && hit.insidePresetBootHold ) { + touchState.touchTarget = M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD; + + touchState.lastTouchInsidePresetBootHold = true; + + presetSaveHoldStartTime = now; + + presetSaveHoldTriggered = false; + } + } + } + + } + + // ========================================================= + // Touch Hold + // + // Preserves pressed visuals, long-press and repeat behavior. + // ========================================================= + + void handleTouchHold( const M5StackTouchFrameContext& context ) { + M5StackTouchRuntimeState& touchState = context.state; + const M5StackTouchHitState& hit = context.hit; + const unsigned long now = context.now; + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_POWER ) { + touchState.lastTouchInsidePower = hit.insidePower; + + if ( hit.insidePower != touchState.powerButtonVisualPressed ) { + drawPowerButton( bri > 0, hit.insidePower ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_WIFI_RECOVERY ) { + touchState.lastTouchInsideWiFiRecovery = hit.insideWiFiRecovery; + + if ( !hit.insideWiFiRecovery ) { + // Leaving the header area cancels this hold completely. Re-entering + // during the same gesture must not trigger Recovery AP immediately. + touchState.wifiRecoveryHoldState.pressStart = 0; + + if ( touchState.wifiRecoveryVisualPressed ) { + touchState.wifiRecoveryVisualPressed = false; + + drawMainNetworkStatusLine( + getCurrentNetworkDisplayText() + ); + } + + return; + } + + if ( + !touchState.wifiRecoveryHoldState.longPressActive && + touchState.wifiRecoveryHoldState.pressStart > 0 && + now - touchState.wifiRecoveryHoldState.pressStart >= WIFI_RECOVERY_HOLD_MS + ) { + touchState.wifiRecoveryHoldState.longPressActive = true; + + drawMainNetworkStatusLine( + "Starting Recovery AP...", + TFT_YELLOW + ); + + if ( startWiFiRecoveryAP() ) { + NetworkAccessMode recoveryMode = getNetworkAccessMode(); + String recoveryText = getNetworkDisplayText( recoveryMode ); + + lastNetworkAccessMode = recoveryMode; + lastNetworkDisplayText = recoveryText; + + touchState.wifiRecoveryVisualPressed = false; + + drawMainNetworkStatusLine( + recoveryText, + TFT_GREEN + ); + } + else { + // Keep the visual marked active so release restores the normal + // offline hint after a failed start attempt. + drawMainNetworkStatusLine( + "Recovery AP failed", + TFT_RED + ); + } + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN, M5STACK_TOUCH_TARGET_BRIGHTNESS_UP ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN, hit.insideBrightnessDown, M5STACK_TOUCH_TARGET_BRIGHTNESS_UP, hit.insideBrightnessUp ); + + touchState.lastTouchInsideBrightness = insideSelectedButton; + + if ( insideSelectedButton != touchState.brightnessButtonVisualPressed ) { + drawBrightness( bri, insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.brightnessButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.brightnessRepeatState, now, BRI_LONG_PRESS_MS, BRI_REPEAT_MS ) ) { + brightnessLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_EFFECT_PREV, M5STACK_TOUCH_TARGET_EFFECT_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_EFFECT_PREV, hit.insideEffectPrev, M5STACK_TOUCH_TARGET_EFFECT_NEXT, hit.insideEffectNext ); + + touchState.lastTouchInsideEffect = insideSelectedButton; + + if ( insideSelectedButton != touchState.effectButtonVisualPressed ) { + drawEffect( getCurrentEffectMode(), insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.effectButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.effectRepeatState, now, EFFECT_LONG_PRESS_MS, EFFECT_REPEAT_MS ) ) { + effectLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_EFFECT_DETAIL ) { + bool insideSelectedButton = hit.insideEffectDetail; + + touchState.lastTouchInsideEffectDetail = insideSelectedButton; + + if ( insideSelectedButton != touchState.effectDetailVisualPressed ) { + drawEffectDetailButton( getCurrentEffectMode(), insideSelectedButton ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_OPEN ) { + touchState.lastTouchInsideColor = hit.insideColor; + + if ( hit.insideColor != touchState.colorButtonVisualPressed ) { + drawColorButton( getPrimaryColor(), hit.insideColor ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_OPEN ) { + touchState.lastTouchInsidePresetOpen = hit.insidePresetOpen; + + if ( hit.insidePresetOpen != touchState.presetOpenButtonVisualPressed ) { + drawPresetOpenButton( hit.insidePresetOpen ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_BACK ) { + touchState.lastTouchInsideBack = hit.insideBack; + + if ( hit.insideBack != touchState.backButtonVisualPressed ) { + drawBackButton( hit.insideBack ); + } + + return; + } + + if ( + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_1 || + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_2 || + touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_3 + ) { + bool insideSelectedSlot = false; + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_1 ) { + insideSelectedSlot = hit.insideColorSlot1; + } + else if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_2 ) { + insideSelectedSlot = hit.insideColorSlot2; + } + else { + insideSelectedSlot = hit.insideColorSlot3; + } + + touchState.lastTouchInsideColorSlot = insideSelectedSlot; + + if (!insideSelectedSlot) { + return; + } + + if ( + !touchState.colorSlotHoldState.longPressActive && + touchState.colorSlotHoldState.pressStart > 0 && + now - touchState.colorSlotHoldState.pressStart >= COLOR_SLOT_LONG_PRESS_MS + ) { + touchState.colorSlotHoldState.longPressActive = true; + + uint8_t colorSlot = 0; + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_2 ) { + colorSlot = 1; + } + else if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_3 ) { + colorSlot = 2; + } + + toggleColorSlotBlack( colorSlot ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_HUE_DOWN, M5STACK_TOUCH_TARGET_HUE_UP ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_HUE_DOWN, hit.insideHueDown, M5STACK_TOUCH_TARGET_HUE_UP, hit.insideHueUp ); + + touchState.lastTouchInsideHue = insideSelectedButton; + + if ( insideSelectedButton != touchState.hueButtonVisualPressed ) { + drawHue( logicalHueValue, insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.hueButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.hueRepeatState, now, HUE_LONG_PRESS_MS, HUE_REPEAT_MS ) ) { + hueLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_SATURATION_DOWN, M5STACK_TOUCH_TARGET_SATURATION_UP ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_SATURATION_DOWN, hit.insideSaturationDown, M5STACK_TOUCH_TARGET_SATURATION_UP, hit.insideSaturationUp ); + + touchState.lastTouchInsideSaturation = insideSelectedButton; + + if ( insideSelectedButton != touchState.saturationButtonVisualPressed ) { + drawSaturation( logicalSaturationValue, insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.saturationButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.saturationRepeatState, now, SATURATION_LONG_PRESS_MS, SATURATION_REPEAT_MS ) ) { + saturationLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_SPEED_DOWN, M5STACK_TOUCH_TARGET_SPEED_UP ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_SPEED_DOWN, hit.insideSpeedDown, M5STACK_TOUCH_TARGET_SPEED_UP, hit.insideSpeedUp ); + + touchState.lastTouchInsideSpeed = insideSelectedButton; + + if ( insideSelectedButton != touchState.speedButtonVisualPressed ) { + drawSpeed( getCurrentSpeed(), insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.speedButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.speedRepeatState, now, SPEED_LONG_PRESS_MS, SPEED_REPEAT_MS ) ) { + speedLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_INTENSITY_DOWN, M5STACK_TOUCH_TARGET_INTENSITY_UP ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_INTENSITY_DOWN, hit.insideIntensityDown, M5STACK_TOUCH_TARGET_INTENSITY_UP, hit.insideIntensityUp ); + + touchState.lastTouchInsideIntensity = insideSelectedButton; + + if ( insideSelectedButton != touchState.intensityButtonVisualPressed ) { + drawIntensity( getCurrentIntensity(), insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.intensityButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.intensityRepeatState, now, INTENSITY_LONG_PRESS_MS, INTENSITY_REPEAT_MS ) ) { + intensityLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_PALETTE_PREV, M5STACK_TOUCH_TARGET_PALETTE_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_PALETTE_PREV, hit.insidePalettePrev, M5STACK_TOUCH_TARGET_PALETTE_NEXT, hit.insidePaletteNext ); + + touchState.lastTouchInsidePalette = insideSelectedButton; + + if ( insideSelectedButton != touchState.paletteButtonVisualPressed ) { + drawPalette( getCurrentPalette(), insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.paletteButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.paletteRepeatState, now, PALETTE_LONG_PRESS_MS, PALETTE_REPEAT_MS ) ) { + paletteLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_PRESET_PREV, M5STACK_TOUCH_TARGET_PRESET_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_PRESET_PREV, hit.insidePresetPrev, M5STACK_TOUCH_TARGET_PRESET_NEXT, hit.insidePresetNext ); + + touchState.lastTouchInsidePresetNav = insideSelectedButton; + + if ( insideSelectedButton != touchState.presetNavButtonVisualPressed ) { + drawPresetNavigation( insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.presetNavButtonVisualPressed = insideSelectedButton; + } + + if (!insideSelectedButton) { + return; + } + + if ( M5StackDisplayTouchHelpers::serviceRepeatTouch( touchState.presetRepeatState, now, PRESET_LONG_PRESS_MS, PRESET_REPEAT_MS ) ) { + presetLongPressStep( touchState.touchTarget ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_MANAGE ) { + touchState.lastTouchInsidePresetManage = hit.insidePresetManage; + + if ( hit.insidePresetManage != touchState.presetManageButtonVisualPressed ) { + drawPresetManageButton( hit.insidePresetManage ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_SAVE_NEW ) { + touchState.lastTouchInsidePresetSaveNew = hit.insidePresetSaveNew; + + if ( hit.insidePresetSaveNew != touchState.presetSaveNewButtonVisualPressed ) { + drawPresetSaveNewButton( hit.insidePresetSaveNew ); + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_SAVE_HOLD ) { + touchState.lastTouchInsidePresetSaveHold = hit.insidePresetSaveHold; + + if ( hit.insidePresetSaveHold != touchState.presetSaveHoldButtonVisualPressed ) { + drawPresetSaveHoldButton( hit.insidePresetSaveHold ); + } + + if (!hit.insidePresetSaveHold) { + presetSaveHoldStartTime = 0; + + return; + } + + if ( presetSaveHoldStartTime == 0 ) { + presetSaveHoldStartTime = now; + } + + if ( !presetSaveHoldTriggered && now - presetSaveHoldStartTime >= PRESET_SAVE_HOLD_MS ) { + presetSaveHoldTriggered = true; + + requestNewPresetSave(); + + return; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_OPEN ) { + touchState.lastTouchInsidePresetOverwriteOpen = hit.insidePresetOverwriteOpen; + + if ( hit.insidePresetOverwriteOpen != touchState.presetOverwriteOpenButtonVisualPressed ) { + drawPresetOverwriteOpenButton( hit.insidePresetOverwriteOpen ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV, hit.insidePresetOverwritePrev, M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT, hit.insidePresetOverwriteNext ); + + touchState.lastTouchInsidePresetOverwriteNav = insideSelectedButton; + + if ( insideSelectedButton != touchState.presetOverwriteNavButtonVisualPressed ) { + drawPresetOverwriteNavigation( insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.presetOverwriteNavButtonVisualPressed = insideSelectedButton; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_HOLD ) { + touchState.lastTouchInsidePresetOverwriteHold = hit.insidePresetOverwriteHold; + + if ( hit.insidePresetOverwriteHold != touchState.presetOverwriteHoldButtonVisualPressed ) { + drawPresetOverwriteHoldButton( hit.insidePresetOverwriteHold ); + } + + if (!hit.insidePresetOverwriteHold) { + presetSaveHoldStartTime = 0; + + return; + } + + if ( presetSaveHoldStartTime == 0 ) { + presetSaveHoldStartTime = now; + } + + if ( !presetSaveHoldTriggered && now - presetSaveHoldStartTime >= PRESET_SAVE_HOLD_MS ) { + presetSaveHoldTriggered = true; + + requestPresetOverwrite(); + + return; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN ) { + touchState.lastTouchInsidePresetDeleteOpen = hit.insidePresetDeleteOpen; + + if ( hit.insidePresetDeleteOpen != touchState.presetDeleteOpenButtonVisualPressed ) { + drawPresetDeleteOpenButton( hit.insidePresetDeleteOpen ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV, M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV, hit.insidePresetDeletePrev, M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT, hit.insidePresetDeleteNext ); + + touchState.lastTouchInsidePresetDeleteNav = insideSelectedButton; + + if ( insideSelectedButton != touchState.presetDeleteNavButtonVisualPressed ) { + drawPresetDeleteNavigation( insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.presetDeleteNavButtonVisualPressed = insideSelectedButton; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_HOLD ) { + touchState.lastTouchInsidePresetDeleteHold = hit.insidePresetDeleteHold; + + if ( hit.insidePresetDeleteHold != touchState.presetDeleteHoldButtonVisualPressed ) { + drawPresetDeleteHoldButton( hit.insidePresetDeleteHold ); + } + + if (!hit.insidePresetDeleteHold) { + presetSaveHoldStartTime = 0; + + return; + } + + if ( presetSaveHoldStartTime == 0 ) { + presetSaveHoldStartTime = now; + } + + if ( !presetSaveHoldTriggered && now - presetSaveHoldStartTime >= PRESET_SAVE_HOLD_MS ) { + presetSaveHoldTriggered = true; + + requestPresetDelete(); + + return; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN ) { + touchState.lastTouchInsidePresetBootOpen = hit.insidePresetBootOpen; + + if ( hit.insidePresetBootOpen != touchState.presetBootOpenButtonVisualPressed ) { + drawPresetBootOpenButton( hit.insidePresetBootOpen ); + } + + return; + } + + if ( M5StackDisplayTouchHelpers::isTouchTargetPair( touchState.touchTarget, M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV, M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT ) ) { + bool insideSelectedButton = isSelectedTouchPairInside( M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV, hit.insidePresetBootPrev, M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT, hit.insidePresetBootNext ); + + touchState.lastTouchInsidePresetBootNav = insideSelectedButton; + + if ( insideSelectedButton != touchState.presetBootNavButtonVisualPressed ) { + drawPresetBootNavigation( insideSelectedButton ? touchState.touchTarget : M5STACK_TOUCH_TARGET_NONE ); + + touchState.presetBootNavButtonVisualPressed = insideSelectedButton; + } + + return; + } + + if ( touchState.touchTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD ) { + touchState.lastTouchInsidePresetBootHold = hit.insidePresetBootHold; + + if ( hit.insidePresetBootHold != touchState.presetBootHoldButtonVisualPressed ) { + drawPresetBootHoldButton( hit.insidePresetBootHold ); + } + + if (!hit.insidePresetBootHold) { + presetSaveHoldStartTime = 0; + + return; + } + + if ( presetSaveHoldStartTime == 0 ) { + presetSaveHoldStartTime = now; + } + + if ( !presetSaveHoldTriggered && now - presetSaveHoldStartTime >= PRESET_SAVE_HOLD_MS ) { + presetSaveHoldTriggered = true; + + requestPresetBootSetting(); + + return; + } + + return; + } + + return; + } + + // ========================================================= + // Touch Release action state + // + // Only one M5StackTouchTarget can own a gesture, so release-time + // execution is represented by one action instead of parallel + // boolean flags. + // ========================================================= + + enum TouchReleaseAction : uint8_t { + TOUCH_RELEASE_ACTION_NONE = 0, + TOUCH_RELEASE_ACTION_POWER, + TOUCH_RELEASE_ACTION_BRIGHTNESS_SHORT, + TOUCH_RELEASE_ACTION_EFFECT_STEP, + TOUCH_RELEASE_ACTION_EFFECT_DETAIL, + TOUCH_RELEASE_ACTION_COLOR_OPEN, + TOUCH_RELEASE_ACTION_COLOR_SLOT_SELECT, + TOUCH_RELEASE_ACTION_PRESET_OPEN, + TOUCH_RELEASE_ACTION_BACK, + TOUCH_RELEASE_ACTION_HUE_SHORT, + TOUCH_RELEASE_ACTION_SATURATION_SHORT, + TOUCH_RELEASE_ACTION_SPEED_SHORT, + TOUCH_RELEASE_ACTION_INTENSITY_SHORT, + TOUCH_RELEASE_ACTION_PALETTE_SHORT, + TOUCH_RELEASE_ACTION_PRESET_SHORT, + TOUCH_RELEASE_ACTION_PRESET_MANAGE_OPEN, + TOUCH_RELEASE_ACTION_PRESET_SAVE_NEW, + TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_OPEN, + TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_STEP, + TOUCH_RELEASE_ACTION_PRESET_DELETE_OPEN, + TOUCH_RELEASE_ACTION_PRESET_DELETE_STEP, + TOUCH_RELEASE_ACTION_PRESET_BOOT_OPEN, + TOUCH_RELEASE_ACTION_PRESET_BOOT_STEP + }; + + struct ColorEditSnapshot { + bool hueValid; + CHSV32 hueHsv; + uint8_t hueValue; + uint8_t hueWhite; + + bool saturationValid; + CHSV32 saturationHsv; + uint8_t saturationValue; + uint8_t saturationWhite; + }; + + TouchReleaseAction determineTouchReleaseAction( M5StackTouchTarget releasedTarget, unsigned long now ) { + switch (releasedTarget) { + case M5STACK_TOUCH_TARGET_POWER: + return ( touchState.lastTouchInsidePower && now - touchState.lastTouchAction >= TOUCH_ACTION_COOLDOWN_MS ) ? TOUCH_RELEASE_ACTION_POWER : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_WIFI_RECOVERY: + // Recovery AP is a hold-only gesture. Short release intentionally + // performs no action. + return TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN: + case M5STACK_TOUCH_TARGET_BRIGHTNESS_UP: + return ( touchState.lastTouchInsideBrightness && !touchState.brightnessRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_BRIGHTNESS_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_EFFECT_PREV: + case M5STACK_TOUCH_TARGET_EFFECT_NEXT: + return ( touchState.lastTouchInsideEffect && !touchState.effectRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_EFFECT_STEP : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_EFFECT_DETAIL: + return touchState.lastTouchInsideEffectDetail ? TOUCH_RELEASE_ACTION_EFFECT_DETAIL : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_COLOR_OPEN: + return touchState.lastTouchInsideColor ? TOUCH_RELEASE_ACTION_COLOR_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_COLOR_SLOT_1: + case M5STACK_TOUCH_TARGET_COLOR_SLOT_2: + case M5STACK_TOUCH_TARGET_COLOR_SLOT_3: + return ( + touchState.lastTouchInsideColorSlot && + !touchState.colorSlotHoldState.longPressActive + ) + ? TOUCH_RELEASE_ACTION_COLOR_SLOT_SELECT + : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_OPEN: + return touchState.lastTouchInsidePresetOpen ? TOUCH_RELEASE_ACTION_PRESET_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_BACK: + return touchState.lastTouchInsideBack ? TOUCH_RELEASE_ACTION_BACK : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_HUE_DOWN: + case M5STACK_TOUCH_TARGET_HUE_UP: + return ( touchState.lastTouchInsideHue && !touchState.hueRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_HUE_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_SATURATION_DOWN: + case M5STACK_TOUCH_TARGET_SATURATION_UP: + return ( touchState.lastTouchInsideSaturation && !touchState.saturationRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_SATURATION_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_SPEED_DOWN: + case M5STACK_TOUCH_TARGET_SPEED_UP: + return ( touchState.lastTouchInsideSpeed && !touchState.speedRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_SPEED_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_INTENSITY_DOWN: + case M5STACK_TOUCH_TARGET_INTENSITY_UP: + return ( touchState.lastTouchInsideIntensity && !touchState.intensityRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_INTENSITY_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PALETTE_PREV: + case M5STACK_TOUCH_TARGET_PALETTE_NEXT: + return ( touchState.lastTouchInsidePalette && !touchState.paletteRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_PALETTE_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_PREV: + case M5STACK_TOUCH_TARGET_PRESET_NEXT: + return ( touchState.lastTouchInsidePresetNav && !touchState.presetRepeatState.longPressActive ) ? TOUCH_RELEASE_ACTION_PRESET_SHORT : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_MANAGE: + return touchState.lastTouchInsidePresetManage ? TOUCH_RELEASE_ACTION_PRESET_MANAGE_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_SAVE_NEW: + return touchState.lastTouchInsidePresetSaveNew ? TOUCH_RELEASE_ACTION_PRESET_SAVE_NEW : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_OPEN: + return touchState.lastTouchInsidePresetOverwriteOpen ? TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV: + case M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT: + return touchState.lastTouchInsidePresetOverwriteNav ? TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_STEP : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN: + return touchState.lastTouchInsidePresetDeleteOpen ? TOUCH_RELEASE_ACTION_PRESET_DELETE_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV: + case M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT: + return touchState.lastTouchInsidePresetDeleteNav ? TOUCH_RELEASE_ACTION_PRESET_DELETE_STEP : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN: + return touchState.lastTouchInsidePresetBootOpen ? TOUCH_RELEASE_ACTION_PRESET_BOOT_OPEN : TOUCH_RELEASE_ACTION_NONE; + + case M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV: + case M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT: + return touchState.lastTouchInsidePresetBootNav ? TOUCH_RELEASE_ACTION_PRESET_BOOT_STEP : TOUCH_RELEASE_ACTION_NONE; + + default: + return TOUCH_RELEASE_ACTION_NONE; + } + } + + void releaseTouchVisualState( M5StackTouchTarget releasedTarget ) { + if ( + releasedTarget == M5STACK_TOUCH_TARGET_WIFI_RECOVERY && + touchState.wifiRecoveryVisualPressed + ) { + drawMainNetworkStatusLine( + getCurrentNetworkDisplayText() + ); + + touchState.wifiRecoveryVisualPressed = false; + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_POWER && touchState.powerButtonVisualPressed ) { + drawPowerButton( bri > 0, false ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_BRIGHTNESS_DOWN || releasedTarget == M5STACK_TOUCH_TARGET_BRIGHTNESS_UP ) && touchState.brightnessButtonVisualPressed ) { + drawBrightness( bri, M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_EFFECT_PREV || releasedTarget == M5STACK_TOUCH_TARGET_EFFECT_NEXT ) && touchState.effectButtonVisualPressed ) { + drawEffect( getCurrentEffectMode(), M5STACK_TOUCH_TARGET_NONE ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_EFFECT_DETAIL && touchState.effectDetailVisualPressed ) { + drawEffectDetailButton( getCurrentEffectMode(), false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_COLOR_OPEN && touchState.colorButtonVisualPressed ) { + drawColorButton( getPrimaryColor(), false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OPEN && touchState.presetOpenButtonVisualPressed ) { + drawPresetOpenButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_BACK && touchState.backButtonVisualPressed ) { + drawBackButton( false ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_HUE_DOWN || releasedTarget == M5STACK_TOUCH_TARGET_HUE_UP ) && touchState.hueButtonVisualPressed ) { + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_SATURATION_DOWN || releasedTarget == M5STACK_TOUCH_TARGET_SATURATION_UP ) && touchState.saturationButtonVisualPressed ) { + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_SPEED_DOWN || releasedTarget == M5STACK_TOUCH_TARGET_SPEED_UP ) && touchState.speedButtonVisualPressed ) { + drawSpeed( getCurrentSpeed(), M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_INTENSITY_DOWN || releasedTarget == M5STACK_TOUCH_TARGET_INTENSITY_UP ) && touchState.intensityButtonVisualPressed ) { + drawIntensity( getCurrentIntensity(), M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_PALETTE_PREV || releasedTarget == M5STACK_TOUCH_TARGET_PALETTE_NEXT ) && touchState.paletteButtonVisualPressed ) { + drawPalette( getCurrentPalette(), M5STACK_TOUCH_TARGET_NONE ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_PREV || releasedTarget == M5STACK_TOUCH_TARGET_PRESET_NEXT ) && touchState.presetNavButtonVisualPressed ) { + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_MANAGE && touchState.presetManageButtonVisualPressed ) { + drawPresetManageButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_SAVE_NEW && touchState.presetSaveNewButtonVisualPressed ) { + drawPresetSaveNewButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_SAVE_HOLD && touchState.presetSaveHoldButtonVisualPressed ) { + drawPresetSaveHoldButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_OPEN && touchState.presetOverwriteOpenButtonVisualPressed ) { + drawPresetOverwriteOpenButton( false ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV || releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_NEXT ) && touchState.presetOverwriteNavButtonVisualPressed ) { + drawPresetOverwriteNavigation( M5STACK_TOUCH_TARGET_NONE ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_HOLD && touchState.presetOverwriteHoldButtonVisualPressed ) { + drawPresetOverwriteHoldButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN && touchState.presetDeleteOpenButtonVisualPressed ) { + drawPresetDeleteOpenButton( false ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV || releasedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT ) && touchState.presetDeleteNavButtonVisualPressed ) { + drawPresetDeleteNavigation( M5STACK_TOUCH_TARGET_NONE ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_HOLD && touchState.presetDeleteHoldButtonVisualPressed ) { + drawPresetDeleteHoldButton( false ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN && touchState.presetBootOpenButtonVisualPressed ) { + drawPresetBootOpenButton( false ); + } + + if ( ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV || releasedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT ) && touchState.presetBootNavButtonVisualPressed ) { + drawPresetBootNavigation( M5STACK_TOUCH_TARGET_NONE ); + } + + if ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD && touchState.presetBootHoldButtonVisualPressed ) { + drawPresetBootHoldButton( false ); + } + } + + void clearColorEditState() { + hueEditValid = false; + saturationEditValid = false; + } + + void executeBackReleaseAction() { + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_BOOT ) { + presetBootOperationState = PRESET_BOOT_OP_IDLE; + presetBootTargetId = 0; + presetBootTargetName = "NONE"; + presetBootResultStartMs = 0; + + drawPresetManageScreen(); + + return; + } + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_DELETE ) { + presetDeleteOperationState = PRESET_DELETE_OP_IDLE; + + presetDeleteTargetId = 0; + + presetDeleteTargetName = ""; + + presetDeleteWasCurrentPreset = false; + presetDeleteWasBootPreset = false; + + presetDeleteResultStartMs = 0; + + drawPresetManageScreen(); + + return; + } + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_OVERWRITE ) { + presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + presetSaveOperationIsOverwrite = false; + + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + presetOverwriteTargetId = 0; + + presetOverwriteTargetName = ""; + + drawPresetManageScreen(); + + return; + } + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_SAVE ) { + presetSaveOperationState = PRESET_SAVE_OP_IDLE; + + presetSaveCandidateId = 0; + + presetSaveCandidateName = ""; + + drawPresetManageScreen(); + + return; + } + + if ( currentPage == SCREEN_PRESET && presetSubPage == PRESET_SUBPAGE_MANAGE ) { + drawPresetScreen(); + + return; + } + + drawMainScreen( getCurrentNetworkDisplayText() ); + } + + void executeTouchReleaseAction( TouchReleaseAction action, M5StackTouchTarget releasedTarget, unsigned long now ) { + switch (action) { + case TOUCH_RELEASE_ACTION_POWER: + touchState.lastTouchAction = now; + + toggleLedPowerFromTouch(); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_BRIGHTNESS_SHORT: + brightnessShortPress( releasedTarget ); + + drawBrightness( bri, M5STACK_TOUCH_TARGET_NONE ); + + lastBrightnessValue = bri; + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_EFFECT_STEP: + if ( releasedTarget == M5STACK_TOUCH_TARGET_EFFECT_PREV ) { + applyEffectStep( -1 ); + } + else { + applyEffectStep( 1 ); + } + + drawEffect( getCurrentEffectMode(), M5STACK_TOUCH_TARGET_NONE ); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_EFFECT_DETAIL: + clearColorEditState(); + + drawEffectDetailScreen(); + return; + + case TOUCH_RELEASE_ACTION_COLOR_OPEN: + clearColorEditState(); + + drawColorScreen(); + return; + + case TOUCH_RELEASE_ACTION_COLOR_SLOT_SELECT: + clearColorEditState(); + + if ( releasedTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_1 ) { + selectColorSlot( 0 ); + } + else if ( releasedTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_2 ) { + selectColorSlot( 1 ); + } + else if ( releasedTarget == M5STACK_TOUCH_TARGET_COLOR_SLOT_3 ) { + selectColorSlot( 2 ); + } + + return; + + case TOUCH_RELEASE_ACTION_PRESET_OPEN: + clearColorEditState(); + + presetNoEntries = false; + + drawPresetScreen(); + return; + + case TOUCH_RELEASE_ACTION_PRESET_MANAGE_OPEN: + clearColorEditState(); + + drawPresetManageScreen(); + return; + + case TOUCH_RELEASE_ACTION_PRESET_SAVE_NEW: + clearColorEditState(); + + if ( preparePresetSaveCandidate() ) { + drawPresetSaveScreen(); + } + else { + drawPresetManageScreen(); + } + return; + + case TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_OPEN: + clearColorEditState(); + + if ( preparePresetOverwriteTarget() ) { + drawPresetOverwriteScreen(); + } + else { + drawPresetManageScreen(); + } + return; + + case TOUCH_RELEASE_ACTION_PRESET_OVERWRITE_STEP: { + int direction = ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_OVERWRITE_PREV ) ? -1 : 1; + + if ( stepPresetOverwriteTarget( direction ) ) { + drawPresetOverwriteScreen(); + } + + clearColorEditState(); + return; + } + + case TOUCH_RELEASE_ACTION_PRESET_DELETE_OPEN: + clearColorEditState(); + + if ( preparePresetDeleteTarget() ) { + drawPresetDeleteScreen(); + } + else { + drawPresetManageScreen(); + } + return; + + case TOUCH_RELEASE_ACTION_PRESET_DELETE_STEP: { + int direction = ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV ) ? -1 : 1; + + if ( stepPresetDeleteTarget( direction ) ) { + drawPresetDeleteScreen(); + } + + clearColorEditState(); + return; + } + + case TOUCH_RELEASE_ACTION_PRESET_BOOT_OPEN: + clearColorEditState(); + + if ( preparePresetBootTarget() ) { + drawPresetBootScreen(); + } + else { + drawPresetManageScreen(); + } + return; + + case TOUCH_RELEASE_ACTION_PRESET_BOOT_STEP: { + int direction = ( releasedTarget == M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV ) ? -1 : 1; + + if ( stepPresetBootTarget( direction ) ) { + drawPresetBootScreen(); + } + + clearColorEditState(); + return; + } + + case TOUCH_RELEASE_ACTION_BACK: + clearColorEditState(); + + executeBackReleaseAction(); + return; + + case TOUCH_RELEASE_ACTION_HUE_SHORT: + hueShortPress( releasedTarget ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_SATURATION_SHORT: + saturationShortPress( releasedTarget ); + + drawHue( logicalHueValue, M5STACK_TOUCH_TARGET_NONE ); + + drawSaturation( logicalSaturationValue, M5STACK_TOUCH_TARGET_NONE ); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_SPEED_SHORT: + speedShortPress( releasedTarget ); + + drawSpeed( getCurrentSpeed(), M5STACK_TOUCH_TARGET_NONE ); + + lastSpeedValue = getCurrentSpeed(); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_INTENSITY_SHORT: + intensityShortPress( releasedTarget ); + + drawIntensity( getCurrentIntensity(), M5STACK_TOUCH_TARGET_NONE ); + + lastIntensityValue = getCurrentIntensity(); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_PALETTE_SHORT: + paletteShortPress( releasedTarget ); + + drawPalette( getCurrentPalette(), M5STACK_TOUCH_TARGET_NONE ); + + lastPaletteValue = getCurrentPalette(); + + clearColorEditState(); + return; + + case TOUCH_RELEASE_ACTION_PRESET_SHORT: + presetShortPress( releasedTarget ); + + drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); + + clearColorEditState(); + return; + + default: + clearColorEditState(); + return; + } + } + + // ========================================================= + // Touch Release + // + // Release confirmation is unchanged. Action selection, + // pressed-visual release, gesture reset, and action execution + // are now separated into focused helpers. + // ========================================================= + + void handleTouchRelease( const M5StackTouchReleaseContext& context ) { + M5StackTouchRuntimeState& touchState = context.state; + const unsigned long now = context.now; + + if (!touchState.touchActive) { + return; + } + + if ( touchState.touchReleaseCandidate == 0 ) { + touchState.touchReleaseCandidate = now; + + return; + } + + if ( now - touchState.touchReleaseCandidate < TOUCH_RELEASE_CONFIRM_MS ) { + return; + } + + M5StackTouchTarget releasedTarget = touchState.touchTarget; + + TouchReleaseAction releaseAction = determineTouchReleaseAction( releasedTarget, now ); + + releaseTouchVisualState( releasedTarget ); + + ColorEditSnapshot editSnapshot = { + hueEditValid, hueEditHsv, hueEditValue, hueEditWhite, + saturationEditValid, saturationEditHsv, saturationEditValue, saturationEditWhite + }; + + resetTouchGesture(); + + hueEditValid = editSnapshot.hueValid; + hueEditHsv = editSnapshot.hueHsv; + hueEditValue = editSnapshot.hueValue; + hueEditWhite = editSnapshot.hueWhite; + + saturationEditValid = editSnapshot.saturationValid; + saturationEditHsv = editSnapshot.saturationHsv; + saturationEditValue = editSnapshot.saturationValue; + saturationEditWhite = editSnapshot.saturationWhite; + + executeTouchReleaseAction( releaseAction, releasedTarget, now ); + } + + // ========================================================= + // Touch dispatcher + // + // Poll touch once, then route the same state through + // Press / Hold / Release helpers. + // ========================================================= + + void handleTouch() { + if (!touchReady) { + return; + } + + if (!readyScreenShown) { + return; + } + + if ( isPresetSaveBusy() || isPresetDeleteBusy() || isPresetBootBusy() ) { + return; + } + + unsigned long now = millis(); + + if ( now - touchState.lastTouchPoll < TOUCH_POLL_MS ) { + return; + } + + touchState.lastTouchPoll = now; + + int16_t touchX = -1; + + int16_t touchY = -1; + + bool touching = readDisplayTouch( touchX, touchY ); + + if (touching) { + + lastUserActivityMs = now; + + touchState.touchReleaseCandidate = 0; + + touchState.lastTouchX = touchX; + + touchState.lastTouchY = touchY; + + M5StackTouchHitState hit = buildTouchHitState( touchX, touchY ); + + M5StackTouchFrameContext touchContext = { + touchState, + hit, + now + }; + + handleTouchPress( touchContext ); + + handleTouchHold( touchContext ); + + return; + } + + M5StackTouchReleaseContext releaseContext = { + touchState, + now + }; + + handleTouchRelease( releaseContext ); + } diff --git a/usermods/CoreS3_Display/M5StackDisplayUI.h b/usermods/CoreS3_Display/M5StackDisplayUI.h new file mode 100644 index 0000000000..e5e125ddf0 --- /dev/null +++ b/usermods/CoreS3_Display/M5StackDisplayUI.h @@ -0,0 +1,68 @@ +#pragma once + +#include + +// Common M5Stack controller UI geometry. +// +// The 320 x 240 hit areas below are the hardware-verified CoreS3 +// baseline. Another M5Stack profile may reuse them only after +// real-hardware validation. + +// =========================================================== +// Shared 320 x 240 UI touch rectangles +// +// These hit areas are part of the common M5Stack controller UI. +// Their numeric values remain the hardware-verified CoreS3 baseline +// and can be reused by another profile only after hardware validation. +// =========================================================== + +struct M5StackTouchRect { + int16_t x; + int16_t y; + int16_t w; + int16_t h; +}; + +static constexpr M5StackTouchRect M5STACK_TOUCH_POWER = { 8, 8, 44, 44 }; +// MAIN header: Recovery AP hold applies only to the network-status area. +// The right-side battery indicator is display-only and intentionally has +// no touch target. +static constexpr M5StackTouchRect M5STACK_TOUCH_WIFI_RECOVERY = { 60, 28, 178, 28 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BRIGHTNESS_DOWN = { 16, 82, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BRIGHTNESS_UP = { 240, 82, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_EFFECT_PREV = { 16, 138, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_EFFECT_DETAIL = { 88, 138, 144, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_EFFECT_NEXT = { 240, 138, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_COLOR_OPEN = { 8, 180, 152, 60 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_OPEN = { 160, 180, 152, 60 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BACK = { 268, 8, 44, 44 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_COLOR_SLOT_1 = { 24, 62, 88, 54 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_COLOR_SLOT_2 = { 116, 62, 88, 54 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_COLOR_SLOT_3 = { 208, 62, 88, 54 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_HUE_DOWN = { 16, 151, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_HUE_UP = { 240, 151, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_SATURATION_DOWN = { 16, 204, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_SATURATION_UP = { 240, 204, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_SPEED_DOWN = { 16, 82, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_SPEED_UP = { 240, 82, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_INTENSITY_DOWN = { 16, 140, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_INTENSITY_UP = { 240, 140, 64, 34 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PALETTE_PREV = { 8, 188, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PALETTE_NEXT = { 232, 188, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_PREV = { 8, 188, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_NEXT = { 232, 188, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_MANAGE = { 88, 188, 144, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_SAVE_NEW = { 48, 60, 224, 42 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_PRESET_SAVE_HOLD = { 48, 170, 224, 66 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_OVERWRITE_OPEN = { 48, 102, 224, 42 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_OVERWRITE_PREV = { 8, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_OVERWRITE_NEXT = { 232, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_OVERWRITE_HOLD = { 48, 184, 224, 56 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_DELETE_OPEN = { 48, 144, 224, 42 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_DELETE_PREV = { 8, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_DELETE_NEXT = { 232, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_DELETE_HOLD = { 48, 184, 224, 56 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_OPEN = { 48, 186, 224, 48 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_PREV = { 8, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_NEXT = { 232, 116, 80, 52 }; +static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_HOLD = { 48, 184, 224, 56 }; diff --git a/usermods/CoreS3_Display/library.json b/usermods/CoreS3_Display/library.json new file mode 100644 index 0000000000..356e43e2a1 --- /dev/null +++ b/usermods/CoreS3_Display/library.json @@ -0,0 +1,11 @@ +{ + "name": "CoreS3_Display", + "version": "0.1.0", + "description": "M5Stack CoreS3 display usermod for WLED", + "build": { + "libArchive": false + }, + "dependencies": { + "M5GFX": "https://github.com/m5stack/M5GFX.git#0.2.26" + } +} diff --git a/usermods/CoreS3_Display/platformio_override.ini.example b/usermods/CoreS3_Display/platformio_override.ini.example new file mode 100644 index 0000000000..59f735c8af --- /dev/null +++ b/usermods/CoreS3_Display/platformio_override.ini.example @@ -0,0 +1,79 @@ +; ============================================================================= +; WLED - M5Stack CoreS3 +; Public build example for the CoreS3 production configuration +; +; Usage: +; Copy this file to the WLED repository root as: +; platformio_override.ini +; +; Validated runtime basis: +; WLED 17.0.0-devV5 / current main-derived tree +; pioarduino 55.03.39 +; Arduino Core 3.3.9 +; ESP-IDF 5.5.4 +; M5Stack CoreS3: 16MB Flash + 8MB QUAD PSRAM +; +; This example preserves: +; - the validated CoreS3 Quad PSRAM configuration +; - CoreS3 Power / Display / Audio / AudioReactive integration +; - the validated ESP32-S3 NeoPixelBus RMT DMA1024 and LCD/GDMA patches +; - watchdog-reset upload recovery for ESP32-S3 native USB +; +; IMPORTANT: +; Do NOT set lib_archive=yes in this custom environment. +; WLED main already contains [v5_pioarduino_workaround]. +; ============================================================================= + +[platformio] +default_envs = m5stack_cores3 + +[env:m5stack_cores3] + +; WLED's current 16MB ESP32-S3 parent environment. +extends = env:esp32s3dev_16MB_opi + +; CoreS3 uses 8MB QUAD PSRAM, not Octal PSRAM. +board_build.arduino.memory_type = qio_qspi +board_build.flash_mode = qio + +; Current WLED pioarduino / ESP-IDF 5 build family. +platform = ${esp32_idf_V5.platform_pioarduino} +platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} + +; Preserve normal WLED build scripts. +; +; PRE 1: +; Select watchdog reset for the post-upload ESP32-S3 reset sequence +; before PlatformIO constructs the esptool command. +; +; PRE 2: +; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024 and LCD/GDMA patches. +extra_scripts = + ${scripts_defaults.extra_scripts} + pre:pio-scripts/cores3_upload_watchdog_reset.py + pre:pio-scripts/cores3_v17_neopixelbus_patch.py + +custom_usermods = + CoreS3_Power + CoreS3_Display + CoreS3_Audio + audioreactive + +build_unflags = + ${env:esp32s3dev_16MB_opi.build_unflags} + +build_flags = + ${env:esp32s3dev_16MB_opi.build_flags} + + ; CoreS3 internal I2C bus. + -D I2CSDAPIN=12 + -D I2CSCLPIN=11 + + ; CoreS3 built-in ES7210 / AudioReactive integration. + -D WLED_M5STACK_CORES3_AUDIO=1 + -D UM_AUDIOREACTIVE_ENABLE + -D SR_DMTYPE=7 + + ; GPIO0 is ES7210 MCLK, not a WLED physical button. + -D BTNPIN=-1 + -D BTNTYPE=0 diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md new file mode 100644 index 0000000000..13b80cccc1 --- /dev/null +++ b/usermods/CoreS3_Display/readme.md @@ -0,0 +1,622 @@ +# WLED on M5Stack CoreS3 + +[日本語](readme_jp.md) + +This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
+**local touch control / built-in microphone Audio Reactive / battery status / safe power-off / LED control** in a single device. + +> **Status:** CoreS3 production baseline
+> **Base:** WLED 17.0.0-devV5
+> **Target:** M5Stack CoreS3 (ESP32-S3 / 16MB Flash / 8MB Quad PSRAM) +> +> This is a community-maintained CoreS3 port/extension of WLED and is not an official WLED or M5Stack firmware release. + +--- + +## Features + +- Native WLED runtime on M5Stack CoreS3 +- Local control with the 320 × 240 touch display +- Bidirectional synchronization between the WLED Web UI and CoreS3 UI +- LED Brightness / Effect / Speed / Intensity / Palette control +- Independent C1 / C2 / C3 multi-color editing +- Preset Call / SAVE / OVERWRITE / DELETE / BOOT PRESET +- Audio Reactive using the built-in ES7210 dual microphones +- Battery level display +- Display Sleep / Wake +- Wi-Fi Offline / Recovery AP UX +- Safe Shutdown using the AXP2101 Power Key +- LED BLACK frame before hard power-off +- Restore of the previous LED state when Safe Shutdown is canceled +- ESP32-S3 / NeoPixelBus RMT DMA1024 and LCD/GDMA runtime-rebuild stabilization +- Browser capture of the current LCD as a BMP image + +--- + +## Screenshots + +The following images were captured directly from the CoreS3 LCD using the Browser Screenshot feature. + +### Main + +

+ +

+ +The MAIN screen provides LED Power, Brightness, Effect, Color, Preset, network status, and battery status. + +### Effect + + + + + + + + + + +
Standard EffectAudio Reactive Effect
+ +Only parameters that are available for the selected Effect are shown.
+Audio Reactive Effects can expose a different parameter set from standard Effects, and the UI follows that dynamically. + +### Color + + + + + + + + + + + + +
C1 EditC1 / C2Color Not Used
+ +C1 / C2 / C3 can be selected and edited independently.
+When the current Effect does not use a Color Slot, the UI shows `COLOR NOT USED`. + +### Preset + + + + + + + + + + + + + + + + + + + + + + +
PresetPreset ManageSave New
Delete PresetBoot Preset
+ +In addition to recalling Presets, CoreS3 can perform SAVE NEW / OVERWRITE / DELETE / BOOT PRESET operations. + +### Browser Screenshot + +The current CoreS3 LCD can be captured from a browser at: + +```text +http:///cores3/screenshot.bmp +``` + +Example: + +```text +http://192.168.1.100/cores3/screenshot.bmp +``` + +The endpoint returns a still image.
+Refreshing the URL captures the current 320 × 240 LCD contents again. + +The LCD is read as RGB565 and converted to a 24-bit BMP.
+Large screenshot buffers are not kept resident during normal operation; they are allocated in PSRAM only while a request is active. + +The PNG images included in this documentation were also created by converting BMP files captured with this Browser Screenshot endpoint on the PC side. + +--- + +## Tested Hardware + +### Controller + +- M5Stack CoreS3 +- ESP32-S3 240 MHz +- 16 MB Flash +- 8 MB Quad PSRAM +- AXP2101 PMIC +- ES7210 audio codec / built-in microphones +- 320 × 240 touch display + +### LED + +Hardware validation used the M5Stack DIGITAL RGB LED STRIP. + +- SK6812 +- RGB type +- 60 LEDs / m +- Two strips connected, for 120 physical LEDs +- WLED Bus Type: `WS281x` +- Color Order: `GRB` +- CoreS3 Port.C +- GPIO17 + +> **Important:** This project does not guarantee the safety of directly powering 120 LEDs at high brightness from the CoreS3.
+> For larger LED counts or higher brightness, use an appropriately sized external 5V LED power supply with a common GND. + +--- + +## Recommended Brightness + +The standard WLED default Brightness is not modified. + +When powering an LED Strip directly from CoreS3, hardware testing showed that the battery level can decrease even while USB-C power is connected at Brightness 128. + +For this reason, **a starting Brightness around 64** is recommended for the CoreS3 test configuration. + +```text +WLED default: 128 +CoreS3 recommendation: 64 +``` + +This does not modify the WLED core default.
+Actual power consumption depends heavily on LED count, colors, Effect, and the external power architecture. + +--- + +## Build Environment + +Validated build environment: + +```text +WLED 17.0.0-devV5 +PlatformIO env m5stack_cores3 +Platform pioarduino / platform-espressif32 55.03.39 +Arduino Core 3.3.9 +ESP-IDF libraries 5.5.4 +NeoPixelBus 2.9.0+sha.76afe83 +``` + +The CoreS3 environment is defined in `platformio_override.ini`. + +Key settings: + +```ini +[platformio] +default_envs = m5stack_cores3 + +[env:m5stack_cores3] +extends = env:esp32s3dev_16MB_opi + +board_build.arduino.memory_type = qio_qspi +board_build.flash_mode = qio +``` + +CoreS3-specific usermods: + +```text +CoreS3_Power +CoreS3_Display +CoreS3_Audio +audioreactive +``` + +Audio Reactive definitions: + +```text +WLED_M5STACK_CORES3_AUDIO=1 +UM_AUDIOREACTIVE_ENABLE +SR_DMTYPE=7 +``` + +GPIO0 is used for the ES7210 MCLK and is therefore excluded from the WLED physical Button configuration. + +--- + + +### Public Build Example + +A ready-to-use CoreS3 PlatformIO override example is included at: + +```text +usermods/CoreS3_Display/platformio_override.ini.example +``` + +Copy it to the WLED repository root and rename it to: + +```text +platformio_override.ini +``` + +The example includes the validated CoreS3 environment, Quad PSRAM settings, CoreS3 usermods, Audio Reactive definitions, NeoPixelBus patch pre-scripts, and the post-upload watchdog-reset handling. + +## Build + +### 1. Requirements + +- Visual Studio Code +- PlatformIO IDE +- Git +- USB-C cable + +### 2. Open the WLED source tree + +Open the WLED repository in VS Code. + +Confirm that `platformio_override.ini` exists at the repository root. If it does not, copy `usermods/CoreS3_Display/platformio_override.ini.example` to the repository root and rename it to `platformio_override.ini`. + +### 3. Build + +Build the following PlatformIO environment: + +```text +m5stack_cores3 +``` + +A successful build ends with: + +```text +Environment Status +-------------- ------- +m5stack_cores3 SUCCESS +``` + +### 4. Upload + +Upload the `m5stack_cores3` environment from PlatformIO. + +If M5Burner Serial Monitor or another program is holding the COM port open, close it before Upload. + +For CoreS3, the PlatformIO post-upload reset is changed to watchdog-reset to improve recovery after ESP32-S3 native USB flashing. + +--- + +## NeoPixelBus / RMT DMA1024 + LCD/GDMA Patches + +With ESP32-S3 + NeoPixelBus RMT output, hardware testing found an intermittent condition where pixels beyond the configured LED Count could light unexpectedly. + +The CoreS3 build applies the following stabilization: + +```text +RMT DMA enabled +mem_block_symbols 1024 +``` + +The patch does not rely on manually editing files under `.pio/libdeps`. + +```text +pio-scripts/cores3_v17_neopixelbus_patch.py +``` + +runs as a PlatformIO pre-script and automatically applies the DMA1024 patch even after NeoPixelBus is downloaded again. + +Example when the patch is applied: + +```text +[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / 1024-symbol patch: ... +``` + +When already present: + +```text +[CoreS3 RMT DMA1024] patch already present: NeoEsp32RmtXMethod.h +``` + +The same pre-script also applies the validated LCD/GDMA teardown fix used during runtime LED-bus rebuilds.
+When the last LCD mux bus is destroyed, the GDMA channel is stopped, reset, disconnected, and deleted so stale LCD peripheral ownership is not carried into the next bus initialization. + +Example when the LCD/GDMA patch is applied: + +```text +[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... +``` + +When already present: + +```text +[CoreS3 LCD GDMA] patch already present: NeoEsp32LcdXMethod.h +``` + +Both patches are idempotent. Their reproducibility has been validated by deleting the NeoPixelBus dependency and rebuilding from a clean dependency state. + +--- + +## Power Management + +### DCDC3 Always-PWM + +During investigation of unexpected complete CoreS3 power-offs, DCDC OVP was observed multiple times in the AXP2101 `PWROFF_STATUS`. + +The CoreS3 Power usermod uses DCDC3 Always-PWM as a stabilization measure. + +```text +DCDC1: AUTO +DCDC3: ALWAYS_PWM +OVP protection: unchanged / enabled +``` + +Long-duration hardware testing and regression checks showed strong stabilization with this setting. + +> This does not claim that DCDC3 itself was proven to be the sole hardware root cause.
+> In this project, DCDC3 Always-PWM is treated as a stabilization measure that has been strongly validated on real hardware. + +### Safe Shutdown + +Holding the physical CoreS3 Power Key sends a BLACK frame to the LED Strip before the PMIC hard power-off. + +Processing sequence: + +```text +Power Key long press + ↓ +LED BLACK frame + ↓ +Strip suspend + ↓ +AXP2101 hard power-off +``` + +If the Power Key is released after BLACK but before the final hard power-off, shutdown is canceled and the previous LED output and Brightness are restored. + +--- + +## Audio Reactive + +The built-in ES7210 and CoreS3 microphones are used. + +The Audio usermod initializes ES7210, while Audio Reactive owns I2S1 / PCM / FFT processing. + +Main settings: + +```text +Codec ES7210 +I2S I2S1 +Sample Rate 16000 Hz +Format Stereo / 16-bit +MCLK GPIO0 +DIN GPIO14 +``` + +Codec initialization is deferred slightly during startup and retried multiple times if necessary. + +Audio status can be checked from WLED Info. + +--- + +## Touch UI + +Main screens: + +```text +MAIN +COLOR +EFFECT +PRESET +PRESET MANAGE +``` + +CoreS3 and the WLED Web UI synchronize in both directions. + +- Change on CoreS3 → reflected in Web UI +- Change in Web UI → reflected on CoreS3 + +Short press / long press / drag touch operations are supported. + +--- + +## Preset Management + +The following operations are available from CoreS3: + +```text +Preset Call +SAVE NEW +OVERWRITE +DELETE +BOOT PRESET +``` + +A Preset cache is used to synchronize with Presets stored by WLED. + +--- + +## Network Recovery + +If Wi-Fi connectivity is lost, Recovery AP can be started from the CoreS3 Display. + +When Recovery AP is active, access: + +```text +http://4.3.2.1 +``` + +The normal WLED Web UI and CoreS3 local controls can be used together. + +--- + +## Battery Status + +Battery level is read from the CoreS3 AXP2101 fuel gauge and displayed on the MAIN screen. + +Battery status is updated periodically instead of continuously polling I2C. + +--- + +## Project Structure + +The main CoreS3-specific files are: + +```text +WLED/ +├─ platformio_override.ini +├─ pio-scripts/ +│ ├─ cores3_upload_watchdog_reset.py +│ └─ cores3_v17_neopixelbus_patch.py +└─ usermods/ + ├─ CoreS3_Power/ + ├─ CoreS3_Display/ + ├─ CoreS3_Audio/ + └─ audioreactive/ +``` + +### CoreS3_Power + +Responsibilities: + +- AXP2101 +- AW9523B +- External 5V +- DCDC3 Always-PWM +- Power Key +- Safe Shutdown +- Power Health + +### CoreS3_Display + +Responsibilities: + +- M5GFX +- Touch +- Local UI +- Battery display +- Wi-Fi status +- Preset UI +- Browser Screenshot + +### CoreS3_Audio + +Responsibilities: + +- ES7210 probe / initialization +- Audio pins +- Audio health +- Audio Reactive handoff + +### audioreactive + +Adds CoreS3 built-in microphone / I2S1 integration. + +--- + +## Design Policy + +This project intentionally minimizes changes to the WLED core. + +CoreS3-specific logic is primarily contained in: + +```text +usermods/ +pio-scripts/ +platformio_override.ini +``` + +This makes future synchronization with WLED upstream easier. + +--- + +## Current Limitations / Notes + +- Segment management is intentionally not implemented in the local UI.
+ Use the WLED Web UI for Segment configuration. +- Directly powering 120 LEDs from CoreS3 at high brightness is not recommended. +- The standard WLED Brightness default of 128 is not modified. +- A starting Brightness around 64 is recommended for CoreS3. +- Browser Screenshot returns a still BMP image; it is not a live stream. +- DCDC OVP protection is not disabled. +- Core2 / Core2 for AWS porting is planned as a separate phase after the CoreS3 version is complete. + +--- + +## Validation Summary + +The following items have been validated on CoreS3 hardware: + +- Normal Boot +- Reset Reboot +- LED ON / OFF +- Effect control +- Multi-Color +- BLACK Toggle +- Preset call / management +- CoreS3 ↔ Web UI sync +- Sleep / Wake +- Wi-Fi reconnect +- Recovery AP +- Audio Reactive +- Battery display +- Runtime Health +- Safe Shutdown cancel +- Safe Shutdown full power-off +- Reboot after shutdown +- Active-use endurance +- Idle endurance +- RMT tail-pixel regression +- NeoPixelBus clean dependency re-patch +- Browser Screenshot +- Screenshot color accuracy +- Screenshot + normal Touch/UI operation + +--- + +## Upstream + +This project is based on WLED: + +https://github.com/wled/WLED + +WLED itself remains the upstream project.
+Please refer to the upstream repository for WLED documentation, supported LED types, API behavior, and licensing. + +--- + +## Licensing + +WLED source in this repository follows the upstream **EUPL v1.2** license.
+NeoPixelBus remains licensed under **LGPL-3.0-or-later**. The CoreS3 build-time patch script modifies the PlatformIO-downloaded NeoPixelBus source while preserving the upstream library license header. + +Refer to the repository `LICENSE` file and the respective upstream projects for complete license terms. + +--- + +## Roadmap + +Remaining work toward CoreS3 v1.0: + +- [x] Power stabilization +- [x] Display / Touch UI +- [x] Audio Reactive +- [x] Preset management +- [x] Battery / Health UX +- [x] Recovery AP +- [x] Safe Shutdown +- [x] NeoPixelBus RMT DMA1024 / LCD-GDMA stabilization +- [x] Browser Screenshot +- [ ] Final release branch / tag +- [ ] Release notes +- [ ] Optional additional screenshots +- [ ] Core2 / Core2 for AWS porting + +--- + +## Acknowledgements + +- WLED project and contributors +- M5Stack +- NeoPixelBus +- Audio Reactive / WLED usermod contributors + +This project builds on the work of many open-source projects and contributors. diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md new file mode 100644 index 0000000000..3dd65fa33e --- /dev/null +++ b/usermods/CoreS3_Display/readme_jp.md @@ -0,0 +1,624 @@ +# WLED on M5Stack CoreS3 + +[English](readme.md) + +M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
+**タッチディスプレイ / 内蔵マイク Audio Reactive / バッテリー表示 / 安全な電源OFF / LED制御** を1台にまとめるプロジェクトです。 + +> **Status:** CoreS3 production baseline
+> **Base:** WLED 17.0.0-devV5
+> **Target:** M5Stack CoreS3 (ESP32-S3 / 16MB Flash / 8MB Quad PSRAM) +> +> 本プロジェクトはコミュニティによる WLED の CoreS3 向け移植・拡張であり、WLED または M5Stack の公式ファームウェアではありません。 + +--- + +## Features + +- WLED を M5Stack CoreS3 上でネイティブ実行 +- 320 × 240 タッチディスプレイによるローカル操作 +- WLED Web UI と CoreS3 UI の双方向同期 +- LED Brightness / Effect / Speed / Intensity / Palette 操作 +- C1 / C2 / C3 のマルチカラー編集 +- Preset 呼び出し / SAVE / OVERWRITE / DELETE / BOOT PRESET +- 内蔵 ES7210 デュアルマイクを利用した Audio Reactive +- バッテリー残量表示 +- Display Sleep / Wake +- Wi-Fi Offline / Recovery AP UX +- AXP2101 Power Key を使った Safe Shutdown +- 電源OFF前に LED BLACK frame を送信 +- Safe Shutdown のキャンセル時は直前の LED 状態を復元 +- ESP32-S3 / NeoPixelBus 向け RMT DMA1024 および LCD/GDMA runtime-rebuild 安定化 +- ブラウザから現在の LCD 画面を BMP で取得 + +--- + +## Screenshots + +以下は、CoreS3 本体の LCD を Browser Screenshot 機能で直接取得した実画面です。 + +### Main + +

+ +

+ +MAIN 画面では、LED Power、Brightness、Effect、Color、Preset、およびネットワーク / バッテリー状態を確認できます。 + +### Effect + + + + + + + + + + +
Standard EffectAudio Reactive Effect
+ +Effect ごとに使用可能なパラメータだけを表示します。
+Audio Reactive Effect では、通常 Effect と異なるパラメータ構成にも追従します。 + +### Color + + + + + + + + + + + + +
C1 EditC1 / C2Color Not Used
+ +C1 / C2 / C3 を個別に選択して編集できます。
+Effect が Color Slot を使用しない場合は `COLOR NOT USED` と表示します。 + +### Preset + + + + + + + + + + + + + + + + + + + + + + +
PresetPreset ManageSave New
Delete PresetBoot Preset
+ +Preset の呼び出しだけでなく、SAVE NEW / OVERWRITE / DELETE / BOOT PRESET まで CoreS3 から操作できます。 + +### Browser Screenshot + +CoreS3 の現在の LCD 表示は、ブラウザから次の URL で取得できます。 + +```text +http:///cores3/screenshot.bmp +``` + +例: + +```text +http://192.168.1.100/cores3/screenshot.bmp +``` + +Screenshot は静止画です。
+URL を更新すると、その時点の 320 × 240 LCD 内容を再取得します。 + +BMP は CoreS3 の LCD から RGB565 で読み出し、24-bit BMP に変換して返します。
+大きな Screenshot バッファは通常時には常駐せず、要求時のみ PSRAM に確保します。 + +このドキュメントに掲載している PNG 画像も、Browser Screenshot で取得した BMP を PC 側で PNG に変換したものです。 + +--- + +## Tested Hardware + +### Controller + +- M5Stack CoreS3 +- ESP32-S3 240 MHz +- 16 MB Flash +- 8 MB Quad PSRAM +- AXP2101 PMIC +- ES7210 audio codec / built-in microphones +- 320 × 240 touch display + +### LED + +実機確認では M5Stack の DIGITAL RGB LED STRIP を使用しています。 + +- SK6812 +- RGB type +- 60 LEDs / m +- 2本連結で物理的には 120 LEDs +- WLED Bus Type: `WS281x` +- Color Order: `GRB` +- CoreS3 Port.C +- GPIO17 + +> **Important:** 120 LEDs を CoreS3 から直接高輝度で駆動する電源構成は、本プロジェクトでは安全性を保証していません。
+> LED 数や輝度が大きい場合は、LED 用の適切な外部 5V 電源と共通 GND を使用してください。 + +--- + +## Recommended Brightness + +WLED 標準の初期 Brightness は変更していません。 + +CoreS3 から LED Strip を直接使用する場合、実機では Brightness 128 でも USB-C 給電中にバッテリー残量が低下する状況を確認しています。 + +そのため、CoreS3 での開始値としては **Brightness 64 前後**を推奨します。 + +```text +WLED default: 128 +CoreS3 recommendation: 64 +``` + +これは WLED 本体のデフォルト値を変更するものではありません。
+使用 LED 数、色、Effect、外部電源構成によって消費電力は大きく変化します。 + +--- + +## Build Environment + +検証済みのビルド環境: + +```text +WLED 17.0.0-devV5 +PlatformIO env m5stack_cores3 +Platform pioarduino / platform-espressif32 55.03.39 +Arduino Core 3.3.9 +ESP-IDF libraries 5.5.4 +NeoPixelBus 2.9.0+sha.76afe83 +``` + +`platformio_override.ini` で CoreS3 用の環境を定義しています。 + +主な設定: + +```ini +[platformio] +default_envs = m5stack_cores3 + +[env:m5stack_cores3] +extends = env:esp32s3dev_16MB_opi + +board_build.arduino.memory_type = qio_qspi +board_build.flash_mode = qio +``` + +CoreS3 固有 Usermod: + +```text +CoreS3_Power +CoreS3_Display +CoreS3_Audio +audioreactive +``` + +Audio Reactive 用: + +```text +WLED_M5STACK_CORES3_AUDIO=1 +UM_AUDIOREACTIVE_ENABLE +SR_DMTYPE=7 +``` + +GPIO0 は ES7210 MCLK として使用するため、WLED の物理 Button から除外しています。 + +--- + + +### 公開用 Build Example + +CoreS3 用の PlatformIO 設定サンプルを次の場所に同梱しています。 + +```text +usermods/CoreS3_Display/platformio_override.ini.example +``` + +このファイルを WLED リポジトリ直下へコピーし、次の名前に変更して使用します。 + +```text +platformio_override.ini +``` + +このサンプルには、検証済みの CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、NeoPixelBus patch 用 pre-script、および Upload 後の watchdog-reset 処理が含まれています。 + +## Build + +### 1. Requirements + +- Visual Studio Code +- PlatformIO IDE +- Git +- USB-C cable + +### 2. Open the WLED source tree + +WLED リポジトリを VS Code で開きます。 + +`platformio_override.ini` がリポジトリ直下にあることを確認してください。存在しない場合は、`usermods/CoreS3_Display/platformio_override.ini.example` をリポジトリ直下へコピーし、`platformio_override.ini` にリネームしてください。 + +### 3. Build + +PlatformIO で次の Environment を Build します。 + +```text +m5stack_cores3 +``` + +正常時は最後に次のように表示されます。 + +```text +Environment Status +-------------- ------- +m5stack_cores3 SUCCESS +``` + +### 4. Upload + +PlatformIO から `m5stack_cores3` を Upload します。 + +M5Burner Serial Monitor などが COM ポートを開いている場合は、Upload 前に閉じてください。 + +CoreS3 では ESP32-S3 native USB の Upload 後復帰を安定させるため、PlatformIO の post-upload reset を watchdog-reset に変更しています。 + +--- + +## NeoPixelBus / RMT DMA1024 + LCD/GDMA Patch + +ESP32-S3 + NeoPixelBus の RMT 出力では、LED Count より後ろのピクセルが不定期に点灯する問題を実機で確認しました。 + +CoreS3 向けには次の安定化を適用しています。 + +```text +RMT DMA enabled +mem_block_symbols 1024 +``` + +パッチは `.pio/libdeps` のライブラリを手作業で変更する方式ではありません。 + +```text +pio-scripts/cores3_v17_neopixelbus_patch.py +``` + +が PlatformIO の pre-script として動作し、NeoPixelBus を新規取得した場合でも自動的に DMA1024 patch を適用します。 + +Build 時の例: + +```text +[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / 1024-symbol patch: ... +``` + +すでに適用済みの場合: + +```text +[CoreS3 RMT DMA1024] patch already present: NeoEsp32RmtXMethod.h +``` + +同じ pre-script では、runtime の LED Bus 再構築時に使用する LCD/GDMA teardown 修正も適用します。
+最後の LCD mux bus を破棄する際に GDMA channel を stop / reset / disconnect / delete し、次回の Bus 初期化へ古い LCD peripheral ownership が残らないようにします。 + +LCD/GDMA patch 適用時の例: + +```text +[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... +``` + +すでに適用済みの場合: + +```text +[CoreS3 LCD GDMA] patch already present: NeoEsp32LcdXMethod.h +``` + +2つの patch はどちらも idempotent です。NeoPixelBus dependency を削除したクリーンな状態からの再 Build でも再適用性を確認済みです。 + +--- + +## Power Management + +### DCDC3 Always-PWM + +CoreS3 の予期しない完全電源OFF調査では、AXP2101 の `PWROFF_STATUS` で DCDC OVP を複数回確認しました。 + +CoreS3 Power Usermod では、DCDC3 を Always-PWM に設定する安定化策を使用しています。 + +```text +DCDC1: AUTO +DCDC3: ALWAYS_PWM +OVP protection: unchanged / enabled +``` + +この設定は長時間実機試験と回帰確認で安定化効果を確認しています。 + +> DCDC3 がハードウェア上の絶対的な根本原因だった、と断定しているわけではありません。
+> 本プロジェクトでは「実機で強く検証された安定化策」として扱っています。 + +### Safe Shutdown + +CoreS3 の物理 Power Key 長押しでは、PMIC hard-off の前に LED Strip へ BLACK frame を送信します。 + +処理の概要: + +```text +Power Key long press + ↓ +LED BLACK frame + ↓ +Strip suspend + ↓ +AXP2101 hard power-off +``` + +BLACK 送信後に Power Key を離して Shutdown をキャンセルした場合は、直前の LED 出力と Brightness を復元します。 + +--- + +## Audio Reactive + +CoreS3 内蔵 ES7210 と内蔵マイクを使用します。 + +Audio Usermod が ES7210 を初期化し、Audio Reactive 側が I2S1 / PCM / FFT を担当します。 + +主な仕様: + +```text +Codec ES7210 +I2S I2S1 +Sample Rate 16000 Hz +Format Stereo / 16-bit +MCLK GPIO0 +DIN GPIO14 +``` + +起動時に Codec 初期化を少し遅延し、失敗時は複数回 Retry する構成です。 + +WLED Info から Audio の状態を確認できます。 + +--- + +## Touch UI + +主な画面: + +```text +MAIN +COLOR +EFFECT +PRESET +PRESET MANAGE +``` + +CoreS3 と WLED Web UI は双方向に同期します。 + +- CoreS3 で変更 → Web UI に反映 +- Web UI で変更 → CoreS3 に反映 + +Touch の短押し / 長押し / ドラッグ操作もサポートしています。 + +--- + +## Preset Management + +CoreS3 から次の操作ができます。 + +```text +Preset Call +SAVE NEW +OVERWRITE +DELETE +BOOT PRESET +``` + +Preset cache を使用し、WLED 側で保存された Preset と同期します。 + +--- + +## Network Recovery + +Wi-Fi 接続が失われた場合、CoreS3 Display から Recovery AP を起動できる構成です。 + +Recovery AP 使用時は次へアクセスします。 + +```text +http://4.3.2.1 +``` + +通常の WLED Web UI と CoreS3 のローカル操作を併用できます。 + +--- + +## Battery Status + +CoreS3 の AXP2101 fuel gauge からバッテリー残量を取得し、MAIN 画面に表示します。 + +Battery status は定期更新され、常時 I2C polling し続けないよう間隔を設けています。 + +--- + +## Project Structure + +CoreS3 対応の中心は次のファイルです。 + +```text +WLED/ +├─ platformio_override.ini +├─ pio-scripts/ +│ ├─ cores3_upload_watchdog_reset.py +│ └─ cores3_v17_neopixelbus_patch.py +└─ usermods/ + ├─ CoreS3_Power/ + ├─ CoreS3_Display/ + ├─ CoreS3_Audio/ + └─ audioreactive/ +``` + +### CoreS3_Power + +担当: + +- AXP2101 +- AW9523B +- External 5V +- DCDC3 Always-PWM +- Power Key +- Safe Shutdown +- Power Health + +### CoreS3_Display + +担当: + +- M5GFX +- Touch +- Local UI +- Battery display +- Wi-Fi status +- Preset UI +- Browser Screenshot + +### CoreS3_Audio + +担当: + +- ES7210 probe / initialization +- Audio pins +- Audio health +- Audio Reactive handoff + +### audioreactive + +CoreS3 built-in microphone / I2S1 連携を追加しています。 + +--- + +## Design Policy + +このプロジェクトでは、できるだけ WLED 本体への変更を増やさない方針を取っています。 + +CoreS3 固有処理は主に: + +```text +usermods/ +pio-scripts/ +platformio_override.ini +``` + +へ閉じ込めています。 + +これにより、将来の WLED upstream 更新への追従をしやすくしています。 + +--- + +## Current Limitations / Notes + +- Local UI では Segment 管理を行いません
+ Segment 設定は WLED Web UI を使用します。 +- 120 LED を CoreS3 から直接高輝度で給電する構成は推奨しません。 +- WLED 標準 Brightness 128 は変更していません。 +- CoreS3 では Brightness 64 前後からの使用を推奨します。 +- Browser Screenshot は BMP の静止画です。ライブストリームではありません。 +- DCDC OVP 保護は無効化していません。 +- Core2 / Core2 for AWS への移植は、CoreS3 版完成後の別フェーズです。 + +--- + +## Validation Summary + +CoreS3 実機で次の項目を確認しています。 + +- Normal Boot +- Reset Reboot +- LED ON / OFF +- Effect control +- Multi-Color +- BLACK Toggle +- Preset call / management +- CoreS3 ↔ Web UI sync +- Sleep / Wake +- Wi-Fi reconnect +- Recovery AP +- Audio Reactive +- Battery display +- Runtime Health +- Safe Shutdown cancel +- Safe Shutdown full power-off +- Reboot after shutdown +- Active-use endurance +- Idle endurance +- RMT tail-pixel regression +- NeoPixelBus clean dependency re-patch +- Browser Screenshot +- Screenshot color accuracy +- Screenshot + normal Touch/UI operation + +--- + +## Upstream + +This project is based on WLED: + +https://github.com/wled/WLED + +WLED itself remains the upstream project.
+Please also refer to the upstream repository for WLED documentation, supported LED types, API behavior, and licensing. + +--- + +## Licensing + +このリポジトリの WLED ソースは、upstream と同じ **EUPL v1.2** に従います。
+NeoPixelBus は **LGPL-3.0-or-later** のままです。CoreS3 の build-time patch script は PlatformIO が取得した NeoPixelBus ソースへ修正を適用しますが、upstream library のライセンスヘッダーは保持します。 + +完全なライセンス条件については、リポジトリの `LICENSE` と各 upstream project を参照してください。 + +--- + +## Roadmap + +CoreS3 v1.0 までの残作業: + +- [x] Power stabilization +- [x] Display / Touch UI +- [x] Audio Reactive +- [x] Preset management +- [x] Battery / Health UX +- [x] Recovery AP +- [x] Safe Shutdown +- [x] NeoPixelBus RMT DMA1024 / LCD-GDMA stabilization +- [x] Browser Screenshot +- [ ] Final release branch / tag +- [ ] Release notes +- [ ] Optional additional screenshots +- [ ] Core2 / Core2 for AWS porting + +--- + +## Acknowledgements + +- WLED project and contributors +- M5Stack +- NeoPixelBus +- Audio Reactive / WLED usermod contributors + +This project builds on the work of many open-source projects and contributors. diff --git a/usermods/CoreS3_Display/screenshots/color-c1-c2.png b/usermods/CoreS3_Display/screenshots/color-c1-c2.png new file mode 100644 index 0000000000000000000000000000000000000000..c01479d8f2d396df56db5e4a4fa7f333c4ec6b44 GIT binary patch literal 2308 zcmXw(dpr~F7soeDqlGSNHM;3$YBoyESE5jwONi8x^^?mlP0Z%{Wk!fxVnr@d79+WI z8NwoS3Av0AxmAq0O%1~?zVrR{d;WM{&+9zrd0yw7&pGdyOIGJMDeh7P005iJFPPW@ z06-(@c^)hy-6hs=0{{TrYHnhDIbdLR2$M`}QI*Hpd~7ig#KH$;{lCN-2zo1I-=k8` zCmYBGCi5Qb&fk1?OG61Yt@?(x^oR9zyX&Z7x2l*ss_z86>A_*mkC({$Fd}%Wp= z(a2E|mq!t3&fw0Z_Lqci?g}3=z0_4TrhQ%=VZfw2jLy9XGz#LASFhCd-idL2>-poO zf3zo|f-!`$;3er{eh<|kmNmcXDd#i$PCPG9X)(j5572_Ys~UVjSe8I&^I20aLJA`# zY1s1PaF4a&B}!tPu4Y|6({zDC5hAdfwcgdNuVCb-c{eM!FuqRpmlTO(wLK1eJsJd+ zabGqHOPo<8o*f~D+-Lr`_zH$x&~v2?G4b5#2S(!6 z?+*6c<4D*sgG|h|^B(Dr?y#b(U6ni@DI^b)hLU*SNbH1BG$DZh>l}@WIdz?qaptDy zn{HusBmW)ez*Lt<3sMg=?&+v3Y+_}`f41G`F8hdR8@ zO!zxg>E^(S3agJRGI!O9*aJP85G|2yvU^p1VU0#wHuBfY`XeFtYv$~YP!(K2EJIuy75<%k^$1GZE&ppq-R&JVtCB_htkJ&E z&^1Seb!+`L5rHCB*w4a*l7^-~rWzBisRf~_Ex||q-$ge7ctc!u!w-U0mxdPt`=-;D zR+cc#%UaupvID*yVv71oo_gnP#+4w9Zeb5Q$P(!zQ*uM^{G z$tbV1ne6xBZaDeGWu$v-X8)k_DQo(_A!YwsK{a3VcTN%Zm^P0WN0EN7IOi5B<%+CLtM;sM0SXtt4`0|@8wOy#gI!%-?qz0o za_?=!6F0>ZOkh970?m@Iz;A>4##L}xjw!T>vI!$4umvcOklv|s;vLs}JK^v90M_%^pw?ge zWFQmg5xoo=2>O0g<}F}eUHdGZDkA8}KC-*>)9*3p34nd9bXFhU>K}3znCCIB3>pzC zsKg|vNBca&V1xUl?JAtGHIYj~HwN*B&CuT$jgK8%3C=Hqi zRNUisRgN)Jqe})(cJvBboX@OGKRhm^>_bM|&b^A}a=Aeyf(D%CH=E(U%$bC zORF}{uACN{Fg13&*nzxD=S-t8#MNMd zx*+0QY;@vg+}ugX#N0!T+DF(ldne!_3*$K+7!|a)S(>qt!M6>%IlNe8s9kw9K0Bd4 zymo2MEY`6}t#STY%D7#bwJKC*D^3Mx6b6(|CL$Ji>ki5Hnifjprgy`%n;K8x-1OmV z1voKsHu|^>#(FIbR9(J1((TLH=M{%#M^>P`Oiv;f?f!o*Psp0Lb=|#muRy!8X!7hJ zw7d((T;eR;^Id}YwwHzAqj;ybw03S}1et*5YUukf`3>cX-h3Hed)feHZ`z4N8J6TA z7K7D6PX6wt$2^_xqjF$6wg0Jh$Dg2oK36}vUFu>+WNmPa+dMQ6-0ouH-{EbuySuP@($^p-PaH9w$?~}n;C)Om=)Py!hJtYup0?Qvp`uw z1@=7-(|uDzEOt=UJQ%wW|w}WeWps#M)@yH z+Jn!Q480d_DnBhBXxg%y5wa^XN-D^P1jnWsoxn@2zcs(n6(Q0Q1S8k65dqlI0MIF` zL>E1V;%U1|9%Pp z_Z0`p*15|G@S^0t%P9lGnzvAm%6+m&8-qP(2Cs?Mz`pGlqy#4s<-fh@jr?;rJ)MX4 zv0Bs%B0hkWN^Rez{`b9>0S{0)Rh-tKdllHH4+e~JG7uRE7+KmpY~#VIdo+VuPZ|Ko Z5P^zOjEs6Glw@5sb5kpm;Mb literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/color-c1.png b/usermods/CoreS3_Display/screenshots/color-c1.png new file mode 100644 index 0000000000000000000000000000000000000000..fb060cbf4b4d87970fb27fe7799ee13a8bcbb1a4 GIT binary patch literal 2173 zcmZvdc{mi>8^?!IXiQqhRzhyp$r`S;$dc?!B-?1AEJG;E5OO1B@>3c_C|QRY`!I|q zQ?kV!`#PB{A)2v_agCX|qviMf{`j3g&U4QDJm8)Yv(4d}Ye&DN0*xpZ(y6&6?L}{iU@>HRm)!UI?Es zF#taOvqU2#P#irJ!#*{ZRLsLY%^d}DKC9^}-{K@aO1hT->aks1@%a3hA8(bsTJ^^y(1DGOm|AY1dZI7 z2$2CdK1SD2&^+?ULzVYiCEoYGI%Pp6n3XPxxYDaS^ zdw7i29R7pRbBaj9JgU~EA+awxeIe!}C&gJEuxPukP3m@$7pMP=sz2SWb|U4hazARw zHbwQHXL1~qDkcHheEjWv^@jcggGIC`VH_W0KgXcnPTiSGAJLD6MdtuHG-hGs4vIuN zeH=}o=mg5>;-7=^N{VD1Gm+)?v!d(7=+Vz7POKKfiKavu9Do;}W(YOSF!koM*72WO7QsNieAGAzfatyr~Q6!$$vQ&$G_{RT!g z@Lh<|<}*)THjJhBZ-u29Slvu>?Q@bxO^K5}g_gI}dEq45HipKBWba+3WIi>}KLu;3 z0P5s>*pacA9MS3*&8jrXyQT^pt&m1J6CGPpA)H|YQOE)$0`~9%`PB?QjDIB{?^8ZQ zX2te_#eP(1y!hj{4%_VY$_VKrnEXuJA60ApAYZ78lh8rS@b(-r*n%X_z0~-=W>mI$ zRi&snB9cj7PrDKJM*CK#MwD+p#IP%t$F_;%v;W($h194#IQ${%h|z6(8?9hJUJs6s z61H}!F{|5E>tmU0f4pwBo&QqylFKWJrXv3HB%h~(6N#pfn?+KuX4{CxcyoRG2 zp2_+l{+4HYteQR9K771})9gi!*zaUMB-xaqjZ+H1i z>Cesoi`!?QdJn94blKmG_cV?wA68CTIKoXoEelZ#8n!`49v4A_cVOZm56j5L2aChF zh5F3ozVQ*?Z>{}IdHlhYbzQZi6+?W?C7&I3BwjB{__=-aX1#bTdP_kTAJ@+m<~Aj? zVFaD=kKeYneo@z)=82TPJB)f`zJ1`V095q$vic2ec+00@oP<=$o4Oxiecvp_u;~Do zSD9B-DhVY$h)P`)p@28cyxstt_ogo)436;oc|lWomIfRW&?*||sfb}!XqOUpc@Y$o z$RsWkJh0&O#F^WobgHCvQVgZDw$# zG`~=<=>=*CoAf@e9sX7$kpENm5HrIRf6t4``O=&td99FFxdXF!4(n|wVPlUizIm%V zeW#BNAN1jxp}iF6P;n6`uSbGwkAzO7?MWMY%#j0ovkv=*GORexL{~*+>t)A8_FyG( zf?thsEGaCa;}CSMvZovkaI!Z3j^S6w$#ilRpNncx_03>A{6){f87^;aaI1WjoR)sE z;#zAvtss5Q>4_a!Oi+&SyE<~g2Ri;-ny=HV-XR7~nhi)DE2{HZrFA!Au8LBezoRZk=yZhnb0XA#N+<<{Zy{XasvI=(JZZ467gUD(@j?tI>alHjrL$C*L;3 zd|+5_b!<=)bxRIeBVc;f1gh$t15G?yxJx-d~2A_T1%DS6$%z)wjQ0B}h ze?y=QX~zql*WQQ zwri3*-B$tx()5ffP0qjDya)2QD!HECnP`r*(>3Dy{eP{9Kg0zhJyV)9u=KtkRDL@- z^(c@j9coVsgrvk?eKtQ4^=j{Ew{#TC2U$x?tekiTLt7BoP({>U`QU3Yo^#O zz%JOB_zz+v&-EgySCfUkmMaC&pH@`WNxpQmyKQz^{bPwB5CF*8*^~1iWVY literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/color-unused.png b/usermods/CoreS3_Display/screenshots/color-unused.png new file mode 100644 index 0000000000000000000000000000000000000000..95699f46627912834cc7df9f94117551b093637f GIT binary patch literal 1377 zcmbu7Yg7^j6vv5*!bCTx=`<~8OUitek0?rfEPRC4v@sJDdtrg%)T#I&&1R1+bver< zW4$buupElYL&}pLLqJNU5H#md$q+*YB*j_Z+n3!B|8vj(-2Xl2-t)V7FcJwdvo`|( z0FbcIknaJ26^P}2dM$8SO;Ow60KjT!SV&+@woI+SmfwNefqHA|m*)E7lZBd04|&su zFU|&8kHT!DPX$lAd0JM5>7};I(T_#je4KW@cxd-7u@A!gocgFCGxH6P0uu677hPQ( zCtfD9Y9=LUXOe&Y%!M`rYkY-bXH9cx^@TrC<~In~#sOjQ#j&9p|prP#SJ5I7Ti z4I7Gk!vk~VuySYr_8tB%W&8pHj?n_l8jl68v@+D9Z%J!t8?N%F4T%O27;dA1MNNgG z?_t_FJk&g7CyTB@T3E(&v0r7s?E}SVYQdQK-$@2zm5D@LE5473pc;wrfzo%5bvf)U zsS_VZ$g4U2hl*N#Xz^{XCQzv+x*iR2$v)2p`Nb8d%?()VZaJ7k!1A;`;cH!T&$oa; zj1Nr}W_B`9yCNXSmqYd3iDNzw zo&4%FZylFaOxsJ292QR6Lp?8^Wq2J3DV0iW-NU4Zp{-Sq$9SU>6rp9fE7aF_;Wt-jMw~7-MW`sGNQi&3Y$E&6aoO7TU9#6 z_T6;Xut|9$8_i>OZz!&epT#rdX5Kw>#3DeQe&kb*FqPLVcf>B^IGzvVWDRSwZ=@$L zBqV+Apdq|*kh59ZI2t~V9JfUNFzC8&`RpDe)!s20XM1`q7mCnl6~P zaHdD$+s^q{D|6@IRkSDKN#lD=q~OX#R}w;4{q!D6_UEI``G=Xu=e-qm8EWbv<}U_D zJULQxV%<39c9%-Ft}9ha;pX*^Q7VdMu>SjUjWnZs24t&{e7#LHHOM%6%0XB_NELxSBi3iEDirlonm zvYR{`qoi=RdizOvliZ<}M1dfEF~*5@EVr(pVXOARf^0ZS`K5DC^CMjq5GwwM&0%FG zC${OZgS`Z*gE&{XK1DZ^25cyxj=lONqAC6k(O8EGIF&)tmG6Vj=DmALm#(iNG{A~0 wklS|`b%`QV=(Q?xz7Gbl@^c^l|Gr$l@lI`HSy~)y&GG+SV4mA z!i10U<=?WF@1MQ*M9}uqcyj7e0)*h$Cv$E`Tfhgm!_YT`&)Tv?Sgf@EA~DwU6}2idSUvr zR~KyyU;V4hs!Ek;Ee~&Bd*o8ITh;2Oy;*biEM&@`nI6;UnR);AINyfE-wlQw z2J54L_OIHzYFqJ4p5GRU&EZZGkJAm4(V7zVW)uGilA{KP~T;XFksPm)`H%EO}4h!H0??Tjv{Yx?*{|Rj=PS zw0?JrhfU_^U#U~>n?8ORoRlN9`YzA)X|Ml;T+@Cyx#`cp3p)x;ZYFu<`1bVMow%m7 zbJ4VEZ;Gx9#aipl{nB;W%BJ#A|5EGPH0B3W=eyn7_kLM=$5HL2NoQxRiuiLfaeMVG z&JWY>9X~eD``Zh<3;eR*mwuno%J={JnF+n?Dg`pM4jTfDzI6;m7iaY%~&Upa9DmuNl-|F*18!tJ2O~?!O6?w z#-rVW@%8)W$Yvb*em`HG5iCk);+yFGnw+ptIr7^Xs@ZlJ^#r!OntAM{I}^`eO1G&pWn%KMk>=V=6dwHw;PoY$OKJRzkB!4jpgSj-N@PPku@$a=u_w23AHQ#MFKd$h_^;=7)*j_VS zU6X%h^PbaRS28&tXOUo$5CCQX$A;NSOp@Vu=N$CR`TBm{?`^%4*6&Py`t-CZHXiw>zpbkCF13z-8(Ew?fA+)Y>tm+IWvr_@zSmv+*Vpo0 zPvbY<>VC(|q;sqz?>nQ|XqxI?y50E)hD}b|IN^CxajxE%atv4>#gFm*ky$iKt5~K%1&SC%YAv%?U?ym2GHns aQ2xg_pE2sqJEq^=AR$jzKbLh*2~7Z2Zy+lG literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/effect-solid.png b/usermods/CoreS3_Display/screenshots/effect-solid.png new file mode 100644 index 0000000000000000000000000000000000000000..3a9c4951464ce864119e0f810cc366c384a98a48 GIT binary patch literal 1892 zcmaKsc{JPU8pnSLU8Hp>Q&cX-DOEuyMp0WzlNm$Y*kdPC+p&fUK~$ry?TG3~wNpz~ zTWSxrg)5|}vB%C8Vrwn6w3Q&1xHo2;d;h!dAMbhJ=e*B(me2EjQ*AKj;t+WV0080^ zzo6^@0AwmWe?1Ep?nl-zV*og#VSzGph@RS*>CK#iOP(6kE^Nj4X7vz_&PhppLq1iK ztBV^5T(}^4y{XoR!rwpk6t0)5GYk0r(ti`f@>=f03EW?!`D>=`CByQQ4b!W4cP=v4 zn$p;s0@_xqN8rCUgX<1P!@3XcP9-I2!0~ZrUtj^;^2!mtkwuy#<4qek6b3J5)*{TE zOXnM$1*J^WVu*1$rPBU}PlrPi)$CT&CYxK!ay#$MZt#r!>crFA!El7lS$Aj?_rf*dfI3Cy$+#UDhc&1^ovD0qMoFQvdwzVcD=0Rj z&-Y!%5q+)@=LWSQ(QS(5^7U;@(e+!E-#n9gLmJ@`q~9bm+3obk-LiDvubsf8_6TeNd5= zkD3kJ(Xn%^{%`nzzG|XMy z6eJkmVIy}3Wvw#~w4_&C_|*1N@U|$wOXbs?sM^5_Vo)MsdDuTUar21w+(WOaD8R;v zob0m` zLge<(*It=r@_h`R`q4v~Ed1BWZ z_Qqa?L6U%oWq+OhT1L0dT)8zZr#6)CNP4D6Bbj*G{p2{McRLp)yU=+fHT1hrx;m|x zYj~tU8}sU1xOXl}f59@kC8*Uq;9r|J z^Z;o>l8FDY3sT&_u<*z*XUYsGtm^z(cvUy2W)O4EzJ^V)z9@G>4FJf2+dp7)vZmRe zFU<7;vPu6SuEd2AqnMjts^~LYUU}n zcdsto0_|vmrBF}QW#{p%^me!6&`8ASNY8p-REJP8#z%fD>tlvJs?n z$nPrVB^Ql3O_Qt&@>wJ3__Q%k0$vg)gwTG*nZk03N^cz{25EU1W>j36XqB^y+)flK zL|Ld1QkhpvVOfb=dT16%#v7#{NB$r;B(8MqI$_Ma+@TPuSNDJ^{1W6OaX2c&zaUSh i$gkme>`owJvrs`3`@{(k_?W|Q6k literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/main.png b/usermods/CoreS3_Display/screenshots/main.png new file mode 100644 index 0000000000000000000000000000000000000000..12152333dd8b3a5f8e13b2c7f7cd00cebe07e346 GIT binary patch literal 2339 zcmX|?c{~&TAIFCrxg}pm$xjDgqmVF~$&p2_uN*OEB@HVQHMb~=lzv67v1++;<;ap_ zYTD!su~LlO#%!!jJNWUfexE-+pU>m{e!L%__vih5J>Ks$S7)erulbb*=g*lP4Zu=AAA^59I%4A&vq9u$CnGWh<#kh^wfhyv&2K8m|m+5g`qo}#+ zW=Uxvqc3{)nt2F*CF}U@Z$`|*R_05plK(6&Jv>W4W2Lt21H&C4m#K# zDC4J^`pB1U@p9zoF~1~KwUN%dkG2{r@`w{RtZoPiiS1AUxic*oU?&(!I*a1OW?I*!y5j^0QmBpJY1Y z$_#o9)z7ivbEuOKvVL|DA3?BV<-4#|3Vk7bbKg9+@wyCOP2j=z$xc~>T%lWd955-e z^nBj&P-$em&Ig|tg4Pm!gjrF z%E#w?jj+kiLWjc7DyGH}?{|3|vEb%qh>}%`~r8yD87*Gs5|j_3c1Q zKC7<8D)8pB6?xL})EdV=tJnziDVxG|CZzDKT7#+M>Y+AqyVT=a?cf@yxje`ECO|}3 z8Z#<>T}Q;1c=@(OLEtb|<35fZ#3#}h5Mhq>l$GUc}>X1t%rPUP?+tS(+mQZNAR3~VW zt!MH*ySpv^9crC|*1#Sx^;>hlw_aa&9qYIY zXTJVv(DH;?puT_P-l8|tP8+#a#&*NYp!V|_^Vc;HhIWybKWo*WQgMAY%TEG?rG=#> z3?3A-fDPU68kP*VEB3p(A0;e@Q152Xg%@bohwl4JS=K%;@vY+AcxlW>k@HWFwiirJ z8XckZz(gs0m=xujj3quQ)b~Q|PV*owQ6+u745$r!vp0U~1Gqm~Qg!BtUJO%{+2!kR z^yaieOPO@3AmGi}nlno>|d%2XnK3@1^ zEj!@t{fnO>7)JRY3ABaCad`1Pea))u;u#)osq;i+v-C@q#Km)=|OiY`-+ zbo}+lo*+A5(`M{xmmSJ$ z*?t6`^VW!XO|FNH$JP?V?~i8{^1Pudo$Og>cZ6#aw6YcYgN=^r0wLo)TQ(|)Wm{7W zJ36Ts5p3p0lb;CKthi71r<>m(bCO{6Je0x{IccB)C*5DLuQFW0#4vjlq4F%+dq|Tdb(G6MW;F7@&*S6G$?B?WSL|-*mi+*3h}I;;ODaVGn?I{ zutfCsi*n%(&gGAOV24*ev`y@?360P~WAaOeRQC98C9mR80A}_jdJmN^0UIQx%eES! zXO7=3^a6a_SzS&<#xo&{xXvQA2?{-qnn>eL{O*iSAuDb27#1%CkrQttJ0g$d+*{yKz#3Ofmh?%G&DpK4j$=ada+ScyWaJh7j5 zV_QljghDl2CneIZ{TL4Qx;1dC+9B8~236t~YgQ|DHmXCL-^0z=Scr=b4SQYW%l0;= zDf#a*Cd~LeY(gIiwnkr3Uzt)W$tvIFRlih!LQ!qUzjIMYC{(n1JL1X&YlN)5fNflxvXC3Hj*uwZaaARZInUSe+-!9KZZ_CVd+Zc3i58g- z#>te{3Q+a}sWgHd7BPX=`-pVXNfF&ITlfxy`rImr4!fImj-< z#oZkB(v?OqR!BretBwpaxY3?ZCmwoZeK=s5 zX6a?Tz~2)bu|h7cyS?_eNriD?a?#g@-&%gfymlG-gUAPTwm?Pk@Ad07JmbwH_}ix1 z#NuW7__*VS=81)IG(ADBcWVd@zeLno7!YTqKB1x&?wY|bYtR>Bl4xxtwBg|23Vd^$ z?#SP2y2A)$*Ipf3>a~i3M45e2wGJJzs+!&w+SA;Mcia_K!pO8j6ek}5Fz~Y$iaAMg zOUhy0Z2pd*_g^pLKD{+`B~Qrcrytp#tABo|wHP|T)vpUaP4Vp$LsZJO|7>bh-`W7< zWkM6(yTow{`?ea)>XGx222d2sJ&w7x=%5n$aUptFpF_u{%`j~OLTabm?*v~kyd$!V zoLZF&+?3!I;S~|oI!{}Za#6gXUw-PyuaJKH?f)1Npyqh6vbXBqRxkD&n=y$FJn`@K z_ZaKO1QBe3W=3jvQFr9T?2)s|N~}8lWSD_!t=7%C`kr4A9%koiTZR(YH8&PAWu745>d*B_S?kIP z2pGDvSNTuqGHBh9vbK81H<$y>T90p9->Z-Cqx4@H_}T)p$Kr)+wh-A6&9M#2n|b=U z4I(ON8m`M?iu?=n&y@J)jDQ_He(3U)Ey4k5|0+^4-N&qON*NT~ zHAi~Wu$T{OEul9aoT65j_t^F{2PGQ_Xb)cBh%r0_;Vn*sFj~pgq&0Pd= z1j?DL;->4Ikn7`jRUPhr`9QyfXLMIR^5is3-*j_x4eN9ExfLeu$iFfrV#YQp3x|2> z1=;KSRNcPoCwLZq51?y<%}9RutJKSg31@qe-B+{I+1sZ*a9oyC+Mi;=X6<1E$a#W> zLOn_k4ljE!QMdk?pTzf%48kmzJjo#TJRRF^^X7Iy`VO6HxA$YZi4HH?HDhZ>xwr{1 z`Y2OTS9!%DB_4qsJhhe*7ul{iFf&PsDX}Z@^$aQqO&^ie{AJr4>PI4zUVBj`<+E7< zOw?g)fZf=AF_jRn?ydU~$nJScw|L=ZUOoJ4AbWIUGqwQztN_6o*0E1=0vY{oc$Zt@ zygf||D^ZT3gxyjMnWq}^( zu(EM@kbkK1U4#$0ZlSLkhusf#qk7u}0Q=`iaZv z{ynOb3;T2^!Vdr#XKFyb&YlZRS76*bh#q<7E{_&lwmTs=SpMT|co--0d>fN*ZZJ+X zNla2`P@OZuQ){w8Nwu_f6kK{X!!+#IfBKhWFKz=}KM+g_Q}Nn|1Ei!~0j$oY1pOrSL&tod?r=(}8hO zP;y$6SOtv?i=2FG;ti4SJ38CvCeYY~a!iQRcFEdOdU>bn(*Y`{Q(;cx946@pVaND= p8T>2wMMNB^Mpek<`|k5-2P;J=J;szuPM zwV0-sP)$f1jUp&TBC!i&Noa@Agv<1vIsJaT=Xsv*d(U~#_de%IbGmu~D6c6G004lF z7tc8Z0AhA~m@g~7S9jpxp8x=vO~-Tgu2IYtHY@a*wT5(mOEIhv51#jIu_mmi9*Mx*=OIQVF9{ILvwA{g*Y*tGlSxy{+*#W8$Xk zx=6EvpEBeX!H}*YF26w(8Rv?0N^E{;8J#m8q{c+SSgd5Pk(9j$4P%R@+Xm4uZE8J7 zFz(;nkakk@-oDv6WUE+^tEVgScK3gIDWi?zUCX&4wX0Q8?BM~-$985dYd&H=yUMz7 zqPSrRgXbJE3>&WkR<>7Mo9xVwqb`Ah3P35{%)Hmipyh9^OSGxHnJ<*DtDcF}qD)(} zY>ut?o7ZJA>T=Clt7%xiz}me1bb2R(tu=c4(E8#@xs7U@gy2dF!uWzgXJxxvPh#Ry zD8hi!5Z|dGso@u8g7(LWP8Vuk(p5u)G&Jj|;)=k4CPC!9z@7Zr z9+FL{smEaaRy1U6*zc+RH-<}3)YL{Jd}k=iD;FL;(}nHhn@ePS-0<|p%#@!ztaWyC zDQs&d2mesIRkU&_m?c0geTI_MYqA{X$TNnpf1PRDu=SQvA|Fv7OxetA#NL2gT)knb zR*qqK-Pb>I@0m=`7D>87xm3n?QJ5#VyI7GLZG#j?Bo*_;95{9*J(n4^XcY;dt+u4&|!r zXV*w@YsU3&z2eL0QeCUnRx_)t1fM|kPPT-kh6>USH)SwyjEXK*+F_i{@Nr5M`^%Y+ zvxUTU9&930$#t5`%S!r7{JSt_eP4icwt`oOMYFLB&=XO=GN47ZI;#pm;Fq;B&@cyc z3*o*dO{leH9RO)KB^K0V2lVBBh}3DuN-h?umP^!`FrCaYU;>BP`(mVI);r}}C2a!#4T3Qcgb zwLdg{;{UL>(27h}Y@hR~zH(=(h2Y8<(w%8uusAAw7$hv>B&0fQBTTdhFbp%*eL0WQ zF=JeqRD%DS%`3Qu5~{J=GG9}hEcS1|asp9>RkYvpJxI3vai}HLU8s+bMJ-#`DLu1G zJ=Z{*Ac@|F!yZi^Nz&fEW#hlpgtYX+b4GYXjZ>HX=htSj4%ZFvvuYRec%B6b=6kMr3-HjSj1`A_uQ3n;rJ&Lm`nJNgO4-D|D=Q^F;)>#mvXF)k%$j~< z1s7shAps2e;pgmZP`rs?Vh-nX2sK!F?Xid%K3?(wpkACeBHfY-l@iFJ?@zW6iNb%x z%<$$)YYBA16VZC(AJkqHFJka)47|%Wf7xKjInmc%o3$Qw_waNR$Jt>4!`Ox0QJpL26;zbAU-z$SsmAb+~nywJv;k34K82=8jl zZywumK`03V$+g?<<(pg{Lx6FkA>5`cMxqxa1@_R2$uooO-6RW~+p-znY zAhwBXqGP7AsWL5K*!xeP^Dt444Z;db2*cOM^S$T?C*$X-hF4#lH?i?qeNu3IvqHMy zm2hjdb;YQle6v~4lm*Yg*R=a%j{O@BHR)RFd)$#$42wL*MORg%4i#8_a3<$nF*aB{ zPF$<yth}8I0^`pi2 zQb%mx?cW2)9+#*_GMt;5=4I9B#yyVLXp9zx@_!-o?(lV<(2KvoKnFy#%@skWZIUnG z9iu~^m2|lI&lAAI5j`7kev9>1$bT0M>pS6aJEyaJZk5^SD;s}BwJuJ9x)!1`dF|4z z+5`po?Ib<367ievtEi?k>fUhUWs+aXQ?gl+XAZ%rl6I7F0E>Tr=$0&So`M5Ne0SzQ y_h8aKk%h=Y_KC!M%>-Bj_TW8v#lL(?N!|s-{5mX_i{j?*3y$Zno~t{Hy!SuI@gzPy40GM(E{l7bzHnuQ%ocN39=cWU+ZsEI$YA@~N zawRwS1}fsKpPD|D<)X#M0akq9eb1LxO!c+;B`SwMW7nP_?1;vf-?e4(^JSA~&EBh7 z2Y_ZmIrsYNiwl4Hyma5S{}IVUNjh4gA+I;s7r$L-sL=cql6iR}82k7~W2pw<=eV<| z(1VGhLq~UoLCc+@PDTDk?yaw_ajov4RL~k9ps+Mi8C@rS?$A?}UQ&uPQ?4gq`i4BC zK#&O+s>9-}m9Q}2N$rMPmCv8{n6s4J%y-gYW1!^+7tBTIxmP`T%en6Tp-veO@E#ha z`=SE~f|_piYL8bx-A1ILO6|L44a^*q8_tbRftG1pdt%HGMeyrJ#KlXQ6qiZCYQ7>- zqIFNRSCVn6iv6bN3F`}szr;pGN|HU6v5GO-9nXaStR<>edbd4{rwFIHcM~C9Q}dhw z?(tSuFmZLTed)uRrfRt6#XkyBG6E3Rr>zLbPivB@|4@^}y*-UX6f!?ZgQB7Wc)$SkEdm4S zR3+e32U&4eJ<~R1jezkjM}7KP^#H@oI|IACclvhA79LMq&`&L;1Yd@kEsmF4M0<0J z7&^O-16Oq~QX-h*u@JUAfHT0Kvh4pIxTuk-Q#>guayqrSWBSe>ynx3U*D;&x!f@V% z&s9>bc1X+$gA@1;&)va~r!4Rrg0~Ix%B~fpGj4k2@01an(>!Jemkoqr)ZD=m-*z_= zuN)>A7%nycFOVX0I9?q(Z(2I0RF6w~d%H+-8Aac(H=rh@?;q3eSq#X9ou zUWh!lPS$RZpWcfBtHi#<$D-5@gwvnL{{ZFPAG)ENv}Mh&J2p#)gVS%RVt{ z-QvX&&Ep{{RXpF~n(&112W3lXP~m(DZRQ zA6>~1IZ_EVKeyBE-?tKT*N7yYZm^?pdCB1R+qVvqt<(LBUk(K$i{t?#-+1<~M$Owk zGBI5i?L$@~YvVg1vCD^Iqb&+dOhN10Ux7k{=DtX8TPv2lp&BlX8zsr_}ZOj0MBW_Lyng#Uj?( zhMBKP{(U=~7CuX?#MqUUvWEh(sAHdMzcT_3Y3jDBhZmE>g)d`f?1d^fJhz7Yt!VH` zQ6lkDp@B`~d4jj{mD6i6H(prQHJKBf`X#f`(v~JFINpCAsz=*EdO=5jQf6pug zZvB+d*LkhelD&1I1lLE4LuO?#Pe^^=pVm)b)`}f3b!_mIK1?gv__|(suc<+vq^+H+ zxHpg<2ZG!$JbH%tY;TZp4(!0@k1`0giFyJ(djeM8Lp5pM`bngZ=X9nJP+mg6bNITy P!jK9)iuUIn#$Nj$IyOno literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/preset-save.png b/usermods/CoreS3_Display/screenshots/preset-save.png new file mode 100644 index 0000000000000000000000000000000000000000..bb9fa1222a8782812403d2984eb58d30ef6c9feb GIT binary patch literal 1806 zcmY*ad0f&77FY1t9Mi@em%LN+9;I<}LcFqEM7P1CB(imB%otA`Z&9?yR=36@9i>o% z`jmHq=_ID2l;fD^uQ5SM@j@ffG$cR;bZuv6KkNJBU7yeUeBSqczVE%8f!`nwY8h&& zsHhx7`ke_@QBn2Y^FRCvw5NAa_UKhi$8>I8zUY+a>}7J2wtnv_ z_RWy0Keu!JT)J}?W0+d#h+@k&#|$i)BNMM)TaUG*cs!IULyl~66?a$(3u@gTf3c~k z%OKH)3WK5!FN-@|SBg+qdNv;33Er^CLV|MDx=Vwlq|(K~{@PmQiDHVH#sPP%##Xwe zM{4OWfKPEniVW}2LfLT@@mrUUm$9y?d9D}Gk_`xQZUjKENRn(4g#Cyaf2=o%0a#@Q z*OjHnVF=!9sslA@uY(7+C;dRRLYN@A*<~yO@IkaVC6;fHV3*cW0Os9iBZaeB9^8qS zLX$C#b9lvw%=gLJFC$@17bl;kOxEax^?wGir(8I7@yeK7#t&_-=9vs5AI4vot~icx z1JZhdAY$A_W;EnEe0(ZfH}>ZOVVwQydGTVA+8PY7x>3UHybA_Xu06t0!t!3&ma5>fg+8PAOut51}Z%A^bTGYQ*0KcAbUn4^TcVV1?QsIxa`^_}Kh{x<(>)0f3A+2NP; z?05nTI>RpR_$*;Z_IPBWvTsP@Y%ue!+`n?OdsW!47zk5CGd#c5aaZ;%)+BzIaUv`T zkIy}JpWK)N;OorLBR+`evA++cx@e9HdL{E8>l@RgE%8LM>lbWk>#LUot%%BCN3R^R z0o-A3J8SciSBoH|y%;UvPt7l!sG`@-v}~MT-5L;#rA<3I)ze{kaaWVT6)>@V8Z;ok z3#Q^%5^lqq9iGgw^r5y`Z&k1sgtow@msDxx?M7cN?lZssJA*F-D%>Xksu@BvbwRdH z&;6a&`!wsqYgnpfWLi;WO%y|kWXq@fGQ%$dI2lu7E0MXjP=co37^T_V0LwkMb;@Qi zUXmiCWmG*nif!_ptaUvTvRvTSpa;3lYP6C4^HKv4upU4OYopFJk!zjEE2wdB=Z zK`+;jno4CDp8XtHyDh(qIBho3Pix`>SlvpF`-*tMJVRPmBG!u6;7VUK4RibhuF`+w zJWKW@uad#eYrf388PK$ipBKPCyeRF({n~Tt1r`Rs;0?> zjm67WP~*EG4R!5Lyw3$`=tJ*cQ&rd2f&{X>>G=FPr|RTRUd`&ZFe_t_Ryjhw*Dw)P z`=Xo?!e{g93F`PRz$47D$tK%fyd+Zk?44fH*!ux!szRD>Df|_5Kecf0#P{I${y(gYL;;9F*f=P!z^$JOEesxL`oUw@}J-ySaRXQTi+khh9>*GmHMUoBC*gL|ABDo y(+&r+uI)qC{M0hXbW*eQHcRKfAL)NXDv;^=hqx`d)uDSn<&eJLoT2$(fA}|)C3_zL literal 0 HcmV?d00001 diff --git a/usermods/CoreS3_Display/screenshots/preset.png b/usermods/CoreS3_Display/screenshots/preset.png new file mode 100644 index 0000000000000000000000000000000000000000..edb21b390ad0d0516c13e7d87741b5217dbdc6fe GIT binary patch literal 1802 zcmZXTdpHyNAIFDWnn{NJpE{kt`uy>JKF{m>e4p?8^L*c*EH{@+N2fF8~Zvgi;hdih`Vx2(V%!MVI znfGXi<_|?LC9M^%kBx`nIQ+b=G9XBq#wd!n5vUn_irr~9UDy!Ms(MZry?!(5j&j3? zcI*kl_rZVqgCQXl>EDhY*PEZ2KNIYpRO%d2O2>LvFw%^LID!q6+J0$Pb(mL$AYH{v zfw#nAar7hPOI!*S2wIMw4fea|Icb&9+k&&(c?d7sdnd}JzGblr$W0-#tBMU$3%%A= z)&H*Isq|ntFYh>IhDSFy=>6&}$VFa>UJxj{$nv;p+}nQYiQ&(dMP&(zv8N;%!{R~F z>(fB&RdSnhWfMV!0dCNu*6_fj8F;=ff$P%+fiw#mf0PEX=qG3|B`-qUI=6^_c}>iS|+>cD2!2Q!d|EZbF`nCHdhHriyk(ilE4iEQy)s9az! z#6L5xl|q~(fUSI)T_>t1)uiimI#$)J^@5vy)#?3vj|3MlYH1Ho*AVAxS#Tz`|JaSH z2V%M?n_q-+2$D}34vIrqSTH^0<6k^3sG#}ZD%rG+>pgsBir_|3OnyGy=7mM;ol>@J zwQm8A?G_HEoLqE$SYb1C=bWLpIVYcD>G6{bGHWPvAg;T^Zo4zgUze_Qq#(vVgU=^I zWU4pX&}CMYEZ-$CTSkS_^LlN}KJvb(d}GmgJ3k!;tfexcvW#{6=6qNo!# z*-ATiwnTI+ zfMd~R&IpbHyPIzjWB=|mkyL(%RhsUX81TNF=j>SXOE)Nt^9!W=er_=ZyiF^iMr1h} zB{)oFd~4?`IjkKWb4U2c#jd%MCo9d;{OhSrwEo4~$!;mD>6&8mPlPIF`TBx*9DPaf zVVlulOJAao*6jzyt$|a0QCsL-wgSzQ6IIM_oq_|MMuobS_X6B=+y`{QTOT?n^o51U z+Y0);vo~wT`(FE1_I9B|5B+ZWFP%RN6h=O7Ibvnxv;1yx?&3@$%sKa%kBjohuOUci zC;4yej#Y>c&&jQQy`Kxdl3* zi7m~B3$ot~`W3`m^xKEoPi*e*kJ48O+a<<6DDBCGCD^R1fd#UD#R2DMb_i?u1bu{g zV)~^w@gsY*GA%C(>P)^;r|!A%oW{~`0ai-K(4L#-TvuCKIc@2ekEgO3+t>e^U~x-R zvw?5GKfz7gp=871nJBu{>A#;ngB24~syYUAZkG(nLnP4faoXLc<5Jv)&3vzuDM-D! zvE>+ty&My4ArR!M`_EEH`+~89Qx!gIHL3w0gw!qPXTQzcv>NZ-HLBN|{?eu(>itV` zHl_Pc~=B zJ+sR+0#;0FNwv|*{zxl*V7GVZsu?v4kep<9X!~S0?yC>(3 zOgOTW=Rh&0V9 +#include +#include +#include + +/* + * CoreS3 Power Production Baseline + * + * Production baseline for M5Stack CoreS3 power and LED-bus runtime handling. + * + * LED runtime re-init protection: + * - WLED core re-init gate guarantees the usermod can inspect each pending + * bus configuration before strip.finalizeInit() replaces the old buses. + * - Physical digital buses are compared per primary data pin. + * - If any existing output shrinks or disappears, the complete old LED range + * is turned OFF and explicitly rendered BLACK before the bus rebuild. + * - WLED ON/OFF state is restored after re-init; an already-OFF state remains OFF. + * - Pure growth / unchanged configurations pass through without OFF/ON cycling. + * + * Preserved validated power behavior: + * - AW9523B external-5V enable writes (BOOST then BUS). + * - Runtime External 5V re-assert after M5GFX initialization. + * - AXP2101 power-key IRQ handling for Safe Shutdown. + * - Safe Physical Shutdown LED BLACK / suspend / cancel-and-restore logic. + * - AXP2101 DCDC3 Always-PWM stability setting. + * - Concise boot reset / PWRON / PWROFF cause reporting. + */ + +// ============================================================ +// Read-only CoreS3 power health bridge +// +// CoreS3_Display consumes these states for user-facing warning UX only. +// Power ownership and all register writes remain inside this usermod. +// ============================================================ +static volatile bool coreS3PowerInitializationCompleteState = false; +static volatile bool coreS3PowerExternal5VReadyState = false; +static volatile bool coreS3PowerSafeShutdownMonitorReadyState = false; + +// Guaranteed WLED bus re-init gate handshake. +// +// wled.cpp calls coreS3PowerShouldDeferBusReinit() immediately before it would +// consume doInitBusses and call strip.finalizeInit(). +// +// prepared=false -> leave doInitBusses asserted and defer finalizeInit(). +// prepared=true -> allow WLED to consume doInitBusses and rebuild now. +static volatile bool coreS3PowerBusReinitPreparedState = false; + +static inline void coreS3PowerMarkBusReinitPrepared() +{ + coreS3PowerBusReinitPreparedState = true; +} + +extern "C" bool coreS3PowerShouldDeferBusReinit() +{ + // Keep normal WLED startup behavior before this usermod completes setup. + if (!coreS3PowerInitializationCompleteState) return false; + + if (!coreS3PowerBusReinitPreparedState) return true; + + coreS3PowerBusReinitPreparedState = false; + return false; +} + +extern "C" bool coreS3PowerInitializationComplete() +{ + return coreS3PowerInitializationCompleteState; +} + +extern "C" bool coreS3PowerExternal5VReady() +{ + return coreS3PowerExternal5VReadyState; +} + +extern "C" bool coreS3PowerSafeShutdownMonitorReady() +{ + return coreS3PowerSafeShutdownMonitorReadyState; +} + +class CoreS3PowerUsermod : public Usermod +{ +private: + static constexpr uint8_t AW9523B_ADDR = 0x58; + static constexpr uint8_t AXP2101_ADDR = 0x34; + + static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; + static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; + + static constexpr uint8_t AXP2101_REG_CHIP_ID = 0x03; + static constexpr uint8_t AXP2101_REG_PWRON_STATUS = 0x20; + static constexpr uint8_t AXP2101_REG_PWROFF_STATUS = 0x21; + + static constexpr uint8_t AXP2101_PWROFF_PWRON_PULLDOWN_MASK = 0x01; + static constexpr uint8_t AXP2101_PWROFF_SOFTWARE_MASK = 0x02; + static constexpr uint8_t AXP2101_PWROFF_PWRON_LOW_MASK = 0x04; + static constexpr uint8_t AXP2101_PWROFF_VSYS_UV_MASK = 0x08; + static constexpr uint8_t AXP2101_PWROFF_VBUS_OV_MASK = 0x10; + static constexpr uint8_t AXP2101_PWROFF_DCDC_UV_MASK = 0x20; + static constexpr uint8_t AXP2101_PWROFF_DCDC_OV_MASK = 0x40; + static constexpr uint8_t AXP2101_PWROFF_OVER_TEMP_MASK = 0x80; + + static constexpr uint8_t AXP2101_REG_IRQ_ENABLE_1 = 0x41; + static constexpr uint8_t AXP2101_REG_IRQ_STATUS_1 = 0x49; + + static constexpr uint8_t AXP2101_REG_DCDC_FORCE_PWM = 0x81; + // AXP2101 REG0x81: bit2=DCDC1 mode, bit4=DCDC3 mode. + // 0=Auto PWM/PFM, 1=Always PWM. Keep all unrelated bits untouched. + static constexpr uint8_t AXP2101_DCDC1_ALWAYS_PWM_MASK = 0x04; + static constexpr uint8_t AXP2101_DCDC3_ALWAYS_PWM_MASK = 0x10; + + static constexpr uint8_t AXP2101_PKEY_POSITIVE_MASK = 0x01; + static constexpr uint8_t AXP2101_PKEY_NEGATIVE_MASK = 0x02; + static constexpr uint8_t AXP2101_PKEY_LONG_MASK = 0x04; + static constexpr uint8_t AXP2101_PKEY_SHORT_MASK = 0x08; + static constexpr uint8_t AXP2101_PKEY_EVENT_MASK = 0x0F; + + static constexpr uint8_t AXP2101_PKEY_IRQ_ENABLE_MASK = + AXP2101_PKEY_POSITIVE_MASK | + AXP2101_PKEY_NEGATIVE_MASK | + AXP2101_PKEY_LONG_MASK; + + static constexpr unsigned long POWER_KEY_POLL_INTERVAL_MS = 20; + static constexpr unsigned long SAFE_SHUTDOWN_FALLBACK_HOLD_MS = 1500; + static constexpr unsigned long SAFE_SHUTDOWN_BLACK_REFRESH_MS = 100; + static constexpr unsigned long SAFE_SHUTDOWN_SHOW_WAIT_MS = 150; + static constexpr unsigned long RUNTIME_POWER_HEALTH_POLL_MS = 10000; + + // Runtime LED-bus shrink protection timing. + static constexpr unsigned long LED_REINIT_OFF_CONFIRM_TIMEOUT_MS = 3000; + static constexpr unsigned long LED_REINIT_OFF_SETTLE_MS = 500; + static constexpr unsigned long LED_REINIT_OUTPUT_WAIT_MS = 500; + + // Require multiple actual overlay/show frames that explicitly overwrite + // the complete OLD logical range with BLACK before allowing finalizeInit(). + static constexpr uint8_t LED_REINIT_REQUIRED_BLACK_FRAMES = 3; + static constexpr unsigned long LED_REINIT_BLACK_FRAME_TRIGGER_MS = 50; + static constexpr unsigned long LED_REINIT_POST_BLACK_GUARD_MS = 20; + + static constexpr uint8_t REG_OUTPUT_P0 = 0x02; + static constexpr uint8_t REG_OUTPUT_P1 = 0x03; + static constexpr uint8_t REG_CONFIG_P0 = 0x04; + static constexpr uint8_t REG_CONFIG_P1 = 0x05; + static constexpr uint8_t REG_GCR = 0x11; + static constexpr uint8_t REG_LEDMODE_P0 = 0x12; + static constexpr uint8_t REG_LEDMODE_P1 = 0x13; + + static constexpr uint8_t BUS_EN_MASK = 0x02; + static constexpr uint8_t BOOST_EN_MASK = 0x80; + + static constexpr uint8_t CORE_S3_CONFIG_P0 = 0x18; + static constexpr uint8_t CORE_S3_CONFIG_P1 = 0x0C; + static constexpr uint8_t CORE_S3_GCR = 0x10; + static constexpr uint8_t CORE_S3_LEDMODE_P0 = 0xFF; + static constexpr uint8_t CORE_S3_LEDMODE_P1 = 0xFF; + + bool aw9523Found = false; + bool axp2101Found = false; + // CoreS3 external 5V state. + bool external5VEnableAttempted = false; + bool external5VEnableSuccess = false; + bool runtimeExternal5VEnableAttempted = false; + bool runtimeExternal5VEnableSuccess = false; + bool runtimeExternal5VViolationLogged = false; + unsigned long lastRuntimePowerHealthPoll = 0; + + bool busEnabled = false; + bool boostEnabled = false; + + uint8_t p0Before = 0; + uint8_t p1Before = 0; + uint8_t p0After = 0; + uint8_t p1After = 0; + + bool powerKeyMonitorReady = false; + bool runtimePowerKeyMonitorAttempted = false; + bool runtimePowerKeyBusReadyLogged = false; + bool powerKeyPressed = false; + bool safeShutdownBlankActive = false; + bool safeShutdownEverTriggered = false; + bool safeShutdownLastCanceled = false; + + uint8_t axpIrqEnableBefore = 0; + uint8_t axpIrqEnableAfter = 0; + uint8_t lastPowerKeyStatus = 0; + uint8_t lastSafeShutdownTriggerStatus = 0; + uint8_t savedLogicalBrightness = 0; + + unsigned long powerKeyPressedAt = 0; + unsigned long lastPowerKeyPoll = 0; + unsigned long lastShutdownBlackRefresh = 0; + unsigned long lastRuntimeI2CFailureLog = 0; + + esp_reset_reason_t bootResetReason = ESP_RST_UNKNOWN; + bool bootPowerOnStatusValid = false; + bool bootPowerOffStatusValid = false; + uint8_t bootPowerOnStatus = 0; + uint8_t bootPowerOffStatus = 0; + + // DCDC3 Always-PWM stability state. + bool dcdc3StabilityAttempted = false; + bool dcdc3StabilityApplied = false; + uint8_t dcdcModeBefore = 0; + uint8_t dcdcModeAfter = 0; + + + // Runtime LED shrink state machine. + // + // The WLED core gate keeps doInitBusses pending until either loop() or + // handleOverlayDraw() classifies the new bus configuration. A real shrink + // then runs OFF -> confirmed BLACK -> rebuild -> state restore. + enum class LedShrinkSaveState : uint8_t { + IDLE = 0, + OFF_REQUEST_PENDING, + WAIT_OFF, + WAIT_REINIT_COMPLETE + }; + + LedShrinkSaveState ledShrinkSaveState = LedShrinkSaveState::IDLE; + + bool ledShrinkWasOn = false; + uint8_t ledShrinkOriginalBrightness = 0; + uint8_t ledShrinkOriginalBriLast = 0; + + uint16_t ledShrinkOldPhysical = 0; + uint16_t ledShrinkTargetPhysical = 0; + + unsigned long ledShrinkOffRequestedAt = 0; + unsigned long ledShrinkOffConfirmedAt = 0; + + // Actual BLACK frame confirmation. + uint8_t ledShrinkBlackOverlayFrames = 0; + unsigned long ledShrinkLastBlackTriggerAt = 0; + + const char* resetReasonText(esp_reset_reason_t reason) + { + switch (reason) { + case ESP_RST_UNKNOWN: return "UNKNOWN"; + case ESP_RST_POWERON: return "POWERON"; + case ESP_RST_EXT: return "EXT"; + case ESP_RST_SW: return "SOFTWARE"; + case ESP_RST_PANIC: return "PANIC"; + case ESP_RST_INT_WDT: return "INT_WDT"; + case ESP_RST_TASK_WDT: return "TASK_WDT"; + case ESP_RST_WDT: return "WDT"; + case ESP_RST_DEEPSLEEP: return "DEEPSLEEP"; + case ESP_RST_BROWNOUT: return "BROWNOUT"; + case ESP_RST_SDIO: return "SDIO"; + default: return "OTHER"; + } + } + + void printPowerOffSource(uint8_t status) + { + Serial.printf("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS=0x%02X\n", status); + + if (status == 0) { + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: NONE LATCHED / UNKNOWN")); + return; + } + + if (status & AXP2101_PWROFF_OVER_TEMP_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PMIC DIE OVER TEMPERATURE")); + if (status & AXP2101_PWROFF_DCDC_OV_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: DCDC OVER VOLTAGE")); + if (status & AXP2101_PWROFF_DCDC_UV_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: DCDC UNDER VOLTAGE")); + if (status & AXP2101_PWROFF_VBUS_OV_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: VBUS OVER VOLTAGE")); + if (status & AXP2101_PWROFF_VSYS_UV_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: VSYS UNDER VOLTAGE")); + if (status & AXP2101_PWROFF_PWRON_LOW_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PWRON HELD LOW / EN MODE")); + if (status & AXP2101_PWROFF_SOFTWARE_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: SOFTWARE POWER OFF")); + if (status & AXP2101_PWROFF_PWRON_PULLDOWN_MASK) + Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PWRON / POWER KEY PULL-DOWN")); + } + + bool probeI2C(uint8_t address) + { + Wire.beginTransmission(address); + return (Wire.endTransmission() == 0); + } + + bool readRegister(uint8_t address, uint8_t reg, uint8_t &value) + { + Wire.beginTransmission(address); + Wire.write(reg); + if (Wire.endTransmission(false) != 0) return false; + + uint8_t count = Wire.requestFrom(address, (uint8_t)1); + if (count != 1 || !Wire.available()) return false; + + value = Wire.read(); + return true; + } + + bool writeRegister(uint8_t address, uint8_t reg, uint8_t value) + { + Wire.beginTransmission(address); + Wire.write(reg); + Wire.write(value); + return (Wire.endTransmission() == 0); + } + + bool readRuntimeRegister(uint8_t address, uint8_t reg, uint8_t& value) + { + auto result = lgfx::i2c::transactionWriteRead( + CORES3_INTERNAL_I2C_PORT, + address, + ®, + 1, + &value, + 1, + CORES3_INTERNAL_I2C_FREQUENCY + ); + return result.has_value(); + } + + bool writeRuntimeRegister(uint8_t address, uint8_t reg, uint8_t value) + { + const uint8_t data[2] = { reg, value }; + auto result = lgfx::i2c::transactionWrite( + CORES3_INTERNAL_I2C_PORT, + address, + data, + sizeof(data), + CORES3_INTERNAL_I2C_FREQUENCY + ); + return result.has_value(); + } + + void captureBootAxpDiagnostics() + { + bootPowerOnStatusValid = + readRegister(AXP2101_ADDR, AXP2101_REG_PWRON_STATUS, bootPowerOnStatus); + bootPowerOffStatusValid = + readRegister(AXP2101_ADDR, AXP2101_REG_PWROFF_STATUS, bootPowerOffStatus); + + if (bootPowerOnStatusValid) { + Serial.printf("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS=0x%02X\n", bootPowerOnStatus); + } + else { + Serial.println(F("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS read FAILED")); + } + + if (bootPowerOffStatusValid) { + printPowerOffSource(bootPowerOffStatus); + } + else { + Serial.println(F("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS read FAILED")); + } + } + + bool configureAW9523() + { + bool ok = true; + ok &= writeRegister(AW9523B_ADDR, REG_CONFIG_P0, CORE_S3_CONFIG_P0); + ok &= writeRegister(AW9523B_ADDR, REG_CONFIG_P1, CORE_S3_CONFIG_P1); + ok &= writeRegister(AW9523B_ADDR, REG_GCR, CORE_S3_GCR); + ok &= writeRegister(AW9523B_ADDR, REG_LEDMODE_P0, CORE_S3_LEDMODE_P0); + ok &= writeRegister(AW9523B_ADDR, REG_LEDMODE_P1, CORE_S3_LEDMODE_P1); + return ok; + } + + // ------------------------------------------------------------ + // CoreS3 External 5V startup enable + // + // CoreS3 external 5V: + // AW9523B P0 bit1 = BUS_EN + // AW9523B P1 bit7 = BOOST_EN + // + // Both outputs are enabled for normal CoreS3 external 5V operation. + // Enable BOOST first, then BUS, matching the previously validated CoreS3 + // external-5V startup ordering. + // + // ------------------------------------------------------------ + bool enableExternal5VAtStartup() + { + external5VEnableAttempted = true; + + if (!readRegister(AW9523B_ADDR, REG_OUTPUT_P0, p0Before)) return false; + if (!readRegister(AW9523B_ADDR, REG_OUTPUT_P1, p1Before)) return false; + if (!configureAW9523()) return false; + + // Bring up the boost source first. + uint8_t newP1 = p1Before | BOOST_EN_MASK; + if (!writeRegister(AW9523B_ADDR, REG_OUTPUT_P1, newP1)) return false; + delay(10); + + // Then connect the external BUS. + uint8_t newP0 = p0Before | BUS_EN_MASK; + if (!writeRegister(AW9523B_ADDR, REG_OUTPUT_P0, newP0)) return false; + delay(10); + + if (!readRegister(AW9523B_ADDR, REG_OUTPUT_P0, p0After)) return false; + if (!readRegister(AW9523B_ADDR, REG_OUTPUT_P1, p1After)) return false; + + busEnabled = (p0After & BUS_EN_MASK) != 0; + boostEnabled = (p1After & BOOST_EN_MASK) != 0; + + return busEnabled && boostEnabled; + } + + // Re-assert the intended ON state after CoreS3_Display/M5GFX has taken + // ownership of the internal I2C bus. + bool applyRuntimeExternal5VEnable() + { + uint8_t p0 = 0; + uint8_t p1 = 0; + + if (!readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P0, p0)) return false; + if (!readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P1, p1)) return false; + + uint8_t newP1 = p1 | BOOST_EN_MASK; + if (!writeRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P1, newP1)) return false; + delay(2); + + uint8_t newP0 = p0 | BUS_EN_MASK; + if (!writeRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P0, newP0)) return false; + delay(2); + + uint8_t verifyP0 = 0; + uint8_t verifyP1 = 0; + + if (!readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P0, verifyP0)) return false; + if (!readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P1, verifyP1)) return false; + + p0After = verifyP0; + p1After = verifyP1; + + busEnabled = (verifyP0 & BUS_EN_MASK) != 0; + boostEnabled = (verifyP1 & BOOST_EN_MASK) != 0; + + return busEnabled && boostEnabled; + } + + void serviceRuntimeExternal5VEnable() + { + if (!powerKeyMonitorReady || runtimeExternal5VEnableAttempted) return; + + runtimeExternal5VEnableAttempted = true; + runtimeExternal5VEnableSuccess = applyRuntimeExternal5VEnable(); + coreS3PowerExternal5VReadyState = runtimeExternal5VEnableSuccess; + + Serial.printf( + "[CoreS3_Power][EXT5V] Runtime External 5V enable: %s BUS_EN=%s BOOST_EN=%s P0=0x%02X P1=0x%02X\n", + runtimeExternal5VEnableSuccess ? "APPLIED" : "FAILED", + busEnabled ? "ON" : "OFF", + boostEnabled ? "ON" : "OFF", + p0After, + p1After + ); + } + + bool configureRuntimePowerKeyMonitor() + { + runtimePowerKeyMonitorAttempted = true; + uint8_t chipId = 0; + + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_CHIP_ID, chipId)) { + Serial.println(F("[CoreS3_Power] Runtime power key: M5GFX I2C1 AXP2101 read FAILED")); + return false; + } + + if (chipId != 0x4A) { + Serial.printf("[CoreS3_Power] Runtime power key: unexpected AXP2101 chip ID 0x%02X\n", chipId); + return false; + } + + if (!runtimePowerKeyBusReadyLogged) { + runtimePowerKeyBusReadyLogged = true; + Serial.printf("[CoreS3_Power] Runtime I2C: M5GFX I2C_NUM_1 AXP2101 READY (ID=0x%02X)\n", chipId); + } + + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_ENABLE_1, axpIrqEnableBefore)) return false; + + uint8_t newIrqEnable = axpIrqEnableBefore | AXP2101_PKEY_IRQ_ENABLE_MASK; + if (!writeRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_ENABLE_1, newIrqEnable)) return false; + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_ENABLE_1, axpIrqEnableAfter)) return false; + + if ((axpIrqEnableAfter & AXP2101_PKEY_IRQ_ENABLE_MASK) != AXP2101_PKEY_IRQ_ENABLE_MASK) return false; + + uint8_t staleStatus = 0; + if (readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, staleStatus)) { + uint8_t stalePowerKeyFlags = staleStatus & AXP2101_PKEY_EVENT_MASK; + + if (stalePowerKeyFlags != 0) { + Serial.printf( + "[CoreS3_Power][BOOT] Stale PKEY IRQ before clear: 0x%02X%s%s%s%s\n", + stalePowerKeyFlags, + (stalePowerKeyFlags & AXP2101_PKEY_POSITIVE_MASK) ? " RELEASE" : "", + (stalePowerKeyFlags & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "", + (stalePowerKeyFlags & AXP2101_PKEY_LONG_MASK) ? " LONG" : "", + (stalePowerKeyFlags & AXP2101_PKEY_SHORT_MASK) ? " SHORT" : "" + ); + + writeRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, stalePowerKeyFlags); + } + } + + powerKeyPressed = false; + powerKeyPressedAt = 0; + lastPowerKeyPoll = millis(); + + Serial.printf("[CoreS3_Power] Runtime PKEY IRQEN1: 0x%02X -> 0x%02X\n", axpIrqEnableBefore, axpIrqEnableAfter); + Serial.println(F("[CoreS3_Power] Runtime power key monitor: ARMED on M5GFX I2C1")); + return true; + } + + // ------------------------------------------------------------ + // CoreS3 DCDC3 Always-PWM stability measure + // + // Keep DCDC1 in its existing mode and force DCDC3 to Always-PWM. + // REG0x81 is updated read-modify-write so unrelated bits are preserved. + // DCDC voltages/enables, charger/input/ADC settings, and REG0x23 + // DCDC OVP/UVP protection are not changed. + // ------------------------------------------------------------ + void serviceDcdc3StabilityMode() + { + if (dcdc3StabilityAttempted || !powerKeyMonitorReady) return; + + dcdc3StabilityAttempted = true; + + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, dcdcModeBefore)) { + Serial.println(F("[CoreS3_Power][DCDC3] REG81 read FAILED - mode unchanged")); + return; + } + + // R5A minimal stability change: + // set DCDC3 Always-PWM only; preserve DCDC1 and every unrelated REG81 bit. + const uint8_t target = dcdcModeBefore | AXP2101_DCDC3_ALWAYS_PWM_MASK; + + if (!writeRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, target)) { + Serial.println(F("[CoreS3_Power][DCDC3] REG81 write FAILED")); + return; + } + + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, dcdcModeAfter)) { + Serial.println(F("[CoreS3_Power][DCDC3] REG81 verify read FAILED")); + return; + } + + dcdc3StabilityApplied = + (dcdcModeAfter & AXP2101_DCDC3_ALWAYS_PWM_MASK) != 0; + + Serial.printf( + "[CoreS3_Power][DCDC3] REG81 0x%02X -> 0x%02X result=%s DCDC1=%s DCDC3=%s OVP_PROTECTION=UNCHANGED\n", + dcdcModeBefore, + dcdcModeAfter, + dcdc3StabilityApplied ? "APPLIED" : "VERIFY_FAILED", + (dcdcModeAfter & AXP2101_DCDC1_ALWAYS_PWM_MASK) ? "ALWAYS_PWM" : "AUTO", + (dcdcModeAfter & AXP2101_DCDC3_ALWAYS_PWM_MASK) ? "ALWAYS_PWM" : "AUTO" + ); + } + + void serviceRuntimePowerHealth() + { + if (!powerKeyMonitorReady || safeShutdownBlankActive) return; + + const unsigned long now = millis(); + if ( + lastRuntimePowerHealthPoll != 0 && + now - lastRuntimePowerHealthPoll < RUNTIME_POWER_HEALTH_POLL_MS + ) return; + + lastRuntimePowerHealthPoll = now; + + uint8_t p0 = 0; + uint8_t p1 = 0; + const bool p0Ok = readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P0, p0); + const bool p1Ok = readRuntimeRegister(AW9523B_ADDR, REG_OUTPUT_P1, p1); + + if (!p0Ok || !p1Ok) { + coreS3PowerExternal5VReadyState = false; + if (!runtimeExternal5VViolationLogged) { + runtimeExternal5VViolationLogged = true; + Serial.println(F("[CoreS3_Power][EXT5V] WARNING: runtime state read failed")); + } + return; + } + + busEnabled = (p0 & BUS_EN_MASK) != 0; + boostEnabled = (p1 & BOOST_EN_MASK) != 0; + const bool healthy = busEnabled && boostEnabled; + coreS3PowerExternal5VReadyState = healthy; + + if (!healthy && !runtimeExternal5VViolationLogged) { + runtimeExternal5VViolationLogged = true; + Serial.printf( + "[CoreS3_Power][EXT5V] WARNING: runtime path changed BUS_EN=%s BOOST_EN=%s P0=0x%02X P1=0x%02X\n", + busEnabled ? "ON" : "OFF", + boostEnabled ? "ON" : "OFF", + p0, + p1 + ); + } + else if (healthy) { + runtimeExternal5VViolationLogged = false; + } + } + + void waitForLedOutputComplete() + { + unsigned long waitStart = millis(); + while (strip.isUpdating() && millis() - waitStart < SAFE_SHUTDOWN_SHOW_WAIT_MS) { + delay(1); + } + } + + void beginSafeShutdownBlank(uint8_t triggerStatus) + { + if (safeShutdownBlankActive) return; + + safeShutdownLastCanceled = false; + safeShutdownEverTriggered = true; + lastSafeShutdownTriggerStatus = triggerStatus; + savedLogicalBrightness = bri; + + Serial.printf( + "[CoreS3_Power] Safe shutdown trigger: IRQ=0x%02X%s%s\n", + triggerStatus, + (triggerStatus & AXP2101_PKEY_LONG_MASK) ? " LONG" : "", + (triggerStatus & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "" + ); + + Serial.printf( + "[CoreS3_Power] Safe shutdown: WLED bri=%u, strip=%u -> physical 0\n", + savedLogicalBrightness, + strip.getBrightness() + ); + + strip.waitForIt(); + strip.setBrightness(0, true); + strip.show(); + waitForLedOutputComplete(); + strip.suspend(); + strip.waitForIt(); + + safeShutdownBlankActive = true; + lastShutdownBlackRefresh = millis(); + + Serial.println(F("[CoreS3_Power] Safe shutdown: BLACK frame sent and strip suspended")); + } + + void maintainSafeShutdownBlank(unsigned long now) + { + if (!safeShutdownBlankActive || !powerKeyPressed) return; + if (now - lastShutdownBlackRefresh < SAFE_SHUTDOWN_BLACK_REFRESH_MS) return; + + lastShutdownBlackRefresh = now; + strip.setBrightness(0, true); + strip.show(); + waitForLedOutputComplete(); + } + + void cancelSafeShutdownBlank() + { + if (!safeShutdownBlankActive) return; + + Serial.println(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); + strip.resume(); + + uint8_t restoreBrightness = bri; + if (restoreBrightness == 0 && savedLogicalBrightness > 0) { + restoreBrightness = savedLogicalBrightness; + } + + strip.setBrightness(restoreBrightness, true); + strip.show(); + waitForLedOutputComplete(); + + safeShutdownBlankActive = false; + safeShutdownLastCanceled = true; + strip.trigger(); + + Serial.printf("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); + } + + uint16_t getPendingPhysicalLedCount() const + { + uint32_t total = 0; + + for (const auto &bc : busConfigs) { + if (Bus::isVirtual(bc.type)) continue; + total += bc.count; + } + + if (total > 65535UL) total = 65535UL; + return (uint16_t)total; + } + + // ------------------------------------------------------------ + // Per-physical-bus shrink detection. + // + // Match each existing physical digital Bus to the pending BusConfig by + // primary data pin. Any output that becomes shorter or disappears requires + // old-range BLACK preparation before finalizeInit(). + // ------------------------------------------------------------ + bool pendingConfigShrinksAnyPhysicalBus() + { + const size_t oldBusCount = BusManager::getNumBusses(); + + for (size_t oldIndex = 0; oldIndex < oldBusCount; ++oldIndex) { + Bus* oldBus = BusManager::getBus(oldIndex); + if (oldBus == nullptr) continue; + if (!oldBus->isDigital() || oldBus->isVirtual() || oldBus->isPlaceholder()) continue; + + uint8_t oldPins[OUTPUT_MAX_PINS] = {255, 255, 255, 255, 255}; + const size_t oldPinCount = oldBus->getPins(oldPins); + if (oldPinCount == 0 || oldPins[0] == 255) continue; + + const uint8_t oldPrimaryPin = oldPins[0]; + const uint16_t oldLength = oldBus->getLength(); + + bool matchingPendingBusFound = false; + uint16_t pendingLength = 0; + + for (const auto &bc : busConfigs) { + if (Bus::isVirtual(bc.type)) continue; + if (!Bus::isDigital(bc.type)) continue; + if (bc.pins[0] != oldPrimaryPin) continue; + + matchingPendingBusFound = true; + pendingLength = bc.count; + break; + } + + if (!matchingPendingBusFound) { + Serial.printf( + "[CoreS3_Power][LED] Bus shrink detected: pin=%u %u->removed\n", + oldPrimaryPin, + oldLength + ); + return true; + } + + if (pendingLength < oldLength) { + Serial.printf( + "[CoreS3_Power][LED] Bus shrink detected: pin=%u %u->%u\n", + oldPrimaryPin, + oldLength, + pendingLength + ); + return true; + } + } + + return false; + } + + void captureLedShrinkReinitRequest() + { + if (ledShrinkSaveState != LedShrinkSaveState::IDLE) return; + if (!doInitBusses) return; + + // A previous hook in this same main-loop may already have approved this + // exact re-init request. Avoid duplicate pass-through logging/preparation. + if (coreS3PowerBusReinitPreparedState) return; + + // Safe-shutdown owns LED output. Do not start the shrink OFF/ON sequence; + // simply let normal WLED re-init proceed if a settings request arrives. + if (safeShutdownBlankActive) { + coreS3PowerMarkBusReinitPrepared(); + return; + } + + const uint16_t oldPhysical = strip.getLengthPhysical(); + const uint16_t targetPhysical = getPendingPhysicalLedCount(); + + // Keep an explicit safe pass-through for initialization / an empty pending + // configuration. The normal LED Hardware UI used in this CoreS3 build has + // real physical buses and non-zero counts, so this is only a guardrail. + if ( + BusManager::getNumBusses() == 0 || + oldPhysical == 0 || + targetPhysical == 0 + ) { + coreS3PowerMarkBusReinitPrepared(); + return; + } + + // The decision is now PER OLD PHYSICAL BUS, not aggregate total length. + // This correctly catches e.g. 5/10/15 -> 15/10/5 (total 30 -> 30). + const bool anyPhysicalBusShrinks = pendingConfigShrinksAnyPhysicalBus(); + + if (!anyPhysicalBusShrinks) { + coreS3PowerMarkBusReinitPrepared(); + return; + } + + ledShrinkOldPhysical = oldPhysical; + ledShrinkTargetPhysical = targetPhysical; + ledShrinkWasOn = (bri > 0); + ledShrinkOriginalBrightness = bri; + ledShrinkOriginalBriLast = briLast; + ledShrinkBlackOverlayFrames = 0; + ledShrinkLastBlackTriggerAt = 0; + + // Critical: prevent WLED's doInitBusses block later in this SAME main + // loop. The pending busConfigs vector remains intact for the delayed + // finalizeInit() call. + doInitBusses = false; + ledShrinkSaveState = LedShrinkSaveState::OFF_REQUEST_PENDING; + + Serial.printf( + "[CoreS3_Power][LED] Safe bus re-init start: %u->%u state=%s\n", + ledShrinkOldPhysical, + ledShrinkTargetPhysical, + ledShrinkWasOn ? "ON" : "OFF" + ); + } + + bool waitForLedOutputIdle(unsigned long timeoutMs) + { + const unsigned long startedAt = millis(); + + while (!BusManager::canAllShow()) { + if (millis() - startedAt >= timeoutMs) return false; + delay(1); + } + + return true; + } + + void serviceLedShrinkSaveOffCycle() + { + const unsigned long now = millis(); + + switch (ledShrinkSaveState) { + case LedShrinkSaveState::IDLE: + return; + + case LedShrinkSaveState::OFF_REQUEST_PENDING: + { + // Keep the pending bus rebuild blocked while the OLD physical count + // is still alive. + doInitBusses = false; + + ledShrinkOffRequestedAt = now; + ledShrinkOffConfirmedAt = 0; + + if (ledShrinkWasOn) { + // Use the exact WLED power path proven manually 5/5. + toggleOnOff(); + stateUpdated(CALL_MODE_BUTTON); + strip.trigger(); + } else { + ledShrinkOffConfirmedAt = now; + } + + // Do NOT suspend yet. Normal strip.service() must render the OFF state + // across the complete OLD LED range. + ledShrinkSaveState = LedShrinkSaveState::WAIT_OFF; + return; + } + + case LedShrinkSaveState::WAIT_OFF: + { + doInitBusses = false; + + if (ledShrinkOffConfirmedAt == 0) { + if (bri == 0 && strip.getBrightness() == 0) { + ledShrinkOffConfirmedAt = now; + + } else if (now - ledShrinkOffRequestedAt >= LED_REINIT_OFF_CONFIRM_TIMEOUT_MS) { + // Do not deadlock configuration forever. Keep rendering OFF and + // start the conservative settle window even if the runtime + // brightness report did not reach zero as expected. + ledShrinkOffConfirmedAt = now; + strip.trigger(); + + Serial.printf( + "[CoreS3_Power][LED] WARNING: bus re-init OFF timeout bri=%u strip=%u\n", + bri, + strip.getBrightness() + ); + } + + return; + } + + // Time alone is not enough. Earlier testing showed internal + // brightness=0 did not prove that a complete OLD-range BLACK frame + // had actually passed through WLED's final show path. + // + // handleOverlayDraw() explicitly replaces the full OLD logical + // range with BLACK immediately before WLED paints the frame to + // BusManager and calls show(). Require several such frames. + if (ledShrinkBlackOverlayFrames < LED_REINIT_REQUIRED_BLACK_FRAMES) { + if (now - ledShrinkLastBlackTriggerAt >= LED_REINIT_BLACK_FRAME_TRIGGER_MS) { + ledShrinkLastBlackTriggerAt = now; + strip.trigger(); + } + return; + } + + // Keep a conservative OFF settle window in addition to actual + // overlay/show-frame evidence. + if (now - ledShrinkOffConfirmedAt < LED_REINIT_OFF_SETTLE_MS) return; + + strip.waitForIt(); + + if (!waitForLedOutputIdle(LED_REINIT_OUTPUT_WAIT_MS)) { + Serial.println(F("[CoreS3_Power][LED] WARNING: old LED output busy; bus re-init deferred")); + return; + } + + // NeoPixelBus/LCD output is asynchronous; preserve old buses briefly + // after the final confirmed BLACK show before destroying them. + delay(LED_REINIT_POST_BLACK_GUARD_MS); + + // The complete OLD range has now been explicitly BLACK in multiple + // real WLED show frames. Freeze drawing, then release normal rebuild. + strip.suspend(); + strip.waitForIt(); + + // The old physical range is now safely BLACK. + // Authorize wled.cpp to consume doInitBusses and call finalizeInit(). + coreS3PowerMarkBusReinitPrepared(); + doInitBusses = true; + ledShrinkSaveState = LedShrinkSaveState::WAIT_REINIT_COMPLETE; + + return; + } + + case LedShrinkSaveState::WAIT_REINIT_COMPLETE: + { + // WLED clears doInitBusses immediately before strip.finalizeInit(). + // false here means the previous main-loop iteration completed rebuild. + if (doInitBusses) return; + + strip.resume(); + + if (ledShrinkWasOn) { + if (bri == 0) { + toggleOnOff(); + stateUpdated(CALL_MODE_BUTTON); + } else { + // Defensive fallback if another component changed WLED state. + bri = ledShrinkOriginalBrightness; + briLast = ledShrinkOriginalBriLast; + stateUpdated(CALL_MODE_BUTTON); + } + } else { + // Preserve original OFF state. + bri = 0; + briLast = ledShrinkOriginalBriLast; + stateUpdated(CALL_MODE_BUTTON); + } + + strip.trigger(); + + Serial.printf( + "[CoreS3_Power][LED] Safe bus re-init complete: %u->%u state=%s\n", + ledShrinkOldPhysical, + strip.getLengthPhysical(), + bri > 0 ? "ON" : "OFF" + ); + + ledShrinkSaveState = LedShrinkSaveState::IDLE; + ledShrinkWasOn = false; + ledShrinkOriginalBrightness = 0; + ledShrinkOriginalBriLast = 0; + ledShrinkOldPhysical = 0; + ledShrinkTargetPhysical = 0; + ledShrinkOffRequestedAt = 0; + ledShrinkOffConfirmedAt = 0; + ledShrinkBlackOverlayFrames = 0; + ledShrinkLastBlackTriggerAt = 0; + return; + } + } + } + + void servicePhysicalPowerKey() + { + unsigned long now = millis(); + + if (!powerKeyMonitorReady) { + if (!runtimePowerKeyMonitorAttempted || now - lastRuntimeI2CFailureLog >= 1000) { + if (configureRuntimePowerKeyMonitor()) { + powerKeyMonitorReady = true; + coreS3PowerSafeShutdownMonitorReadyState = true; + } + else { + lastRuntimeI2CFailureLog = now; + } + } + return; + } + + if (now - lastPowerKeyPoll < POWER_KEY_POLL_INTERVAL_MS) { + maintainSafeShutdownBlank(now); + return; + } + + lastPowerKeyPoll = now; + uint8_t status = 0; + + if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, status)) { + if (now - lastRuntimeI2CFailureLog >= 1000) { + lastRuntimeI2CFailureLog = now; + Serial.println(F("[CoreS3_Power] Runtime PKEY status read FAILED on M5GFX I2C1")); + } + maintainSafeShutdownBlank(now); + return; + } + + uint8_t powerKeyStatus = status & AXP2101_PKEY_EVENT_MASK; + + if (powerKeyStatus != 0) { + lastPowerKeyStatus = powerKeyStatus; + + Serial.printf( + "[CoreS3_Power] PKEY IRQ: 0x%02X%s%s%s%s\n", + powerKeyStatus, + (powerKeyStatus & AXP2101_PKEY_POSITIVE_MASK) ? " RELEASE" : "", + (powerKeyStatus & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "", + (powerKeyStatus & AXP2101_PKEY_LONG_MASK) ? " LONG" : "", + (powerKeyStatus & AXP2101_PKEY_SHORT_MASK) ? " SHORT" : "" + ); + + writeRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, powerKeyStatus); + } + + if (powerKeyStatus & AXP2101_PKEY_NEGATIVE_MASK) { + powerKeyPressed = true; + powerKeyPressedAt = now; + } + + if ((powerKeyStatus & AXP2101_PKEY_LONG_MASK) && !safeShutdownBlankActive) { + powerKeyPressed = true; + if (powerKeyPressedAt == 0) powerKeyPressedAt = now; + beginSafeShutdownBlank(powerKeyStatus); + } + + if ( + powerKeyPressed && + !safeShutdownBlankActive && + powerKeyPressedAt > 0 && + now - powerKeyPressedAt >= SAFE_SHUTDOWN_FALLBACK_HOLD_MS + ) { + Serial.println(F("[CoreS3_Power] Safe shutdown: PRESS timer fallback")); + beginSafeShutdownBlank(AXP2101_PKEY_NEGATIVE_MASK); + } + + if (powerKeyStatus & AXP2101_PKEY_POSITIVE_MASK) { + powerKeyPressed = false; + powerKeyPressedAt = 0; + cancelSafeShutdownBlank(); + return; + } + + maintainSafeShutdownBlank(now); + } + +public: + void setup() override + { + coreS3PowerInitializationCompleteState = false; + coreS3PowerExternal5VReadyState = false; + coreS3PowerSafeShutdownMonitorReadyState = false; + coreS3PowerBusReinitPreparedState = false; + + bootResetReason = esp_reset_reason(); + + Serial.println(); + Serial.println(F("[CoreS3_Power][BUILD] CoreS3 Power v0.1.0")); + Serial.println(F("[CoreS3_Power] Initialization start")); + Serial.printf("[CoreS3_Power] I2C SDA=%d SCL=%d\n", i2c_sda, i2c_scl); + Serial.printf( + "[CoreS3_Power][BOOT] ESP reset reason: %s (%d)\n", + resetReasonText(bootResetReason), + (int)bootResetReason + ); + + if (i2c_sda != 12 || i2c_scl != 11) { + Serial.println(F("[CoreS3_Power] ERROR: Invalid CoreS3 I2C pins")); + coreS3PowerInitializationCompleteState = true; + return; + } + + aw9523Found = probeI2C(AW9523B_ADDR); + axp2101Found = probeI2C(AXP2101_ADDR); + + Serial.printf("[CoreS3_Power] AW9523B (0x58): %s\n", aw9523Found ? "FOUND" : "NOT FOUND"); + Serial.printf("[CoreS3_Power] AXP2101 (0x34): %s\n", axp2101Found ? "FOUND" : "NOT FOUND"); + + if (!aw9523Found || !axp2101Found) { + Serial.println(F("[CoreS3_Power] External 5V enable canceled")); + coreS3PowerInitializationCompleteState = true; + return; + } + + captureBootAxpDiagnostics(); + + external5VEnableSuccess = enableExternal5VAtStartup(); + coreS3PowerExternal5VReadyState = external5VEnableSuccess; + + powerKeyMonitorReady = false; + runtimePowerKeyMonitorAttempted = false; + + Serial.println(F("[CoreS3_Power] Power key monitor: DEFERRED until M5GFX I2C1 is active")); + Serial.println(F("[CoreS3_Power] Safe shutdown: AXP2101 LONG IRQ primary trigger")); + Serial.printf("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); + Serial.printf("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); + + coreS3PowerInitializationCompleteState = true; + + Serial.println(F("[CoreS3_Power] LED runtime re-init: READY - per-bus shrink guard")); + Serial.println(F("[CoreS3_Power] Initialization complete")); + Serial.println(); + } + + void loop() override + { + captureLedShrinkReinitRequest(); + serviceLedShrinkSaveOffCycle(); + + servicePhysicalPowerKey(); + serviceRuntimeExternal5VEnable(); + serviceDcdc3StabilityMode(); + serviceRuntimePowerHealth(); + } + + // WLED calls this after effects render and immediately before the frame + // is painted to BusManager/show(). This is both the second re-init capture + // point and the final BLACK override for the complete old logical range. + void handleOverlayDraw() override + { + captureLedShrinkReinitRequest(); + if ( + ledShrinkSaveState == LedShrinkSaveState::WAIT_OFF && + bri == 0 && + strip.getBrightness() == 0 && + ledShrinkOldPhysical > 0 + ) { + strip.setRange(0, ledShrinkOldPhysical - 1, 0); + + if (ledShrinkBlackOverlayFrames < 255) { + ledShrinkBlackOverlayFrames++; + } + + } + } + + void addToJsonInfo(JsonObject& root) override + { + JsonObject user = root["u"]; + if (user.isNull()) user = root.createNestedObject("u"); + + JsonArray i2cInfo = user.createNestedArray("CoreS3 I2C"); + i2cInfo.add((i2c_sda == 12 && i2c_scl == 11) ? "GPIO12 / GPIO11 OK" : "I2C PIN ERROR"); + + JsonArray awInfo = user.createNestedArray("CoreS3 AW9523B"); + awInfo.add(aw9523Found ? "Found (0x58)" : "Not found"); + + JsonArray axpInfo = user.createNestedArray("CoreS3 AXP2101"); + axpInfo.add(axp2101Found ? "Found (0x34)" : "Not found"); + + JsonArray stabilityInfo = user.createNestedArray("CoreS3 Power Stability"); + stabilityInfo.add("DCDC3 Always-PWM"); + + JsonArray ledReinitInfo = user.createNestedArray("CoreS3 LED Runtime Reinit"); + switch (ledShrinkSaveState) { + case LedShrinkSaveState::IDLE: + ledReinitInfo.add("READY - per-bus shrink guard"); + break; + case LedShrinkSaveState::OFF_REQUEST_PENDING: + ledReinitInfo.add("Bus shrink detected - preparing OFF"); + break; + case LedShrinkSaveState::WAIT_OFF: + ledReinitInfo.add("Clearing old LED range"); + break; + case LedShrinkSaveState::WAIT_REINIT_COMPLETE: + ledReinitInfo.add("Rebuilding LED buses"); + break; + } + + JsonArray pwmInfo = user.createNestedArray("CoreS3 DCDC3 Mode"); + if (!dcdc3StabilityAttempted) { + pwmInfo.add("PENDING"); + } + else if (!dcdc3StabilityApplied) { + pwmInfo.add("FAILED / NOT APPLIED"); + } + else { + pwmInfo.add((dcdcModeAfter & AXP2101_DCDC1_ALWAYS_PWM_MASK) ? "DCDC1 Always PWM" : "DCDC1 Auto PWM/PFM"); + pwmInfo.add((dcdcModeAfter & AXP2101_DCDC3_ALWAYS_PWM_MASK) ? "DCDC3 Always PWM" : "DCDC3 Auto PWM/PFM"); + pwmInfo.add("OVP protection unchanged"); + } + + JsonArray shutdownInfo = user.createNestedArray("CoreS3 Safe Shutdown"); + if (!powerKeyMonitorReady) { + shutdownInfo.add(runtimePowerKeyMonitorAttempted ? "Runtime M5GFX I2C1 monitor unavailable" : "Runtime M5GFX I2C1 monitor pending"); + } + else if (safeShutdownBlankActive) { + shutdownInfo.add("BLACK output active - waiting for PMIC off"); + } + else if (safeShutdownLastCanceled) { + shutdownInfo.add("ARMED - last shutdown hold canceled"); + } + else if (safeShutdownEverTriggered) { + shutdownInfo.add("ARMED - shutdown BLACK previously triggered"); + } + else { + shutdownInfo.add("ARMED"); + } + + shutdownInfo.add("Trigger: AXP2101 Long Press IRQ"); + + char fallbackText[48]; + snprintf(fallbackText, sizeof(fallbackText), "PRESS fallback: %lu ms", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); + shutdownInfo.add(fallbackText); + + char lastIrqText[40]; + snprintf(lastIrqText, sizeof(lastIrqText), "Last IRQ status: 0x%02X", lastPowerKeyStatus); + shutdownInfo.add(lastIrqText); + + char triggerIrqText[40]; + snprintf(triggerIrqText, sizeof(triggerIrqText), "Last BLACK trigger: 0x%02X", lastSafeShutdownTriggerStatus); + shutdownInfo.add(triggerIrqText); + + char irqText[48]; + snprintf(irqText, sizeof(irqText), "IRQEN1 0x%02X -> 0x%02X", axpIrqEnableBefore, axpIrqEnableAfter); + shutdownInfo.add(irqText); + + JsonArray powerInfo = user.createNestedArray("CoreS3 Ext 5V"); + if (!external5VEnableAttempted) { + powerInfo.add("Enable not attempted"); + } + else if (external5VEnableSuccess) { + powerInfo.add("ENABLED - ON"); + } + else { + powerInfo.add("Enable FAILED"); + } + + JsonArray comparisonInfo = user.createNestedArray("CoreS3 Ext 5V Runtime"); + if (!runtimeExternal5VEnableAttempted) { + comparisonInfo.add("Startup enabled; runtime confirmation pending"); + } + else if (runtimeExternal5VEnableSuccess && busEnabled && boostEnabled) { + comparisonInfo.add("ACTIVE - BUS_EN ON / BOOST_EN ON"); + } + else { + comparisonInfo.add("FAILED / external 5V path changed"); + } + + JsonArray busInfo = user.createNestedArray("CoreS3 BUS_EN"); + busInfo.add(busEnabled ? "ON" : "OFF"); + + JsonArray boostInfo = user.createNestedArray("CoreS3 BOOST_EN"); + boostInfo.add(boostEnabled ? "ON" : "OFF"); + + char p0Text[32]; + snprintf(p0Text, sizeof(p0Text), "0x%02X -> 0x%02X", p0Before, p0After); + JsonArray p0Info = user.createNestedArray("CoreS3 AW P0"); + p0Info.add(p0Text); + + char p1Text[32]; + snprintf(p1Text, sizeof(p1Text), "0x%02X -> 0x%02X", p1Before, p1After); + JsonArray p1Info = user.createNestedArray("CoreS3 AW P1"); + p1Info.add(p1Text); + + JsonArray resetInfo = user.createNestedArray("CoreS3 Last ESP Reset"); + resetInfo.add(resetReasonText(bootResetReason)); + + JsonArray offInfo = user.createNestedArray("CoreS3 Last PMIC Off"); + if (bootPowerOffStatusValid) { + char offText[24]; + snprintf(offText, sizeof(offText), "PWROFF 0x%02X", bootPowerOffStatus); + offInfo.add(offText); + } + else { + offInfo.add("Unavailable"); + } + + JsonArray onInfo = user.createNestedArray("CoreS3 PMIC Power On"); + if (bootPowerOnStatusValid) { + char onText[24]; + snprintf(onText, sizeof(onText), "PWRON 0x%02X", bootPowerOnStatus); + onInfo.add(onText); + } + else { + onInfo.add("Unavailable"); + } + } +}; + +static CoreS3PowerUsermod coreS3PowerUsermod; +REGISTER_USERMOD(coreS3PowerUsermod); diff --git a/usermods/CoreS3_Power/library.json b/usermods/CoreS3_Power/library.json new file mode 100644 index 0000000000..4d8c8c8228 --- /dev/null +++ b/usermods/CoreS3_Power/library.json @@ -0,0 +1,11 @@ +{ + "name": "CoreS3_Power", + "version": "0.1.0", + "description": "M5Stack CoreS3 power management usermod for WLED", + "build": { + "libArchive": false + }, + "dependencies": { + "M5GFX": "https://github.com/m5stack/M5GFX.git#0.2.26" + } +} diff --git a/usermods/audioreactive/audio_reactive.cpp b/usermods/audioreactive/audio_reactive.cpp index c257b4e59c..4ddb0daa6f 100644 --- a/usermods/audioreactive/audio_reactive.cpp +++ b/usermods/audioreactive/audio_reactive.cpp @@ -201,6 +201,58 @@ static FFTsampleType* windowFFT = nullptr; // use audio source class (ESP32 specific) #include "audio_source.h" + +// ----------------------------------------------------------------------------- +// M5Stack CoreS3 built-in ES7210 microphone integration +// +// Board-specific responsibilities in this file are limited to: +// - fixed internal audio pin selection +// - GPIO0 button ownership release for MCLK +// - deferred I2S source startup after codec readiness +// - CoreS3-specific sample rate / FFT mapping +// +// ES7210 codec configuration itself remains owned by CoreS3_Audio. +// ----------------------------------------------------------------------------- +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +extern "C" bool coreS3AudioCodecReady(); + +static constexpr int8_t CORES3_AUDIO_SD_PIN = 14; +static constexpr int8_t CORES3_AUDIO_WS_PIN = 33; +static constexpr int8_t CORES3_AUDIO_BCLK_PIN = 34; +static constexpr int8_t CORES3_AUDIO_MCLK_PIN = 0; + +static void coreS3ApplyFixedAudioPins( + int8_t& sdPin, + int8_t& wsPin, + int8_t& bclkPin, + int8_t& mclkPin +) { + sdPin = CORES3_AUDIO_SD_PIN; + wsPin = CORES3_AUDIO_WS_PIN; + bclkPin = CORES3_AUDIO_BCLK_PIN; + mclkPin = CORES3_AUDIO_MCLK_PIN; +} + +static void coreS3ReleaseMclkButtonOwnership() +{ + // GPIO0 is the CoreS3 ES7210 MCLK. + if (PinManager::getPinOwner(CORES3_AUDIO_MCLK_PIN) == PinOwner::Button) { + PinManager::deallocatePin(CORES3_AUDIO_MCLK_PIN, PinOwner::Button); + } + + // Also neutralize any persisted/default WLED button configuration on GPIO0. + for (auto& button : buttons) { + if (button.pin == CORES3_AUDIO_MCLK_PIN) { + button.pin = -1; + button.type = BTN_TYPE_NONE; + button.pressedBefore = false; + button.longPressed = false; + button.pressedTime = 0; + button.waitTime = 0; + } + } +} +#endif constexpr i2s_port_t I2S_PORT = I2S_NUM_0; // I2S port to use (do not change !) constexpr int BLOCK_SIZE = 128; // I2S buffer size (samples) @@ -239,6 +291,12 @@ const float agcSampleSmooth[AGC_NUM_PRESETS] = { 1/12.f, 1/6.f, 1/16.f}; // // AGC presets end static AudioSource *audioSource = nullptr; +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +extern "C" bool coreS3AudioReactiveSourceReady() +{ + return audioSource && audioSource->isInitialized(); +} +#endif static bool useBandPassFilter = false; // if true, enables a hard cutoff bandpass filter. Applies after FFT. static bool useMicFilter = false; // if true, enables a IIR bandpass filter 80Hz-20Khz to remove noise. Applies before FFT. //////////////////// @@ -270,11 +328,19 @@ static float fftResultMax[NUM_GEQ_CHANNELS] = {0.0f}; // A table #endif // audio source parameters and constant +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +constexpr SRate_t SAMPLE_RATE = 16000; // Hardware-verified CoreS3 ES7210 sample rate +#else constexpr SRate_t SAMPLE_RATE = 22050; // Base sample rate in Hz - 22Khz is a standard rate. Physical sample time -> 23ms +#endif //constexpr SRate_t SAMPLE_RATE = 16000; // 16kHz - use if FFTtask takes more than 20ms. Physical sample time -> 32ms //constexpr SRate_t SAMPLE_RATE = 20480; // Base sample rate in Hz - 20Khz is experimental. Physical sample time -> 25ms //constexpr SRate_t SAMPLE_RATE = 10240; // Base sample rate in Hz - previous default. Physical sample time -> 50ms +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +#define FFT_MIN_CYCLE 30 // CoreS3: 16kHz / 512 samples ~= 32ms physical window +#else #define FFT_MIN_CYCLE 21 // minimum time before FFT task is repeated. Use with 22Khz sampling +#endif //#define FFT_MIN_CYCLE 30 // Use with 16Khz sampling //#define FFT_MIN_CYCLE 23 // minimum time before FFT task is repeated. Use with 20Khz sampling //#define FFT_MIN_CYCLE 46 // minimum time before FFT task is repeated. Use with 10Khz sampling @@ -282,6 +348,12 @@ constexpr SRate_t SAMPLE_RATE = 22050; // Base sample rate in Hz - 22Khz // FFT Constants constexpr uint16_t samplesFFT = 512; // Samples in an FFT batch - This value MUST ALWAYS be a power of 2 constexpr uint16_t samplesFFT_2 = 256; // meaningfull part of FFT results - only the "lower half" contains useful information. + +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +static_assert(SAMPLE_RATE == 16000, "CoreS3 FFT calibration requires 16 kHz sampling"); +static_assert(samplesFFT == 512, "CoreS3 FFT calibration requires 512 FFT samples"); +constexpr float CORES3_FFT_BIN_HZ = (float)SAMPLE_RATE / (float)samplesFFT; // 31.25 Hz/bin +#endif // the following are observed values, supported by a bit of "educated guessing" //#define FFT_DOWNSCALE 0.65f // 20kHz - downscaling factor for FFT results - "Flat-Top" window @20Khz, old freq channels #ifdef FFT_PREFER_EXACT_PEAKS @@ -548,6 +620,46 @@ void FFTcode(void * parameter) fftCalc[13] = fftAddAvg(111,147); // 2220 - 2960 fftCalc[14] = fftAddAvg(147,194); // 2940 - 3900 fftCalc[15] = fftAddAvg(194,250); // 3880 - 5000 // avoid the last 5 bins, which are usually inaccurate +#else +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + /* + * CoreS3 ES7210 mapping calibrated for 16 kHz / 512 samples. + * + * Bin width = 16000 / 512 = 31.25 Hz. + * Bands 0..14 preserve approximately the same physical frequency + * regions as WLED's standard 22.05 kHz mapping. Band 15 is + * Nyquist-limited and stops at bin 250 (7.8125 kHz), deliberately + * avoiding the final five FFT bins. + * + * This branch changes only CoreS3. All other AudioReactive sources + * continue to use the original WLED 22.05 kHz mapping below. + */ + if (useBandPassFilter) { + // preserve the standard >~100 Hz band-pass behavior + fftCalc[ 0] = 0.8f * fftAddAvg(4,6); // 125 - 188 Hz + fftCalc[ 1] = 0.9f * fftAddAvg(6,7); // 188 - 219 Hz + fftCalc[ 2] = fftAddAvg(7,8); // 219 - 250 Hz + fftCalc[ 3] = fftAddAvg(8,10); // 250 - 313 Hz + fftCalc[15] = fftAddAvg(227,250) * 0.75f; // 7094 - 7813 Hz, Nyquist-limited + } else { + fftCalc[ 0] = fftAddAvg(1,3); // 31 - 94 Hz sub-bass + fftCalc[ 1] = fftAddAvg(3,4); // 94 - 125 Hz bass + fftCalc[ 2] = fftAddAvg(4,7); // 125 - 219 Hz bass + fftCalc[ 3] = fftAddAvg(7,10); // 219 - 313 Hz bass + midrange + fftCalc[15] = fftAddAvg(227,250) * 0.70f; // 7094 - 7813 Hz, Nyquist-limited + } + + fftCalc[ 4] = fftAddAvg(10,14); // 313 - 438 Hz midrange + fftCalc[ 5] = fftAddAvg(14,18); // 438 - 563 Hz midrange + fftCalc[ 6] = fftAddAvg(18,26); // 563 - 813 Hz midrange + fftCalc[ 7] = fftAddAvg(26,36); // 813 - 1125 Hz midrange; 1 kHz centered + fftCalc[ 8] = fftAddAvg(36,45); // 1125 - 1406 Hz midrange + fftCalc[ 9] = fftAddAvg(45,61); // 1406 - 1906 Hz midrange + fftCalc[10] = fftAddAvg(61,77); // 1906 - 2406 Hz midrange + high mid + fftCalc[11] = fftAddAvg(77,96); // 2406 - 3000 Hz high mid + fftCalc[12] = fftAddAvg(96,119); // 3000 - 3719 Hz high mid + fftCalc[13] = fftAddAvg(119,143); // 3719 - 4469 Hz high mid + fftCalc[14] = fftAddAvg(143,227) * 0.88f; // 4469 - 7094 Hz high mid + high #else /* new mapping, optimized for 22050 Hz by softhack007 */ // bins frequency range @@ -578,6 +690,7 @@ void FFTcode(void * parameter) fftCalc[12] = fftAddAvg(70,86); // 16 3015 - 3704 high mid fftCalc[13] = fftAddAvg(86,104); // 18 3704 - 4479 high mid fftCalc[14] = fftAddAvg(104,165) * 0.88f; // 61 4479 - 7106 high mid + high -- with slight damping +#endif #endif } else { // noise gate closed - just decay old values for (int i=0; i < NUM_GEQ_CHANNELS; i++) { @@ -830,6 +943,12 @@ class AudioReactive : public Usermod { #else int8_t mclkPin = MCLK_PIN; #endif + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + static constexpr uint8_t CORES3_I2S_INIT_MAX_ATTEMPTS = 5; + static constexpr unsigned long CORES3_I2S_INIT_RETRY_MS = 1000; + uint8_t coreS3I2sInitAttempts = 0; + unsigned long coreS3LastI2sInitAttemptMs = 0; + #endif #endif // new "V2" audiosync struct - 44 Bytes @@ -1414,9 +1533,18 @@ class AudioReactive : public Usermod { #ifdef ARDUINO_ARCH_ESP32 - // Reset I2S peripheral for good measure - i2s_driver_uninstall(I2S_NUM_0); // E (696) I2S: i2s_driver_uninstall(2006): I2S port 0 has not installed - #if !defined(CONFIG_IDF_TARGET_ESP32C3) && (ESP_IDF_VERSION_MAJOR < 5) + // Reset the selected I2S peripheral for good measure. + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType == 7) { + // CoreS3 I2S1 is deferred and has no driver to uninstall at boot. + // CoreS3ES7210Source becomes the sole owner after ES7210 is READY. + } else { + i2s_driver_uninstall(I2S_NUM_0); + } + #else + i2s_driver_uninstall(I2S_NUM_0); + #endif + #if !defined(WLED_M5STACK_CORES3_AUDIO) && !defined(CONFIG_IDF_TARGET_ESP32C3) && (ESP_IDF_VERSION_MAJOR < 5) delay(100); periph_module_reset(PERIPH_I2S0_MODULE); // not possible on -C3, neither on esp-idf V5 #endif @@ -1477,6 +1605,23 @@ class AudioReactive : public Usermod { delay(100); if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + case 7: + DEBUGSR_PRINTLN(F("AR: M5Stack CoreS3 ES7210 built-in dual microphone")); + coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); + coreS3ReleaseMclkButtonOwnership(); + audioSource = new CoreS3ES7210Source(SAMPLE_RATE, BLOCK_SIZE); + useMicFilter = true; + coreS3I2sInitAttempts = 0; + coreS3LastI2sInitAttemptMs = 0; + + if (audioSource && coreS3AudioCodecReady()) { + coreS3I2sInitAttempts = 1; + coreS3LastI2sInitAttemptMs = millis(); + audioSource->initialize(); + } + break; + #endif #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) // legacy ADC driver is not available any more in esp-idf V5.x.y // ADC over I2S is only possible on "classic" ESP32 @@ -1507,12 +1652,21 @@ class AudioReactive : public Usermod { delay(250); // give microphone enough time to initialise if (!audioSource && (dmType != SR_DMTYPE_NETWORK_ONLY)) enabled = false;// audio failed to initialise + + bool coreS3SourcePending = false; + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + coreS3SourcePending = (dmType == 7) && audioSource && !audioSource->isInitialized(); + #endif #endif - if (enabled) onUpdateBegin(false); // create FFT task, and initialize network + #ifdef ARDUINO_ARCH_ESP32 + if (enabled && !coreS3SourcePending) onUpdateBegin(false); // create FFT task when input is ready + #else + if (enabled) onUpdateBegin(false); + #endif #ifdef ARDUINO_ARCH_ESP32 - if (audioSource && FFT_Task == nullptr) enabled = false; // FFT task creation failed + if (audioSource && FFT_Task == nullptr && !coreS3SourcePending) enabled = false; // FFT task creation failed if((!audioSource) || (!audioSource->isInitialized())) { // audio source failed to initialize. Still stay "enabled", as there might be input arriving via UDP Sound Sync #ifdef WLED_DEBUG #define AR_INIT_DEBUG_PRINT DEBUG_PRINTLN @@ -1528,7 +1682,11 @@ class AudioReactive : public Usermod { disableSoundProcessing = true; } #endif + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (enabled && !coreS3SourcePending) disableSoundProcessing = false; + #else if (enabled) disableSoundProcessing = false; // all good - enable audio processing + #endif if (enabled) connectUDPSoundSync(); if (enabled && addPalettes) createAudioPalettes(); initDone = true; @@ -1570,6 +1728,39 @@ class AudioReactive : public Usermod { { static unsigned long lastUMRun = millis(); +#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (enabled && dmType == 7 && audioSource && !audioSource->isInitialized() + && coreS3AudioCodecReady() + && coreS3I2sInitAttempts < CORES3_I2S_INIT_MAX_ATTEMPTS + && (coreS3I2sInitAttempts == 0 + || millis() - coreS3LastI2sInitAttemptMs >= CORES3_I2S_INIT_RETRY_MS)) { + + coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); + coreS3ReleaseMclkButtonOwnership(); + + coreS3I2sInitAttempts++; + coreS3LastI2sInitAttemptMs = millis(); + + DEBUGSR_PRINTF( + "AR: CoreS3 ES7210 I2S1 init attempt %u/%u.\n", + coreS3I2sInitAttempts, + CORES3_I2S_INIT_MAX_ATTEMPTS + ); + + audioSource->initialize(); + + if (audioSource->isInitialized()) { + DEBUGSR_PRINTLN(F("AR: CoreS3 ES7210 I2S1 source READY.")); + if (FFT_Task == nullptr) onUpdateBegin(false); + disableSoundProcessing = false; + lastUMRun = millis(); + } else { + DEBUGSR_PRINTLN(F("AR: CoreS3 ES7210 I2S1 source initialization FAILED.")); + disableSoundProcessing = true; + } + } +#endif + if (!enabled) { disableSoundProcessing = true; // keep processing suspended (FFT task) lastUMRun = millis(); // update time keeping @@ -1888,6 +2079,9 @@ class AudioReactive : public Usermod { infoArr.add(F("ADC analog")); } else { if (dmType == 5) infoArr.add(F("PDM digital")); // dmType 5 => generic PDM microphone + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + else if (dmType == 7) infoArr.add(F("CoreS3 ES7210 / I2S1")); + #endif else infoArr.add(F("I2S digital")); } // input level or "silence" @@ -1900,8 +2094,22 @@ class AudioReactive : public Usermod { } } else { // error during audio source setup - infoArr.add(F("not initialized")); - infoArr.add(F(" - check pin settings")); + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType == 7) { + infoArr.add(F("CoreS3 ES7210 / I2S1")); + if (!coreS3AudioCodecReady()) { + infoArr.add(F(" - waiting codec")); + } else if (coreS3I2sInitAttempts >= CORES3_I2S_INIT_MAX_ATTEMPTS) { + infoArr.add(F(" - init failed")); + } else { + infoArr.add(F(" - initializing")); + } + } else + #endif + { + infoArr.add(F("not initialized")); + infoArr.add(F(" - check pin settings")); + } } } @@ -1913,6 +2121,16 @@ class AudioReactive : public Usermod { infoArr.add(F("suspended")); } + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType == 7) { + infoArr = user.createNestedArray(F("CoreS3 FFT")); + infoArr.add(F("16kHz / 512 samples / 31.25Hz bin")); + + infoArr = user.createNestedArray(F("CoreS3 FFT Mapping")); + infoArr.add(F("16 bands calibrated / 7.81kHz max")); + } + #endif + // AGC or manual Gain if ((soundAgc==0) && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) { infoArr = user.createNestedArray(F("Manual Gain")); @@ -2064,11 +2282,17 @@ class AudioReactive : public Usermod { JsonObject dmic = top.createNestedObject(FPSTR(_digitalmic)); dmic["type"] = dmType; - JsonArray pinArray = dmic.createNestedArray("pin"); - pinArray.add(i2ssdPin); - pinArray.add(i2swsPin); - pinArray.add(i2sckPin); - pinArray.add(mclkPin); + + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType != 7) + #endif + { + JsonArray pinArray = dmic.createNestedArray("pin"); + pinArray.add(i2ssdPin); + pinArray.add(i2swsPin); + pinArray.add(i2sckPin); + pinArray.add(mclkPin); + } JsonObject cfg = top.createNestedObject(FPSTR(_config)); cfg[F("squelch")] = soundSquelch; @@ -2137,10 +2361,17 @@ class AudioReactive : public Usermod { if (dmType == 5) dmType = SR_DMTYPE; // MCU does not support PDM #endif - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][0], i2ssdPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][1], i2swsPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][2], i2sckPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][3], mclkPin); + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType == 7) { + coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); + } else + #endif + { + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][0], i2ssdPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][1], i2swsPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][2], i2sckPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][3], mclkPin); + } configComplete &= getJsonValue(top[FPSTR(_config)][F("squelch")], soundSquelch); configComplete &= getJsonValue(top[FPSTR(_config)][F("gain")], sampleGain); @@ -2190,6 +2421,9 @@ class AudioReactive : public Usermod { uiScript.print(F("addOption(dd,'Generic PDM',5);")); #endif uiScript.print(F("addOption(dd,'ES8388',6);")); + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + uiScript.print(F("addOption(dd,'M5Stack CoreS3 ES7210',7);")); + #endif uiScript.print(F("addOption(dd,'None - network receive only',")); uiScript.print(SR_DMTYPE_NETWORK_ONLY); uiScript.print(F(");")); @@ -2221,15 +2455,25 @@ class AudioReactive : public Usermod { #endif uiScript.print(F("addOption(dd,'Receive',2);")); #ifdef ARDUINO_ARCH_ESP32 - uiScript.print(F("addInfo(ux+':digitalmic:type',1,'requires reboot!');")); // 0 is field type, 1 is actual field - uiScript.print(F("addInfo(uxp,0,'sd/data/dout','I2S SD');")); - uiScript.print(F("addInfo(uxp,1,'ws/clk/lrck','I2S WS');")); - uiScript.print(F("addInfo(uxp,2,'sck/bclk','I2S SCK');")); - #if defined(CONFIG_IDF_TARGET_ESP32) - uiScript.print(F("addInfo(uxp,3,'only use -1, 0, 1 or 3','I2S MCLK');")); - #else - uiScript.print(F("addInfo(uxp,3,'master clock','I2S MCLK');")); - #endif + #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + if (dmType == 7) { + uiScript.print(F( + "addInfo(ux+':digitalmic:type',1," + "'fixed internal pins: DIN14 / WS33 / BCLK34 / MCLK0; requires reboot!');" + )); + } else + #endif + { + uiScript.print(F("addInfo(ux+':digitalmic:type',1,'requires reboot!');")); // 0 is field type, 1 is actual field + uiScript.print(F("addInfo(uxp,0,'sd/data/dout','I2S SD');")); + uiScript.print(F("addInfo(uxp,1,'ws/clk/lrck','I2S WS');")); + uiScript.print(F("addInfo(uxp,2,'sck/bclk','I2S SCK');")); + #if defined(CONFIG_IDF_TARGET_ESP32) + uiScript.print(F("addInfo(uxp,3,'only use -1, 0, 1 or 3','I2S MCLK');")); + #else + uiScript.print(F("addInfo(uxp,3,'master clock','I2S MCLK');")); + #endif + } #endif } diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index c2e67c2be5..03347301ac 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -168,8 +168,18 @@ class AudioSource { */ class I2SSource : public AudioSource { public: - I2SSource(SRate_t sampleRate, int blockSize, float sampleScale = 1.0f) : - AudioSource(sampleRate, blockSize, sampleScale) { + I2SSource( + SRate_t sampleRate, + int blockSize, + float sampleScale = 1.0f, + i2s_port_t i2sPort = I2S_NUM_0, + i2s_channel_t channelMode = I2S_CHANNEL_MONO, + bool managePins = true + ) : + AudioSource(sampleRate, blockSize, sampleScale), + _i2sPort(i2sPort), + _channelMode(channelMode), + _managePins(managePins) { _config = { .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = _sampleRate, @@ -195,7 +205,7 @@ class I2SSource : public AudioSource { virtual void initialize(int8_t i2swsPin = I2S_PIN_NO_CHANGE, int8_t i2ssdPin = I2S_PIN_NO_CHANGE, int8_t i2sckPin = I2S_PIN_NO_CHANGE, int8_t mclkPin = I2S_PIN_NO_CHANGE) { DEBUGSR_PRINTLN(F("I2SSource:: initialize().")); - if (i2swsPin != I2S_PIN_NO_CHANGE && i2ssdPin != I2S_PIN_NO_CHANGE) { + if (_managePins && i2swsPin != I2S_PIN_NO_CHANGE && i2ssdPin != I2S_PIN_NO_CHANGE) { if (!PinManager::allocatePin(i2swsPin, true, PinOwner::UM_Audioreactive) || !PinManager::allocatePin(i2ssdPin, false, PinOwner::UM_Audioreactive)) { // #206 DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pins: ws=%d, sd=%d\n", i2swsPin, i2ssdPin); @@ -205,7 +215,7 @@ class I2SSource : public AudioSource { // i2ssckPin needs special treatment, since it might be unused on PDM mics if (i2sckPin != I2S_PIN_NO_CHANGE) { - if (!PinManager::allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) { + if (_managePins && !PinManager::allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) { DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pins: sck=%d\n", i2sckPin); return; } @@ -250,13 +260,16 @@ class I2SSource : public AudioSource { #endif #endif - // Reserve the master clock pin if provided + // Reserve the master clock pin if provided. + // CoreS3 internal audio pins are fixed board resources and intentionally + // bypass WLED PinManager; generic AudioReactive sources keep the + // original PinManager behavior. _mclkPin = mclkPin; if (mclkPin != I2S_PIN_NO_CHANGE) { - if(!PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { - DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pin: MCLK=%d\n", mclkPin); + if (_managePins && !PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { + DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pin: MCLK=%d\n", mclkPin); return; - } else + } _routeMclk(mclkPin); } @@ -272,32 +285,32 @@ class I2SSource : public AudioSource { //DEBUGSR_PRINTF("[AR] I2S: SD=%d, WS=%d, SCK=%d, MCLK=%d\n", i2ssdPin, i2swsPin, i2sckPin, mclkPin); - esp_err_t err = i2s_driver_install(I2S_NUM_0, &_config, 0, nullptr); + esp_err_t err = i2s_driver_install(_i2sPort, &_config, 0, nullptr); if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to install i2s driver: %d\n", err); return; } - DEBUGSR_PRINTF("AR: I2S#0 driver %s aPLL; fixed_mclk=%d.\n", _config.use_apll? "uses":"without", _config.fixed_mclk); + DEBUGSR_PRINTF("AR: I2S#%d driver %s aPLL; fixed_mclk=%d.\n", (int)_i2sPort, _config.use_apll? "uses":"without", _config.fixed_mclk); DEBUGSR_PRINTF("AR: %d bits, Sample scaling factor = %6.4f\n", _config.bits_per_sample, _sampleScale); if (_config.mode & I2S_MODE_PDM) { - DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in PDM MASTER mode.")); + DEBUGSR_PRINTF("AR: I2S#%d driver installed in PDM MASTER mode.\n", (int)_i2sPort); } else { - DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in MASTER mode.")); + DEBUGSR_PRINTF("AR: I2S#%d driver installed in MASTER mode.\n", (int)_i2sPort); } - err = i2s_set_pin(I2S_NUM_0, &_pinConfig); + err = i2s_set_pin(_i2sPort, &_pinConfig); if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to set i2s pin config: %d\n", err); - i2s_driver_uninstall(I2S_NUM_0); // uninstall already-installed driver + i2s_driver_uninstall(_i2sPort); // uninstall already-installed driver return; } #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - err = i2s_set_clk(I2S_NUM_0, _sampleRate, I2S_SAMPLE_RESOLUTION, I2S_CHANNEL_MONO); // set bit clocks. Also takes care of MCLK routing if needed. + err = i2s_set_clk(_i2sPort, _sampleRate, _config.bits_per_sample, _channelMode); // set bit clocks. Also takes care of MCLK routing if needed. if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to configure i2s clocks: %d\n", err); - i2s_driver_uninstall(I2S_NUM_0); // uninstall already-installed driver + i2s_driver_uninstall(_i2sPort); // uninstall already-installed driver return; } #endif @@ -306,16 +319,18 @@ class I2SSource : public AudioSource { virtual void deinitialize() { _initialized = false; - esp_err_t err = i2s_driver_uninstall(I2S_NUM_0); + esp_err_t err = i2s_driver_uninstall(_i2sPort); if (err != ESP_OK) { DEBUGSR_PRINTF("Failed to uninstall i2s driver: %d\n", err); return; } - if (_pinConfig.ws_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.ws_io_num, PinOwner::UM_Audioreactive); - if (_pinConfig.data_in_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.data_in_num, PinOwner::UM_Audioreactive); - if (_pinConfig.bck_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.bck_io_num, PinOwner::UM_Audioreactive); - // Release the master clock pin - if (_mclkPin != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_mclkPin, PinOwner::UM_Audioreactive); + if (_managePins) { + if (_pinConfig.ws_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.ws_io_num, PinOwner::UM_Audioreactive); + if (_pinConfig.data_in_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.data_in_num, PinOwner::UM_Audioreactive); + if (_pinConfig.bck_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.bck_io_num, PinOwner::UM_Audioreactive); + // Release the master clock pin + if (_mclkPin != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_mclkPin, PinOwner::UM_Audioreactive); + } } virtual void getSamples(FFTsampleType *buffer, uint16_t num_samples) { @@ -324,7 +339,7 @@ class I2SSource : public AudioSource { size_t bytes_read = 0; /* Counter variable to check if we actually got enough data */ I2S_datatype newSamples[num_samples]; /* Intermediary sample storage */ - err = i2s_read(I2S_NUM_0, (void *)newSamples, sizeof(newSamples), &bytes_read, portMAX_DELAY); + err = i2s_read(_i2sPort, (void *)newSamples, sizeof(newSamples), &bytes_read, portMAX_DELAY); if (err != ESP_OK) { DEBUGSR_PRINTF("Failed to get samples: %d\n", err); return; @@ -396,8 +411,116 @@ class I2SSource : public AudioSource { i2s_config_t _config; i2s_pin_config_t _pinConfig; int8_t _mclkPin; + i2s_port_t _i2sPort; + i2s_channel_t _channelMode; + bool _managePins; }; + +#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) +/* + * M5Stack CoreS3 ES7210 source + * + * ES7210 codec setup is performed by CoreS3_Audio. This source waits until + * the codec-ready signal is published, then becomes the sole owner of + * I2S_NUM_1 for PCM sampling. + * + * Hardware-verified CoreS3 audio path: + * MCLK GPIO0 + * BCLK GPIO34 + * WS GPIO33 + * DIN GPIO14 + * I2S_NUM_1 + * Stereo 16-bit / 16000 Hz + */ +class CoreS3ES7210Source : public I2SSource { + public: + static constexpr uint16_t MAX_MONO_SAMPLES = 512; + CoreS3ES7210Source(SRate_t sampleRate, int blockSize) : + I2SSource( + sampleRate, + blockSize, + 1.0f / 16.0f, + I2S_NUM_1, + I2S_CHANNEL_STEREO, + false + ) { + _config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX); + _config.sample_rate = _sampleRate; + _config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; + _config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; + _config.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S); + _config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; + _config.dma_buf_count = 8; + _config.dma_buf_len = _blockSize; + _config.use_apll = false; + _config.tx_desc_auto_clear = false; + _config.fixed_mclk = 0; +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) + _config.mclk_multiple = I2S_MCLK_MULTIPLE_256; + _config.bits_per_chan = I2S_BITS_PER_CHAN_16BIT; +#endif + } + + void initialize( + int8_t = I2S_PIN_NO_CHANGE, + int8_t = I2S_PIN_NO_CHANGE, + int8_t = I2S_PIN_NO_CHANGE, + int8_t = I2S_PIN_NO_CHANGE + ) override { + DEBUGSR_PRINTLN(F("CoreS3ES7210Source:: initialize fixed internal pins;")); + + // CoreS3 ES7210 pins are board-internal fixed resources. + // Ignore Usermod pin dropdown values and bypass WLED PinManager. + I2SSource::initialize( + 33, // WS / LRCK + 14, // SD / DIN + 34, // BCLK + 0 // MCLK + ); + } + + void getSamples(FFTsampleType *buffer, uint16_t num_samples) override { + if (buffer == nullptr || num_samples == 0) return; + memset(buffer, 0, num_samples * sizeof(FFTsampleType)); + + if (!_initialized || num_samples > MAX_MONO_SAMPLES) return; + + int16_t stereoSamples[MAX_MONO_SAMPLES * 2]; + const size_t requestedBytes = (size_t)num_samples * 2U * sizeof(int16_t); + size_t bytesRead = 0; + + esp_err_t err = i2s_read( + _i2sPort, + stereoSamples, + requestedBytes, + &bytesRead, + portMAX_DELAY + ); + + if (err != ESP_OK) { + DEBUGSR_PRINTF("AR: CoreS3 ES7210 sample read failed: %d\n", err); + return; + } + + const size_t framesRead = bytesRead / (sizeof(int16_t) * 2U); + const size_t framesToCopy = min((size_t)num_samples, framesRead); + + for (size_t i = 0; i < framesToCopy; i++) { + const int32_t left = stereoSamples[i * 2U]; + const int32_t right = stereoSamples[i * 2U + 1U]; + const float mono = ((float)(left + right) * 0.5f) * _sampleScale; + +#if defined(UM_AUDIOREACTIVE_USE_INTEGER_FFT) + buffer[i] = (int16_t)constrain((int32_t)lroundf(mono), (int32_t)INT16_MIN, (int32_t)INT16_MAX); +#else + buffer[i] = mono; +#endif + } + } +}; +#endif + /* ES7243 Microphone This is an I2S microphone that requires initialization over I2C before I2S data can be received diff --git a/wled00/wled.cpp b/wled00/wled.cpp index f049ecaaa5..25841c4959 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -25,6 +25,14 @@ #endif extern "C" void usePWMFixedNMI(); +// Optional usermod bus re-init gate. +// The weak default preserves standard WLED behavior. CoreS3_Power provides +// the strong implementation used to protect old LED output before bus rebuild. +extern "C" bool __attribute__((weak)) coreS3PowerShouldDeferBusReinit() +{ + return false; +} + // millis()-rollover counter (millis() wraps every ~50 days) - previously // WLED_GLOBAL. json.cpp and usermods only ever read it for uptime reporting, // so it gets a by-value getter rather than a mutable reference: an accidental @@ -237,14 +245,18 @@ void WLED::loop() //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! if (doInitBusses) { - doInitBusses = false; - DEBUG_PRINTLN(F("Re-init busses.")); - bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) - strip.finalizeInit(); // will create buses and also load default ledmap if present - if (aligned) strip.makeAutoSegments(); - else strip.fixInvalidSegments(); - BusManager::setBrightness(scaledBri(bri)); // fix re-initialised bus' brightness #4005 and #4824 - configNeedsWrite = true; + // Allow a usermod to defer this rebuild until the old LED output is safe. + // The weak default returns false, preserving standard WLED behavior. + if (!coreS3PowerShouldDeferBusReinit()) { + doInitBusses = false; + DEBUG_PRINTLN(F("Re-init busses.")); + bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) + strip.finalizeInit(); // will create buses and also load default ledmap if present + if (aligned) strip.makeAutoSegments(); + else strip.fixInvalidSegments(); + BusManager::setBrightness(scaledBri(bri)); // fix re-initialised bus' brightness #4005 and #4824 + configNeedsWrite = true; + } } if (loadLedmap >= 0) { strip.deserializeMap(loadLedmap); From 9f36e0448a01b89f61768badebc1fe29bb238e01 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sun, 6 Sep 2026 10:29:55 +0900 Subject: [PATCH 02/35] Clean up CoreS3 files from initial PR review --- docs/M5Stack_CoreS3.md | 20 ----- .../M5StackDisplayTouchHelpers.h | 77 ++++++++++--------- .../CoreS3_Display/M5StackDisplayTouchState.h | 6 +- ...example => platformio_override.ini.sample} | 0 4 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 docs/M5Stack_CoreS3.md rename usermods/CoreS3_Display/{platformio_override.ini.example => platformio_override.ini.sample} (100%) diff --git a/docs/M5Stack_CoreS3.md b/docs/M5Stack_CoreS3.md deleted file mode 100644 index 5609d139ae..0000000000 --- a/docs/M5Stack_CoreS3.md +++ /dev/null @@ -1,20 +0,0 @@ -# M5Stack CoreS3 Support for WLED - -The M5Stack CoreS3 integration combines multiple usermods and CoreS3-specific build support, including the local touch UI, power management, built-in microphone Audio Reactive support, and ESP32-S3 RMT stabilization. - -The complete documentation is maintained with the CoreS3 Display usermod: - -- **English:** [M5Stack CoreS3 Documentation](../usermods/CoreS3_Display/readme.md) -- **日本語:** [M5Stack CoreS3 日本語ドキュメント](../usermods/CoreS3_Display/readme_jp.md) - -## Main Components - -- `usermods/CoreS3_Power/` -- `usermods/CoreS3_Display/` -- `usermods/CoreS3_Audio/` -- `usermods/audioreactive/` -- `pio-scripts/cores3_upload_watchdog_reset.py` -- `pio-scripts/cores3_v17_neopixelbus_patch.py` -- `usermods/CoreS3_Display/platformio_override.ini.example` - -The CoreS3-specific implementation is kept outside the WLED core as much as practical, with only a minimal integration hook in `wled00/wled.cpp`, to make future upstream synchronization easier. diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h index 3be4e13bc2..008586af43 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h +++ b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h @@ -15,11 +15,13 @@ inline void resetRepeatTouch( M5StackRepeatTouchState& state ) { state.lastRepeat = 0; state.longPressActive = false; } + inline void beginRepeatTouch( M5StackRepeatTouchState& state, unsigned long now ) { state.pressStart = now; state.lastRepeat = now; state.longPressActive = false; } + inline bool serviceRepeatTouch( M5StackRepeatTouchState& state, unsigned long now, unsigned long longPressMs, unsigned long repeatMs ) { if ( !state.longPressActive && now - state.pressStart >= longPressMs ) { state.longPressActive = true; @@ -34,47 +36,50 @@ inline bool serviceRepeatTouch( M5StackRepeatTouchState& state, unsigned long no return false; } + inline bool pointInsideRect( int16_t px, int16_t py, const M5StackTouchRect& rect ) { return ( px >= rect.x && px < rect.x + rect.w && py >= rect.y && py < rect.y + rect.h ); } -bool isPowerButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_POWER ); } -bool isBrightnessDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_DOWN ); } -bool isBrightnessUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_UP ); } -bool isEffectPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_PREV ); } -bool isEffectDetailTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_DETAIL ); } -bool isEffectNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_NEXT ); } -bool isColorButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_COLOR_OPEN ); } -bool isPresetOpenButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_OPEN ); } -bool isBackButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BACK ); } -bool isHueDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_DOWN ); } -bool isHueUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_UP ); } -bool isSaturationDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_DOWN ); } -bool isSaturationUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_UP ); } -bool isSpeedDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_DOWN ); } -bool isSpeedUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_UP ); } -bool isIntensityDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_DOWN ); } -bool isIntensityUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_UP ); } -bool isPalettePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_PREV ); } -bool isPaletteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_NEXT ); } -bool isPresetPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_PREV ); } -bool isPresetNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_NEXT ); } -bool isPresetManageTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_MANAGE ); } -bool isPresetSaveNewTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_NEW ); } -bool isPresetSaveHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_HOLD ); } -bool isPresetOverwriteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_OPEN ); } -bool isPresetOverwritePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_PREV ); } -bool isPresetOverwriteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_NEXT ); } -bool isPresetOverwriteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_HOLD ); } -bool isPresetDeleteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_OPEN ); } -bool isPresetDeletePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_PREV ); } -bool isPresetDeleteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_NEXT ); } -bool isPresetDeleteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_HOLD ); } -bool isPresetBootOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_OPEN ); } -bool isPresetBootPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_PREV ); } -bool isPresetBootNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_NEXT ); } -bool isPresetBootHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_HOLD ); } +inline bool isPowerButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_POWER ); } +inline bool isBrightnessDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_DOWN ); } +inline bool isBrightnessUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BRIGHTNESS_UP ); } +inline bool isEffectPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_PREV ); } +inline bool isEffectDetailTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_DETAIL ); } +inline bool isEffectNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_EFFECT_NEXT ); } +inline bool isColorButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_COLOR_OPEN ); } +inline bool isPresetOpenButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_OPEN ); } +inline bool isBackButtonTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BACK ); } +inline bool isHueDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_DOWN ); } +inline bool isHueUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_HUE_UP ); } +inline bool isSaturationDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_DOWN ); } +inline bool isSaturationUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SATURATION_UP ); } +inline bool isSpeedDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_DOWN ); } +inline bool isSpeedUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_SPEED_UP ); } +inline bool isIntensityDownTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_DOWN ); } +inline bool isIntensityUpTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_INTENSITY_UP ); } +inline bool isPalettePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_PREV ); } +inline bool isPaletteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PALETTE_NEXT ); } +inline bool isPresetPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_PREV ); } +inline bool isPresetNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_NEXT ); } +inline bool isPresetManageTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_MANAGE ); } +inline bool isPresetSaveNewTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_NEW ); } +inline bool isPresetSaveHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_PRESET_SAVE_HOLD ); } +inline bool isPresetOverwriteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_OPEN ); } +inline bool isPresetOverwritePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_PREV ); } +inline bool isPresetOverwriteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_NEXT ); } +inline bool isPresetOverwriteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_OVERWRITE_HOLD ); } +inline bool isPresetDeleteOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_OPEN ); } +inline bool isPresetDeletePrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_PREV ); } +inline bool isPresetDeleteNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_NEXT ); } +inline bool isPresetDeleteHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_DELETE_HOLD ); } +inline bool isPresetBootOpenTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_OPEN ); } +inline bool isPresetBootPrevTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_PREV ); } +inline bool isPresetBootNextTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_NEXT ); } +inline bool isPresetBootHoldTouched( int16_t x, int16_t y ) { return pointInsideRect( x, y, M5STACK_TOUCH_BOOT_HOLD ); } + inline bool isTouchTargetPair( M5StackTouchTarget target, M5StackTouchTarget firstTarget, M5StackTouchTarget secondTarget ) { return target == firstTarget || target == secondTarget; } + } // namespace M5StackDisplayTouchHelpers diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchState.h b/usermods/CoreS3_Display/M5StackDisplayTouchState.h index f68daa17ae..db28a4e595 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchState.h +++ b/usermods/CoreS3_Display/M5StackDisplayTouchState.h @@ -6,7 +6,7 @@ // // These types contain no hardware access and no WLED operations. // Touch geometry is defined separately in M5StackDisplayUI.h. -// The runtime state machine remains in CoreS3_Display.cpp for now. +// The runtime state machine lives in M5StackDisplayTouchStateMachine.inc. // Touch action selected when a press begins. enum M5StackTouchTarget : uint8_t { @@ -44,7 +44,8 @@ enum M5StackTouchTarget : uint8_t { M5STACK_TOUCH_TARGET_PRESET_DELETE_OPEN, M5STACK_TOUCH_TARGET_PRESET_DELETE_PREV, M5STACK_TOUCH_TARGET_PRESET_DELETE_NEXT, M5STACK_TOUCH_TARGET_PRESET_DELETE_HOLD, - M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN, M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV, M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT, M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD }; + M5STACK_TOUCH_TARGET_PRESET_BOOT_OPEN, M5STACK_TOUCH_TARGET_PRESET_BOOT_PREV, M5STACK_TOUCH_TARGET_PRESET_BOOT_NEXT, M5STACK_TOUCH_TARGET_PRESET_BOOT_HOLD +}; // One hit-test snapshot built from the current Touch coordinates. struct M5StackTouchHitState { @@ -144,6 +145,7 @@ struct M5StackTouchRuntimeState { M5StackRepeatTouchState intensityRepeatState; M5StackRepeatTouchState paletteRepeatState; M5StackRepeatTouchState presetRepeatState; + // ESP32 toolchains use a 16-bit signed short here. // Using the fundamental type also keeps VS Code IntelliSense from // mis-parsing these final coordinate members in this header. diff --git a/usermods/CoreS3_Display/platformio_override.ini.example b/usermods/CoreS3_Display/platformio_override.ini.sample similarity index 100% rename from usermods/CoreS3_Display/platformio_override.ini.example rename to usermods/CoreS3_Display/platformio_override.ini.sample From 109d3fa51259ab6c54ea0aae742a5a8c9174ea61 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 7 Sep 2026 23:06:13 +0900 Subject: [PATCH 03/35] Remove CoreS3 upload watchdog reset workaround --- pio-scripts/cores3_upload_watchdog_reset.py | 51 ------------------- .../platformio_override.ini.sample | 9 +--- usermods/CoreS3_Display/readme.md | 8 ++- usermods/CoreS3_Display/readme_jp.md | 8 ++- 4 files changed, 7 insertions(+), 69 deletions(-) delete mode 100644 pio-scripts/cores3_upload_watchdog_reset.py diff --git a/pio-scripts/cores3_upload_watchdog_reset.py b/pio-scripts/cores3_upload_watchdog_reset.py deleted file mode 100644 index 57c955fa63..0000000000 --- a/pio-scripts/cores3_upload_watchdog_reset.py +++ /dev/null @@ -1,51 +0,0 @@ -# M5Stack CoreS3 upload reset stabilization -# -# Purpose: -# ESP32-S3 native USB / USB-Serial-JTAG uploads can occasionally remain -# in download mode after PlatformIO's default RTS hard reset. -# -# PlatformIO's ESP32 builder constructs esptool's --after argument from: -# board.get("upload.after_reset", "hard_reset") -# -# Set that board option early, before the platform builder creates -# UPLOADERFLAGS. This changes only the post-upload reset method. -# -# Expected esptool behavior: -# --after watchdog-reset -# -# No firmware source, build flag, partition, PSRAM, flash, or runtime -# behavior is modified by this script. - -Import("env") - -TARGET_RESET_METHOD = "watchdog-reset" - - -def configure_upload_reset(): - board = env.BoardConfig() - previous = board.get("upload.after_reset", "hard_reset") - - if previous == TARGET_RESET_METHOD: - print( - "[CoreS3 Upload] post-upload reset already set to " - f"{TARGET_RESET_METHOD}" - ) - return - - board.update("upload.after_reset", TARGET_RESET_METHOD) - - effective = board.get("upload.after_reset", "") - if effective != TARGET_RESET_METHOD: - raise RuntimeError( - "CoreS3 upload reset configuration failed: " - f"expected {TARGET_RESET_METHOD}, got {effective}" - ) - - print( - "[CoreS3 Upload] post-upload reset: " - f"{previous} -> {TARGET_RESET_METHOD}" - ) - - -if not env.IsIntegrationDump(): - configure_upload_reset() diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 59f735c8af..d9d3cc16df 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -17,7 +17,6 @@ ; - the validated CoreS3 Quad PSRAM configuration ; - CoreS3 Power / Display / Audio / AudioReactive integration ; - the validated ESP32-S3 NeoPixelBus RMT DMA1024 and LCD/GDMA patches -; - watchdog-reset upload recovery for ESP32-S3 native USB ; ; IMPORTANT: ; Do NOT set lib_archive=yes in this custom environment. @@ -42,15 +41,9 @@ platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} ; Preserve normal WLED build scripts. ; -; PRE 1: -; Select watchdog reset for the post-upload ESP32-S3 reset sequence -; before PlatformIO constructs the esptool command. -; -; PRE 2: -; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024 and LCD/GDMA patches. +; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024 and LCD/GDMA patches. extra_scripts = ${scripts_defaults.extra_scripts} - pre:pio-scripts/cores3_upload_watchdog_reset.py pre:pio-scripts/cores3_v17_neopixelbus_patch.py custom_usermods = diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 13b80cccc1..1cb30eec94 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -233,7 +233,7 @@ GPIO0 is used for the ES7210 MCLK and is therefore excluded from the WLED physic A ready-to-use CoreS3 PlatformIO override example is included at: ```text -usermods/CoreS3_Display/platformio_override.ini.example +usermods/CoreS3_Display/platformio_override.ini.sample ``` Copy it to the WLED repository root and rename it to: @@ -242,7 +242,7 @@ Copy it to the WLED repository root and rename it to: platformio_override.ini ``` -The example includes the validated CoreS3 environment, Quad PSRAM settings, CoreS3 usermods, Audio Reactive definitions, NeoPixelBus patch pre-scripts, and the post-upload watchdog-reset handling. +The example includes the CoreS3 environment, Quad PSRAM settings, CoreS3 usermods, Audio Reactive definitions, and the NeoPixelBus patch pre-script. ## Build @@ -257,7 +257,7 @@ The example includes the validated CoreS3 environment, Quad PSRAM settings, Core Open the WLED repository in VS Code. -Confirm that `platformio_override.ini` exists at the repository root. If it does not, copy `usermods/CoreS3_Display/platformio_override.ini.example` to the repository root and rename it to `platformio_override.ini`. +Confirm that `platformio_override.ini` exists at the repository root. If it does not, copy `usermods/CoreS3_Display/platformio_override.ini.sample` to the repository root and rename it to `platformio_override.ini`. ### 3. Build @@ -281,7 +281,6 @@ Upload the `m5stack_cores3` environment from PlatformIO. If M5Burner Serial Monitor or another program is holding the COM port open, close it before Upload. -For CoreS3, the PlatformIO post-upload reset is changed to watchdog-reset to improve recovery after ESP32-S3 native USB flashing. --- @@ -464,7 +463,6 @@ The main CoreS3-specific files are: WLED/ ├─ platformio_override.ini ├─ pio-scripts/ -│ ├─ cores3_upload_watchdog_reset.py │ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 3dd65fa33e..108801809f 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -233,7 +233,7 @@ GPIO0 は ES7210 MCLK として使用するため、WLED の物理 Button から CoreS3 用の PlatformIO 設定サンプルを次の場所に同梱しています。 ```text -usermods/CoreS3_Display/platformio_override.ini.example +usermods/CoreS3_Display/platformio_override.ini.sample ``` このファイルを WLED リポジトリ直下へコピーし、次の名前に変更して使用します。 @@ -242,7 +242,7 @@ usermods/CoreS3_Display/platformio_override.ini.example platformio_override.ini ``` -このサンプルには、検証済みの CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、NeoPixelBus patch 用 pre-script、および Upload 後の watchdog-reset 処理が含まれています。 +このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、および NeoPixelBus patch 用 pre-script が含まれています。 ## Build @@ -257,7 +257,7 @@ platformio_override.ini WLED リポジトリを VS Code で開きます。 -`platformio_override.ini` がリポジトリ直下にあることを確認してください。存在しない場合は、`usermods/CoreS3_Display/platformio_override.ini.example` をリポジトリ直下へコピーし、`platformio_override.ini` にリネームしてください。 +`platformio_override.ini` がリポジトリ直下にあることを確認してください。存在しない場合は、`usermods/CoreS3_Display/platformio_override.ini.sample` をリポジトリ直下へコピーし、`platformio_override.ini` にリネームしてください。 ### 3. Build @@ -281,7 +281,6 @@ PlatformIO から `m5stack_cores3` を Upload します。 M5Burner Serial Monitor などが COM ポートを開いている場合は、Upload 前に閉じてください。 -CoreS3 では ESP32-S3 native USB の Upload 後復帰を安定させるため、PlatformIO の post-upload reset を watchdog-reset に変更しています。 --- @@ -464,7 +463,6 @@ CoreS3 対応の中心は次のファイルです。 WLED/ ├─ platformio_override.ini ├─ pio-scripts/ -│ ├─ cores3_upload_watchdog_reset.py │ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ From 88f8a4f9713886d7f0518c6b06ef1eaa100465ff Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 7 Sep 2026 23:34:23 +0900 Subject: [PATCH 04/35] Clean up CoreS3 README --- usermods/CoreS3_Display/readme.md | 72 +------------------------------ 1 file changed, 2 insertions(+), 70 deletions(-) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 1cb30eec94..8c8380a620 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -5,7 +5,7 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
**local touch control / built-in microphone Audio Reactive / battery status / safe power-off / LED control** in a single device. -> **Status:** CoreS3 production baseline
+> **Status:** Hardware-validated CoreS3 implementation
> **Base:** WLED 17.0.0-devV5
> **Target:** M5Stack CoreS3 (ESP32-S3 / 16MB Flash / 8MB Quad PSRAM) > @@ -279,7 +279,7 @@ m5stack_cores3 SUCCESS Upload the `m5stack_cores3` environment from PlatformIO. -If M5Burner Serial Monitor or another program is holding the COM port open, close it before Upload. +If another serial monitor or program is holding the COM port open, close it before Upload. --- @@ -510,22 +510,6 @@ Adds CoreS3 built-in microphone / I2S1 integration. --- -## Design Policy - -This project intentionally minimizes changes to the WLED core. - -CoreS3-specific logic is primarily contained in: - -```text -usermods/ -pio-scripts/ -platformio_override.ini -``` - -This makes future synchronization with WLED upstream easier. - ---- - ## Current Limitations / Notes - Segment management is intentionally not implemented in the local UI.
@@ -535,38 +519,6 @@ This makes future synchronization with WLED upstream easier. - A starting Brightness around 64 is recommended for CoreS3. - Browser Screenshot returns a still BMP image; it is not a live stream. - DCDC OVP protection is not disabled. -- Core2 / Core2 for AWS porting is planned as a separate phase after the CoreS3 version is complete. - ---- - -## Validation Summary - -The following items have been validated on CoreS3 hardware: - -- Normal Boot -- Reset Reboot -- LED ON / OFF -- Effect control -- Multi-Color -- BLACK Toggle -- Preset call / management -- CoreS3 ↔ Web UI sync -- Sleep / Wake -- Wi-Fi reconnect -- Recovery AP -- Audio Reactive -- Battery display -- Runtime Health -- Safe Shutdown cancel -- Safe Shutdown full power-off -- Reboot after shutdown -- Active-use endurance -- Idle endurance -- RMT tail-pixel regression -- NeoPixelBus clean dependency re-patch -- Browser Screenshot -- Screenshot color accuracy -- Screenshot + normal Touch/UI operation --- @@ -590,26 +542,6 @@ Refer to the repository `LICENSE` file and the respective upstream projects for --- -## Roadmap - -Remaining work toward CoreS3 v1.0: - -- [x] Power stabilization -- [x] Display / Touch UI -- [x] Audio Reactive -- [x] Preset management -- [x] Battery / Health UX -- [x] Recovery AP -- [x] Safe Shutdown -- [x] NeoPixelBus RMT DMA1024 / LCD-GDMA stabilization -- [x] Browser Screenshot -- [ ] Final release branch / tag -- [ ] Release notes -- [ ] Optional additional screenshots -- [ ] Core2 / Core2 for AWS porting - ---- - ## Acknowledgements - WLED project and contributors From 3fa8fa081a23d1796097fa0b5e578d84dead63b9 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 7 Sep 2026 23:56:11 +0900 Subject: [PATCH 05/35] Sync Japanese CoreS3 README cleanup --- usermods/CoreS3_Display/readme_jp.md | 74 +--------------------------- 1 file changed, 2 insertions(+), 72 deletions(-) diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 108801809f..d03354cfe6 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -5,7 +5,7 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
**タッチディスプレイ / 内蔵マイク Audio Reactive / バッテリー表示 / 安全な電源OFF / LED制御** を1台にまとめるプロジェクトです。 -> **Status:** CoreS3 production baseline
+> **Status:** CoreS3 実機検証済み実装
> **Base:** WLED 17.0.0-devV5
> **Target:** M5Stack CoreS3 (ESP32-S3 / 16MB Flash / 8MB Quad PSRAM) > @@ -279,7 +279,7 @@ m5stack_cores3 SUCCESS PlatformIO から `m5stack_cores3` を Upload します。 -M5Burner Serial Monitor などが COM ポートを開いている場合は、Upload 前に閉じてください。 +シリアルモニタなどのプログラムが COM ポートを開いている場合は、Upload 前に閉じてください。 --- @@ -510,24 +510,6 @@ CoreS3 built-in microphone / I2S1 連携を追加しています。 --- -## Design Policy - -このプロジェクトでは、できるだけ WLED 本体への変更を増やさない方針を取っています。 - -CoreS3 固有処理は主に: - -```text -usermods/ -pio-scripts/ -platformio_override.ini -``` - -へ閉じ込めています。 - -これにより、将来の WLED upstream 更新への追従をしやすくしています。 - ---- - ## Current Limitations / Notes - Local UI では Segment 管理を行いません
@@ -537,38 +519,6 @@ platformio_override.ini - CoreS3 では Brightness 64 前後からの使用を推奨します。 - Browser Screenshot は BMP の静止画です。ライブストリームではありません。 - DCDC OVP 保護は無効化していません。 -- Core2 / Core2 for AWS への移植は、CoreS3 版完成後の別フェーズです。 - ---- - -## Validation Summary - -CoreS3 実機で次の項目を確認しています。 - -- Normal Boot -- Reset Reboot -- LED ON / OFF -- Effect control -- Multi-Color -- BLACK Toggle -- Preset call / management -- CoreS3 ↔ Web UI sync -- Sleep / Wake -- Wi-Fi reconnect -- Recovery AP -- Audio Reactive -- Battery display -- Runtime Health -- Safe Shutdown cancel -- Safe Shutdown full power-off -- Reboot after shutdown -- Active-use endurance -- Idle endurance -- RMT tail-pixel regression -- NeoPixelBus clean dependency re-patch -- Browser Screenshot -- Screenshot color accuracy -- Screenshot + normal Touch/UI operation --- @@ -592,26 +542,6 @@ NeoPixelBus は **LGPL-3.0-or-later** のままです。CoreS3 の build-time pa --- -## Roadmap - -CoreS3 v1.0 までの残作業: - -- [x] Power stabilization -- [x] Display / Touch UI -- [x] Audio Reactive -- [x] Preset management -- [x] Battery / Health UX -- [x] Recovery AP -- [x] Safe Shutdown -- [x] NeoPixelBus RMT DMA1024 / LCD-GDMA stabilization -- [x] Browser Screenshot -- [ ] Final release branch / tag -- [ ] Release notes -- [ ] Optional additional screenshots -- [ ] Core2 / Core2 for AWS porting - ---- - ## Acknowledgements - WLED project and contributors From d59e5b96c38e2bc52d62ce8cad72862b586a21ea Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Wed, 16 Sep 2026 14:16:17 +0900 Subject: [PATCH 06/35] Refactor CoreS3 audio to generic ES7210 source --- usermods/CoreS3_Audio/CoreS3_Audio.cpp | 599 ------------------ usermods/CoreS3_Audio/library.json | 11 - usermods/CoreS3_Display/CoreS3_Display.cpp | 94 +-- .../platformio_override.ini.sample | 9 +- usermods/CoreS3_Display/readme.md | 23 +- usermods/CoreS3_Display/readme_jp.md | 23 +- usermods/audioreactive/audio_reactive.cpp | 300 +-------- usermods/audioreactive/audio_source.h | 363 +++++++---- 8 files changed, 295 insertions(+), 1127 deletions(-) delete mode 100644 usermods/CoreS3_Audio/CoreS3_Audio.cpp delete mode 100644 usermods/CoreS3_Audio/library.json diff --git a/usermods/CoreS3_Audio/CoreS3_Audio.cpp b/usermods/CoreS3_Audio/CoreS3_Audio.cpp deleted file mode 100644 index 2a9817b82b..0000000000 --- a/usermods/CoreS3_Audio/CoreS3_Audio.cpp +++ /dev/null @@ -1,599 +0,0 @@ -#include "wled.h" -#include -#include - -// =========================================================== -// M5Stack CoreS3 Audio Usermod -// -// CoreS3 Audio Production Baseline -// -// Responsibilities -// - Access the CoreS3 internal I2C bus through the M5GFX -// I2C_NUM_1 owner shared with Display / Touch. -// - Detect the AXP2101 PMU and ES7210 audio codec. -// - Configure and verify the built-in ES7210 dual microphones. -// - Reserve the fixed internal audio pins used by WLED. -// - Publish codec readiness to the AudioReactive usermod. -// - Report hardware and integration status to WLED Info. -// -// Ownership -// CoreS3_Audio -// - GPIO0 / GPIO14 reservation -// - ES7210 configuration -// - shared internal I2C access -// - codec-ready signal -// -// AudioReactive -// - I2S_NUM_1 -// - PCM sampling -// - AGC / FFT -// - audio-reactive effects -// -// This usermod intentionally does not install or read I2S, run FFT, -// change LED state, modify Display / Touch behavior, change power rails, -// or reinitialize the CoreS3 internal I2C bus. -// -// CoreS3 internal audio hardware -// ES7210 I2C : 0x40 -// I2C SDA : GPIO12 -// I2C SCL : GPIO11 -// I2S MCLK : GPIO0 -// I2S BCLK : GPIO34 -// I2S WS : GPIO33 -// I2S DATA : GPIO14 -// I2S Port : I2S_NUM_1 -// Channels : Stereo (MIC1 / MIC2) -// -// M5GFX owns the CoreS3 internal I2C bus after Display initialization. -// Audio therefore uses lgfx::i2c transactions on I2C_NUM_1 and never -// calls Wire.begin(), i2c_driver_install(), lgfx::i2c::init(), or -// release() on that shared bus. -// -// The ES7210 register configuration and pin mapping follow the -// M5Stack/M5Unified CoreS3 microphone implementation. -// =========================================================== - -static volatile bool coreS3AudioCodecReadyState = false; -static volatile bool coreS3AudioInitializationFinishedState = false; - -extern "C" bool coreS3AudioCodecReady() -{ - return coreS3AudioCodecReadyState; -} - -extern "C" bool coreS3AudioInitializationFinished() -{ - return coreS3AudioInitializationFinishedState; -} - -#if defined(WLED_M5STACK_CORES3_AUDIO) -extern "C" bool coreS3AudioReactiveSourceReady(); -#endif - -class CoreS3AudioUsermod : public Usermod -{ -private: - // --------------------------------------------------------- - // CoreS3 hardware constants - // --------------------------------------------------------- - - static constexpr uint8_t ES7210_ADDR = 0x40; - static constexpr uint8_t AXP2101_ADDR = 0x34; - static constexpr int CORES3_I2C_SDA = 12; - static constexpr int CORES3_I2C_SCL = 11; - static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; - static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; - - static constexpr int AUDIO_MCLK_PIN = 0; - static constexpr int AUDIO_BCLK_PIN = 34; - static constexpr int AUDIO_WS_PIN = 33; - static constexpr int AUDIO_DATA_IN_PIN = 14; - static constexpr uint32_t AUDIO_SAMPLE_RATE = 16000; - static constexpr unsigned long AUDIO_INIT_DELAY_MS = 2500; - static constexpr unsigned long AUDIO_INIT_RETRY_MS = 1000; - static constexpr uint8_t AUDIO_INIT_MAX_ATTEMPTS = 5; - - // --------------------------------------------------------- - // Runtime state - // --------------------------------------------------------- - - bool coreS3PinsValid = false; - bool audioPinsReserved = false; - bool es7210Found = false; - bool es7210Configured = false; - bool initializationFinished = false; - uint8_t initAttemptCount = 0; - - unsigned long setupStartMs = 0; - unsigned long lastInitAttemptMs = 0; - - bool axp2101Found = false; - bool axp2101RegistersRead = false; - uint8_t axp2101Reg90 = 0; - uint8_t axp2101Reg93 = 0; - uint8_t es7210ProbeReg00 = 0; - - void finishInitialization() - { - initializationFinished = true; - coreS3AudioInitializationFinishedState = true; - } - - // --------------------------------------------------------- - // CoreS3 internal audio pin reservation - // - // GPIO0 = ES7210 MCLK (output) - // GPIO14 = ES7210 DIN (input) - // - // GPIO33/34 are already unavailable to WLED on this CoreS3 build - // and appear as "System" in Pin Info, so only GPIO0/14 need an - // explicit PinManager reservation. - // - // PinOwner::UM_Audioreactive is used intentionally so WLED Pin Info - // reports these as Usermod-owned and all normal pin selectors treat - // them as unavailable. - // --------------------------------------------------------- - - void neutralizePersistedGpio0Button() - { - if (PinManager::getPinOwner(AUDIO_MCLK_PIN) == PinOwner::Button) { - PinManager::deallocatePin(AUDIO_MCLK_PIN, PinOwner::Button); - } - - for (auto& button : buttons) { - if (button.pin == AUDIO_MCLK_PIN) { - button.pin = -1; - button.type = BTN_TYPE_NONE; - button.pressedBefore = false; - button.longPressed = false; - button.pressedTime = 0; - button.waitTime = 0; - } - } - } - - bool reserveInternalAudioPins() - { - neutralizePersistedGpio0Button(); - - if (PinManager::isPinAllocated(AUDIO_MCLK_PIN, PinOwner::UM_Audioreactive) && - PinManager::isPinAllocated(AUDIO_DATA_IN_PIN, PinOwner::UM_Audioreactive)) { - return true; - } - - if (PinManager::isPinAllocated(AUDIO_MCLK_PIN) || - PinManager::isPinAllocated(AUDIO_DATA_IN_PIN)) { - Serial.printf( - "[CoreS3_Audio] ERROR: internal audio pin conflict MCLK0=%s DIN14=%s\n", - PinManager::getPinOwnerName(AUDIO_MCLK_PIN), - PinManager::getPinOwnerName(AUDIO_DATA_IN_PIN) - ); - return false; - } - - const managed_pin_type audioPins[] = { - { AUDIO_MCLK_PIN, true }, // ES7210 master clock output - { AUDIO_DATA_IN_PIN, false } // ES7210 PCM data input - }; - - if (!PinManager::allocateMultiplePins( - audioPins, - sizeof(audioPins) / sizeof(audioPins[0]), - PinOwner::UM_Audioreactive - )) { - Serial.println(F("[CoreS3_Audio] ERROR: failed to reserve GPIO0/GPIO14 for internal audio")); - return false; - } - - return true; - } - - // --------------------------------------------------------- - // Shared CoreS3 internal I2C helpers - // - // IMPORTANT: - // M5GFX owns the CoreS3 internal bus as I2C_NUM_1 on GPIO12/11. - // Audio must use that same owner after Display initialization. - // Do not call Wire.begin(), i2c_driver_install(), lgfx::i2c::init(), - // release(), or otherwise reinitialize the bus here. - // --------------------------------------------------------- - - bool readRegister(uint8_t address, uint8_t reg, uint8_t& value) - { - auto result = lgfx::i2c::transactionWriteRead( - CORES3_INTERNAL_I2C_PORT, - address, - ®, - 1, - &value, - 1, - CORES3_INTERNAL_I2C_FREQUENCY - ); - - return result.has_value(); - } - - bool writeRegister(uint8_t address, uint8_t reg, uint8_t value) - { - const uint8_t data[2] = { reg, value }; - - auto result = lgfx::i2c::transactionWrite( - CORES3_INTERNAL_I2C_PORT, - address, - data, - sizeof(data), - CORES3_INTERNAL_I2C_FREQUENCY - ); - - return result.has_value(); - } - - // --------------------------------------------------------- - // ES7210 microphone configuration - // - // MIC1 / MIC2 are enabled for the two built-in microphones. - // MIC3 / MIC4 remain powered down. - // --------------------------------------------------------- - - bool configureES7210() - { - struct RegisterValue { - uint8_t reg; - uint8_t value; - }; - - // Reset before applying the CoreS3 microphone profile. - if (!writeRegister(ES7210_ADDR, 0x00, 0xFF)) { - return false; - } - - delay(1); - - static constexpr RegisterValue registers[] = { - { 0x00, 0x41 }, - { 0x01, 0x1F }, - { 0x06, 0x00 }, - { 0x07, 0x20 }, - { 0x08, 0x10 }, - { 0x09, 0x30 }, - { 0x0A, 0x30 }, - { 0x20, 0x0A }, - { 0x21, 0x2A }, - { 0x22, 0x0A }, - { 0x23, 0x2A }, - { 0x02, 0xC1 }, - { 0x04, 0x01 }, - { 0x05, 0x00 }, - { 0x11, 0x60 }, - { 0x40, 0x42 }, - { 0x41, 0x70 }, - { 0x42, 0x70 }, - { 0x43, 0x1B }, - { 0x44, 0x1B }, - { 0x45, 0x00 }, - { 0x46, 0x00 }, - { 0x47, 0x00 }, - { 0x48, 0x00 }, - { 0x49, 0x00 }, - { 0x4A, 0x00 }, - { 0x4B, 0x00 }, - { 0x4C, 0xFF }, - { 0x01, 0x14 } - }; - - for (const auto& item : registers) { - if (!writeRegister(ES7210_ADDR, item.reg, item.value)) { - return false; - } - } - - // Verify a few stable key values instead of assuming the writes succeeded. - uint8_t clockControl = 0; - uint8_t mic1Gain = 0; - uint8_t mic2Gain = 0; - uint8_t mic12Power = 0; - uint8_t mic34Power = 0; - - if (!readRegister(ES7210_ADDR, 0x01, clockControl) || - !readRegister(ES7210_ADDR, 0x43, mic1Gain) || - !readRegister(ES7210_ADDR, 0x44, mic2Gain) || - !readRegister(ES7210_ADDR, 0x4B, mic12Power) || - !readRegister(ES7210_ADDR, 0x4C, mic34Power)) { - return false; - } - - return clockControl == 0x14 && - mic1Gain == 0x1B && - mic2Gain == 0x1B && - mic12Power == 0x00 && - mic34Power == 0xFF; - } - - // --------------------------------------------------------- - // Deferred initialization - // - // Audio initialization is delayed until all WLED usermods have - // completed setup. This avoids depending on usermod registration - // order while the existing CoreS3 Display / Power startup remains - // unchanged. - // --------------------------------------------------------- - - void attemptInitialization() - { - if (!audioPinsReserved) { - Serial.println(F("[CoreS3_Audio] ERROR: audio pin reservation unavailable; codec initialization blocked")); - finishInitialization(); - return; - } - - initAttemptCount++; - - Serial.printf( - "[CoreS3_Audio] Initialization attempt %u/%u\n", - initAttemptCount, - AUDIO_INIT_MAX_ATTEMPTS - ); - - if (i2c_sda != CORES3_I2C_SDA || i2c_scl != CORES3_I2C_SCL) { - Serial.printf( - "[CoreS3_Audio] ERROR: invalid CoreS3 I2C pins SDA=%d SCL=%d\n", - i2c_sda, - i2c_scl - ); - - coreS3PinsValid = false; - finishInitialization(); - return; - } - - coreS3PinsValid = true; - - // Read-only PMU diagnostics. Power-rail ownership remains - // separate from this usermod. Reading AXP2101 register 0x90 also - // verifies that the shared M5GFX I2C_NUM_1 bus is reachable. - axp2101RegistersRead = false; - axp2101Found = readRegister(AXP2101_ADDR, 0x90, axp2101Reg90); - - if (axp2101Found) { - axp2101RegistersRead = - readRegister(AXP2101_ADDR, 0x93, axp2101Reg93); - - if (!axp2101RegistersRead) { - Serial.println(F("[CoreS3_Audio] WARNING: AXP2101 REG93 unavailable on M5GFX I2C1")); - } - } - else { - Serial.println(F("[CoreS3_Audio] WARNING: AXP2101 unavailable on M5GFX I2C1")); - } - - // ES7210 register 0x00 is RESET_CTL and is safe to read. - // This probe does not modify the codec or its power rail. - es7210Found = readRegister(ES7210_ADDR, 0x00, es7210ProbeReg00); - - Serial.printf( - "[CoreS3_Audio] ES7210: %s\n", - es7210Found ? "FOUND" : "NOT FOUND" - ); - - if (!es7210Found) { - if (initAttemptCount >= AUDIO_INIT_MAX_ATTEMPTS) { - Serial.println( - F("[CoreS3_Audio] ES7210 unavailable after retries; microphone initialization stopped") - ); - finishInitialization(); - } - - return; - } - - es7210Configured = configureES7210(); - - Serial.printf( - "[CoreS3_Audio] ES7210 configuration: %s\n", - es7210Configured ? "VERIFIED" : "FAILED" - ); - - if (!es7210Configured) { - finishInitialization(); - return; - } - - coreS3AudioCodecReadyState = true; - finishInitialization(); - - Serial.println(F("[CoreS3_Audio] READY - waiting for AudioReactive I2S1")); - } - - const char* getAudioStatusName() const - { - if (!initializationFinished) { - return "INITIALIZING"; - } - - if (!audioPinsReserved) { - return "AUDIO PIN RESERVATION ERROR"; - } - - if (!coreS3PinsValid) { - return "I2C PIN ERROR"; - } - - if (!es7210Found) { - return "ES7210 NOT FOUND"; - } - - if (!es7210Configured) { - return "ES7210 CONFIG FAILED"; - } - -#if defined(WLED_M5STACK_CORES3_AUDIO) - if (coreS3AudioReactiveSourceReady()) { - return "READY - AudioReactive"; - } - - return "CODEC READY - waiting AudioReactive"; -#else - return "AUDIOREACTIVE BUILD FLAG MISSING"; -#endif - } - -public: - // --------------------------------------------------------- - // WLED Usermod setup - // --------------------------------------------------------- - - void setup() override - { - coreS3AudioCodecReadyState = false; - coreS3AudioInitializationFinishedState = false; - initializationFinished = false; - - Serial.println(); - Serial.println(F("[CoreS3_Audio][BUILD] CoreS3 Audio v0.1.5")); - Serial.println(F("[CoreS3_Audio] Initialization start")); - - audioPinsReserved = reserveInternalAudioPins(); - - Serial.printf( - "[CoreS3_Audio] Internal audio pins: %s (MCLK=GPIO0 DIN=GPIO14)\n", - audioPinsReserved ? "READY" : "FAILED" - ); - - Serial.println(F("[CoreS3_Audio] Codec initialization deferred")); - - setupStartMs = millis(); - lastInitAttemptMs = setupStartMs; - } - - // --------------------------------------------------------- - // WLED Usermod loop - // --------------------------------------------------------- - - void loop() override - { - const unsigned long now = millis(); - - if (!initializationFinished) { - if (now - setupStartMs < AUDIO_INIT_DELAY_MS) { - return; - } - - if (initAttemptCount == 0 || - now - lastInitAttemptMs >= AUDIO_INIT_RETRY_MS) { - lastInitAttemptMs = now; - attemptInitialization(); - } - - return; - } - - // No periodic PCM work here. - // Audio Reactive owns I2S1 sampling and FFT processing. - } - - // --------------------------------------------------------- - // WLED Info status - // --------------------------------------------------------- - - void addToJsonInfo(JsonObject& root) override - { - JsonObject user = root["u"]; - - if (user.isNull()) { - user = root.createNestedObject("u"); - } - - JsonArray statusInfo = user.createNestedArray("CoreS3 Audio"); - statusInfo.add(getAudioStatusName()); - - JsonArray i2cInfo = user.createNestedArray("CoreS3 Audio I2C"); - i2cInfo.add("M5GFX I2C1 GPIO12/GPIO11 400kHz"); - - JsonArray codecInfo = user.createNestedArray("CoreS3 ES7210"); - - if (!initializationFinished && !es7210Found) { - codecInfo.add("Waiting for probe"); - } - else if (!es7210Found) { - codecInfo.add("Not found on M5GFX I2C1 (0x40)"); - } - else if (!es7210Configured) { - codecInfo.add("Found - config failed"); - } - else { - codecInfo.add("Found / configured / verified on I2C1"); - } - - JsonArray integrationInfo = user.createNestedArray("CoreS3 Audio Integration"); - -#if defined(WLED_M5STACK_CORES3_AUDIO) - integrationInfo.add( - coreS3AudioReactiveSourceReady() - ? "AudioReactive source READY" - : (coreS3AudioCodecReadyState - ? "Codec ready / waiting I2S1 source" - : "Waiting for ES7210 codec") - ); -#else - integrationInfo.add("AudioReactive CoreS3 build flag missing"); -#endif - - JsonArray i2sInfo = user.createNestedArray("CoreS3 Audio I2S"); - i2sInfo.add("AudioReactive owner: I2S1 Stereo 16bit 16000Hz"); - - JsonArray pmuInfo = user.createNestedArray("CoreS3 Audio PMU"); - - if (!initializationFinished && !axp2101Found) { - pmuInfo.add("Waiting for probe"); - } - else if (!axp2101Found) { - pmuInfo.add("AXP2101 not found on M5GFX I2C1"); - } - else if (!axp2101RegistersRead) { - pmuInfo.add("AXP2101 found / registers unavailable"); - } - else { - char pmuText[48]; - - snprintf( - pmuText, - sizeof(pmuText), - "REG90=0x%02X REG93=0x%02X", - axp2101Reg90, - axp2101Reg93 - ); - - pmuInfo.add(pmuText); - } - - JsonArray pinReservationInfo = - user.createNestedArray("CoreS3 Audio Pin Reservation"); - - if (audioPinsReserved) { - pinReservationInfo.add("GPIO0 MCLK / GPIO14 DIN RESERVED (Usermod)"); - } - else { - char reservationText[80]; - - snprintf( - reservationText, - sizeof(reservationText), - "FAILED: GPIO0=%s GPIO14=%s", - PinManager::getPinOwnerName(AUDIO_MCLK_PIN), - PinManager::getPinOwnerName(AUDIO_DATA_IN_PIN) - ); - - pinReservationInfo.add(reservationText); - } - - JsonArray pinInfo = user.createNestedArray("CoreS3 Audio Pins"); - pinInfo.add("FIXED: MCLK0 BCLK34 WS33 DIN14"); - } -}; - -// ----------------------------------------------------------- -// Register CoreS3 Audio Usermod with WLED -// ----------------------------------------------------------- - -static CoreS3AudioUsermod coreS3AudioUsermod; -REGISTER_USERMOD(coreS3AudioUsermod); diff --git a/usermods/CoreS3_Audio/library.json b/usermods/CoreS3_Audio/library.json deleted file mode 100644 index 14aa812370..0000000000 --- a/usermods/CoreS3_Audio/library.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "CoreS3_Audio", - "version": "0.1.5", - "description": "M5Stack CoreS3 built-in microphone Audio Reactive integration usermod for WLED", - "build": { - "libArchive": false - }, - "dependencies": { - "M5GFX": "https://github.com/m5stack/M5GFX.git#0.2.26" - } -} diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 5ef2a60b29..99115fd2ed 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -61,13 +61,6 @@ extern "C" bool coreS3PowerInitializationComplete(); extern "C" bool coreS3PowerExternal5VReady(); extern "C" bool coreS3PowerSafeShutdownMonitorReady(); -#if defined(WLED_M5STACK_CORES3_AUDIO) -// CoreS3_Audio publishes terminal initialization and codec-ready state. -// Display consumes these signals only to annotate Audio Reactive effects when -// the built-in microphone is definitively unavailable. -extern "C" bool coreS3AudioInitializationFinished(); -extern "C" bool coreS3AudioCodecReady(); -#endif class CoreS3DisplayUsermod : public Usermod { private: @@ -184,12 +177,6 @@ class CoreS3DisplayUsermod : public Usermod { static constexpr unsigned long POWER_SAFETY_WARNING_GRACE_MS = 5000; static constexpr unsigned long RUNTIME_HEALTH_WARNING_HOLD_MS = 2200; - // Audio health is contextual rather than a global warning: only an effect - // that actually depends on audio is annotated when codec initialization has - // definitively failed. During initialization the normal capability text is - // kept unchanged. - bool lastAudioUnavailable = false; - // ========================================================= // Cached WLED state // ========================================================= @@ -3309,31 +3296,6 @@ class CoreS3DisplayUsermod : public Usermod { return capability; } - bool effectUsesAudioReactive( - const M5StackEffectCapabilities& capability - ) const { - return capability.audioVolume || capability.audioFrequency; - } - - bool isCoreS3AudioUnavailable() const { -#if defined(WLED_M5STACK_CORES3_AUDIO) - return - coreS3AudioInitializationFinished() && - !coreS3AudioCodecReady(); -#else - return false; -#endif - } - - bool isAudioUnavailableForEffect( uint8_t effectMode ) { - const M5StackEffectCapabilities capability = - getEffectCapabilities( effectMode ); - - return - effectUsesAudioReactive( capability ) && - isCoreS3AudioUnavailable(); - } - bool effectRequires2D( const M5StackEffectCapabilities& capability ) { return capability.supports2D && !capability.supports1D; } @@ -3422,9 +3384,6 @@ class CoreS3DisplayUsermod : public Usermod { incompatibleWithCurrentSegment = effectRequires2D( capability ) && !currentMainSegmentIs2D(); - const bool audioUnavailable = - effectUsesAudioReactive( capability ) && - isCoreS3AudioUnavailable(); char dimensionText[16]; char audioText[8]; @@ -3433,10 +3392,7 @@ class CoreS3DisplayUsermod : public Usermod { getEffectAudioText( capability, audioText, sizeof(audioText) ); if ( incompatibleWithCurrentSegment ) { - if ( audioUnavailable ) { - snprintf( text, textSize, "2D REQ | AUDIO UNAVAILABLE" ); - } - else if ( audioText[0] != '\0' ) { + if ( audioText[0] != '\0' ) { snprintf( text, textSize, "2D REQUIRED | %s", audioText ); } else { @@ -3446,10 +3402,6 @@ class CoreS3DisplayUsermod : public Usermod { return; } - if ( audioUnavailable ) { - snprintf( text, textSize, "AUDIO UNAVAILABLE" ); - return; - } if ( audioText[0] != '\0' ) { snprintf( text, textSize, "%s | %s", dimensionText, audioText ); @@ -3927,7 +3879,7 @@ class CoreS3DisplayUsermod : public Usermod { display.setTextDatum( textdatum_t::middle_center ); display.setTextColor( - ( incompatibleWithCurrentSegment || isAudioUnavailableForEffect( effectMode ) ) + incompatibleWithCurrentSegment ? TFT_YELLOW : TFT_WHITE, TFT_BLACK @@ -4357,7 +4309,7 @@ class CoreS3DisplayUsermod : public Usermod { display.drawString( effectName, screenWidth / 2, 36 ); display.setTextColor( - ( incompatibleWithCurrentSegment || isAudioUnavailableForEffect( effectMode ) ) + incompatibleWithCurrentSegment ? TFT_YELLOW : TFT_CYAN, TFT_BLACK @@ -6642,45 +6594,6 @@ class CoreS3DisplayUsermod : public Usermod { #include "M5StackDisplayTouchStateMachine.inc" - void serviceAudioHealthPresentation( uint8_t effectMode ) { - const bool audioUnavailable = isCoreS3AudioUnavailable(); - - if ( audioUnavailable == lastAudioUnavailable ) { - return; - } - - const bool relevantVisiblePage = - currentPage == SCREEN_MAIN || currentPage == SCREEN_EFFECT; - - const bool currentEffectUsesAudio = - effectUsesAudioReactive( getEffectCapabilities( effectMode ) ); - - // Avoid changing effect visuals in the middle of a touch gesture. Keep the - // previous cached state so the change is retried after release. - if ( - relevantVisiblePage && - currentEffectUsesAudio && - touchState.touchTarget != M5STACK_TOUCH_TARGET_NONE - ) { - return; - } - - lastAudioUnavailable = audioUnavailable; - - if ( !currentEffectUsesAudio ) { - return; - } - - if ( currentPage == SCREEN_MAIN ) { - drawEffect( effectMode, M5STACK_TOUCH_TARGET_NONE ); - return; - } - - if ( currentPage == SCREEN_EFFECT ) { - drawEffectPageName( effectMode ); - } - } - // ========================================================= // Page-specific runtime display synchronization // @@ -7463,7 +7376,6 @@ class CoreS3DisplayUsermod : public Usermod { uint8_t effectMode = getCurrentEffectMode(); - serviceAudioHealthPresentation( effectMode ); uint8_t currentSpeed = getCurrentSpeed(); diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index d9d3cc16df..730429a9e7 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -49,7 +49,6 @@ extra_scripts = custom_usermods = CoreS3_Power CoreS3_Display - CoreS3_Audio audioreactive build_unflags = @@ -58,14 +57,20 @@ build_unflags = build_flags = ${env:esp32s3dev_16MB_opi.build_flags} + ; CoreS3 target identification. + -D WLED_M5STACK_CORES3=1 + ; CoreS3 internal I2C bus. -D I2CSDAPIN=12 -D I2CSCLPIN=11 ; CoreS3 built-in ES7210 / AudioReactive integration. - -D WLED_M5STACK_CORES3_AUDIO=1 -D UM_AUDIOREACTIVE_ENABLE -D SR_DMTYPE=7 + -D I2S_SDPIN=14 + -D I2S_WSPIN=33 + -D I2S_CKPIN=34 + -D MCLK_PIN=0 ; GPIO0 is ES7210 MCLK, not a WLED physical button. -D BTNPIN=-1 diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 8c8380a620..d3d06c449e 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -211,14 +211,12 @@ CoreS3-specific usermods: ```text CoreS3_Power CoreS3_Display -CoreS3_Audio audioreactive ``` Audio Reactive definitions: ```text -WLED_M5STACK_CORES3_AUDIO=1 UM_AUDIOREACTIVE_ENABLE SR_DMTYPE=7 ``` @@ -377,15 +375,16 @@ If the Power Key is released after BLACK but before the final hard power-off, sh The built-in ES7210 and CoreS3 microphones are used. -The Audio usermod initializes ES7210, while Audio Reactive owns I2S1 / PCM / FFT processing. +Audio Reactive initializes ES7210 and uses the standard WLED I2S0 / PCM / FFT processing path. Main settings: ```text Codec ES7210 -I2S I2S1 -Sample Rate 16000 Hz -Format Stereo / 16-bit +I2S I2S0 +Sample Rate 22050 Hz +Codec Format 24-bit I2S +I2S Input Mono / WLED 32-bit container MCLK GPIO0 DIN GPIO14 ``` @@ -467,7 +466,6 @@ WLED/ └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ - ├─ CoreS3_Audio/ └─ audioreactive/ ``` @@ -495,18 +493,9 @@ Responsibilities: - Preset UI - Browser Screenshot -### CoreS3_Audio - -Responsibilities: - -- ES7210 probe / initialization -- Audio pins -- Audio health -- Audio Reactive handoff - ### audioreactive -Adds CoreS3 built-in microphone / I2S1 integration. +Adds ES7210 support for the CoreS3 built-in microphone through the standard WLED Audio Reactive I2S path. --- diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index d03354cfe6..d38320efd7 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -211,14 +211,12 @@ CoreS3 固有 Usermod: ```text CoreS3_Power CoreS3_Display -CoreS3_Audio audioreactive ``` Audio Reactive 用: ```text -WLED_M5STACK_CORES3_AUDIO=1 UM_AUDIOREACTIVE_ENABLE SR_DMTYPE=7 ``` @@ -377,15 +375,16 @@ BLACK 送信後に Power Key を離して Shutdown をキャンセルした場 CoreS3 内蔵 ES7210 と内蔵マイクを使用します。 -Audio Usermod が ES7210 を初期化し、Audio Reactive 側が I2S1 / PCM / FFT を担当します。 +Audio Reactive が ES7210 を初期化し、WLED 標準の I2S0 / PCM / FFT 処理経路を使用します。 主な仕様: ```text Codec ES7210 -I2S I2S1 -Sample Rate 16000 Hz -Format Stereo / 16-bit +I2S I2S0 +Sample Rate 22050 Hz +Codec Format 24-bit I2S +I2S Input Mono / WLED 32-bit container MCLK GPIO0 DIN GPIO14 ``` @@ -467,7 +466,6 @@ WLED/ └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ - ├─ CoreS3_Audio/ └─ audioreactive/ ``` @@ -495,18 +493,9 @@ WLED/ - Preset UI - Browser Screenshot -### CoreS3_Audio - -担当: - -- ES7210 probe / initialization -- Audio pins -- Audio health -- Audio Reactive handoff - ### audioreactive -CoreS3 built-in microphone / I2S1 連携を追加しています。 +CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S 経路へ追加しています。 --- diff --git a/usermods/audioreactive/audio_reactive.cpp b/usermods/audioreactive/audio_reactive.cpp index 4ddb0daa6f..9ab25d31fc 100644 --- a/usermods/audioreactive/audio_reactive.cpp +++ b/usermods/audioreactive/audio_reactive.cpp @@ -201,58 +201,6 @@ static FFTsampleType* windowFFT = nullptr; // use audio source class (ESP32 specific) #include "audio_source.h" - -// ----------------------------------------------------------------------------- -// M5Stack CoreS3 built-in ES7210 microphone integration -// -// Board-specific responsibilities in this file are limited to: -// - fixed internal audio pin selection -// - GPIO0 button ownership release for MCLK -// - deferred I2S source startup after codec readiness -// - CoreS3-specific sample rate / FFT mapping -// -// ES7210 codec configuration itself remains owned by CoreS3_Audio. -// ----------------------------------------------------------------------------- -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) -extern "C" bool coreS3AudioCodecReady(); - -static constexpr int8_t CORES3_AUDIO_SD_PIN = 14; -static constexpr int8_t CORES3_AUDIO_WS_PIN = 33; -static constexpr int8_t CORES3_AUDIO_BCLK_PIN = 34; -static constexpr int8_t CORES3_AUDIO_MCLK_PIN = 0; - -static void coreS3ApplyFixedAudioPins( - int8_t& sdPin, - int8_t& wsPin, - int8_t& bclkPin, - int8_t& mclkPin -) { - sdPin = CORES3_AUDIO_SD_PIN; - wsPin = CORES3_AUDIO_WS_PIN; - bclkPin = CORES3_AUDIO_BCLK_PIN; - mclkPin = CORES3_AUDIO_MCLK_PIN; -} - -static void coreS3ReleaseMclkButtonOwnership() -{ - // GPIO0 is the CoreS3 ES7210 MCLK. - if (PinManager::getPinOwner(CORES3_AUDIO_MCLK_PIN) == PinOwner::Button) { - PinManager::deallocatePin(CORES3_AUDIO_MCLK_PIN, PinOwner::Button); - } - - // Also neutralize any persisted/default WLED button configuration on GPIO0. - for (auto& button : buttons) { - if (button.pin == CORES3_AUDIO_MCLK_PIN) { - button.pin = -1; - button.type = BTN_TYPE_NONE; - button.pressedBefore = false; - button.longPressed = false; - button.pressedTime = 0; - button.waitTime = 0; - } - } -} -#endif constexpr i2s_port_t I2S_PORT = I2S_NUM_0; // I2S port to use (do not change !) constexpr int BLOCK_SIZE = 128; // I2S buffer size (samples) @@ -291,12 +239,6 @@ const float agcSampleSmooth[AGC_NUM_PRESETS] = { 1/12.f, 1/6.f, 1/16.f}; // // AGC presets end static AudioSource *audioSource = nullptr; -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) -extern "C" bool coreS3AudioReactiveSourceReady() -{ - return audioSource && audioSource->isInitialized(); -} -#endif static bool useBandPassFilter = false; // if true, enables a hard cutoff bandpass filter. Applies after FFT. static bool useMicFilter = false; // if true, enables a IIR bandpass filter 80Hz-20Khz to remove noise. Applies before FFT. //////////////////// @@ -328,19 +270,11 @@ static float fftResultMax[NUM_GEQ_CHANNELS] = {0.0f}; // A table #endif // audio source parameters and constant -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) -constexpr SRate_t SAMPLE_RATE = 16000; // Hardware-verified CoreS3 ES7210 sample rate -#else constexpr SRate_t SAMPLE_RATE = 22050; // Base sample rate in Hz - 22Khz is a standard rate. Physical sample time -> 23ms -#endif //constexpr SRate_t SAMPLE_RATE = 16000; // 16kHz - use if FFTtask takes more than 20ms. Physical sample time -> 32ms //constexpr SRate_t SAMPLE_RATE = 20480; // Base sample rate in Hz - 20Khz is experimental. Physical sample time -> 25ms //constexpr SRate_t SAMPLE_RATE = 10240; // Base sample rate in Hz - previous default. Physical sample time -> 50ms -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) -#define FFT_MIN_CYCLE 30 // CoreS3: 16kHz / 512 samples ~= 32ms physical window -#else #define FFT_MIN_CYCLE 21 // minimum time before FFT task is repeated. Use with 22Khz sampling -#endif //#define FFT_MIN_CYCLE 30 // Use with 16Khz sampling //#define FFT_MIN_CYCLE 23 // minimum time before FFT task is repeated. Use with 20Khz sampling //#define FFT_MIN_CYCLE 46 // minimum time before FFT task is repeated. Use with 10Khz sampling @@ -348,12 +282,6 @@ constexpr SRate_t SAMPLE_RATE = 22050; // Base sample rate in Hz - 22Khz // FFT Constants constexpr uint16_t samplesFFT = 512; // Samples in an FFT batch - This value MUST ALWAYS be a power of 2 constexpr uint16_t samplesFFT_2 = 256; // meaningfull part of FFT results - only the "lower half" contains useful information. - -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) -static_assert(SAMPLE_RATE == 16000, "CoreS3 FFT calibration requires 16 kHz sampling"); -static_assert(samplesFFT == 512, "CoreS3 FFT calibration requires 512 FFT samples"); -constexpr float CORES3_FFT_BIN_HZ = (float)SAMPLE_RATE / (float)samplesFFT; // 31.25 Hz/bin -#endif // the following are observed values, supported by a bit of "educated guessing" //#define FFT_DOWNSCALE 0.65f // 20kHz - downscaling factor for FFT results - "Flat-Top" window @20Khz, old freq channels #ifdef FFT_PREFER_EXACT_PEAKS @@ -620,46 +548,6 @@ void FFTcode(void * parameter) fftCalc[13] = fftAddAvg(111,147); // 2220 - 2960 fftCalc[14] = fftAddAvg(147,194); // 2940 - 3900 fftCalc[15] = fftAddAvg(194,250); // 3880 - 5000 // avoid the last 5 bins, which are usually inaccurate -#else -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - /* - * CoreS3 ES7210 mapping calibrated for 16 kHz / 512 samples. - * - * Bin width = 16000 / 512 = 31.25 Hz. - * Bands 0..14 preserve approximately the same physical frequency - * regions as WLED's standard 22.05 kHz mapping. Band 15 is - * Nyquist-limited and stops at bin 250 (7.8125 kHz), deliberately - * avoiding the final five FFT bins. - * - * This branch changes only CoreS3. All other AudioReactive sources - * continue to use the original WLED 22.05 kHz mapping below. - */ - if (useBandPassFilter) { - // preserve the standard >~100 Hz band-pass behavior - fftCalc[ 0] = 0.8f * fftAddAvg(4,6); // 125 - 188 Hz - fftCalc[ 1] = 0.9f * fftAddAvg(6,7); // 188 - 219 Hz - fftCalc[ 2] = fftAddAvg(7,8); // 219 - 250 Hz - fftCalc[ 3] = fftAddAvg(8,10); // 250 - 313 Hz - fftCalc[15] = fftAddAvg(227,250) * 0.75f; // 7094 - 7813 Hz, Nyquist-limited - } else { - fftCalc[ 0] = fftAddAvg(1,3); // 31 - 94 Hz sub-bass - fftCalc[ 1] = fftAddAvg(3,4); // 94 - 125 Hz bass - fftCalc[ 2] = fftAddAvg(4,7); // 125 - 219 Hz bass - fftCalc[ 3] = fftAddAvg(7,10); // 219 - 313 Hz bass + midrange - fftCalc[15] = fftAddAvg(227,250) * 0.70f; // 7094 - 7813 Hz, Nyquist-limited - } - - fftCalc[ 4] = fftAddAvg(10,14); // 313 - 438 Hz midrange - fftCalc[ 5] = fftAddAvg(14,18); // 438 - 563 Hz midrange - fftCalc[ 6] = fftAddAvg(18,26); // 563 - 813 Hz midrange - fftCalc[ 7] = fftAddAvg(26,36); // 813 - 1125 Hz midrange; 1 kHz centered - fftCalc[ 8] = fftAddAvg(36,45); // 1125 - 1406 Hz midrange - fftCalc[ 9] = fftAddAvg(45,61); // 1406 - 1906 Hz midrange - fftCalc[10] = fftAddAvg(61,77); // 1906 - 2406 Hz midrange + high mid - fftCalc[11] = fftAddAvg(77,96); // 2406 - 3000 Hz high mid - fftCalc[12] = fftAddAvg(96,119); // 3000 - 3719 Hz high mid - fftCalc[13] = fftAddAvg(119,143); // 3719 - 4469 Hz high mid - fftCalc[14] = fftAddAvg(143,227) * 0.88f; // 4469 - 7094 Hz high mid + high #else /* new mapping, optimized for 22050 Hz by softhack007 */ // bins frequency range @@ -690,7 +578,6 @@ void FFTcode(void * parameter) fftCalc[12] = fftAddAvg(70,86); // 16 3015 - 3704 high mid fftCalc[13] = fftAddAvg(86,104); // 18 3704 - 4479 high mid fftCalc[14] = fftAddAvg(104,165) * 0.88f; // 61 4479 - 7106 high mid + high -- with slight damping -#endif #endif } else { // noise gate closed - just decay old values for (int i=0; i < NUM_GEQ_CHANNELS; i++) { @@ -943,12 +830,6 @@ class AudioReactive : public Usermod { #else int8_t mclkPin = MCLK_PIN; #endif - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - static constexpr uint8_t CORES3_I2S_INIT_MAX_ATTEMPTS = 5; - static constexpr unsigned long CORES3_I2S_INIT_RETRY_MS = 1000; - uint8_t coreS3I2sInitAttempts = 0; - unsigned long coreS3LastI2sInitAttemptMs = 0; - #endif #endif // new "V2" audiosync struct - 44 Bytes @@ -1533,18 +1414,9 @@ class AudioReactive : public Usermod { #ifdef ARDUINO_ARCH_ESP32 - // Reset the selected I2S peripheral for good measure. - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType == 7) { - // CoreS3 I2S1 is deferred and has no driver to uninstall at boot. - // CoreS3ES7210Source becomes the sole owner after ES7210 is READY. - } else { - i2s_driver_uninstall(I2S_NUM_0); - } - #else - i2s_driver_uninstall(I2S_NUM_0); - #endif - #if !defined(WLED_M5STACK_CORES3_AUDIO) && !defined(CONFIG_IDF_TARGET_ESP32C3) && (ESP_IDF_VERSION_MAJOR < 5) + // Reset I2S peripheral for good measure + i2s_driver_uninstall(I2S_NUM_0); // E (696) I2S: i2s_driver_uninstall(2006): I2S port 0 has not installed + #if !defined(CONFIG_IDF_TARGET_ESP32C3) && (ESP_IDF_VERSION_MAJOR < 5) delay(100); periph_module_reset(PERIPH_I2S0_MODULE); // not possible on -C3, neither on esp-idf V5 #endif @@ -1605,23 +1477,15 @@ class AudioReactive : public Usermod { delay(100); if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) + + // AI: below section was generated by an AI case 7: - DEBUGSR_PRINTLN(F("AR: M5Stack CoreS3 ES7210 built-in dual microphone")); - coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); - coreS3ReleaseMclkButtonOwnership(); - audioSource = new CoreS3ES7210Source(SAMPLE_RATE, BLOCK_SIZE); - useMicFilter = true; - coreS3I2sInitAttempts = 0; - coreS3LastI2sInitAttemptMs = 0; - - if (audioSource && coreS3AudioCodecReady()) { - coreS3I2sInitAttempts = 1; - coreS3LastI2sInitAttemptMs = millis(); - audioSource->initialize(); - } + DEBUGSR_PRINTLN(F("AR: ES7210 Source")); + audioSource = new ES7210Source(SAMPLE_RATE, BLOCK_SIZE); + delay(100); + if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; - #endif + // AI: end #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) // legacy ADC driver is not available any more in esp-idf V5.x.y // ADC over I2S is only possible on "classic" ESP32 @@ -1652,21 +1516,12 @@ class AudioReactive : public Usermod { delay(250); // give microphone enough time to initialise if (!audioSource && (dmType != SR_DMTYPE_NETWORK_ONLY)) enabled = false;// audio failed to initialise - - bool coreS3SourcePending = false; - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - coreS3SourcePending = (dmType == 7) && audioSource && !audioSource->isInitialized(); - #endif #endif - #ifdef ARDUINO_ARCH_ESP32 - if (enabled && !coreS3SourcePending) onUpdateBegin(false); // create FFT task when input is ready - #else - if (enabled) onUpdateBegin(false); - #endif + if (enabled) onUpdateBegin(false); // create FFT task, and initialize network #ifdef ARDUINO_ARCH_ESP32 - if (audioSource && FFT_Task == nullptr && !coreS3SourcePending) enabled = false; // FFT task creation failed + if (audioSource && FFT_Task == nullptr) enabled = false; // FFT task creation failed if((!audioSource) || (!audioSource->isInitialized())) { // audio source failed to initialize. Still stay "enabled", as there might be input arriving via UDP Sound Sync #ifdef WLED_DEBUG #define AR_INIT_DEBUG_PRINT DEBUG_PRINTLN @@ -1682,11 +1537,7 @@ class AudioReactive : public Usermod { disableSoundProcessing = true; } #endif - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (enabled && !coreS3SourcePending) disableSoundProcessing = false; - #else if (enabled) disableSoundProcessing = false; // all good - enable audio processing - #endif if (enabled) connectUDPSoundSync(); if (enabled && addPalettes) createAudioPalettes(); initDone = true; @@ -1728,39 +1579,6 @@ class AudioReactive : public Usermod { { static unsigned long lastUMRun = millis(); -#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (enabled && dmType == 7 && audioSource && !audioSource->isInitialized() - && coreS3AudioCodecReady() - && coreS3I2sInitAttempts < CORES3_I2S_INIT_MAX_ATTEMPTS - && (coreS3I2sInitAttempts == 0 - || millis() - coreS3LastI2sInitAttemptMs >= CORES3_I2S_INIT_RETRY_MS)) { - - coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); - coreS3ReleaseMclkButtonOwnership(); - - coreS3I2sInitAttempts++; - coreS3LastI2sInitAttemptMs = millis(); - - DEBUGSR_PRINTF( - "AR: CoreS3 ES7210 I2S1 init attempt %u/%u.\n", - coreS3I2sInitAttempts, - CORES3_I2S_INIT_MAX_ATTEMPTS - ); - - audioSource->initialize(); - - if (audioSource->isInitialized()) { - DEBUGSR_PRINTLN(F("AR: CoreS3 ES7210 I2S1 source READY.")); - if (FFT_Task == nullptr) onUpdateBegin(false); - disableSoundProcessing = false; - lastUMRun = millis(); - } else { - DEBUGSR_PRINTLN(F("AR: CoreS3 ES7210 I2S1 source initialization FAILED.")); - disableSoundProcessing = true; - } - } -#endif - if (!enabled) { disableSoundProcessing = true; // keep processing suspended (FFT task) lastUMRun = millis(); // update time keeping @@ -2079,9 +1897,6 @@ class AudioReactive : public Usermod { infoArr.add(F("ADC analog")); } else { if (dmType == 5) infoArr.add(F("PDM digital")); // dmType 5 => generic PDM microphone - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - else if (dmType == 7) infoArr.add(F("CoreS3 ES7210 / I2S1")); - #endif else infoArr.add(F("I2S digital")); } // input level or "silence" @@ -2094,22 +1909,8 @@ class AudioReactive : public Usermod { } } else { // error during audio source setup - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType == 7) { - infoArr.add(F("CoreS3 ES7210 / I2S1")); - if (!coreS3AudioCodecReady()) { - infoArr.add(F(" - waiting codec")); - } else if (coreS3I2sInitAttempts >= CORES3_I2S_INIT_MAX_ATTEMPTS) { - infoArr.add(F(" - init failed")); - } else { - infoArr.add(F(" - initializing")); - } - } else - #endif - { - infoArr.add(F("not initialized")); - infoArr.add(F(" - check pin settings")); - } + infoArr.add(F("not initialized")); + infoArr.add(F(" - check pin settings")); } } @@ -2121,16 +1922,6 @@ class AudioReactive : public Usermod { infoArr.add(F("suspended")); } - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType == 7) { - infoArr = user.createNestedArray(F("CoreS3 FFT")); - infoArr.add(F("16kHz / 512 samples / 31.25Hz bin")); - - infoArr = user.createNestedArray(F("CoreS3 FFT Mapping")); - infoArr.add(F("16 bands calibrated / 7.81kHz max")); - } - #endif - // AGC or manual Gain if ((soundAgc==0) && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) { infoArr = user.createNestedArray(F("Manual Gain")); @@ -2282,17 +2073,11 @@ class AudioReactive : public Usermod { JsonObject dmic = top.createNestedObject(FPSTR(_digitalmic)); dmic["type"] = dmType; - - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType != 7) - #endif - { - JsonArray pinArray = dmic.createNestedArray("pin"); - pinArray.add(i2ssdPin); - pinArray.add(i2swsPin); - pinArray.add(i2sckPin); - pinArray.add(mclkPin); - } + JsonArray pinArray = dmic.createNestedArray("pin"); + pinArray.add(i2ssdPin); + pinArray.add(i2swsPin); + pinArray.add(i2sckPin); + pinArray.add(mclkPin); JsonObject cfg = top.createNestedObject(FPSTR(_config)); cfg[F("squelch")] = soundSquelch; @@ -2361,17 +2146,10 @@ class AudioReactive : public Usermod { if (dmType == 5) dmType = SR_DMTYPE; // MCU does not support PDM #endif - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType == 7) { - coreS3ApplyFixedAudioPins(i2ssdPin, i2swsPin, i2sckPin, mclkPin); - } else - #endif - { - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][0], i2ssdPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][1], i2swsPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][2], i2sckPin); - configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][3], mclkPin); - } + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][0], i2ssdPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][1], i2swsPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][2], i2sckPin); + configComplete &= getJsonValue(top[FPSTR(_digitalmic)]["pin"][3], mclkPin); configComplete &= getJsonValue(top[FPSTR(_config)][F("squelch")], soundSquelch); configComplete &= getJsonValue(top[FPSTR(_config)][F("gain")], sampleGain); @@ -2421,9 +2199,7 @@ class AudioReactive : public Usermod { uiScript.print(F("addOption(dd,'Generic PDM',5);")); #endif uiScript.print(F("addOption(dd,'ES8388',6);")); - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - uiScript.print(F("addOption(dd,'M5Stack CoreS3 ES7210',7);")); - #endif + uiScript.print(F("addOption(dd,'ES7210',7);")); uiScript.print(F("addOption(dd,'None - network receive only',")); uiScript.print(SR_DMTYPE_NETWORK_ONLY); uiScript.print(F(");")); @@ -2455,25 +2231,15 @@ class AudioReactive : public Usermod { #endif uiScript.print(F("addOption(dd,'Receive',2);")); #ifdef ARDUINO_ARCH_ESP32 - #if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) - if (dmType == 7) { - uiScript.print(F( - "addInfo(ux+':digitalmic:type',1," - "'fixed internal pins: DIN14 / WS33 / BCLK34 / MCLK0; requires reboot!');" - )); - } else - #endif - { - uiScript.print(F("addInfo(ux+':digitalmic:type',1,'requires reboot!');")); // 0 is field type, 1 is actual field - uiScript.print(F("addInfo(uxp,0,'sd/data/dout','I2S SD');")); - uiScript.print(F("addInfo(uxp,1,'ws/clk/lrck','I2S WS');")); - uiScript.print(F("addInfo(uxp,2,'sck/bclk','I2S SCK');")); - #if defined(CONFIG_IDF_TARGET_ESP32) - uiScript.print(F("addInfo(uxp,3,'only use -1, 0, 1 or 3','I2S MCLK');")); - #else - uiScript.print(F("addInfo(uxp,3,'master clock','I2S MCLK');")); - #endif - } + uiScript.print(F("addInfo(ux+':digitalmic:type',1,'requires reboot!');")); // 0 is field type, 1 is actual field + uiScript.print(F("addInfo(uxp,0,'sd/data/dout','I2S SD');")); + uiScript.print(F("addInfo(uxp,1,'ws/clk/lrck','I2S WS');")); + uiScript.print(F("addInfo(uxp,2,'sck/bclk','I2S SCK');")); + #if defined(CONFIG_IDF_TARGET_ESP32) + uiScript.print(F("addInfo(uxp,3,'only use -1, 0, 1 or 3','I2S MCLK');")); + #else + uiScript.print(F("addInfo(uxp,3,'master clock','I2S MCLK');")); + #endif #endif } diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 03347301ac..58491bec45 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -2,6 +2,10 @@ #ifdef ARDUINO_ARCH_ESP32 #include "wled.h" #include +#if defined(WLED_M5STACK_CORES3) +#include +#include +#endif #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) #include // legacy ADC driver causes bootloops in V5 #endif @@ -168,18 +172,8 @@ class AudioSource { */ class I2SSource : public AudioSource { public: - I2SSource( - SRate_t sampleRate, - int blockSize, - float sampleScale = 1.0f, - i2s_port_t i2sPort = I2S_NUM_0, - i2s_channel_t channelMode = I2S_CHANNEL_MONO, - bool managePins = true - ) : - AudioSource(sampleRate, blockSize, sampleScale), - _i2sPort(i2sPort), - _channelMode(channelMode), - _managePins(managePins) { + I2SSource(SRate_t sampleRate, int blockSize, float sampleScale = 1.0f) : + AudioSource(sampleRate, blockSize, sampleScale) { _config = { .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = _sampleRate, @@ -205,7 +199,7 @@ class I2SSource : public AudioSource { virtual void initialize(int8_t i2swsPin = I2S_PIN_NO_CHANGE, int8_t i2ssdPin = I2S_PIN_NO_CHANGE, int8_t i2sckPin = I2S_PIN_NO_CHANGE, int8_t mclkPin = I2S_PIN_NO_CHANGE) { DEBUGSR_PRINTLN(F("I2SSource:: initialize().")); - if (_managePins && i2swsPin != I2S_PIN_NO_CHANGE && i2ssdPin != I2S_PIN_NO_CHANGE) { + if (i2swsPin != I2S_PIN_NO_CHANGE && i2ssdPin != I2S_PIN_NO_CHANGE) { if (!PinManager::allocatePin(i2swsPin, true, PinOwner::UM_Audioreactive) || !PinManager::allocatePin(i2ssdPin, false, PinOwner::UM_Audioreactive)) { // #206 DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pins: ws=%d, sd=%d\n", i2swsPin, i2ssdPin); @@ -215,7 +209,7 @@ class I2SSource : public AudioSource { // i2ssckPin needs special treatment, since it might be unused on PDM mics if (i2sckPin != I2S_PIN_NO_CHANGE) { - if (_managePins && !PinManager::allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) { + if (!PinManager::allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) { DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pins: sck=%d\n", i2sckPin); return; } @@ -260,16 +254,13 @@ class I2SSource : public AudioSource { #endif #endif - // Reserve the master clock pin if provided. - // CoreS3 internal audio pins are fixed board resources and intentionally - // bypass WLED PinManager; generic AudioReactive sources keep the - // original PinManager behavior. + // Reserve the master clock pin if provided _mclkPin = mclkPin; if (mclkPin != I2S_PIN_NO_CHANGE) { - if (_managePins && !PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { + if(!PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pin: MCLK=%d\n", mclkPin); return; - } + } else _routeMclk(mclkPin); } @@ -285,32 +276,32 @@ class I2SSource : public AudioSource { //DEBUGSR_PRINTF("[AR] I2S: SD=%d, WS=%d, SCK=%d, MCLK=%d\n", i2ssdPin, i2swsPin, i2sckPin, mclkPin); - esp_err_t err = i2s_driver_install(_i2sPort, &_config, 0, nullptr); + esp_err_t err = i2s_driver_install(I2S_NUM_0, &_config, 0, nullptr); if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to install i2s driver: %d\n", err); return; } - DEBUGSR_PRINTF("AR: I2S#%d driver %s aPLL; fixed_mclk=%d.\n", (int)_i2sPort, _config.use_apll? "uses":"without", _config.fixed_mclk); + DEBUGSR_PRINTF("AR: I2S#0 driver %s aPLL; fixed_mclk=%d.\n", _config.use_apll? "uses":"without", _config.fixed_mclk); DEBUGSR_PRINTF("AR: %d bits, Sample scaling factor = %6.4f\n", _config.bits_per_sample, _sampleScale); if (_config.mode & I2S_MODE_PDM) { - DEBUGSR_PRINTF("AR: I2S#%d driver installed in PDM MASTER mode.\n", (int)_i2sPort); + DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in PDM MASTER mode.")); } else { - DEBUGSR_PRINTF("AR: I2S#%d driver installed in MASTER mode.\n", (int)_i2sPort); + DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in MASTER mode.")); } - err = i2s_set_pin(_i2sPort, &_pinConfig); + err = i2s_set_pin(I2S_NUM_0, &_pinConfig); if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to set i2s pin config: %d\n", err); - i2s_driver_uninstall(_i2sPort); // uninstall already-installed driver + i2s_driver_uninstall(I2S_NUM_0); // uninstall already-installed driver return; } #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - err = i2s_set_clk(_i2sPort, _sampleRate, _config.bits_per_sample, _channelMode); // set bit clocks. Also takes care of MCLK routing if needed. + err = i2s_set_clk(I2S_NUM_0, _sampleRate, I2S_SAMPLE_RESOLUTION, I2S_CHANNEL_MONO); // set bit clocks. Also takes care of MCLK routing if needed. if (err != ESP_OK) { DEBUGSR_PRINTF("AR: Failed to configure i2s clocks: %d\n", err); - i2s_driver_uninstall(_i2sPort); // uninstall already-installed driver + i2s_driver_uninstall(I2S_NUM_0); // uninstall already-installed driver return; } #endif @@ -319,18 +310,16 @@ class I2SSource : public AudioSource { virtual void deinitialize() { _initialized = false; - esp_err_t err = i2s_driver_uninstall(_i2sPort); + esp_err_t err = i2s_driver_uninstall(I2S_NUM_0); if (err != ESP_OK) { DEBUGSR_PRINTF("Failed to uninstall i2s driver: %d\n", err); return; } - if (_managePins) { - if (_pinConfig.ws_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.ws_io_num, PinOwner::UM_Audioreactive); - if (_pinConfig.data_in_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.data_in_num, PinOwner::UM_Audioreactive); - if (_pinConfig.bck_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.bck_io_num, PinOwner::UM_Audioreactive); - // Release the master clock pin - if (_mclkPin != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_mclkPin, PinOwner::UM_Audioreactive); - } + if (_pinConfig.ws_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.ws_io_num, PinOwner::UM_Audioreactive); + if (_pinConfig.data_in_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.data_in_num, PinOwner::UM_Audioreactive); + if (_pinConfig.bck_io_num != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_pinConfig.bck_io_num, PinOwner::UM_Audioreactive); + // Release the master clock pin + if (_mclkPin != I2S_PIN_NO_CHANGE) PinManager::deallocatePin(_mclkPin, PinOwner::UM_Audioreactive); } virtual void getSamples(FFTsampleType *buffer, uint16_t num_samples) { @@ -339,7 +328,7 @@ class I2SSource : public AudioSource { size_t bytes_read = 0; /* Counter variable to check if we actually got enough data */ I2S_datatype newSamples[num_samples]; /* Intermediary sample storage */ - err = i2s_read(_i2sPort, (void *)newSamples, sizeof(newSamples), &bytes_read, portMAX_DELAY); + err = i2s_read(I2S_NUM_0, (void *)newSamples, sizeof(newSamples), &bytes_read, portMAX_DELAY); if (err != ESP_OK) { DEBUGSR_PRINTF("Failed to get samples: %d\n", err); return; @@ -411,115 +400,243 @@ class I2SSource : public AudioSource { i2s_config_t _config; i2s_pin_config_t _pinConfig; int8_t _mclkPin; - i2s_port_t _i2sPort; - i2s_channel_t _channelMode; - bool _managePins; }; - -#if defined(WLED_M5STACK_CORES3_AUDIO) && defined(CONFIG_IDF_TARGET_ESP32S3) /* - * M5Stack CoreS3 ES7210 source + * ES7210 Microphone + * + * Generic ES7210 I2S ADC source for AudioReactive. * - * ES7210 codec setup is performed by CoreS3_Audio. This source waits until - * the codec-ready signal is published, then becomes the sole owner of - * I2S_NUM_1 for PCM sampling. + * WLED samples at 22050 Hz. The ES7210 is configured for: + * - 22050 Hz LRCK + * - 11.2896 MHz MCLK (512 * sample rate) + * - 24-bit I2S output + * - MIC1 / MIC2 enabled * - * Hardware-verified CoreS3 audio path: - * MCLK GPIO0 - * BCLK GPIO34 - * WS GPIO33 - * DIN GPIO14 - * I2S_NUM_1 - * Stereo 16-bit / 16000 Hz + * Board-specific pin assignments remain in the build configuration. */ -class CoreS3ES7210Source : public I2SSource { - public: - static constexpr uint16_t MAX_MONO_SAMPLES = 512; - CoreS3ES7210Source(SRate_t sampleRate, int blockSize) : - I2SSource( - sampleRate, - blockSize, - 1.0f / 16.0f, - I2S_NUM_1, - I2S_CHANNEL_STEREO, - false - ) { - _config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX); - _config.sample_rate = _sampleRate; - _config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; - _config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; - _config.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S); - _config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; - _config.dma_buf_count = 8; - _config.dma_buf_len = _blockSize; - _config.use_apll = false; - _config.tx_desc_auto_clear = false; - _config.fixed_mclk = 0; -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) - _config.mclk_multiple = I2S_MCLK_MULTIPLE_256; - _config.bits_per_chan = I2S_BITS_PER_CHAN_16BIT; +#ifndef ES7210_ADDR +#define ES7210_ADDR 0x40 #endif - } - void initialize( - int8_t = I2S_PIN_NO_CHANGE, - int8_t = I2S_PIN_NO_CHANGE, - int8_t = I2S_PIN_NO_CHANGE, - int8_t = I2S_PIN_NO_CHANGE - ) override { - DEBUGSR_PRINTLN(F("CoreS3ES7210Source:: initialize fixed internal pins;")); +// AI: below section was generated by an AI +class ES7210Source : public I2SSource { + private: + struct RegisterValue { + uint8_t reg; + uint8_t value; + }; - // CoreS3 ES7210 pins are board-internal fixed resources. - // Ignore Usermod pin dropdown values and bypass WLED PinManager. - I2SSource::initialize( - 33, // WS / LRCK - 14, // SD / DIN - 34, // BCLK - 0 // MCLK + bool _es7210I2cWrite(uint8_t reg, uint8_t value) { +#if defined(WLED_M5STACK_CORES3) + /* + * CoreS3 Display/Touch owns the internal GPIO12/GPIO11 bus through + * M5GFX on I2C_NUM_1 after display.begin(). Reuse that initialized + * bus instead of accessing the same pins through Arduino Wire. + */ + static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; + static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; + + const uint8_t data[2] = { reg, value }; + auto result = lgfx::i2c::transactionWrite( + CORES3_INTERNAL_I2C_PORT, + ES7210_ADDR, + data, + sizeof(data), + CORES3_INTERNAL_I2C_FREQUENCY ); - } - void getSamples(FFTsampleType *buffer, uint16_t num_samples) override { - if (buffer == nullptr || num_samples == 0) return; - memset(buffer, 0, num_samples * sizeof(FFTsampleType)); + if (!result.has_value()) { + DEBUGSR_PRINTF( + "AR: ES7210 CoreS3 I2C1 write failed " + "(addr=0x%X, reg=0x%X, val=0x%X).\n", + ES7210_ADDR, + reg, + value + ); + return false; + } - if (!_initialized || num_samples > MAX_MONO_SAMPLES) return; + return true; +#else + Wire.beginTransmission(ES7210_ADDR); + Wire.write(reg); + Wire.write(value); - int16_t stereoSamples[MAX_MONO_SAMPLES * 2]; - const size_t requestedBytes = (size_t)num_samples * 2U * sizeof(int16_t); - size_t bytesRead = 0; + uint8_t i2cErr = Wire.endTransmission(); - esp_err_t err = i2s_read( - _i2sPort, - stereoSamples, - requestedBytes, - &bytesRead, - portMAX_DELAY - ); + if (i2cErr != 0) { + DEBUGSR_PRINTF( + "AR: ES7210 I2C write failed with error=%d " + "(addr=0x%X, reg=0x%X, val=0x%X).\n", + i2cErr, + ES7210_ADDR, + reg, + value + ); + return false; + } - if (err != ESP_OK) { - DEBUGSR_PRINTF("AR: CoreS3 ES7210 sample read failed: %d\n", err); - return; + return true; +#endif + } + + bool _es7210InitAdc() { + if (_sampleRate != 22050) { + DEBUGSR_PRINTF( + "AR: ES7210 requires 22050 Hz sampling; requested %u Hz.\n", + (unsigned)_sampleRate + ); + return false; } - const size_t framesRead = bytesRead / (sizeof(int16_t) * 2U); - const size_t framesToCopy = min((size_t)num_samples, framesRead); + // Reset before applying the ES7210 ADC profile. + if (!_es7210I2cWrite(0x00, 0xFF)) { + return false; + } - for (size_t i = 0; i < framesToCopy; i++) { - const int32_t left = stereoSamples[i * 2U]; - const int32_t right = stereoSamples[i * 2U + 1U]; - const float mono = ((float)(left + right) * 0.5f) * _sampleScale; + delay(1); + + /* + * The CoreS3 hardware-validated ES7210 sequence is retained for + * analog power, microphone bias, HPF and MIC1/MIC2 selection. + * + * The clock and sample-format registers are changed from the old + * 16 kHz / 16-bit profile to WLED's standard: + * + * 22050 Hz + * MCLK = 22050 * 512 = 11.2896 MHz + * 24-bit I2S + * + * ES7210 22.05 kHz coefficient: + * REG02 = 0x81 + * REG07 = 0x20 + * REG04 = 0x02 + * REG05 = 0x00 + * + * ES7210 24-bit sample format: + * REG11 = 0x00 + */ + static constexpr RegisterValue registers[] = { + { 0x00, 0x41 }, + { 0x01, 0x1F }, + { 0x06, 0x00 }, + { 0x07, 0x20 }, + { 0x08, 0x10 }, + { 0x09, 0x30 }, + { 0x0A, 0x30 }, + + { 0x20, 0x0A }, + { 0x21, 0x2A }, + { 0x22, 0x0A }, + { 0x23, 0x2A }, + + // 22.05 kHz with 11.2896 MHz MCLK. + { 0x02, 0x81 }, + { 0x04, 0x02 }, + { 0x05, 0x00 }, + + // 24-bit I2S output. + { 0x11, 0x00 }, + + // Analog / microphone configuration. + { 0x40, 0x42 }, + { 0x41, 0x70 }, + { 0x42, 0x70 }, + + // MIC1 / MIC2 enabled using the validated gain profile. + { 0x43, 0x1B }, + { 0x44, 0x1B }, + + // MIC3 / MIC4 remain unused. + { 0x45, 0x00 }, + { 0x46, 0x00 }, + { 0x47, 0x00 }, + { 0x48, 0x00 }, + { 0x49, 0x00 }, + { 0x4A, 0x00 }, + + { 0x4B, 0x00 }, + { 0x4C, 0xFF }, + + // Enable the required ES7210 clocks. + { 0x01, 0x14 } + }; -#if defined(UM_AUDIOREACTIVE_USE_INTEGER_FFT) - buffer[i] = (int16_t)constrain((int32_t)lroundf(mono), (int32_t)INT16_MIN, (int32_t)INT16_MAX); -#else - buffer[i] = mono; + for (const auto& item : registers) { + if (!_es7210I2cWrite(item.reg, item.value)) { + return false; + } + } + + return true; + } + + public: + ES7210Source( + SRate_t sampleRate, + int blockSize, + float sampleScale = 1.0f + ) : + I2SSource(sampleRate, blockSize, sampleScale) { + +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) + /* + * ES7210 22.05 kHz uses an 11.2896 MHz master clock. + * 22050 * 512 = 11289600. + * + * Keep the standard WLED I2S0 source and request the supported + * 512x MCLK multiplier from the ESP-IDF legacy I2S driver. + */ + _config.mclk_multiple = I2S_MCLK_MULTIPLE_512; #endif + } + + void initialize( + int8_t i2swsPin, + int8_t i2ssdPin, + int8_t i2sckPin, + int8_t mclkPin + ) override { + DEBUGSR_PRINTLN(F("ES7210Source:: initialize();")); + + if ((i2sckPin < 0) || (mclkPin < 0)) { + DEBUGSR_PRINTF( + "\nAR: invalid ES7210 I2S pin: SCK=%d, MCLK=%d\n", + i2sckPin, + mclkPin + ); + return; + } + +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0) + DEBUGSR_PRINTLN( + F("AR: ES7210 requires ESP-IDF 4.4 or newer for 512x MCLK.") + ); + return; +#else + if (!_es7210InitAdc()) { + DEBUGSR_PRINTLN(F("AR: ES7210 codec initialization failed.")); + return; } + + /* + * Standard I2SSource owns: + * - PinManager allocation + * - I2S_NUM_0 + * - I2S driver lifecycle + * - standard sample acquisition + */ + I2SSource::initialize( + i2swsPin, + i2ssdPin, + i2sckPin, + mclkPin + ); +#endif } }; -#endif +// AI: end /* ES7243 Microphone This is an I2S microphone that requires initialization over From eaec8d10fe77e71e5fa0ae045fe5e077b9b944fc Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Wed, 16 Sep 2026 14:20:03 +0900 Subject: [PATCH 07/35] Update CoreS3 audio documentation --- usermods/CoreS3_Display/readme.md | 2 -- usermods/CoreS3_Display/readme_jp.md | 2 -- 2 files changed, 4 deletions(-) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index d3d06c449e..0f42eeec72 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -389,8 +389,6 @@ MCLK GPIO0 DIN GPIO14 ``` -Codec initialization is deferred slightly during startup and retried multiple times if necessary. - Audio status can be checked from WLED Info. --- diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index d38320efd7..07e2716df4 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -389,8 +389,6 @@ MCLK GPIO0 DIN GPIO14 ``` -起動時に Codec 初期化を少し遅延し、失敗時は複数回 Retry する構成です。 - WLED Info から Audio の状態を確認できます。 --- From 52ec5a839dd92445037d3a37ad306247788dfa54 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Wed, 16 Sep 2026 14:24:29 +0900 Subject: [PATCH 08/35] Clarify CoreS3 project structure documentation --- usermods/CoreS3_Display/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 0f42eeec72..741e5f5cba 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -454,7 +454,7 @@ Battery status is updated periodically instead of continuously polling I2C. ## Project Structure -The main CoreS3-specific files are: +The main files involved in CoreS3 support are: ```text WLED/ From 90f46cb2364dc0d64eaddabc4ea837219d8a390a Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 17 Sep 2026 00:03:25 +0900 Subject: [PATCH 09/35] Generalize LED bus re-init deferral for usermods --- usermods/CoreS3_Power/CoreS3_Power.cpp | 89 +++++++++----------------- wled00/fcn_declare.h | 7 ++ wled00/um_manager.cpp | 8 +++ wled00/wled.cpp | 15 ++--- 4 files changed, 49 insertions(+), 70 deletions(-) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index e0ae406170..42d8c652e0 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -10,8 +10,8 @@ * Production baseline for M5Stack CoreS3 power and LED-bus runtime handling. * * LED runtime re-init protection: - * - WLED core re-init gate guarantees the usermod can inspect each pending - * bus configuration before strip.finalizeInit() replaces the old buses. + * - The generic WLED usermod bus re-init gate lets this usermod inspect each + * pending bus configuration before strip.finalizeInit() replaces old buses. * - Physical digital buses are compared per primary data pin. * - If any existing output shrinks or disappears, the complete old LED range * is turned OFF and explicitly rendered BLACK before the bus rebuild. @@ -37,31 +37,6 @@ static volatile bool coreS3PowerInitializationCompleteState = false; static volatile bool coreS3PowerExternal5VReadyState = false; static volatile bool coreS3PowerSafeShutdownMonitorReadyState = false; -// Guaranteed WLED bus re-init gate handshake. -// -// wled.cpp calls coreS3PowerShouldDeferBusReinit() immediately before it would -// consume doInitBusses and call strip.finalizeInit(). -// -// prepared=false -> leave doInitBusses asserted and defer finalizeInit(). -// prepared=true -> allow WLED to consume doInitBusses and rebuild now. -static volatile bool coreS3PowerBusReinitPreparedState = false; - -static inline void coreS3PowerMarkBusReinitPrepared() -{ - coreS3PowerBusReinitPreparedState = true; -} - -extern "C" bool coreS3PowerShouldDeferBusReinit() -{ - // Keep normal WLED startup behavior before this usermod completes setup. - if (!coreS3PowerInitializationCompleteState) return false; - - if (!coreS3PowerBusReinitPreparedState) return true; - - coreS3PowerBusReinitPreparedState = false; - return false; -} - extern "C" bool coreS3PowerInitializationComplete() { return coreS3PowerInitializationCompleteState; @@ -205,9 +180,10 @@ class CoreS3PowerUsermod : public Usermod // Runtime LED shrink state machine. // - // The WLED core gate keeps doInitBusses pending until either loop() or - // handleOverlayDraw() classifies the new bus configuration. A real shrink - // then runs OFF -> confirmed BLACK -> rebuild -> state restore. + // The generic usermod gate keeps doInitBusses pending until either loop(), + // handleOverlayDraw(), or the final gate query classifies the new bus + // configuration. A real shrink then runs OFF -> confirmed BLACK -> rebuild + // -> state restore. enum class LedShrinkSaveState : uint8_t { IDLE = 0, OFF_REQUEST_PENDING, @@ -754,16 +730,9 @@ class CoreS3PowerUsermod : public Usermod if (ledShrinkSaveState != LedShrinkSaveState::IDLE) return; if (!doInitBusses) return; - // A previous hook in this same main-loop may already have approved this - // exact re-init request. Avoid duplicate pass-through logging/preparation. - if (coreS3PowerBusReinitPreparedState) return; - // Safe-shutdown owns LED output. Do not start the shrink OFF/ON sequence; // simply let normal WLED re-init proceed if a settings request arrives. - if (safeShutdownBlankActive) { - coreS3PowerMarkBusReinitPrepared(); - return; - } + if (safeShutdownBlankActive) return; const uint16_t oldPhysical = strip.getLengthPhysical(); const uint16_t targetPhysical = getPendingPhysicalLedCount(); @@ -776,7 +745,6 @@ class CoreS3PowerUsermod : public Usermod oldPhysical == 0 || targetPhysical == 0 ) { - coreS3PowerMarkBusReinitPrepared(); return; } @@ -784,10 +752,7 @@ class CoreS3PowerUsermod : public Usermod // This correctly catches e.g. 5/10/15 -> 15/10/5 (total 30 -> 30). const bool anyPhysicalBusShrinks = pendingConfigShrinksAnyPhysicalBus(); - if (!anyPhysicalBusShrinks) { - coreS3PowerMarkBusReinitPrepared(); - return; - } + if (!anyPhysicalBusShrinks) return; ledShrinkOldPhysical = oldPhysical; ledShrinkTargetPhysical = targetPhysical; @@ -797,10 +762,8 @@ class CoreS3PowerUsermod : public Usermod ledShrinkBlackOverlayFrames = 0; ledShrinkLastBlackTriggerAt = 0; - // Critical: prevent WLED's doInitBusses block later in this SAME main - // loop. The pending busConfigs vector remains intact for the delayed - // finalizeInit() call. - doInitBusses = false; + // doInitBusses stays owned by WLED core. The generic deferBusReinit() + // lifecycle keeps it asserted until the old LED range is safely BLACK. ledShrinkSaveState = LedShrinkSaveState::OFF_REQUEST_PENDING; Serial.printf( @@ -833,10 +796,6 @@ class CoreS3PowerUsermod : public Usermod case LedShrinkSaveState::OFF_REQUEST_PENDING: { - // Keep the pending bus rebuild blocked while the OLD physical count - // is still alive. - doInitBusses = false; - ledShrinkOffRequestedAt = now; ledShrinkOffConfirmedAt = 0; @@ -857,8 +816,6 @@ class CoreS3PowerUsermod : public Usermod case LedShrinkSaveState::WAIT_OFF: { - doInitBusses = false; - if (ledShrinkOffConfirmedAt == 0) { if (bri == 0 && strip.getBrightness() == 0) { ledShrinkOffConfirmedAt = now; @@ -911,14 +868,12 @@ class CoreS3PowerUsermod : public Usermod delay(LED_REINIT_POST_BLACK_GUARD_MS); // The complete OLD range has now been explicitly BLACK in multiple - // real WLED show frames. Freeze drawing, then release normal rebuild. + // real WLED show frames. Freeze drawing, then release the generic rebuild gate. strip.suspend(); strip.waitForIt(); - // The old physical range is now safely BLACK. - // Authorize wled.cpp to consume doInitBusses and call finalizeInit(). - coreS3PowerMarkBusReinitPrepared(); - doInitBusses = true; + // Transitioning to WAIT_REINIT_COMPLETE makes deferBusReinit() return + // false. WLED core will consume doInitBusses and call finalizeInit(). ledShrinkSaveState = LedShrinkSaveState::WAIT_REINIT_COMPLETE; return; @@ -1061,7 +1016,6 @@ class CoreS3PowerUsermod : public Usermod coreS3PowerInitializationCompleteState = false; coreS3PowerExternal5VReadyState = false; coreS3PowerSafeShutdownMonitorReadyState = false; - coreS3PowerBusReinitPreparedState = false; bootResetReason = esp_reset_reason(); @@ -1113,6 +1067,23 @@ class CoreS3PowerUsermod : public Usermod Serial.println(); } + bool deferBusReinit() override + { + // Preserve normal WLED behavior before this usermod completes setup. + if (!coreS3PowerInitializationCompleteState) return false; + + // Classify here as well as from loop()/handleOverlayDraw(). This guarantees + // that a request arriving later in the same main-loop iteration cannot + // bypass old-range BLACK preparation. + captureLedShrinkReinitRequest(); + + // WLED core keeps ownership of doInitBusses. We only hold the generic gate + // while preparing the old output, then release it for finalizeInit(). + return + ledShrinkSaveState == LedShrinkSaveState::OFF_REQUEST_PENDING || + ledShrinkSaveState == LedShrinkSaveState::WAIT_OFF; + } + void loop() override { captureLedShrinkReinitRequest(); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 3409ff91ae..f36908e0b4 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -365,6 +365,12 @@ class Usermod { virtual ~Usermod() { if (um_data) delete um_data; } virtual void setup() = 0; // pure virtual, has to be overriden virtual void loop() = 0; // pure virtual, has to be overriden + + // Return true to keep a pending LED bus re-init deferred. Implementations + // that defer must eventually return false so the core can rebuild buses, + // write configuration, and continue any queued reboot. + virtual bool deferBusReinit() { return false; } + virtual void handleOverlayDraw() {} // called after all effects have been processed, just before strip.show() virtual bool handleButton(uint8_t b) { return false; } // button overrides are possible here virtual bool getUMData(um_data_t **data) { if (data) *data = nullptr; return false; }; // usermod data exchange [see examples for audio effects] @@ -400,6 +406,7 @@ class Usermod { namespace UsermodManager { void loop(); + bool deferBusReinit(); void handleOverlayDraw(); bool handleButton(uint8_t b); bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods diff --git a/wled00/um_manager.cpp b/wled00/um_manager.cpp index 504b5ba97c..cd3f85e8d8 100644 --- a/wled00/um_manager.cpp +++ b/wled00/um_manager.cpp @@ -21,6 +21,14 @@ static size_t getCount() { void UsermodManager::setup() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->setup(); } void UsermodManager::connected() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->connected(); } void UsermodManager::loop() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->loop(); } + +bool UsermodManager::deferBusReinit() { + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { + if ((*mod)->deferBusReinit()) return true; + } + return false; +} + void UsermodManager::handleOverlayDraw() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->handleOverlayDraw(); } void UsermodManager::appendConfigData(Print& dest) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->appendConfigData(dest); } bool UsermodManager::handleButton(uint8_t b) { diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 25841c4959..33278b86bd 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -25,14 +25,6 @@ #endif extern "C" void usePWMFixedNMI(); -// Optional usermod bus re-init gate. -// The weak default preserves standard WLED behavior. CoreS3_Power provides -// the strong implementation used to protect old LED output before bus rebuild. -extern "C" bool __attribute__((weak)) coreS3PowerShouldDeferBusReinit() -{ - return false; -} - // millis()-rollover counter (millis() wraps every ~50 days) - previously // WLED_GLOBAL. json.cpp and usermods only ever read it for uptime reporting, // so it gets a by-value getter rather than a mutable reference: an accidental @@ -245,9 +237,10 @@ void WLED::loop() //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! if (doInitBusses) { - // Allow a usermod to defer this rebuild until the old LED output is safe. - // The weak default returns false, preserving standard WLED behavior. - if (!coreS3PowerShouldDeferBusReinit()) { + // Usermods may defer the rebuild while they prepare the existing LED + // output. A deferring usermod must eventually release the gate so config + // writes and any queued reboot can continue. + if (!UsermodManager::deferBusReinit()) { doInitBusses = false; DEBUG_PRINTLN(F("Re-init busses.")); bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) From b7689fbe8614f754777e5173818fa383b7ebaf67 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 17 Sep 2026 00:18:38 +0900 Subject: [PATCH 10/35] Bound LED bus re-init BLACK confirmation wait --- usermods/CoreS3_Power/CoreS3_Power.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index 42d8c652e0..ee82e4ae58 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -843,13 +843,28 @@ class CoreS3PowerUsermod : public Usermod // // handleOverlayDraw() explicitly replaces the full OLD logical // range with BLACK immediately before WLED paints the frame to - // BusManager and calls show(). Require several such frames. + // BusManager and calls show(). Require several such frames, but keep + // the confirmation bounded so a failed render path cannot leave the + // generic bus re-init gate asserted forever. if (ledShrinkBlackOverlayFrames < LED_REINIT_REQUIRED_BLACK_FRAMES) { if (now - ledShrinkLastBlackTriggerAt >= LED_REINIT_BLACK_FRAME_TRIGGER_MS) { ledShrinkLastBlackTriggerAt = now; strip.trigger(); } - return; + + if (now - ledShrinkOffConfirmedAt < LED_REINIT_OFF_CONFIRM_TIMEOUT_MS) { + return; + } + + Serial.printf( + "[CoreS3_Power][LED] WARNING: BLACK frame confirmation timeout %u/%u; continuing bus re-init\n", + ledShrinkBlackOverlayFrames, + LED_REINIT_REQUIRED_BLACK_FRAMES + ); + + // Release this stage after the bounded fallback so the warning is + // emitted only once if a later output-idle check needs another loop. + ledShrinkBlackOverlayFrames = LED_REINIT_REQUIRED_BLACK_FRAMES; } // Keep a conservative OFF settle window in addition to actual @@ -868,7 +883,8 @@ class CoreS3PowerUsermod : public Usermod delay(LED_REINIT_POST_BLACK_GUARD_MS); // The complete OLD range has now been explicitly BLACK in multiple - // real WLED show frames. Freeze drawing, then release the generic rebuild gate. + // real WLED show frames, or the bounded confirmation fallback expired. + // Freeze drawing, then release the generic rebuild gate. strip.suspend(); strip.waitForIt(); From e7d128e017d5af2722a99d9425634d77e1d95e7d Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sun, 20 Sep 2026 21:15:20 +0900 Subject: [PATCH 11/35] Fix ES7210 source ID and IDF4 compatibility --- .../CoreS3_Display/platformio_override.ini.sample | 2 +- usermods/audioreactive/audio_reactive.cpp | 8 ++++++-- usermods/audioreactive/audio_source.h | 15 ++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 730429a9e7..1caeaad279 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -66,7 +66,7 @@ build_flags = ; CoreS3 built-in ES7210 / AudioReactive integration. -D UM_AUDIOREACTIVE_ENABLE - -D SR_DMTYPE=7 + -D SR_DMTYPE=10 -D I2S_SDPIN=14 -D I2S_WSPIN=33 -D I2S_CKPIN=34 diff --git a/usermods/audioreactive/audio_reactive.cpp b/usermods/audioreactive/audio_reactive.cpp index 9ab25d31fc..8460fe5af3 100644 --- a/usermods/audioreactive/audio_reactive.cpp +++ b/usermods/audioreactive/audio_reactive.cpp @@ -1478,14 +1478,16 @@ class AudioReactive : public Usermod { if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; + #if ESP_IDF_VERSION_MAJOR >= 5 // AI: below section was generated by an AI - case 7: + case 10: DEBUGSR_PRINTLN(F("AR: ES7210 Source")); audioSource = new ES7210Source(SAMPLE_RATE, BLOCK_SIZE); delay(100); if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; // AI: end + #endif #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) // legacy ADC driver is not available any more in esp-idf V5.x.y // ADC over I2S is only possible on "classic" ESP32 @@ -2199,7 +2201,9 @@ class AudioReactive : public Usermod { uiScript.print(F("addOption(dd,'Generic PDM',5);")); #endif uiScript.print(F("addOption(dd,'ES8388',6);")); - uiScript.print(F("addOption(dd,'ES7210',7);")); + #if ESP_IDF_VERSION_MAJOR >= 5 + uiScript.print(F("addOption(dd,'ES7210',10);")); + #endif uiScript.print(F("addOption(dd,'None - network receive only',")); uiScript.print(SR_DMTYPE_NETWORK_ONLY); uiScript.print(F(");")); diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 58491bec45..c37b894e12 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -419,6 +419,11 @@ class I2SSource : public AudioSource { #define ES7210_ADDR 0x40 #endif +#if defined(SR_DMTYPE) && (SR_DMTYPE == 10) && (ESP_IDF_VERSION_MAJOR < 5) +#error "ES7210 audio source requires ESP-IDF 5.x or newer" +#endif + +#if ESP_IDF_VERSION_MAJOR >= 5 // AI: below section was generated by an AI class ES7210Source : public I2SSource { private: @@ -580,7 +585,6 @@ class ES7210Source : public I2SSource { ) : I2SSource(sampleRate, blockSize, sampleScale) { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) /* * ES7210 22.05 kHz uses an 11.2896 MHz master clock. * 22050 * 512 = 11289600. @@ -589,7 +593,6 @@ class ES7210Source : public I2SSource { * 512x MCLK multiplier from the ESP-IDF legacy I2S driver. */ _config.mclk_multiple = I2S_MCLK_MULTIPLE_512; -#endif } void initialize( @@ -609,12 +612,6 @@ class ES7210Source : public I2SSource { return; } -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0) - DEBUGSR_PRINTLN( - F("AR: ES7210 requires ESP-IDF 4.4 or newer for 512x MCLK.") - ); - return; -#else if (!_es7210InitAdc()) { DEBUGSR_PRINTLN(F("AR: ES7210 codec initialization failed.")); return; @@ -633,10 +630,10 @@ class ES7210Source : public I2SSource { i2sckPin, mclkPin ); -#endif } }; // AI: end +#endif // ESP_IDF_VERSION_MAJOR >= 5 /* ES7243 Microphone This is an I2S microphone that requires initialization over From 5add5a8a2ddceb462e3f30306582dc79a74376a6 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sun, 20 Sep 2026 23:10:12 +0900 Subject: [PATCH 12/35] Guard CoreS3 power usermod on non-CoreS3 targets --- usermods/CoreS3_Power/CoreS3_Power.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index ee82e4ae58..54a7c6e151 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -1,4 +1,7 @@ #include "wled.h" + +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) + #include #include #include @@ -1278,3 +1281,24 @@ class CoreS3PowerUsermod : public Usermod static CoreS3PowerUsermod coreS3PowerUsermod; REGISTER_USERMOD(coreS3PowerUsermod); + +#else + +/* + * CoreS3_Power is hardware-specific. + * + * Generic usermod CI compiles each usermod on several ESP32 targets. + * Keep a minimal registered module on non-CoreS3 targets so the build and + * module validation can run without compiling CoreS3-only M5GFX/I2C1 code. + */ +class CoreS3PowerUsermod : public Usermod +{ +public: + void setup() override {} + void loop() override {} +}; + +static CoreS3PowerUsermod coreS3PowerUsermod; +REGISTER_USERMOD(coreS3PowerUsermod); + +#endif // WLED_M5STACK_CORES3 && CONFIG_IDF_TARGET_ESP32S3 From 15887b66e7aa75dc7d4e7a1e348def08bcf628ec Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 21 Sep 2026 00:34:29 +0900 Subject: [PATCH 13/35] Remove deferred LED bus re-init from CoreS3 PR --- usermods/CoreS3_Power/CoreS3_Power.cpp | 400 +------------------------ wled00/fcn_declare.h | 7 - wled00/um_manager.cpp | 8 - wled00/wled.cpp | 21 +- 4 files changed, 9 insertions(+), 427 deletions(-) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index 54a7c6e151..2d6b133e80 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -10,16 +10,7 @@ /* * CoreS3 Power Production Baseline * - * Production baseline for M5Stack CoreS3 power and LED-bus runtime handling. - * - * LED runtime re-init protection: - * - The generic WLED usermod bus re-init gate lets this usermod inspect each - * pending bus configuration before strip.finalizeInit() replaces old buses. - * - Physical digital buses are compared per primary data pin. - * - If any existing output shrinks or disappears, the complete old LED range - * is turned OFF and explicitly rendered BLACK before the bus rebuild. - * - WLED ON/OFF state is restored after re-init; an already-OFF state remains OFF. - * - Pure growth / unchanged configurations pass through without OFF/ON cycling. + * Production baseline for M5Stack CoreS3 power handling. * * Preserved validated power behavior: * - AW9523B external-5V enable writes (BOOST then BUS). @@ -103,17 +94,6 @@ class CoreS3PowerUsermod : public Usermod static constexpr unsigned long SAFE_SHUTDOWN_SHOW_WAIT_MS = 150; static constexpr unsigned long RUNTIME_POWER_HEALTH_POLL_MS = 10000; - // Runtime LED-bus shrink protection timing. - static constexpr unsigned long LED_REINIT_OFF_CONFIRM_TIMEOUT_MS = 3000; - static constexpr unsigned long LED_REINIT_OFF_SETTLE_MS = 500; - static constexpr unsigned long LED_REINIT_OUTPUT_WAIT_MS = 500; - - // Require multiple actual overlay/show frames that explicitly overwrite - // the complete OLD logical range with BLACK before allowing finalizeInit(). - static constexpr uint8_t LED_REINIT_REQUIRED_BLACK_FRAMES = 3; - static constexpr unsigned long LED_REINIT_BLACK_FRAME_TRIGGER_MS = 50; - static constexpr unsigned long LED_REINIT_POST_BLACK_GUARD_MS = 20; - static constexpr uint8_t REG_OUTPUT_P0 = 0x02; static constexpr uint8_t REG_OUTPUT_P1 = 0x03; static constexpr uint8_t REG_CONFIG_P0 = 0x04; @@ -181,35 +161,6 @@ class CoreS3PowerUsermod : public Usermod uint8_t dcdcModeAfter = 0; - // Runtime LED shrink state machine. - // - // The generic usermod gate keeps doInitBusses pending until either loop(), - // handleOverlayDraw(), or the final gate query classifies the new bus - // configuration. A real shrink then runs OFF -> confirmed BLACK -> rebuild - // -> state restore. - enum class LedShrinkSaveState : uint8_t { - IDLE = 0, - OFF_REQUEST_PENDING, - WAIT_OFF, - WAIT_REINIT_COMPLETE - }; - - LedShrinkSaveState ledShrinkSaveState = LedShrinkSaveState::IDLE; - - bool ledShrinkWasOn = false; - uint8_t ledShrinkOriginalBrightness = 0; - uint8_t ledShrinkOriginalBriLast = 0; - - uint16_t ledShrinkOldPhysical = 0; - uint16_t ledShrinkTargetPhysical = 0; - - unsigned long ledShrinkOffRequestedAt = 0; - unsigned long ledShrinkOffConfirmedAt = 0; - - // Actual BLACK frame confirmation. - uint8_t ledShrinkBlackOverlayFrames = 0; - unsigned long ledShrinkLastBlackTriggerAt = 0; - const char* resetReasonText(esp_reset_reason_t reason) { switch (reason) { @@ -656,297 +607,6 @@ class CoreS3PowerUsermod : public Usermod Serial.printf("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); } - uint16_t getPendingPhysicalLedCount() const - { - uint32_t total = 0; - - for (const auto &bc : busConfigs) { - if (Bus::isVirtual(bc.type)) continue; - total += bc.count; - } - - if (total > 65535UL) total = 65535UL; - return (uint16_t)total; - } - - // ------------------------------------------------------------ - // Per-physical-bus shrink detection. - // - // Match each existing physical digital Bus to the pending BusConfig by - // primary data pin. Any output that becomes shorter or disappears requires - // old-range BLACK preparation before finalizeInit(). - // ------------------------------------------------------------ - bool pendingConfigShrinksAnyPhysicalBus() - { - const size_t oldBusCount = BusManager::getNumBusses(); - - for (size_t oldIndex = 0; oldIndex < oldBusCount; ++oldIndex) { - Bus* oldBus = BusManager::getBus(oldIndex); - if (oldBus == nullptr) continue; - if (!oldBus->isDigital() || oldBus->isVirtual() || oldBus->isPlaceholder()) continue; - - uint8_t oldPins[OUTPUT_MAX_PINS] = {255, 255, 255, 255, 255}; - const size_t oldPinCount = oldBus->getPins(oldPins); - if (oldPinCount == 0 || oldPins[0] == 255) continue; - - const uint8_t oldPrimaryPin = oldPins[0]; - const uint16_t oldLength = oldBus->getLength(); - - bool matchingPendingBusFound = false; - uint16_t pendingLength = 0; - - for (const auto &bc : busConfigs) { - if (Bus::isVirtual(bc.type)) continue; - if (!Bus::isDigital(bc.type)) continue; - if (bc.pins[0] != oldPrimaryPin) continue; - - matchingPendingBusFound = true; - pendingLength = bc.count; - break; - } - - if (!matchingPendingBusFound) { - Serial.printf( - "[CoreS3_Power][LED] Bus shrink detected: pin=%u %u->removed\n", - oldPrimaryPin, - oldLength - ); - return true; - } - - if (pendingLength < oldLength) { - Serial.printf( - "[CoreS3_Power][LED] Bus shrink detected: pin=%u %u->%u\n", - oldPrimaryPin, - oldLength, - pendingLength - ); - return true; - } - } - - return false; - } - - void captureLedShrinkReinitRequest() - { - if (ledShrinkSaveState != LedShrinkSaveState::IDLE) return; - if (!doInitBusses) return; - - // Safe-shutdown owns LED output. Do not start the shrink OFF/ON sequence; - // simply let normal WLED re-init proceed if a settings request arrives. - if (safeShutdownBlankActive) return; - - const uint16_t oldPhysical = strip.getLengthPhysical(); - const uint16_t targetPhysical = getPendingPhysicalLedCount(); - - // Keep an explicit safe pass-through for initialization / an empty pending - // configuration. The normal LED Hardware UI used in this CoreS3 build has - // real physical buses and non-zero counts, so this is only a guardrail. - if ( - BusManager::getNumBusses() == 0 || - oldPhysical == 0 || - targetPhysical == 0 - ) { - return; - } - - // The decision is now PER OLD PHYSICAL BUS, not aggregate total length. - // This correctly catches e.g. 5/10/15 -> 15/10/5 (total 30 -> 30). - const bool anyPhysicalBusShrinks = pendingConfigShrinksAnyPhysicalBus(); - - if (!anyPhysicalBusShrinks) return; - - ledShrinkOldPhysical = oldPhysical; - ledShrinkTargetPhysical = targetPhysical; - ledShrinkWasOn = (bri > 0); - ledShrinkOriginalBrightness = bri; - ledShrinkOriginalBriLast = briLast; - ledShrinkBlackOverlayFrames = 0; - ledShrinkLastBlackTriggerAt = 0; - - // doInitBusses stays owned by WLED core. The generic deferBusReinit() - // lifecycle keeps it asserted until the old LED range is safely BLACK. - ledShrinkSaveState = LedShrinkSaveState::OFF_REQUEST_PENDING; - - Serial.printf( - "[CoreS3_Power][LED] Safe bus re-init start: %u->%u state=%s\n", - ledShrinkOldPhysical, - ledShrinkTargetPhysical, - ledShrinkWasOn ? "ON" : "OFF" - ); - } - - bool waitForLedOutputIdle(unsigned long timeoutMs) - { - const unsigned long startedAt = millis(); - - while (!BusManager::canAllShow()) { - if (millis() - startedAt >= timeoutMs) return false; - delay(1); - } - - return true; - } - - void serviceLedShrinkSaveOffCycle() - { - const unsigned long now = millis(); - - switch (ledShrinkSaveState) { - case LedShrinkSaveState::IDLE: - return; - - case LedShrinkSaveState::OFF_REQUEST_PENDING: - { - ledShrinkOffRequestedAt = now; - ledShrinkOffConfirmedAt = 0; - - if (ledShrinkWasOn) { - // Use the exact WLED power path proven manually 5/5. - toggleOnOff(); - stateUpdated(CALL_MODE_BUTTON); - strip.trigger(); - } else { - ledShrinkOffConfirmedAt = now; - } - - // Do NOT suspend yet. Normal strip.service() must render the OFF state - // across the complete OLD LED range. - ledShrinkSaveState = LedShrinkSaveState::WAIT_OFF; - return; - } - - case LedShrinkSaveState::WAIT_OFF: - { - if (ledShrinkOffConfirmedAt == 0) { - if (bri == 0 && strip.getBrightness() == 0) { - ledShrinkOffConfirmedAt = now; - - } else if (now - ledShrinkOffRequestedAt >= LED_REINIT_OFF_CONFIRM_TIMEOUT_MS) { - // Do not deadlock configuration forever. Keep rendering OFF and - // start the conservative settle window even if the runtime - // brightness report did not reach zero as expected. - ledShrinkOffConfirmedAt = now; - strip.trigger(); - - Serial.printf( - "[CoreS3_Power][LED] WARNING: bus re-init OFF timeout bri=%u strip=%u\n", - bri, - strip.getBrightness() - ); - } - - return; - } - - // Time alone is not enough. Earlier testing showed internal - // brightness=0 did not prove that a complete OLD-range BLACK frame - // had actually passed through WLED's final show path. - // - // handleOverlayDraw() explicitly replaces the full OLD logical - // range with BLACK immediately before WLED paints the frame to - // BusManager and calls show(). Require several such frames, but keep - // the confirmation bounded so a failed render path cannot leave the - // generic bus re-init gate asserted forever. - if (ledShrinkBlackOverlayFrames < LED_REINIT_REQUIRED_BLACK_FRAMES) { - if (now - ledShrinkLastBlackTriggerAt >= LED_REINIT_BLACK_FRAME_TRIGGER_MS) { - ledShrinkLastBlackTriggerAt = now; - strip.trigger(); - } - - if (now - ledShrinkOffConfirmedAt < LED_REINIT_OFF_CONFIRM_TIMEOUT_MS) { - return; - } - - Serial.printf( - "[CoreS3_Power][LED] WARNING: BLACK frame confirmation timeout %u/%u; continuing bus re-init\n", - ledShrinkBlackOverlayFrames, - LED_REINIT_REQUIRED_BLACK_FRAMES - ); - - // Release this stage after the bounded fallback so the warning is - // emitted only once if a later output-idle check needs another loop. - ledShrinkBlackOverlayFrames = LED_REINIT_REQUIRED_BLACK_FRAMES; - } - - // Keep a conservative OFF settle window in addition to actual - // overlay/show-frame evidence. - if (now - ledShrinkOffConfirmedAt < LED_REINIT_OFF_SETTLE_MS) return; - - strip.waitForIt(); - - if (!waitForLedOutputIdle(LED_REINIT_OUTPUT_WAIT_MS)) { - Serial.println(F("[CoreS3_Power][LED] WARNING: old LED output busy; bus re-init deferred")); - return; - } - - // NeoPixelBus/LCD output is asynchronous; preserve old buses briefly - // after the final confirmed BLACK show before destroying them. - delay(LED_REINIT_POST_BLACK_GUARD_MS); - - // The complete OLD range has now been explicitly BLACK in multiple - // real WLED show frames, or the bounded confirmation fallback expired. - // Freeze drawing, then release the generic rebuild gate. - strip.suspend(); - strip.waitForIt(); - - // Transitioning to WAIT_REINIT_COMPLETE makes deferBusReinit() return - // false. WLED core will consume doInitBusses and call finalizeInit(). - ledShrinkSaveState = LedShrinkSaveState::WAIT_REINIT_COMPLETE; - - return; - } - - case LedShrinkSaveState::WAIT_REINIT_COMPLETE: - { - // WLED clears doInitBusses immediately before strip.finalizeInit(). - // false here means the previous main-loop iteration completed rebuild. - if (doInitBusses) return; - - strip.resume(); - - if (ledShrinkWasOn) { - if (bri == 0) { - toggleOnOff(); - stateUpdated(CALL_MODE_BUTTON); - } else { - // Defensive fallback if another component changed WLED state. - bri = ledShrinkOriginalBrightness; - briLast = ledShrinkOriginalBriLast; - stateUpdated(CALL_MODE_BUTTON); - } - } else { - // Preserve original OFF state. - bri = 0; - briLast = ledShrinkOriginalBriLast; - stateUpdated(CALL_MODE_BUTTON); - } - - strip.trigger(); - - Serial.printf( - "[CoreS3_Power][LED] Safe bus re-init complete: %u->%u state=%s\n", - ledShrinkOldPhysical, - strip.getLengthPhysical(), - bri > 0 ? "ON" : "OFF" - ); - - ledShrinkSaveState = LedShrinkSaveState::IDLE; - ledShrinkWasOn = false; - ledShrinkOriginalBrightness = 0; - ledShrinkOriginalBriLast = 0; - ledShrinkOldPhysical = 0; - ledShrinkTargetPhysical = 0; - ledShrinkOffRequestedAt = 0; - ledShrinkOffConfirmedAt = 0; - ledShrinkBlackOverlayFrames = 0; - ledShrinkLastBlackTriggerAt = 0; - return; - } - } - } - void servicePhysicalPowerKey() { unsigned long now = millis(); @@ -1081,60 +741,18 @@ class CoreS3PowerUsermod : public Usermod coreS3PowerInitializationCompleteState = true; - Serial.println(F("[CoreS3_Power] LED runtime re-init: READY - per-bus shrink guard")); Serial.println(F("[CoreS3_Power] Initialization complete")); Serial.println(); } - bool deferBusReinit() override - { - // Preserve normal WLED behavior before this usermod completes setup. - if (!coreS3PowerInitializationCompleteState) return false; - - // Classify here as well as from loop()/handleOverlayDraw(). This guarantees - // that a request arriving later in the same main-loop iteration cannot - // bypass old-range BLACK preparation. - captureLedShrinkReinitRequest(); - - // WLED core keeps ownership of doInitBusses. We only hold the generic gate - // while preparing the old output, then release it for finalizeInit(). - return - ledShrinkSaveState == LedShrinkSaveState::OFF_REQUEST_PENDING || - ledShrinkSaveState == LedShrinkSaveState::WAIT_OFF; - } - void loop() override { - captureLedShrinkReinitRequest(); - serviceLedShrinkSaveOffCycle(); - servicePhysicalPowerKey(); serviceRuntimeExternal5VEnable(); serviceDcdc3StabilityMode(); serviceRuntimePowerHealth(); } - // WLED calls this after effects render and immediately before the frame - // is painted to BusManager/show(). This is both the second re-init capture - // point and the final BLACK override for the complete old logical range. - void handleOverlayDraw() override - { - captureLedShrinkReinitRequest(); - if ( - ledShrinkSaveState == LedShrinkSaveState::WAIT_OFF && - bri == 0 && - strip.getBrightness() == 0 && - ledShrinkOldPhysical > 0 - ) { - strip.setRange(0, ledShrinkOldPhysical - 1, 0); - - if (ledShrinkBlackOverlayFrames < 255) { - ledShrinkBlackOverlayFrames++; - } - - } - } - void addToJsonInfo(JsonObject& root) override { JsonObject user = root["u"]; @@ -1152,22 +770,6 @@ class CoreS3PowerUsermod : public Usermod JsonArray stabilityInfo = user.createNestedArray("CoreS3 Power Stability"); stabilityInfo.add("DCDC3 Always-PWM"); - JsonArray ledReinitInfo = user.createNestedArray("CoreS3 LED Runtime Reinit"); - switch (ledShrinkSaveState) { - case LedShrinkSaveState::IDLE: - ledReinitInfo.add("READY - per-bus shrink guard"); - break; - case LedShrinkSaveState::OFF_REQUEST_PENDING: - ledReinitInfo.add("Bus shrink detected - preparing OFF"); - break; - case LedShrinkSaveState::WAIT_OFF: - ledReinitInfo.add("Clearing old LED range"); - break; - case LedShrinkSaveState::WAIT_REINIT_COMPLETE: - ledReinitInfo.add("Rebuilding LED buses"); - break; - } - JsonArray pwmInfo = user.createNestedArray("CoreS3 DCDC3 Mode"); if (!dcdc3StabilityAttempted) { pwmInfo.add("PENDING"); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index d20af21ef7..35b3ae0e1b 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -365,12 +365,6 @@ class Usermod { virtual ~Usermod() { if (um_data) delete um_data; } virtual void setup() = 0; // pure virtual, has to be overriden virtual void loop() = 0; // pure virtual, has to be overriden - - // Return true to keep a pending LED bus re-init deferred. Implementations - // that defer must eventually return false so the core can rebuild buses, - // write configuration, and continue any queued reboot. - virtual bool deferBusReinit() { return false; } - virtual void handleOverlayDraw() {} // called after all effects have been processed, just before strip.show() virtual bool handleButton(uint8_t b) { return false; } // button overrides are possible here virtual bool getUMData(um_data_t **data) { if (data) *data = nullptr; return false; }; // usermod data exchange [see examples for audio effects] @@ -406,7 +400,6 @@ class Usermod { namespace UsermodManager { void loop(); - bool deferBusReinit(); void handleOverlayDraw(); bool handleButton(uint8_t b); bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods diff --git a/wled00/um_manager.cpp b/wled00/um_manager.cpp index cd3f85e8d8..504b5ba97c 100644 --- a/wled00/um_manager.cpp +++ b/wled00/um_manager.cpp @@ -21,14 +21,6 @@ static size_t getCount() { void UsermodManager::setup() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->setup(); } void UsermodManager::connected() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->connected(); } void UsermodManager::loop() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->loop(); } - -bool UsermodManager::deferBusReinit() { - for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { - if ((*mod)->deferBusReinit()) return true; - } - return false; -} - void UsermodManager::handleOverlayDraw() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->handleOverlayDraw(); } void UsermodManager::appendConfigData(Print& dest) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->appendConfigData(dest); } bool UsermodManager::handleButton(uint8_t b) { diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 33278b86bd..f049ecaaa5 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -237,19 +237,14 @@ void WLED::loop() //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! if (doInitBusses) { - // Usermods may defer the rebuild while they prepare the existing LED - // output. A deferring usermod must eventually release the gate so config - // writes and any queued reboot can continue. - if (!UsermodManager::deferBusReinit()) { - doInitBusses = false; - DEBUG_PRINTLN(F("Re-init busses.")); - bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) - strip.finalizeInit(); // will create buses and also load default ledmap if present - if (aligned) strip.makeAutoSegments(); - else strip.fixInvalidSegments(); - BusManager::setBrightness(scaledBri(bri)); // fix re-initialised bus' brightness #4005 and #4824 - configNeedsWrite = true; - } + doInitBusses = false; + DEBUG_PRINTLN(F("Re-init busses.")); + bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) + strip.finalizeInit(); // will create buses and also load default ledmap if present + if (aligned) strip.makeAutoSegments(); + else strip.fixInvalidSegments(); + BusManager::setBrightness(scaledBri(bri)); // fix re-initialised bus' brightness #4005 and #4824 + configNeedsWrite = true; } if (loadLedmap >= 0) { strip.deserializeMap(loadLedmap); From cb6bc6fb08c88a1d3ad8d97416d5d582116cfca2 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 21 Sep 2026 00:47:36 +0900 Subject: [PATCH 14/35] Use C++ linkage for CoreS3 power getters --- usermods/CoreS3_Display/CoreS3_Display.cpp | 6 +++--- usermods/CoreS3_Power/CoreS3_Power.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 99115fd2ed..e390ea5d6b 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -57,9 +57,9 @@ static const char CORES3_DISPLAY_CONFIG_NAME[] PROGMEM = "CoreS3_Display"; // CoreS3_Power publishes read-only runtime health state. // Display consumes these signals only for user-facing warning UX; it does // not own or modify the power-control implementation. -extern "C" bool coreS3PowerInitializationComplete(); -extern "C" bool coreS3PowerExternal5VReady(); -extern "C" bool coreS3PowerSafeShutdownMonitorReady(); +bool coreS3PowerInitializationComplete(); +bool coreS3PowerExternal5VReady(); +bool coreS3PowerSafeShutdownMonitorReady(); class CoreS3DisplayUsermod : public Usermod { diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index 2d6b133e80..a7a1a09c43 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -31,17 +31,17 @@ static volatile bool coreS3PowerInitializationCompleteState = false; static volatile bool coreS3PowerExternal5VReadyState = false; static volatile bool coreS3PowerSafeShutdownMonitorReadyState = false; -extern "C" bool coreS3PowerInitializationComplete() +bool coreS3PowerInitializationComplete() { return coreS3PowerInitializationCompleteState; } -extern "C" bool coreS3PowerExternal5VReady() +bool coreS3PowerExternal5VReady() { return coreS3PowerExternal5VReadyState; } -extern "C" bool coreS3PowerSafeShutdownMonitorReady() +bool coreS3PowerSafeShutdownMonitorReady() { return coreS3PowerSafeShutdownMonitorReadyState; } From e498c4a535352c1e5880e7b58d0d4738870a0bcb Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 21 Sep 2026 14:10:42 +0900 Subject: [PATCH 15/35] Remove NeoPixelBus build-time patch from CoreS3 PR --- pio-scripts/cores3_v17_neopixelbus_patch.py | 519 ------------------ .../platformio_override.ini.sample | 4 - usermods/CoreS3_Display/readme.md | 57 +- usermods/CoreS3_Display/readme_jp.md | 57 +- 4 files changed, 4 insertions(+), 633 deletions(-) delete mode 100644 pio-scripts/cores3_v17_neopixelbus_patch.py diff --git a/pio-scripts/cores3_v17_neopixelbus_patch.py b/pio-scripts/cores3_v17_neopixelbus_patch.py deleted file mode 100644 index 52de00242f..0000000000 --- a/pio-scripts/cores3_v17_neopixelbus_patch.py +++ /dev/null @@ -1,519 +0,0 @@ -# M5Stack CoreS3 / WLED V17 NeoPixelBus Production Integration -# -# PlatformIO PRE script for the m5stack_cores3 environment. -# -# This script makes the two hardware-validated NeoPixelBus fixes reproducible: -# -# 1) ESP32-S3 RMT output: -# - DMA enabled -# - 1024-symbol TX buffer -# - non-blocking readiness check without ESP-IDF timeout=0 log noise -# - guarded RMT handle lifecycle -# -# 2) ESP32-S3 LCD/GDMA output: -# - fully stop/reset/disconnect/delete the GDMA channel when the last -# LCD mux bus is destroyed -# - prevents stale LCD peripheral ownership across runtime bus rebuilds -# -# The script patches the NeoPixelBus source downloaded by PlatformIO under -# .pio/libdeps. It does not require modified library files to be committed. -# -# NeoPixelBus itself is licensed under LGPL-3.0-or-later. This script keeps -# the upstream license header in the patched files untouched. -# -# The patch is idempotent and is safe to run on every PlatformIO build. - -from pathlib import Path - -Import("env") - -RMT_MARKER = "CoreS3 NeoPixelBus RMT DMA1024 production patch" -LCD_MARKER = "CoreS3 NeoPixelBus LCD GDMA teardown production patch" - -LCD_TEST_PRAGMAS = ( - '#pragma message("=== CORES3 TEST: ACTIVE NeoPixelBus NeoEsp32LcdXMethod.h IS COMPILED ===")', - '#pragma message("=== CORES3 TEST: NeoEsp32LcdXMethod.h IS COMPILED ===")', -) - - -def _replace_function(source: str, signature: str, replacement: str) -> str: - '''Replace one C++ function body by matching balanced braces.''' - sig_pos = source.find(signature) - if sig_pos < 0: - raise RuntimeError(f"signature not found: {signature}") - - brace_start = source.find("{", sig_pos + len(signature)) - if brace_start < 0: - raise RuntimeError(f"opening brace not found: {signature}") - - depth = 0 - in_string = False - in_char = False - escape = False - line_comment = False - block_comment = False - i = brace_start - - while i < len(source): - ch = source[i] - nxt = source[i + 1] if i + 1 < len(source) else "" - - if line_comment: - if ch == "\n": - line_comment = False - i += 1 - continue - - if block_comment: - if ch == "*" and nxt == "/": - block_comment = False - i += 2 - continue - i += 1 - continue - - if escape: - escape = False - i += 1 - continue - - if (in_string or in_char) and ch == "\\": - escape = True - i += 1 - continue - - if not in_string and not in_char: - if ch == "/" and nxt == "/": - line_comment = True - i += 2 - continue - if ch == "/" and nxt == "*": - block_comment = True - i += 2 - continue - - if not in_char and ch == '"': - in_string = not in_string - i += 1 - continue - - if not in_string and ch == "'": - in_char = not in_char - i += 1 - continue - - if not in_string and not in_char: - if ch == "{": - depth += 1 - elif ch == "}": - depth -= 1 - if depth == 0: - return source[:sig_pos] + replacement + source[i + 1:] - - i += 1 - - raise RuntimeError(f"matching closing brace not found: {signature}") - - -def _find_rmt_target() -> Path: - '''Locate the active NeoPixelBus RMT header used by this PIO environment.''' - libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") - - candidates = [] - for path in libdeps_root.rglob("NeoEsp32RmtXMethod.h"): - try: - text = path.read_text(encoding="utf-8") - except Exception: - continue - - if "NeoEsp32RmtMethodBase" in text and "rmt_new_tx_channel" in text: - candidates.append(path) - - # WLED's source-pinned NeoPixelBus package is installed as NeoPixelBus@src-... - # Prefer that copy when PlatformIO also leaves another NeoPixelBus directory. - preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] - - if len(preferred) == 1: - return preferred[0] - if len(candidates) == 1: - return candidates[0] - - detail = "\n".join(f" {p}" for p in candidates) or " " - raise RuntimeError( - "CoreS3 NeoPixelBus patch could not uniquely locate " - "NeoEsp32RmtXMethod.h:\n" + detail - ) - - -def _find_lcd_target(rmt_target: Path) -> Path: - ''' - Locate NeoEsp32LcdXMethod.h from the same active NeoPixelBus package. - - The sibling lookup is intentionally preferred so RMT and LCD patches can - never be applied to different NeoPixelBus copies in .pio/libdeps. - ''' - sibling = rmt_target.with_name("NeoEsp32LcdXMethod.h") - if sibling.is_file(): - try: - text = sibling.read_text(encoding="utf-8") - except Exception as exc: - raise RuntimeError( - f"CoreS3 LCD patch could not read {sibling}: {exc}" - ) from exc - - if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: - return sibling - - libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") - candidates = [] - - for path in libdeps_root.rglob("NeoEsp32LcdXMethod.h"): - try: - text = path.read_text(encoding="utf-8") - except Exception: - continue - - if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: - candidates.append(path) - - preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] - - if len(preferred) == 1: - return preferred[0] - if len(candidates) == 1: - return candidates[0] - - detail = "\n".join(f" {p}" for p in candidates) or " " - raise RuntimeError( - "CoreS3 NeoPixelBus patch could not uniquely locate " - "NeoEsp32LcdXMethod.h:\n" + detail - ) - - -def _patch_rmt(target: Path) -> None: - '''Apply the already validated ESP32-S3 RMT DMA1024 production patch.''' - text = target.read_text(encoding="utf-8") - - if RMT_MARKER in text: - required = [ - "config.mem_block_symbols = 1024;", - "config.flags.with_dma = true;", - "return (_channel != nullptr && _led_encoder != nullptr);", - ] - missing = [item for item in required if item not in text] - if missing: - raise RuntimeError( - "CoreS3 RMT patch marker exists but verification failed; " - f"missing: {missing}" - ) - - print(f"[CoreS3 RMT DMA1024] patch already present: {target.name}") - return - - destructor = f''' ~NeoEsp32RmtMethodBase() - {{ - // {RMT_MARKER} - if (_channel != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT( - rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS) - ); - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_disable(_channel)); - }} - - if (_led_encoder != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); - _led_encoder = nullptr; - }} - - if (_channel != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - }} - - gpio_matrix_out(_pin, 0x100, false, false); - pinMode(_pin, INPUT); - - free(_dataEditing); - free(_dataSending); - }}''' - - ready = f''' bool IsReadyToUpdate() const - {{ - // {RMT_MARKER} - // ESP-IDF 5.5 emits an error log when timeout=0 is used as a busy poll. - // Update() performs the actual completion wait before transmitting. - return (_channel != nullptr && _led_encoder != nullptr); - }}''' - - initialize = f''' void Initialize() - {{ - // {RMT_MARKER} - rmt_tx_channel_config_t config = {{}}; - config.clk_src = RMT_CLK_SRC_DEFAULT; - config.gpio_num = static_cast(_pin); - -#if defined(CONFIG_IDF_TARGET_ESP32S3) - // CoreS3 / ESP32-S3 uses DMA with a 1024-symbol buffer. - // A 32-pixel RGB frame is 768 RMT symbols, so the complete frame - // fits in one DMA buffer and avoids the refill boundary implicated - // in the previously observed tail-pixel anomaly. - config.mem_block_symbols = 1024; -#else - config.mem_block_symbols = 192; -#endif - - config.resolution_hz = T_SPEED::RmtTicksPerSecond; - config.trans_queue_depth = 4; - config.flags.invert_out = T_INVERTED::Inverted; -#if defined(CONFIG_IDF_TARGET_ESP32S3) - config.flags.with_dma = true; -#else - config.flags.with_dma = false; -#endif - - esp_err_t ret = rmt_new_tx_channel(&config, &_channel); - if (ret != ESP_OK || _channel == nullptr) - {{ - _channel = nullptr; - return; - }} - - led_strip_encoder_config_t encoder_config = {{}}; - encoder_config.resolution = T_SPEED::RmtTicksPerSecond; - _tx_config.loop_count = 0; - - ret = rmt_new_led_strip_encoder( - &encoder_config, - &_led_encoder, - T_SPEED::RmtBit0, - T_SPEED::RmtBit1 - ); - - if (ret != ESP_OK || _led_encoder == nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - _led_encoder = nullptr; - return; - }} - - ret = rmt_enable(_channel); - if (ret != ESP_OK) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); - _led_encoder = nullptr; - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - return; - }} - }}''' - - update = f''' void Update(bool maintainBufferConsistency) - {{ - // {RMT_MARKER} - if (_channel == nullptr || _led_encoder == nullptr) - {{ - return; - }} - - // Serialize writes: wait for the previous asynchronous RMT transfer - // to finish before starting the next frame. - if (ESP_OK == ESP_ERROR_CHECK_WITHOUT_ABORT( - rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS))) - {{ - const esp_err_t ret = rmt_transmit( - _channel, - _led_encoder, - _dataEditing, - _sizeData, - &_tx_config - ); - - if (ret != ESP_OK) - {{ - return; - }} - - if (maintainBufferConsistency) - {{ - memcpy(_dataSending, _dataEditing, _sizeData); - }} - - std::swap(_dataSending, _dataEditing); - }} - }}''' - - try: - text = _replace_function( - text, "~NeoEsp32RmtMethodBase()", destructor - ) - text = _replace_function( - text, "bool IsReadyToUpdate() const", ready - ) - text = _replace_function( - text, "void Initialize()", initialize - ) - text = _replace_function( - text, "void Update(bool maintainBufferConsistency)", update - ) - except RuntimeError as exc: - raise RuntimeError( - f"CoreS3 RMT DMA1024 patch failed for {target}: {exc}" - ) from exc - - required = [ - RMT_MARKER, - "config.mem_block_symbols = 1024;", - "config.flags.with_dma = true;", - "return (_channel != nullptr && _led_encoder != nullptr);", - ] - for item in required: - if item not in text: - raise RuntimeError( - f"CoreS3 RMT DMA1024 verification failed: missing {item}" - ) - - target.write_text(text, encoding="utf-8", newline="\n") - print( - "[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / " - f"1024-symbol patch: {target}" - ) - - -def _patch_lcd_gdma(target: Path) -> None: - '''Apply the validated LCD/GDMA full teardown and remove TEST-only markers.''' - text = target.read_text(encoding="utf-8") - original = text - - # Remove the compile-time TEST banner used during hardware diagnosis. - for pragma in LCD_TEST_PRAGMAS: - text = text.replace(pragma + "\n\n", "") - text = text.replace(pragma + "\r\n\r\n", "") - text = text.replace(pragma + "\n", "") - text = text.replace(pragma + "\r\n", "") - text = text.replace(pragma, "") - - if LCD_MARKER not in text: - destruct = f''' void Destruct() - {{ - if (_dmaItems == nullptr) - {{ - return; - }} - - // {LCD_MARKER} - // - // gdma_reset() alone resets the channel state but does not release - // the LCD peripheral ownership held by the GDMA driver. A runtime - // WLED bus rebuild can therefore fail on the next gdma_connect(). - // - // NeoEsp32LcdXMethodBase waits for the active LCD transfer to finish - // before DeregisterMuxBus() reaches this teardown. - if (_dmaChannel != nullptr) - {{ - esp_err_t dmaResult; - - dmaResult = gdma_stop(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA stop during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_reset(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA reset during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_disconnect(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA disconnect during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_del_channel(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA delete during teardown failed: %d", - (int)dmaResult - ); - }} - - _dmaChannel = nullptr; - }} - - periph_module_disable(PERIPH_LCD_CAM_MODULE); - periph_module_reset(PERIPH_LCD_CAM_MODULE); - - heap_caps_free(LcdBuffer); - heap_caps_free(_dmaItems); - - LcdBufferSize = 0; - _dmaItems = nullptr; - LcdBuffer = nullptr; - - MuxMap.Reset(); - }}''' - - try: - text = _replace_function(text, "void Destruct()", destruct) - except RuntimeError as exc: - raise RuntimeError( - f"CoreS3 LCD GDMA teardown patch failed for {target}: {exc}" - ) from exc - - required = [ - LCD_MARKER, - "gdma_stop(_dmaChannel);", - "gdma_reset(_dmaChannel);", - "gdma_disconnect(_dmaChannel);", - "gdma_del_channel(_dmaChannel);", - "_dmaChannel = nullptr;", - ] - for item in required: - if item not in text: - raise RuntimeError( - f"CoreS3 LCD GDMA verification failed: missing {item}" - ) - - for pragma in LCD_TEST_PRAGMAS: - if pragma in text: - raise RuntimeError( - "CoreS3 LCD GDMA production cleanup failed: " - f"TEST pragma still present: {pragma}" - ) - - if text == original: - print(f"[CoreS3 LCD GDMA] patch already present: {target.name}") - return - - target.write_text(text, encoding="utf-8", newline="\n") - print( - "[CoreS3 LCD GDMA] applied full GDMA teardown production patch: " - f"{target}" - ) - - -def _apply_patches() -> None: - rmt_target = _find_rmt_target() - lcd_target = _find_lcd_target(rmt_target) - - _patch_rmt(rmt_target) - _patch_lcd_gdma(lcd_target) - - -if not env.IsIntegrationDump(): - _apply_patches() diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 1caeaad279..1a55d965b4 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -16,7 +16,6 @@ ; This example preserves: ; - the validated CoreS3 Quad PSRAM configuration ; - CoreS3 Power / Display / Audio / AudioReactive integration -; - the validated ESP32-S3 NeoPixelBus RMT DMA1024 and LCD/GDMA patches ; ; IMPORTANT: ; Do NOT set lib_archive=yes in this custom environment. @@ -40,11 +39,8 @@ platform = ${esp32_idf_V5.platform_pioarduino} platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} ; Preserve normal WLED build scripts. -; -; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024 and LCD/GDMA patches. extra_scripts = ${scripts_defaults.extra_scripts} - pre:pio-scripts/cores3_v17_neopixelbus_patch.py custom_usermods = CoreS3_Power diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 741e5f5cba..098643bfbc 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -28,7 +28,6 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
-When the last LCD mux bus is destroyed, the GDMA channel is stopped, reset, disconnected, and deleted so stale LCD peripheral ownership is not carried into the next bus initialization. - -Example when the LCD/GDMA patch is applied: - -```text -[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... -``` - -When already present: - -```text -[CoreS3 LCD GDMA] patch already present: NeoEsp32LcdXMethod.h -``` - -Both patches are idempotent. Their reproducibility has been validated by deleting the NeoPixelBus dependency and rebuilding from a clean dependency state. - --- ## Power Management @@ -459,8 +408,6 @@ The main files involved in CoreS3 support are: ```text WLED/ ├─ platformio_override.ini -├─ pio-scripts/ -│ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ @@ -523,7 +470,7 @@ Please refer to the upstream repository for WLED documentation, supported LED ty ## Licensing WLED source in this repository follows the upstream **EUPL v1.2** license.
-NeoPixelBus remains licensed under **LGPL-3.0-or-later**. The CoreS3 build-time patch script modifies the PlatformIO-downloaded NeoPixelBus source while preserving the upstream library license header. +NeoPixelBus remains licensed under **LGPL-3.0-or-later**. Refer to the repository `LICENSE` file and the respective upstream projects for complete license terms. diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 07e2716df4..d617344ef6 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -28,7 +28,6 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
- AXP2101 Power Key を使った Safe Shutdown - 電源OFF前に LED BLACK frame を送信 - Safe Shutdown のキャンセル時は直前の LED 状態を復元 -- ESP32-S3 / NeoPixelBus 向け RMT DMA1024 および LCD/GDMA runtime-rebuild 安定化 - ブラウザから現在の LCD 画面を BMP で取得 --- @@ -240,7 +239,7 @@ usermods/CoreS3_Display/platformio_override.ini.sample platformio_override.ini ``` -このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、および NeoPixelBus patch 用 pre-script が含まれています。 +このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、および Audio Reactive 定義が含まれています。 ## Build @@ -280,56 +279,6 @@ PlatformIO から `m5stack_cores3` を Upload します。 シリアルモニタなどのプログラムが COM ポートを開いている場合は、Upload 前に閉じてください。 ---- - -## NeoPixelBus / RMT DMA1024 + LCD/GDMA Patch - -ESP32-S3 + NeoPixelBus の RMT 出力では、LED Count より後ろのピクセルが不定期に点灯する問題を実機で確認しました。 - -CoreS3 向けには次の安定化を適用しています。 - -```text -RMT DMA enabled -mem_block_symbols 1024 -``` - -パッチは `.pio/libdeps` のライブラリを手作業で変更する方式ではありません。 - -```text -pio-scripts/cores3_v17_neopixelbus_patch.py -``` - -が PlatformIO の pre-script として動作し、NeoPixelBus を新規取得した場合でも自動的に DMA1024 patch を適用します。 - -Build 時の例: - -```text -[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / 1024-symbol patch: ... -``` - -すでに適用済みの場合: - -```text -[CoreS3 RMT DMA1024] patch already present: NeoEsp32RmtXMethod.h -``` - -同じ pre-script では、runtime の LED Bus 再構築時に使用する LCD/GDMA teardown 修正も適用します。
-最後の LCD mux bus を破棄する際に GDMA channel を stop / reset / disconnect / delete し、次回の Bus 初期化へ古い LCD peripheral ownership が残らないようにします。 - -LCD/GDMA patch 適用時の例: - -```text -[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... -``` - -すでに適用済みの場合: - -```text -[CoreS3 LCD GDMA] patch already present: NeoEsp32LcdXMethod.h -``` - -2つの patch はどちらも idempotent です。NeoPixelBus dependency を削除したクリーンな状態からの再 Build でも再適用性を確認済みです。 - --- ## Power Management @@ -459,8 +408,6 @@ CoreS3 対応の中心は次のファイルです。 ```text WLED/ ├─ platformio_override.ini -├─ pio-scripts/ -│ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ @@ -523,7 +470,7 @@ Please also refer to the upstream repository for WLED documentation, supported L ## Licensing このリポジトリの WLED ソースは、upstream と同じ **EUPL v1.2** に従います。
-NeoPixelBus は **LGPL-3.0-or-later** のままです。CoreS3 の build-time patch script は PlatformIO が取得した NeoPixelBus ソースへ修正を適用しますが、upstream library のライセンスヘッダーは保持します。 +NeoPixelBus は **LGPL-3.0-or-later** のままです。 完全なライセンス条件については、リポジトリの `LICENSE` と各 upstream project を参照してください。 From fe74fc892435826e57eef0cbb28cb55a2db77d6c Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Tue, 22 Sep 2026 18:37:17 +0900 Subject: [PATCH 16/35] Add CoreS3 safe LED settings reboot guard --- usermods/CoreS3_Power/CoreS3_Power.cpp | 168 +++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index a7a1a09c43..9373c03d38 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -12,6 +12,13 @@ * * Production baseline for M5Stack CoreS3 power handling. * + * CoreS3 LED settings save guard: + * - Intercepts POST /settings/leds before WLED registers its generic handler. + * - Defers the request so BLACK is rendered from the normal WLED loop context. + * - Keeps the old bus suspended after BLACK while WLED parses the new settings. + * - Lets the standard WLED bus rebuild/config serialization path run unchanged. + * - Arms a standard WLED software reboot from the following loop iteration. + * * Preserved validated power behavior: * - AW9523B external-5V enable writes (BOOST then BUS). * - Runtime External 5V re-assert after M5GFX initialization. @@ -160,6 +167,23 @@ class CoreS3PowerUsermod : public Usermod uint8_t dcdcModeBefore = 0; uint8_t dcdcModeAfter = 0; + // LED & Hardware settings save guard. + // + // The HTTP callback only changes this small state machine and delegates back + // to WLED. Physical LED I/O is kept in the normal WLED loop context. + enum class LedSettingsSaveState : uint8_t { + IDLE = 0, + BLACK_PENDING, + BLACK_READY, + REBOOT_ARM_PENDING, + WAIT_REBOOT, + RESTORE_PENDING + }; + + volatile LedSettingsSaveState ledSettingsSaveState = LedSettingsSaveState::IDLE; + uint8_t ledSettingsSavedStripBrightness = 0; + bool ledSettingsHandlerRegistered = false; + const char* resetReasonText(esp_reset_reason_t reason) { @@ -538,6 +562,147 @@ class CoreS3PowerUsermod : public Usermod } } + // Process the LED settings save guard only from WLED's normal loop context. + // This keeps physical strip I/O out of the AsyncWebServer callback context. + void serviceLedSettingsSaveGuard() + { + switch (ledSettingsSaveState) { + case LedSettingsSaveState::IDLE: + case LedSettingsSaveState::BLACK_READY: + case LedSettingsSaveState::WAIT_REBOOT: + return; + + case LedSettingsSaveState::BLACK_PENDING: + { + // Physical shutdown already owns the LED output. If this ever overlaps + // with a settings save, keep the request deferred until that sequence + // has finished or the device powers off. + if (safeShutdownBlankActive) return; + + ledSettingsSavedStripBrightness = strip.getBrightness(); + + Serial.printf( + "[CoreS3_Power][LED] Settings save: BLACK old bus before re-init (len=%u, bri=%u)\n", + strip.getLengthPhysical(), + ledSettingsSavedStripBrightness + ); + + // Use the same physical BLACK path already validated by Safe Shutdown. + // Keep WLED's logical bri untouched; only the physical strip brightness + // is forced to zero for this final old-bus frame. + strip.waitForIt(); + strip.setBrightness(0, true); + strip.show(); + waitForLedOutputComplete(); + strip.suspend(); + strip.waitForIt(); + + ledSettingsSaveState = LedSettingsSaveState::BLACK_READY; + Serial.println(F("[CoreS3_Power][LED] Settings save: old bus BLACK and suspended")); + return; + } + + case LedSettingsSaveState::REBOOT_ARM_PENDING: + // Do not set doReboot in the AsyncWebServer callback. Arming it here + // guarantees WLED's main loop gets a chance to consume doInitBusses and + // serialize the new bus configuration before the standard reset check. + doReboot = true; + ledSettingsSaveState = LedSettingsSaveState::WAIT_REBOOT; + Serial.println(F("[CoreS3_Power][LED] Bus re-init detected; safe reboot armed for post-save reset")); + return; + + case LedSettingsSaveState::RESTORE_PENDING: + // A valid LED settings POST normally creates pending bus configs. If it + // did not, no re-init/reboot is needed, so restore the old physical + // output that was blanked while the request was deferred. + strip.resume(); + strip.setBrightness(ledSettingsSavedStripBrightness, true); + strip.show(); + waitForLedOutputComplete(); + strip.trigger(); + + ledSettingsSaveState = LedSettingsSaveState::IDLE; + Serial.println(F("[CoreS3_Power][LED] Settings save: no bus re-init; old output restored")); + return; + } + } + + // Local copy of WLED's settings-origin check. The upstream helpers live as + // static functions in wled_server.cpp, so a separately compiled usermod + // cannot call them directly. Keep this scoped to the LED settings guard. + bool isLocalLedSettingsClient(const IPAddress& client) + { + auto inSubnetLocal = [](const IPAddress& ip, const IPAddress& subnet, const IPAddress& mask) { + return (((uint32_t)ip & (uint32_t)mask) == ((uint32_t)subnet & (uint32_t)mask)); + }; + + return + inSubnetLocal(client, IPAddress(10, 0, 0, 0), IPAddress(255, 0, 0, 0)) || + inSubnetLocal(client, IPAddress(192, 168, 0, 0), IPAddress(255, 255, 0, 0)) || + inSubnetLocal(client, IPAddress(172, 16, 0, 0), IPAddress(255, 240, 0, 0)) || + (inSubnetLocal(client, IPAddress(4, 3, 2, 0), IPAddress(255, 255, 255, 0)) && apActive) || + inSubnetLocal(client, WLEDNetwork.localIP(), WLEDNetwork.subnetMask()); + } + + // Register before WLED::initServer(). UsermodManager::setup() runs earlier in + // WLED::setup(), so this exact /settings/leds POST handler gets first chance + // to safely blank the old CoreS3 LED bus. WLED's own serveSettings() remains + // the single parser/validator for the actual settings request. + void registerLedSettingsSaveHandler() + { + if (ledSettingsHandlerRegistered) return; + ledSettingsHandlerRegistered = true; + + server.on(F("/settings/leds"), HTTP_POST, [this](AsyncWebServerRequest* request) { + // Preserve WLED's normal access-control/PIN behavior. Do not blank LEDs + // for a request that cannot yet be treated as a real LED settings save. + if ( + !isLocalLedSettingsClient(request->client()->remoteIP()) || + (!correctPIN && strlen(settingsPIN) > 0) + ) { + serveSettings(request, true); + return; + } + + switch (ledSettingsSaveState) { + case LedSettingsSaveState::IDLE: + ledSettingsSaveState = LedSettingsSaveState::BLACK_PENDING; + Serial.println(F("[CoreS3_Power][LED] LED settings POST detected; deferring for old-bus BLACK")); + request->deferResponse(); + return; + + case LedSettingsSaveState::BLACK_PENDING: + request->deferResponse(); + return; + + case LedSettingsSaveState::BLACK_READY: + // The old physical bus is now BLACK and suspended. Delegate parsing + // and validation to the unchanged WLED settings implementation. + serveSettings(request, true); + + if (doInitBusses) { + // Arm reboot from Usermod::loop(), never from this callback. This + // avoids racing WLED's doInitBusses/configNeedsWrite main-loop path. + ledSettingsSaveState = LedSettingsSaveState::REBOOT_ARM_PENDING; + Serial.println(F("[CoreS3_Power][LED] LED settings accepted; standard bus re-init pending")); + } else { + ledSettingsSaveState = LedSettingsSaveState::RESTORE_PENDING; + } + return; + + case LedSettingsSaveState::REBOOT_ARM_PENDING: + case LedSettingsSaveState::WAIT_REBOOT: + case LedSettingsSaveState::RESTORE_PENDING: + // A previous save is still being completed. Keep any duplicate POST + // deferred rather than allowing overlapping bus lifecycle operations. + request->deferResponse(); + return; + } + }); + + Serial.println(F("[CoreS3_Power][LED] Settings save guard: ARMED")); + } + void beginSafeShutdownBlank(uint8_t triggerStatus) { if (safeShutdownBlankActive) return; @@ -739,6 +904,8 @@ class CoreS3PowerUsermod : public Usermod Serial.printf("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); Serial.printf("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); + registerLedSettingsSaveHandler(); + coreS3PowerInitializationCompleteState = true; Serial.println(F("[CoreS3_Power] Initialization complete")); @@ -747,6 +914,7 @@ class CoreS3PowerUsermod : public Usermod void loop() override { + serviceLedSettingsSaveGuard(); servicePhysicalPowerKey(); serviceRuntimeExternal5VEnable(); serviceDcdc3StabilityMode(); From 7680ed0668c4fcc81f8134086ff71aaf7ccf40ac Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Tue, 22 Sep 2026 21:51:38 +0900 Subject: [PATCH 17/35] Document CoreS3 LED settings save limitation --- usermods/CoreS3_Display/readme.md | 2 ++ usermods/CoreS3_Display/readme_jp.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 098643bfbc..ceae73dde0 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -452,6 +452,8 @@ Adds ES7210 support for the CoreS3 built-in microphone through the standard WLED - The standard WLED Brightness default of 128 is not modified. - A starting Brightness around 64 is recommended for CoreS3. - Browser Screenshot returns a still BMP image; it is not a live stream. +- On M5Stack CoreS3, saving changes to the LED bus configuration under **LED & Hardware** may temporarily report a GDMA/LCD peripheral conflict during runtime bus reinitialization.
+ The CoreS3 Power usermod blanks the existing LED output, lets WLED save the new configuration, and then performs a software reboot. The saved configuration is applied normally after reboot; no additional user action is required. - DCDC OVP protection is not disabled. --- diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index d617344ef6..c85ff2d9d4 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -452,6 +452,8 @@ CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S - WLED 標準 Brightness 128 は変更していません。 - CoreS3 では Brightness 64 前後からの使用を推奨します。 - Browser Screenshot は BMP の静止画です。ライブストリームではありません。 +- M5Stack CoreS3 では、**LED & Hardware** で LED バス構成を変更して保存すると、ランタイムでの LED バス再初期化中に GDMA/LCD peripheral conflict のログが一時的に出力される場合があります。
+ CoreS3 Power Usermod は旧 LED 出力を消灯して WLED に新しい構成を保存させた後、Software Reboot を実行します。再起動後は保存した構成が正常に適用され、追加操作は不要です。 - DCDC OVP 保護は無効化していません。 --- From e80bb59b0bccf459091e135ec0be3f8e93a31d1f Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Tue, 22 Sep 2026 23:42:27 +0900 Subject: [PATCH 18/35] Use WLED debug macros for CoreS3 diagnostics --- usermods/CoreS3_Display/CoreS3_Display.cpp | 92 +++++++------- .../M5StackDisplayHardwareBackend.cpp | 20 ++-- usermods/CoreS3_Power/CoreS3_Power.cpp | 112 +++++++++--------- 3 files changed, 112 insertions(+), 112 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index e390ea5d6b..322ace6532 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -957,7 +957,7 @@ class CoreS3DisplayUsermod : public Usermod { presetCacheBuildSourceModifiedTime = presetsModifiedTime; - Serial.printf( "[CoreS3_Display] " "Preset cache rebuild start " "(modified=%lu)\n", presetCacheBuildSourceModifiedTime ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset cache rebuild start " "(modified=%lu)\n", presetCacheBuildSourceModifiedTime ); } // ========================================================= @@ -977,10 +977,10 @@ class CoreS3DisplayUsermod : public Usermod { syncPresetNavigationCursorFromCurrentPreset(); - Serial.printf( "[CoreS3_Display] " "Preset cache ready: %u preset(s)\n", (unsigned)presetCacheCount ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset cache ready: %u preset(s)\n", (unsigned)presetCacheCount ); if ( presetsModifiedTime != presetCacheSourceModifiedTime ) { - Serial.println( F( "[CoreS3_Display] " "Preset changed during cache build. Rebuilding." ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset changed during cache build. Rebuilding." ) ); startPresetCacheRebuild(); @@ -1372,7 +1372,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Preset save request rejected" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset save request rejected" ) ); return false; } @@ -1386,7 +1386,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Preset save could not be queued" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset save could not be queued" ) ); return false; } @@ -1399,7 +1399,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.printf( "[CoreS3_Display] " "Preset save request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset save request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); return true; } @@ -1418,7 +1418,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Preset overwrite request rejected" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset overwrite request rejected" ) ); return false; } @@ -1432,7 +1432,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Preset overwrite could not be queued" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset overwrite could not be queued" ) ); return false; } @@ -1445,7 +1445,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetSaveOperationStatus(); - Serial.printf( "[CoreS3_Display] " "Preset overwrite request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset overwrite request: %u (%s)\n", presetSaveCandidateId, presetSaveCandidateName.c_str() ); return true; } @@ -1458,7 +1458,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetDeleteOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Preset delete request rejected" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset delete request rejected" ) ); return false; } @@ -1480,7 +1480,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetDeleteOperationStatus(); - Serial.printf( "[CoreS3_Display] " "Preset delete request: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset delete request: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); return true; } @@ -1502,7 +1502,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetBootOperationStatus(); - Serial.println( F( "[CoreS3_Display] " "Boot Preset request rejected" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Boot Preset request rejected" ) ); return false; } @@ -1517,10 +1517,10 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetBootOperationStatus(); if ( presetBootTargetId == 0 ) { - Serial.println( F( "[CoreS3_Display] " "Boot Preset clear request" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Boot Preset clear request" ) ); } else { - Serial.printf( "[CoreS3_Display] " "Boot Preset request: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Boot Preset request: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); } return true; @@ -1573,12 +1573,12 @@ class CoreS3DisplayUsermod : public Usermod { // position without necessarily changing WLED's active Preset. lastObservedCurrentPreset = currentPreset; - Serial.printf( "[CoreS3_Display] " "%s verified: %u (%s)\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId, presetSaveCandidateName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "%s verified: %u (%s)\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId, presetSaveCandidateName.c_str() ); } else { presetSaveOperationState = PRESET_SAVE_OP_FAILED; - Serial.printf( "[CoreS3_Display] " "%s verification failed: %u\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId ); + DEBUG_PRINTF( "[CoreS3_Display] " "%s verification failed: %u\n", presetSaveOperationIsOverwrite ? "Preset overwrite" : "Preset save", presetSaveCandidateId ); } presetSaveResultStartMs = now; @@ -1667,17 +1667,17 @@ class CoreS3DisplayUsermod : public Usermod { configNeedsWrite = true; lastBootPresetValue = -1; - Serial.println( F( "[CoreS3_Display] " "Deleted Boot Preset cleared" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Deleted Boot Preset cleared" ) ); } presetDeleteOperationState = PRESET_DELETE_OP_SUCCESS; - Serial.printf( "[CoreS3_Display] " "Preset delete verified: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset delete verified: %u (%s)\n", presetDeleteTargetId, presetDeleteTargetName.c_str() ); } else { presetDeleteOperationState = PRESET_DELETE_OP_FAILED; - Serial.printf( "[CoreS3_Display] " "Preset delete verification failed: %u\n", presetDeleteTargetId ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset delete verification failed: %u\n", presetDeleteTargetId ); } presetDeleteResultStartMs = now; @@ -1745,16 +1745,16 @@ class CoreS3DisplayUsermod : public Usermod { presetBootOperationState = PRESET_BOOT_OP_SUCCESS; if ( presetBootTargetId == 0 ) { - Serial.println( F( "[CoreS3_Display] " "Boot Preset clear verified" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Boot Preset clear verified" ) ); } else { - Serial.printf( "[CoreS3_Display] " "Boot Preset verified: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Boot Preset verified: %u (%s)\n", presetBootTargetId, presetBootTargetName.c_str() ); } } else { presetBootOperationState = PRESET_BOOT_OP_FAILED; - Serial.printf( "[CoreS3_Display] " "Boot Preset verification failed: target=%u actual=%u\n", presetBootTargetId, bootPreset ); + DEBUG_PRINTF( "[CoreS3_Display] " "Boot Preset verification failed: target=%u actual=%u\n", presetBootTargetId, bootPreset ); } presetBootResultStartMs = now; @@ -2066,7 +2066,7 @@ class CoreS3DisplayUsermod : public Usermod { resetTouchGesture(); drawRuntimeHealthWarning( nextWarning ); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Runtime health warning: %u\n", (unsigned)nextWarning ); @@ -2092,7 +2092,7 @@ class CoreS3DisplayUsermod : public Usermod { display.drawString( "WLED M5Stack CoreS3", screenWidth / 2, 68 ); - Serial.println( F( "[CoreS3_Display] " "WARNING: startup PNG draw failed" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "WARNING: startup PNG draw failed" ) ); } } @@ -2283,7 +2283,7 @@ class CoreS3DisplayUsermod : public Usermod { displayPowerState = DISPLAY_POWER_ACTIVE; - Serial.println( F( "[CoreS3_Display] " "Startup animation complete" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Startup animation complete" ) ); } return; @@ -2447,7 +2447,7 @@ class CoreS3DisplayUsermod : public Usermod { setDisplayBrightness( 0 ); - Serial.println( F( "[CoreS3_Display] " "Display sleeping" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Display sleeping" ) ); } return true; @@ -2500,7 +2500,7 @@ class CoreS3DisplayUsermod : public Usermod { resetTouchGesture(); - Serial.println( F( "[CoreS3_Display] " "Display active" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Display active" ) ); } return true; @@ -6303,7 +6303,7 @@ class CoreS3DisplayUsermod : public Usermod { drawPresetNavigation( M5STACK_TOUCH_TARGET_NONE ); } - Serial.println( F( "[CoreS3_Display] " "Preset cache not ready" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "Preset cache not ready" ) ); return false; } @@ -6357,7 +6357,7 @@ class CoreS3DisplayUsermod : public Usermod { lastPresetValue = newPreset; - Serial.printf( "[CoreS3_Display] " "Preset cache request: %u (%s)\n", newPreset, presetName.c_str() ); + DEBUG_PRINTF( "[CoreS3_Display] " "Preset cache request: %u (%s)\n", newPreset, presetName.c_str() ); return true; } @@ -6971,7 +6971,7 @@ class CoreS3DisplayUsermod : public Usermod { if ( !buildScreenshotBmp( bmpData, bmpSize ) ) { screenshotCaptureInProgress = false; - Serial.println( + DEBUG_PRINTLN( F( "[CoreS3_Display] Screenshot: capture/buffer allocation failed" ) ); @@ -7052,7 +7052,7 @@ class CoreS3DisplayUsermod : public Usermod { request->send( response ); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Screenshot: %d x %d BMP (%u bytes)\n", screenWidth, screenHeight, @@ -7069,7 +7069,7 @@ class CoreS3DisplayUsermod : public Usermod { } ); - Serial.println( + DEBUG_PRINTLN( F( "[CoreS3_Display] Screenshot endpoint: /cores3/screenshot.bmp" ) ); } @@ -7096,7 +7096,7 @@ class CoreS3DisplayUsermod : public Usermod { JsonObject top = root[ FPSTR( CORES3_DISPLAY_CONFIG_NAME ) ]; if ( top.isNull() ) { - Serial.println( F( "[CoreS3_Display] " "No display config found. Using defaults." ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "No display config found. Using defaults." ) ); return false; } @@ -7134,7 +7134,7 @@ class CoreS3DisplayUsermod : public Usermod { fadeDurationMs = (uint16_t)newFadeDuration; if ( initDone ) { - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] " "Config updated: " "Sleep=%u sec, " @@ -7182,36 +7182,36 @@ class CoreS3DisplayUsermod : public Usermod { } void setup() override { - Serial.println(); + DEBUG_PRINTLN(""); - Serial.println( F( "[CoreS3_Display][BUILD] CoreS3 Display v0.1.0" ) ); - Serial.println( F( "[CoreS3_Display] Initialization start" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display][BUILD] CoreS3 Display v0.1.0" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] Initialization start" ) ); registerScreenshotEndpoint(); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] " "Hardware: %s, Revision=%s, Runtime=%s\n", getHardwareProfileName(), getHardwareRevisionName(), getHardwareRuntimeModeName() ); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] " "Port status: %s\n", getHardwarePortStatusName() ); - Serial.printf( "[CoreS3_Display] " "Settings: " "Sleep=%u sec, " "LCD=%u, " "Fade=%s, " "FadeDuration=%u ms\n", sleepTimeoutSec, lcdBrightness, fadeEnabled ? "ON" : "OFF", fadeDurationMs ); + DEBUG_PRINTF( "[CoreS3_Display] " "Settings: " "Sleep=%u sec, " "LCD=%u, " "Fade=%s, " "FadeDuration=%u ms\n", sleepTimeoutSec, lcdBrightness, fadeEnabled ? "ON" : "OFF", fadeDurationMs ); runHardwareDiagnostics(); if ( isCore2DiagnosticOnlyMode() ) { initDone = true; - Serial.println( F( "[CoreS3_Display] Core2 diagnostic-only runtime complete" ) ); - Serial.println( F( "[CoreS3_Display] Display/Touch/LCD brightness initialization intentionally skipped" ) ); - Serial.println( F( "[CoreS3_Display] Capture the hardware probe result before enabling Core2 UI/Power support" ) ); - Serial.println(); + DEBUG_PRINTLN( F( "[CoreS3_Display] Core2 diagnostic-only runtime complete" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] Display/Touch/LCD brightness initialization intentionally skipped" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] Capture the hardware probe result before enabling Core2 UI/Power support" ) ); + DEBUG_PRINTLN(""); return; } @@ -7258,9 +7258,9 @@ class CoreS3DisplayUsermod : public Usermod { initDone = true; - Serial.println( F( "[CoreS3_Display] Initialization complete" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] Initialization complete" ) ); - Serial.println(); + DEBUG_PRINTLN(""); } void loop() override { diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 606a188b1b..48e4324e00 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -238,7 +238,7 @@ void M5StackDisplayHardwareBackend::classifyCore2HardwareProbe( TwoWire& wire ) } void M5StackDisplayHardwareBackend::printCore2HardwareProbeResult() { - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Hardware probe: %s, Variant=%s, PMU=%s, IMU=%s", probeStateName(), detectedVariantName(), @@ -247,17 +247,17 @@ void M5StackDisplayHardwareBackend::printCore2HardwareProbeResult() { ); if ( hardwareProbe.imuChipId > 0 ) { - Serial.printf( " (ID=0x%02X)", hardwareProbe.imuChipId ); + DEBUG_PRINTF( " (ID=0x%02X)", hardwareProbe.imuChipId ); } - Serial.println(); + DEBUG_PRINTLN(""); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Detected revision: %s\n", detectedRevisionName() ); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Core2 I2C signature: " "34=%s 35=%s 38=%s 40=%s 51=%s 68=%s\n", hardwareProbe.address34 ? "YES" : "NO", @@ -450,7 +450,7 @@ void M5StackDisplayHardwareBackend::runDiagnostics() { if ( !probeWire.begin( CORE2_INTERNAL_I2C_SDA, CORE2_INTERNAL_I2C_SCL, CORE2_INTERNAL_I2C_FREQUENCY ) ) { hardwareProbe.state = M5STACK_HARDWARE_PROBE_FAILED; - Serial.println( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C start failed" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C start failed" ) ); return; } @@ -475,7 +475,7 @@ void M5StackDisplayHardwareBackend::runDiagnostics() { bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int16_t& screenHeight, bool& touchReady ) { if ( !isDisplayRuntimeEnabled() ) { - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Display] Hardware profile not enabled for Display runtime: %s (%s)\n", profileName(), revisionName() @@ -491,17 +491,17 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int screenWidth = display.width(); screenHeight = display.height(); - Serial.printf( "[CoreS3_Display] " "Display size: %d x %d\n", screenWidth, screenHeight ); + DEBUG_PRINTF( "[CoreS3_Display] " "Display size: %d x %d\n", screenWidth, screenHeight ); if ( screenWidth <= 0 || screenHeight <= 0 ) { - Serial.println( F( "[CoreS3_Display] " "ERROR: Display not detected" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] " "ERROR: Display not detected" ) ); return false; } touchReady = ( display.touch() != nullptr ); - Serial.printf( "[CoreS3_Display] " "Touch: %s\n", touchReady ? "READY" : "NOT FOUND" ); + DEBUG_PRINTF( "[CoreS3_Display] " "Touch: %s\n", touchReady ? "READY" : "NOT FOUND" ); return true; } diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index 9373c03d38..e06e01413d 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -205,29 +205,29 @@ class CoreS3PowerUsermod : public Usermod void printPowerOffSource(uint8_t status) { - Serial.printf("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS=0x%02X\n", status); + DEBUG_PRINTF("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS=0x%02X\n", status); if (status == 0) { - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: NONE LATCHED / UNKNOWN")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: NONE LATCHED / UNKNOWN")); return; } if (status & AXP2101_PWROFF_OVER_TEMP_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PMIC DIE OVER TEMPERATURE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: PMIC DIE OVER TEMPERATURE")); if (status & AXP2101_PWROFF_DCDC_OV_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: DCDC OVER VOLTAGE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: DCDC OVER VOLTAGE")); if (status & AXP2101_PWROFF_DCDC_UV_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: DCDC UNDER VOLTAGE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: DCDC UNDER VOLTAGE")); if (status & AXP2101_PWROFF_VBUS_OV_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: VBUS OVER VOLTAGE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: VBUS OVER VOLTAGE")); if (status & AXP2101_PWROFF_VSYS_UV_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: VSYS UNDER VOLTAGE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: VSYS UNDER VOLTAGE")); if (status & AXP2101_PWROFF_PWRON_LOW_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PWRON HELD LOW / EN MODE")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: PWRON HELD LOW / EN MODE")); if (status & AXP2101_PWROFF_SOFTWARE_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: SOFTWARE POWER OFF")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: SOFTWARE POWER OFF")); if (status & AXP2101_PWROFF_PWRON_PULLDOWN_MASK) - Serial.println(F("[CoreS3_Power][BOOT] Power-off cause: PWRON / POWER KEY PULL-DOWN")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] Power-off cause: PWRON / POWER KEY PULL-DOWN")); } bool probeI2C(uint8_t address) @@ -292,17 +292,17 @@ class CoreS3PowerUsermod : public Usermod readRegister(AXP2101_ADDR, AXP2101_REG_PWROFF_STATUS, bootPowerOffStatus); if (bootPowerOnStatusValid) { - Serial.printf("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS=0x%02X\n", bootPowerOnStatus); + DEBUG_PRINTF("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS=0x%02X\n", bootPowerOnStatus); } else { - Serial.println(F("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS read FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] AXP2101 PWRON_STATUS read FAILED")); } if (bootPowerOffStatusValid) { printPowerOffSource(bootPowerOffStatus); } else { - Serial.println(F("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS read FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power][BOOT] AXP2101 PWROFF_STATUS read FAILED")); } } @@ -397,7 +397,7 @@ class CoreS3PowerUsermod : public Usermod runtimeExternal5VEnableSuccess = applyRuntimeExternal5VEnable(); coreS3PowerExternal5VReadyState = runtimeExternal5VEnableSuccess; - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power][EXT5V] Runtime External 5V enable: %s BUS_EN=%s BOOST_EN=%s P0=0x%02X P1=0x%02X\n", runtimeExternal5VEnableSuccess ? "APPLIED" : "FAILED", busEnabled ? "ON" : "OFF", @@ -413,18 +413,18 @@ class CoreS3PowerUsermod : public Usermod uint8_t chipId = 0; if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_CHIP_ID, chipId)) { - Serial.println(F("[CoreS3_Power] Runtime power key: M5GFX I2C1 AXP2101 read FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key: M5GFX I2C1 AXP2101 read FAILED")); return false; } if (chipId != 0x4A) { - Serial.printf("[CoreS3_Power] Runtime power key: unexpected AXP2101 chip ID 0x%02X\n", chipId); + DEBUG_PRINTF("[CoreS3_Power] Runtime power key: unexpected AXP2101 chip ID 0x%02X\n", chipId); return false; } if (!runtimePowerKeyBusReadyLogged) { runtimePowerKeyBusReadyLogged = true; - Serial.printf("[CoreS3_Power] Runtime I2C: M5GFX I2C_NUM_1 AXP2101 READY (ID=0x%02X)\n", chipId); + DEBUG_PRINTF("[CoreS3_Power] Runtime I2C: M5GFX I2C_NUM_1 AXP2101 READY (ID=0x%02X)\n", chipId); } if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_ENABLE_1, axpIrqEnableBefore)) return false; @@ -440,7 +440,7 @@ class CoreS3PowerUsermod : public Usermod uint8_t stalePowerKeyFlags = staleStatus & AXP2101_PKEY_EVENT_MASK; if (stalePowerKeyFlags != 0) { - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power][BOOT] Stale PKEY IRQ before clear: 0x%02X%s%s%s%s\n", stalePowerKeyFlags, (stalePowerKeyFlags & AXP2101_PKEY_POSITIVE_MASK) ? " RELEASE" : "", @@ -457,8 +457,8 @@ class CoreS3PowerUsermod : public Usermod powerKeyPressedAt = 0; lastPowerKeyPoll = millis(); - Serial.printf("[CoreS3_Power] Runtime PKEY IRQEN1: 0x%02X -> 0x%02X\n", axpIrqEnableBefore, axpIrqEnableAfter); - Serial.println(F("[CoreS3_Power] Runtime power key monitor: ARMED on M5GFX I2C1")); + DEBUG_PRINTF("[CoreS3_Power] Runtime PKEY IRQEN1: 0x%02X -> 0x%02X\n", axpIrqEnableBefore, axpIrqEnableAfter); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key monitor: ARMED on M5GFX I2C1")); return true; } @@ -477,7 +477,7 @@ class CoreS3PowerUsermod : public Usermod dcdc3StabilityAttempted = true; if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, dcdcModeBefore)) { - Serial.println(F("[CoreS3_Power][DCDC3] REG81 read FAILED - mode unchanged")); + DEBUG_PRINTLN(F("[CoreS3_Power][DCDC3] REG81 read FAILED - mode unchanged")); return; } @@ -486,19 +486,19 @@ class CoreS3PowerUsermod : public Usermod const uint8_t target = dcdcModeBefore | AXP2101_DCDC3_ALWAYS_PWM_MASK; if (!writeRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, target)) { - Serial.println(F("[CoreS3_Power][DCDC3] REG81 write FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power][DCDC3] REG81 write FAILED")); return; } if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_DCDC_FORCE_PWM, dcdcModeAfter)) { - Serial.println(F("[CoreS3_Power][DCDC3] REG81 verify read FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power][DCDC3] REG81 verify read FAILED")); return; } dcdc3StabilityApplied = (dcdcModeAfter & AXP2101_DCDC3_ALWAYS_PWM_MASK) != 0; - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power][DCDC3] REG81 0x%02X -> 0x%02X result=%s DCDC1=%s DCDC3=%s OVP_PROTECTION=UNCHANGED\n", dcdcModeBefore, dcdcModeAfter, @@ -529,7 +529,7 @@ class CoreS3PowerUsermod : public Usermod coreS3PowerExternal5VReadyState = false; if (!runtimeExternal5VViolationLogged) { runtimeExternal5VViolationLogged = true; - Serial.println(F("[CoreS3_Power][EXT5V] WARNING: runtime state read failed")); + DEBUG_PRINTLN(F("[CoreS3_Power][EXT5V] WARNING: runtime state read failed")); } return; } @@ -541,7 +541,7 @@ class CoreS3PowerUsermod : public Usermod if (!healthy && !runtimeExternal5VViolationLogged) { runtimeExternal5VViolationLogged = true; - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power][EXT5V] WARNING: runtime path changed BUS_EN=%s BOOST_EN=%s P0=0x%02X P1=0x%02X\n", busEnabled ? "ON" : "OFF", boostEnabled ? "ON" : "OFF", @@ -581,7 +581,7 @@ class CoreS3PowerUsermod : public Usermod ledSettingsSavedStripBrightness = strip.getBrightness(); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power][LED] Settings save: BLACK old bus before re-init (len=%u, bri=%u)\n", strip.getLengthPhysical(), ledSettingsSavedStripBrightness @@ -598,7 +598,7 @@ class CoreS3PowerUsermod : public Usermod strip.waitForIt(); ledSettingsSaveState = LedSettingsSaveState::BLACK_READY; - Serial.println(F("[CoreS3_Power][LED] Settings save: old bus BLACK and suspended")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: old bus BLACK and suspended")); return; } @@ -608,7 +608,7 @@ class CoreS3PowerUsermod : public Usermod // serialize the new bus configuration before the standard reset check. doReboot = true; ledSettingsSaveState = LedSettingsSaveState::WAIT_REBOOT; - Serial.println(F("[CoreS3_Power][LED] Bus re-init detected; safe reboot armed for post-save reset")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] Bus re-init detected; safe reboot armed for post-save reset")); return; case LedSettingsSaveState::RESTORE_PENDING: @@ -622,7 +622,7 @@ class CoreS3PowerUsermod : public Usermod strip.trigger(); ledSettingsSaveState = LedSettingsSaveState::IDLE; - Serial.println(F("[CoreS3_Power][LED] Settings save: no bus re-init; old output restored")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: no bus re-init; old output restored")); return; } } @@ -667,7 +667,7 @@ class CoreS3PowerUsermod : public Usermod switch (ledSettingsSaveState) { case LedSettingsSaveState::IDLE: ledSettingsSaveState = LedSettingsSaveState::BLACK_PENDING; - Serial.println(F("[CoreS3_Power][LED] LED settings POST detected; deferring for old-bus BLACK")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] LED settings POST detected; deferring for old-bus BLACK")); request->deferResponse(); return; @@ -684,7 +684,7 @@ class CoreS3PowerUsermod : public Usermod // Arm reboot from Usermod::loop(), never from this callback. This // avoids racing WLED's doInitBusses/configNeedsWrite main-loop path. ledSettingsSaveState = LedSettingsSaveState::REBOOT_ARM_PENDING; - Serial.println(F("[CoreS3_Power][LED] LED settings accepted; standard bus re-init pending")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] LED settings accepted; standard bus re-init pending")); } else { ledSettingsSaveState = LedSettingsSaveState::RESTORE_PENDING; } @@ -700,7 +700,7 @@ class CoreS3PowerUsermod : public Usermod } }); - Serial.println(F("[CoreS3_Power][LED] Settings save guard: ARMED")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save guard: ARMED")); } void beginSafeShutdownBlank(uint8_t triggerStatus) @@ -712,14 +712,14 @@ class CoreS3PowerUsermod : public Usermod lastSafeShutdownTriggerStatus = triggerStatus; savedLogicalBrightness = bri; - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power] Safe shutdown trigger: IRQ=0x%02X%s%s\n", triggerStatus, (triggerStatus & AXP2101_PKEY_LONG_MASK) ? " LONG" : "", (triggerStatus & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "" ); - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power] Safe shutdown: WLED bri=%u, strip=%u -> physical 0\n", savedLogicalBrightness, strip.getBrightness() @@ -735,7 +735,7 @@ class CoreS3PowerUsermod : public Usermod safeShutdownBlankActive = true; lastShutdownBlackRefresh = millis(); - Serial.println(F("[CoreS3_Power] Safe shutdown: BLACK frame sent and strip suspended")); + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: BLACK frame sent and strip suspended")); } void maintainSafeShutdownBlank(unsigned long now) @@ -753,7 +753,7 @@ class CoreS3PowerUsermod : public Usermod { if (!safeShutdownBlankActive) return; - Serial.println(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); strip.resume(); uint8_t restoreBrightness = bri; @@ -769,7 +769,7 @@ class CoreS3PowerUsermod : public Usermod safeShutdownLastCanceled = true; strip.trigger(); - Serial.printf("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); + DEBUG_PRINTF("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); } void servicePhysicalPowerKey() @@ -800,7 +800,7 @@ class CoreS3PowerUsermod : public Usermod if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, status)) { if (now - lastRuntimeI2CFailureLog >= 1000) { lastRuntimeI2CFailureLog = now; - Serial.println(F("[CoreS3_Power] Runtime PKEY status read FAILED on M5GFX I2C1")); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime PKEY status read FAILED on M5GFX I2C1")); } maintainSafeShutdownBlank(now); return; @@ -811,7 +811,7 @@ class CoreS3PowerUsermod : public Usermod if (powerKeyStatus != 0) { lastPowerKeyStatus = powerKeyStatus; - Serial.printf( + DEBUG_PRINTF( "[CoreS3_Power] PKEY IRQ: 0x%02X%s%s%s%s\n", powerKeyStatus, (powerKeyStatus & AXP2101_PKEY_POSITIVE_MASK) ? " RELEASE" : "", @@ -840,7 +840,7 @@ class CoreS3PowerUsermod : public Usermod powerKeyPressedAt > 0 && now - powerKeyPressedAt >= SAFE_SHUTDOWN_FALLBACK_HOLD_MS ) { - Serial.println(F("[CoreS3_Power] Safe shutdown: PRESS timer fallback")); + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: PRESS timer fallback")); beginSafeShutdownBlank(AXP2101_PKEY_NEGATIVE_MASK); } @@ -863,18 +863,18 @@ class CoreS3PowerUsermod : public Usermod bootResetReason = esp_reset_reason(); - Serial.println(); - Serial.println(F("[CoreS3_Power][BUILD] CoreS3 Power v0.1.0")); - Serial.println(F("[CoreS3_Power] Initialization start")); - Serial.printf("[CoreS3_Power] I2C SDA=%d SCL=%d\n", i2c_sda, i2c_scl); - Serial.printf( + DEBUG_PRINTLN(""); + DEBUG_PRINTLN(F("[CoreS3_Power][BUILD] CoreS3 Power v0.1.0")); + DEBUG_PRINTLN(F("[CoreS3_Power] Initialization start")); + DEBUG_PRINTF("[CoreS3_Power] I2C SDA=%d SCL=%d\n", i2c_sda, i2c_scl); + DEBUG_PRINTF( "[CoreS3_Power][BOOT] ESP reset reason: %s (%d)\n", resetReasonText(bootResetReason), (int)bootResetReason ); if (i2c_sda != 12 || i2c_scl != 11) { - Serial.println(F("[CoreS3_Power] ERROR: Invalid CoreS3 I2C pins")); + DEBUG_PRINTLN(F("[CoreS3_Power] ERROR: Invalid CoreS3 I2C pins")); coreS3PowerInitializationCompleteState = true; return; } @@ -882,11 +882,11 @@ class CoreS3PowerUsermod : public Usermod aw9523Found = probeI2C(AW9523B_ADDR); axp2101Found = probeI2C(AXP2101_ADDR); - Serial.printf("[CoreS3_Power] AW9523B (0x58): %s\n", aw9523Found ? "FOUND" : "NOT FOUND"); - Serial.printf("[CoreS3_Power] AXP2101 (0x34): %s\n", axp2101Found ? "FOUND" : "NOT FOUND"); + DEBUG_PRINTF("[CoreS3_Power] AW9523B (0x58): %s\n", aw9523Found ? "FOUND" : "NOT FOUND"); + DEBUG_PRINTF("[CoreS3_Power] AXP2101 (0x34): %s\n", axp2101Found ? "FOUND" : "NOT FOUND"); if (!aw9523Found || !axp2101Found) { - Serial.println(F("[CoreS3_Power] External 5V enable canceled")); + DEBUG_PRINTLN(F("[CoreS3_Power] External 5V enable canceled")); coreS3PowerInitializationCompleteState = true; return; } @@ -899,17 +899,17 @@ class CoreS3PowerUsermod : public Usermod powerKeyMonitorReady = false; runtimePowerKeyMonitorAttempted = false; - Serial.println(F("[CoreS3_Power] Power key monitor: DEFERRED until M5GFX I2C1 is active")); - Serial.println(F("[CoreS3_Power] Safe shutdown: AXP2101 LONG IRQ primary trigger")); - Serial.printf("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); - Serial.printf("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); + DEBUG_PRINTLN(F("[CoreS3_Power] Power key monitor: DEFERRED until M5GFX I2C1 is active")); + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: AXP2101 LONG IRQ primary trigger")); + DEBUG_PRINTF("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); + DEBUG_PRINTF("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); registerLedSettingsSaveHandler(); coreS3PowerInitializationCompleteState = true; - Serial.println(F("[CoreS3_Power] Initialization complete")); - Serial.println(); + DEBUG_PRINTLN(F("[CoreS3_Power] Initialization complete")); + DEBUG_PRINTLN(""); } void loop() override From 9fcee85f7d32574ace45d357c7f6df81ca71fe02 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 28 Sep 2026 16:24:02 +0900 Subject: [PATCH 19/35] Use WLED global I2C for CoreS3 peripherals --- usermods/CoreS3_Display/CoreS3_Display.cpp | 2 +- .../M5StackDisplayHardwareBackend.cpp | 709 +++++++++++++++++- usermods/CoreS3_Power/CoreS3_Power.cpp | 42 +- usermods/audioreactive/audio_source.h | 37 +- 4 files changed, 703 insertions(+), 87 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 322ace6532..abcb6a5f4a 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -172,7 +172,7 @@ class CoreS3DisplayUsermod : public Usermod { static constexpr uint8_t RUNTIME_HEALTH_SHOWN_POWER_SAFETY = 0x02; static constexpr uint8_t RUNTIME_HEALTH_SHOWN_TOUCH = 0x04; - // Give the deferred M5GFX I2C1 power-key monitor time to ARM before + // Give the deferred global Wire / I2C0 power-key monitor time to ARM before // declaring Safe Shutdown unavailable. static constexpr unsigned long POWER_SAFETY_WARNING_GRACE_MS = 5000; static constexpr unsigned long RUNTIME_HEALTH_WARNING_HOLD_MS = 2200; diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 48e4324e00..222b84f341 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -4,6 +4,655 @@ #include +#include + +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) +#include +#include +#include +#include + +// =========================================================== +// Phase I2C0-R1-C CoreS3 display runtime +// +// CoreS3 Display / Touch / backlight use WLED's already initialized global +// Wire / I2C0 bus. M5GFX board autodetection is intentionally bypassed so +// the usermod does not create, own, or reinitialize a separate I2C bus. +// +// The panel, touch, and backlight behavior remains aligned with the +// M5GFX 0.2.26 CoreS3 implementation while I2C access stays on global Wire. +// =========================================================== + +namespace { + +static constexpr uint8_t CORES3_GLOBAL_WIRE_AW9523B_ADDR = 0x58; +static constexpr uint8_t CORES3_GLOBAL_WIRE_AXP2101_ADDR = 0x34; +static constexpr uint8_t CORES3_GLOBAL_WIRE_FT5X06_ADDR = 0x38; + +static constexpr uint8_t CORES3_AW9523B_OUTPUT_P0 = 0x02; +static constexpr uint8_t CORES3_AW9523B_OUTPUT_P1 = 0x03; +static constexpr uint8_t CORES3_AW9523B_CONFIG_P0 = 0x04; +static constexpr uint8_t CORES3_AW9523B_CONFIG_P1 = 0x05; +static constexpr uint8_t CORES3_AW9523B_LEDMODE_P0 = 0x12; + +static constexpr uint8_t CORES3_AW9523B_TOUCH_RST_MASK = 0x01; +static constexpr uint8_t CORES3_AW9523B_LCD_RST_MASK = 0x02; +static constexpr uint8_t CORES3_AW9523B_TOUCH_INT_MASK = 0x04; + +static constexpr uint8_t CORES3_AXP2101_LDO_ENABLE_REG = 0x90; +static constexpr uint8_t CORES3_AXP2101_DLDO1_ENABLE_MASK = 0x80; +static constexpr uint8_t CORES3_AXP2101_DLDO1_VOLTAGE_REG = 0x99; + +static bool coreS3GlobalWireReadBytes( + uint8_t address, + uint8_t reg, + uint8_t* data, + size_t length +) { + if ( data == nullptr || length == 0 ) { + return false; + } + + Wire.beginTransmission( address ); + Wire.write( reg ); + + if ( Wire.endTransmission( false ) != 0 ) { + return false; + } + + const size_t received = Wire.requestFrom( address, length, true ); + + if ( received != length ) { + while ( Wire.available() ) { + Wire.read(); + } + + return false; + } + + for ( size_t index = 0; index < length; ++index ) { + if ( !Wire.available() ) { + return false; + } + + data[index] = (uint8_t)Wire.read(); + } + + return true; +} + +static bool coreS3GlobalWireReadRegister8( + uint8_t address, + uint8_t reg, + uint8_t& value +) { + return coreS3GlobalWireReadBytes( + address, + reg, + &value, + 1 + ); +} + +static bool coreS3GlobalWireWriteRegister8( + uint8_t address, + uint8_t reg, + uint8_t value +) { + Wire.beginTransmission( address ); + Wire.write( reg ); + Wire.write( value ); + + return Wire.endTransmission() == 0; +} + +static bool coreS3GlobalWireUpdateRegister8( + uint8_t address, + uint8_t reg, + uint8_t mask, + uint8_t value +) { + uint8_t current = 0; + + if ( !coreS3GlobalWireReadRegister8( address, reg, current ) ) { + return false; + } + + const uint8_t updated = + ( current & static_cast( ~mask ) ) | + ( value & mask ); + + if ( updated == current ) { + return true; + } + + return coreS3GlobalWireWriteRegister8( + address, + reg, + updated + ); +} + + +class CoreS3PanelGlobalWire : public lgfx::Panel_ILI9342 { + public: + + CoreS3PanelGlobalWire() { + _cfg.pin_cs = GPIO_NUM_3; + _cfg.invert = true; + _cfg.offset_rotation = 3; + + _rotation = 1; + } + + protected: + + void rst_control( bool level ) override { + coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_OUTPUT_P1, + CORES3_AW9523B_LCD_RST_MASK, + level ? CORES3_AW9523B_LCD_RST_MASK : 0 + ); + } + + void cs_control( bool level ) override { + lgfx::Panel_ILI9342::cs_control( level ); + + // CoreS3 shares GPIO35 between LCD MISO and LCD D/C. + // Preserve the same GPIO-matrix switching used by M5GFX 0.2.26. + *(volatile uint32_t*)GPIO_FUNC35_OUT_SEL_CFG_REG = + level ? FSPIQ_OUT_IDX : SIG_GPIO_OUT_IDX; + + *(volatile uint32_t*)( + level + ? GPIO_ENABLE1_W1TC_REG + : GPIO_ENABLE1_W1TS_REG + ) = 1u << ( GPIO_NUM_35 & 31 ); + } +}; + + +class CoreS3LightGlobalWire : public lgfx::ILight { + public: + + bool init( uint8_t brightness ) override { + setBrightness( brightness ); + + return true; + } + + void setBrightness( uint8_t brightness ) override { + uint8_t ldoEnable = 0; + + if ( + !coreS3GlobalWireReadRegister8( + CORES3_GLOBAL_WIRE_AXP2101_ADDR, + CORES3_AXP2101_LDO_ENABLE_REG, + ldoEnable + ) + ) { + return; + } + + uint8_t dldo1Voltage = 0; + + if ( brightness > 0 ) { + ldoEnable |= CORES3_AXP2101_DLDO1_ENABLE_MASK; + dldo1Voltage = (uint8_t)( ( brightness + 641u ) >> 5 ); + } + else { + ldoEnable &= (uint8_t)~CORES3_AXP2101_DLDO1_ENABLE_MASK; + } + + if ( + !coreS3GlobalWireWriteRegister8( + CORES3_GLOBAL_WIRE_AXP2101_ADDR, + CORES3_AXP2101_LDO_ENABLE_REG, + ldoEnable + ) + ) { + return; + } + + coreS3GlobalWireWriteRegister8( + CORES3_GLOBAL_WIRE_AXP2101_ADDR, + CORES3_AXP2101_DLDO1_VOLTAGE_REG, + dldo1Voltage + ); + } +}; + + +class CoreS3TouchGlobalWire : public lgfx::ITouch { + private: + + static constexpr uint8_t FT5X06_TOUCH_DATA_REG = 0x02; + static constexpr uint8_t FT5X06_CIPHER_REG = 0xA3; + static constexpr uint8_t FT5X06_INTMODE_REG = 0xA4; + static constexpr uint8_t FT5X06_POWER_REG = 0xA5; + + static constexpr uint8_t FT5X06_MONITOR = 0x01; + static constexpr uint8_t FT5X06_SLEEP_IN = 0x03; + + static constexpr uint8_t MAX_TOUCH_POINTS = 5; + static constexpr size_t MAX_TOUCH_FRAME_LENGTH = + MAX_TOUCH_POINTS * 6 - 1; + + bool released = false; + + bool writeRegister( uint8_t reg, uint8_t value ) { + return coreS3GlobalWireWriteRegister8( + CORES3_GLOBAL_WIRE_FT5X06_ADDR, + reg, + value + ); + } + + bool readRegisters( + uint8_t reg, + uint8_t* data, + size_t length + ) { + return coreS3GlobalWireReadBytes( + CORES3_GLOBAL_WIRE_FT5X06_ADDR, + reg, + data, + length + ); + } + + size_t readTouchFrame( uint8_t* data ) { + uint8_t pointCount = 0; + + if ( + data == nullptr || + !readRegisters( + FT5X06_TOUCH_DATA_REG, + &pointCount, + 1 + ) + ) { + return 0; + } + + uint8_t points = pointCount & 0x0F; + + if ( points > MAX_TOUCH_POINTS ) { + points = MAX_TOUCH_POINTS; + } + + if ( points == 0 ) { + data[0] = pointCount; + + return 1; + } + + const size_t frameLength = + static_cast( points ) * 6 - 1; + + if ( + !readRegisters( + FT5X06_TOUCH_DATA_REG, + data, + frameLength + ) + ) { + return 0; + } + + return frameLength; + } + + void clearAw9523Interrupt() { + uint8_t ignored = 0; + + coreS3GlobalWireReadRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + 0x00, + ignored + ); + + coreS3GlobalWireReadRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + 0x01, + ignored + ); + } + + bool prepareTouchHardware() { + // CoreS3 routes FT6336U TOUCH_RST through AW9523B P0_0. + // Reproduce the touch-specific state without creating/reinitializing + // another I2C controller. WLED global Wire / I2C0 remains the owner. + if ( + !coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_CONFIG_P0, + CORES3_AW9523B_TOUCH_RST_MASK, + 0 + ) + ) { + return false; + } + + if ( + !coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_LEDMODE_P0, + CORES3_AW9523B_TOUCH_RST_MASK, + CORES3_AW9523B_TOUCH_RST_MASK + ) + ) { + return false; + } + + if ( + !coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_CONFIG_P1, + CORES3_AW9523B_TOUCH_INT_MASK, + CORES3_AW9523B_TOUCH_INT_MASK + ) + ) { + return false; + } + + // FT6336U requires RESET low for at least 5 ms and at least 300 ms + // between reset release and the first I2C access. + // Pulse reset explicitly so cold, warm, and software boots behave + // consistently while WLED global Wire / I2C0 remains active. + if ( + !coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_OUTPUT_P0, + CORES3_AW9523B_TOUCH_RST_MASK, + 0 + ) + ) { + return false; + } + + delay( 10 ); + + if ( + !coreS3GlobalWireUpdateRegister8( + CORES3_GLOBAL_WIRE_AW9523B_ADDR, + CORES3_AW9523B_OUTPUT_P0, + CORES3_AW9523B_TOUCH_RST_MASK, + CORES3_AW9523B_TOUCH_RST_MASK + ) + ) { + return false; + } + + delay( 350 ); + + return true; + } + + public: + + bool isReady() const { + return _inited; + } + + CoreS3TouchGlobalWire() { + _cfg.pin_int = GPIO_NUM_21; + _cfg.pin_rst = -1; + + _cfg.pin_sda = GPIO_NUM_12; + _cfg.pin_scl = GPIO_NUM_11; + _cfg.i2c_addr = CORES3_GLOBAL_WIRE_FT5X06_ADDR; + _cfg.i2c_port = I2C_NUM_0; + _cfg.freq = 400000; + + _cfg.x_min = 0; + _cfg.x_max = 319; + _cfg.y_min = 0; + _cfg.y_max = 239; + + _cfg.offset_rotation = 0; + _cfg.bus_shared = false; + } + + bool init() override { + _inited = false; + released = false; + + if ( _cfg.pin_int >= 0 ) { + pinMode( _cfg.pin_int, INPUT_PULLUP ); + } + + if ( !prepareTouchHardware() ) { + return false; + } + + const bool workModeOk = writeRegister( 0x00, 0x00 ); + + uint8_t identity[6] = { 0 }; + const bool identityReadOk = readRegisters( + FT5X06_CIPHER_REG, + identity, + sizeof( identity ) + ); + + const bool intModeOk = writeRegister( + FT5X06_INTMODE_REG, + 0x00 + ); + + const bool vendorIdPresent = + identityReadOk && + identity[5] != 0; + + _inited = + workModeOk && + identityReadOk && + intModeOk && + vendorIdPresent; + + return _inited; + } + + void wakeup() override { + if ( !_inited && !init() ) { + return; + } + + if ( _cfg.pin_int >= 0 ) { + pinMode( _cfg.pin_int, INPUT_PULLDOWN ); + delayMicroseconds( 512 ); + pinMode( _cfg.pin_int, INPUT_PULLUP ); + } + + writeRegister( + FT5X06_POWER_REG, + FT5X06_MONITOR + ); + } + + void sleep() override { + if ( !_inited && !init() ) { + return; + } + + writeRegister( + FT5X06_POWER_REG, + FT5X06_SLEEP_IN + ); + } + + uint_fast8_t getTouchRaw( + lgfx::touch_point_t* points, + uint_fast8_t count + ) override { + if ( + points == nullptr || + count == 0 || + ( !_inited && !init() ) + ) { + return 0; + } + + if ( _cfg.pin_int >= 0 ) { + const bool currentReleased = + digitalRead( _cfg.pin_int ) != LOW; + + if ( released != currentReleased ) { + released = currentReleased; + + writeRegister( + FT5X06_INTMODE_REG, + 0x00 + ); + } + + if ( released ) { + return 0; + } + } + + if ( count > MAX_TOUCH_POINTS ) { + count = MAX_TOUCH_POINTS; + } + + uint8_t frame[2][MAX_TOUCH_FRAME_LENGTH] = {}; + size_t frameLength[2] = { + readTouchFrame( frame[0] ), + 0 + }; + + if ( frameLength[0] <= 1 ) { + clearAw9523Interrupt(); + + return 0; + } + + bool stableFrame = false; + + for ( uint8_t retry = 0; retry < 5; ++retry ) { + frameLength[1] = readTouchFrame( frame[1] ); + + if ( + frameLength[0] == frameLength[1] && + frameLength[0] > 1 && + memcmp( + frame[0], + frame[1], + frameLength[0] + ) == 0 + ) { + stableFrame = true; + break; + } + + if ( + frameLength[1] > 0 && + frameLength[1] <= MAX_TOUCH_FRAME_LENGTH + ) { + memcpy( + frame[0], + frame[1], + frameLength[1] + ); + + frameLength[0] = frameLength[1]; + } + } + + if ( !stableFrame ) { + clearAw9523Interrupt(); + + return 0; + } + + uint8_t detectedPoints = frame[0][0] & 0x0F; + + if ( detectedPoints > MAX_TOUCH_POINTS ) { + detectedPoints = MAX_TOUCH_POINTS; + } + + if ( count > detectedPoints ) { + count = detectedPoints; + } + + for ( uint_fast8_t index = 0; index < count; ++index ) { + const uint8_t* data = + &frame[0][static_cast( index ) * 6]; + + points[index].size = 1; + points[index].x = + ( ( data[1] & 0x0F ) << 8 ) | + data[2]; + + points[index].y = + ( ( data[3] & 0x0F ) << 8 ) | + data[4]; + + points[index].id = + data[3] >> 4; + } + + return count; + } +}; + + +class CoreS3DisplayGlobalWireRuntime { + private: + + lgfx::Bus_SPI bus; + CoreS3PanelGlobalWire panel; + CoreS3TouchGlobalWire touch; + CoreS3LightGlobalWire light; + + public: + + CoreS3DisplayGlobalWireRuntime() { + auto busConfig = bus.config(); + + busConfig.spi_host = SPI2_HOST; + busConfig.dma_channel = SPI_DMA_CH_AUTO; + + busConfig.freq_write = 40000000; + busConfig.freq_read = 16000000; + + busConfig.pin_mosi = GPIO_NUM_37; + busConfig.pin_miso = GPIO_NUM_35; + busConfig.pin_sclk = GPIO_NUM_36; + busConfig.pin_dc = GPIO_NUM_35; + + busConfig.spi_mode = 0; + busConfig.spi_3wire = true; + + bus.config( busConfig ); + + panel.bus( &bus ); + panel.setLight( &light ); + panel.setTouch( &touch ); + } + + bool begin( M5GFX& display ) { + return display.init( &panel ); + } + + bool isTouchReady() const { + return touch.isReady(); + } +}; + + +static CoreS3DisplayGlobalWireRuntime& +coreS3DisplayGlobalWireRuntime() { + static CoreS3DisplayGlobalWireRuntime runtime; + + return runtime; +} + +} // namespace +#endif + + // =========================================================== // Compile-time M5Stack hardware profile // @@ -135,9 +784,8 @@ static constexpr const M5StackDisplayHardwareCapabilities& // =========================================================== // CoreS3 runtime battery telemetry // -// CoreS3 uses AXP2101 at 0x34. After Display initialization the internal -// GPIO12/GPIO11 bus is owned by M5GFX I2C_NUM_1, matching the verified -// CoreS3 Power/Audio runtime architecture. +// CoreS3 uses AXP2101 at 0x34 on the same WLED-owned global Wire / I2C0 +// bus used by CoreS3 Power, Display / Touch, and ES7210 Audio. // // AXP2101: // 0x00 bit3 = battery present @@ -145,25 +793,29 @@ static constexpr const M5StackDisplayHardwareCapabilities& // 0xA4 = fuel-gauge battery percentage // =========================================================== -static constexpr i2c_port_t CORES3_BATTERY_I2C_PORT = I2C_NUM_1; -static constexpr uint32_t CORES3_BATTERY_I2C_FREQUENCY = 400000; static constexpr uint8_t CORES3_AXP2101_ADDR = 0x34; static constexpr uint8_t AXP2101_REG_PMU_STATUS1 = 0x00; static constexpr uint8_t AXP2101_REG_PMU_STATUS2 = 0x01; static constexpr uint8_t AXP2101_REG_BATTERY_PERCENT = 0xA4; static bool readCoreS3Axp2101Register( uint8_t reg, uint8_t& value ) { - auto result = lgfx::i2c::transactionWriteRead( - CORES3_BATTERY_I2C_PORT, - CORES3_AXP2101_ADDR, - ®, - 1, - &value, - 1, - CORES3_BATTERY_I2C_FREQUENCY - ); + Wire.beginTransmission( CORES3_AXP2101_ADDR ); + Wire.write( reg ); + + if ( Wire.endTransmission( false ) != 0 ) { + return false; + } + + const size_t received = + Wire.requestFrom( CORES3_AXP2101_ADDR, (size_t)1, true ); + + if ( received != 1 || !Wire.available() ) { + return false; + } + + value = (uint8_t)Wire.read(); - return result.has_value(); + return true; } bool M5StackDisplayHardwareBackend::probeI2CAddress( TwoWire& wire, uint8_t address ) { @@ -484,7 +1136,30 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int return false; } - display.begin(); +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) + CoreS3DisplayGlobalWireRuntime& coreS3Runtime = + coreS3DisplayGlobalWireRuntime(); + + DEBUG_PRINTLN( + F( "[CoreS3_Display] Display backend: global Wire / I2C0" ) + ); + + if ( !coreS3Runtime.begin( display ) ) { + DEBUG_PRINTLN( + F( "[CoreS3_Display] ERROR: CoreS3 global Wire display initialization failed" ) + ); + + return false; + } + + touchReady = coreS3Runtime.isTouchReady(); +#else + DEBUG_PRINTLN( + F( "[CoreS3_Display] ERROR: Display runtime has no active hardware backend" ) + ); + + return false; +#endif display.setRotation( ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRotation ); @@ -499,8 +1174,6 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int return false; } - touchReady = ( display.touch() != nullptr ); - DEBUG_PRINTF( "[CoreS3_Display] " "Touch: %s\n", touchReady ? "READY" : "NOT FOUND" ); return true; diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index e06e01413d..b20477129b 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -3,8 +3,6 @@ #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) #include -#include -#include #include /* @@ -59,8 +57,6 @@ class CoreS3PowerUsermod : public Usermod static constexpr uint8_t AW9523B_ADDR = 0x58; static constexpr uint8_t AXP2101_ADDR = 0x34; - static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; - static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; static constexpr uint8_t AXP2101_REG_CHIP_ID = 0x03; static constexpr uint8_t AXP2101_REG_PWRON_STATUS = 0x20; @@ -259,29 +255,12 @@ class CoreS3PowerUsermod : public Usermod bool readRuntimeRegister(uint8_t address, uint8_t reg, uint8_t& value) { - auto result = lgfx::i2c::transactionWriteRead( - CORES3_INTERNAL_I2C_PORT, - address, - ®, - 1, - &value, - 1, - CORES3_INTERNAL_I2C_FREQUENCY - ); - return result.has_value(); + return readRegister(address, reg, value); } bool writeRuntimeRegister(uint8_t address, uint8_t reg, uint8_t value) { - const uint8_t data[2] = { reg, value }; - auto result = lgfx::i2c::transactionWrite( - CORES3_INTERNAL_I2C_PORT, - address, - data, - sizeof(data), - CORES3_INTERNAL_I2C_FREQUENCY - ); - return result.has_value(); + return writeRegister(address, reg, value); } void captureBootAxpDiagnostics() @@ -356,8 +335,7 @@ class CoreS3PowerUsermod : public Usermod return busEnabled && boostEnabled; } - // Re-assert the intended ON state after CoreS3_Display/M5GFX has taken - // ownership of the internal I2C bus. + // Re-assert the intended ON state through WLED's global Wire / I2C0 bus. bool applyRuntimeExternal5VEnable() { uint8_t p0 = 0; @@ -413,7 +391,7 @@ class CoreS3PowerUsermod : public Usermod uint8_t chipId = 0; if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_CHIP_ID, chipId)) { - DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key: M5GFX I2C1 AXP2101 read FAILED")); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key: global Wire / I2C0 AXP2101 read FAILED")); return false; } @@ -424,7 +402,7 @@ class CoreS3PowerUsermod : public Usermod if (!runtimePowerKeyBusReadyLogged) { runtimePowerKeyBusReadyLogged = true; - DEBUG_PRINTF("[CoreS3_Power] Runtime I2C: M5GFX I2C_NUM_1 AXP2101 READY (ID=0x%02X)\n", chipId); + DEBUG_PRINTF("[CoreS3_Power] Runtime I2C: global Wire / I2C0 AXP2101 READY (ID=0x%02X)\n", chipId); } if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_ENABLE_1, axpIrqEnableBefore)) return false; @@ -458,7 +436,7 @@ class CoreS3PowerUsermod : public Usermod lastPowerKeyPoll = millis(); DEBUG_PRINTF("[CoreS3_Power] Runtime PKEY IRQEN1: 0x%02X -> 0x%02X\n", axpIrqEnableBefore, axpIrqEnableAfter); - DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key monitor: ARMED on M5GFX I2C1")); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime power key monitor: ARMED on global Wire / I2C0")); return true; } @@ -800,7 +778,7 @@ class CoreS3PowerUsermod : public Usermod if (!readRuntimeRegister(AXP2101_ADDR, AXP2101_REG_IRQ_STATUS_1, status)) { if (now - lastRuntimeI2CFailureLog >= 1000) { lastRuntimeI2CFailureLog = now; - DEBUG_PRINTLN(F("[CoreS3_Power] Runtime PKEY status read FAILED on M5GFX I2C1")); + DEBUG_PRINTLN(F("[CoreS3_Power] Runtime PKEY status read FAILED on global Wire / I2C0")); } maintainSafeShutdownBlank(now); return; @@ -899,7 +877,7 @@ class CoreS3PowerUsermod : public Usermod powerKeyMonitorReady = false; runtimePowerKeyMonitorAttempted = false; - DEBUG_PRINTLN(F("[CoreS3_Power] Power key monitor: DEFERRED until M5GFX I2C1 is active")); + DEBUG_PRINTLN(F("[CoreS3_Power] Power key monitor: DEFERRED to runtime loop on global Wire / I2C0")); DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: AXP2101 LONG IRQ primary trigger")); DEBUG_PRINTF("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); DEBUG_PRINTF("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); @@ -953,7 +931,7 @@ class CoreS3PowerUsermod : public Usermod JsonArray shutdownInfo = user.createNestedArray("CoreS3 Safe Shutdown"); if (!powerKeyMonitorReady) { - shutdownInfo.add(runtimePowerKeyMonitorAttempted ? "Runtime M5GFX I2C1 monitor unavailable" : "Runtime M5GFX I2C1 monitor pending"); + shutdownInfo.add(runtimePowerKeyMonitorAttempted ? "Runtime Wire / I2C0 monitor unavailable" : "Runtime Wire / I2C0 monitor pending"); } else if (safeShutdownBlankActive) { shutdownInfo.add("BLACK output active - waiting for PMIC off"); @@ -1059,7 +1037,7 @@ REGISTER_USERMOD(coreS3PowerUsermod); * * Generic usermod CI compiles each usermod on several ESP32 targets. * Keep a minimal registered module on non-CoreS3 targets so the build and - * module validation can run without compiling CoreS3-only M5GFX/I2C1 code. + * module validation can run without compiling CoreS3-only power code. */ class CoreS3PowerUsermod : public Usermod { diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index c37b894e12..348fe9642d 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -2,10 +2,7 @@ #ifdef ARDUINO_ARCH_ESP32 #include "wled.h" #include -#if defined(WLED_M5STACK_CORES3) -#include -#include -#endif + #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) #include // legacy ADC driver causes bootloops in V5 #endif @@ -433,37 +430,6 @@ class ES7210Source : public I2SSource { }; bool _es7210I2cWrite(uint8_t reg, uint8_t value) { -#if defined(WLED_M5STACK_CORES3) - /* - * CoreS3 Display/Touch owns the internal GPIO12/GPIO11 bus through - * M5GFX on I2C_NUM_1 after display.begin(). Reuse that initialized - * bus instead of accessing the same pins through Arduino Wire. - */ - static constexpr i2c_port_t CORES3_INTERNAL_I2C_PORT = I2C_NUM_1; - static constexpr uint32_t CORES3_INTERNAL_I2C_FREQUENCY = 400000; - - const uint8_t data[2] = { reg, value }; - auto result = lgfx::i2c::transactionWrite( - CORES3_INTERNAL_I2C_PORT, - ES7210_ADDR, - data, - sizeof(data), - CORES3_INTERNAL_I2C_FREQUENCY - ); - - if (!result.has_value()) { - DEBUGSR_PRINTF( - "AR: ES7210 CoreS3 I2C1 write failed " - "(addr=0x%X, reg=0x%X, val=0x%X).\n", - ES7210_ADDR, - reg, - value - ); - return false; - } - - return true; -#else Wire.beginTransmission(ES7210_ADDR); Wire.write(reg); Wire.write(value); @@ -483,7 +449,6 @@ class ES7210Source : public I2SSource { } return true; -#endif } bool _es7210InitAdc() { From a890f7d19d2d6df6ce693340a12cf60967010dcf Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 28 Sep 2026 18:47:04 +0900 Subject: [PATCH 20/35] Restore CoreS3 NeoPixelBus runtime stabilization --- pio-scripts/cores3_v17_neopixelbus_patch.py | 540 ++++++++++++++++++ .../platformio_override.ini.sample | 5 + usermods/CoreS3_Display/readme.md | 61 +- usermods/CoreS3_Display/readme_jp.md | 59 +- 4 files changed, 655 insertions(+), 10 deletions(-) create mode 100644 pio-scripts/cores3_v17_neopixelbus_patch.py diff --git a/pio-scripts/cores3_v17_neopixelbus_patch.py b/pio-scripts/cores3_v17_neopixelbus_patch.py new file mode 100644 index 0000000000..b2f23e57aa --- /dev/null +++ b/pio-scripts/cores3_v17_neopixelbus_patch.py @@ -0,0 +1,540 @@ +# M5Stack CoreS3 / WLED V17 NeoPixelBus Production Integration +# +# PlatformIO PRE script for the m5stack_cores3 environment. +# +# This script makes the two hardware-validated NeoPixelBus fixes reproducible: +# +# 1) ESP32-S3 RMT output: +# - DMA enabled +# - 1024-symbol TX buffer +# - non-blocking readiness check without ESP-IDF timeout=0 log noise +# - guarded RMT handle lifecycle +# - failed channel allocation reports ready so WLED can tear down/rebuild +# the failed bus instead of waiting forever in BusManager::removeAll() +# +# 2) ESP32-S3 LCD/GDMA output: +# - fully stop/reset/disconnect/delete the GDMA channel when the last +# LCD mux bus is destroyed +# - prevents stale LCD peripheral ownership across runtime bus rebuilds +# +# The script patches the NeoPixelBus source downloaded by PlatformIO under +# .pio/libdeps. It does not require modified library files to be committed. +# +# NeoPixelBus itself is licensed under LGPL-3.0-or-later. This script keeps +# the upstream license header in the patched files untouched. +# +# The patch is idempotent and is safe to run on every PlatformIO build. + +from pathlib import Path + +Import("env") + +RMT_MARKER = "CoreS3 NeoPixelBus RMT DMA1024 production patch" +LCD_MARKER = "CoreS3 NeoPixelBus LCD GDMA teardown production patch" + +LCD_TEST_PRAGMAS = ( + '#pragma message("=== CORES3 TEST: ACTIVE NeoPixelBus NeoEsp32LcdXMethod.h IS COMPILED ===")', + '#pragma message("=== CORES3 TEST: NeoEsp32LcdXMethod.h IS COMPILED ===")', +) + + +def _replace_function(source: str, signature: str, replacement: str) -> str: + '''Replace one C++ function body by matching balanced braces.''' + sig_pos = source.find(signature) + if sig_pos < 0: + raise RuntimeError(f"signature not found: {signature}") + + brace_start = source.find("{", sig_pos + len(signature)) + if brace_start < 0: + raise RuntimeError(f"opening brace not found: {signature}") + + depth = 0 + in_string = False + in_char = False + escape = False + line_comment = False + block_comment = False + i = brace_start + + while i < len(source): + ch = source[i] + nxt = source[i + 1] if i + 1 < len(source) else "" + + if line_comment: + if ch == "\n": + line_comment = False + i += 1 + continue + + if block_comment: + if ch == "*" and nxt == "/": + block_comment = False + i += 2 + continue + i += 1 + continue + + if escape: + escape = False + i += 1 + continue + + if (in_string or in_char) and ch == "\\": + escape = True + i += 1 + continue + + if not in_string and not in_char: + if ch == "/" and nxt == "/": + line_comment = True + i += 2 + continue + if ch == "/" and nxt == "*": + block_comment = True + i += 2 + continue + + if not in_char and ch == '"': + in_string = not in_string + i += 1 + continue + + if not in_string and ch == "'": + in_char = not in_char + i += 1 + continue + + if not in_string and not in_char: + if ch == "{": + depth += 1 + elif ch == "}": + depth -= 1 + if depth == 0: + return source[:sig_pos] + replacement + source[i + 1:] + + i += 1 + + raise RuntimeError(f"matching closing brace not found: {signature}") + + +def _find_rmt_target() -> Path: + '''Locate the active NeoPixelBus RMT header used by this PIO environment.''' + libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") + + candidates = [] + for path in libdeps_root.rglob("NeoEsp32RmtXMethod.h"): + try: + text = path.read_text(encoding="utf-8") + except Exception: + continue + + if "NeoEsp32RmtMethodBase" in text and "rmt_new_tx_channel" in text: + candidates.append(path) + + # WLED's source-pinned NeoPixelBus package is installed as NeoPixelBus@src-... + # Prefer that copy when PlatformIO also leaves another NeoPixelBus directory. + preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] + + if len(preferred) == 1: + return preferred[0] + if len(candidates) == 1: + return candidates[0] + + detail = "\n".join(f" {p}" for p in candidates) or " " + raise RuntimeError( + "CoreS3 NeoPixelBus patch could not uniquely locate " + "NeoEsp32RmtXMethod.h:\n" + detail + ) + + +def _find_lcd_target(rmt_target: Path) -> Path: + ''' + Locate NeoEsp32LcdXMethod.h from the same active NeoPixelBus package. + + The sibling lookup is intentionally preferred so RMT and LCD patches can + never be applied to different NeoPixelBus copies in .pio/libdeps. + ''' + sibling = rmt_target.with_name("NeoEsp32LcdXMethod.h") + if sibling.is_file(): + try: + text = sibling.read_text(encoding="utf-8") + except Exception as exc: + raise RuntimeError( + f"CoreS3 LCD patch could not read {sibling}: {exc}" + ) from exc + + if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: + return sibling + + libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") + candidates = [] + + for path in libdeps_root.rglob("NeoEsp32LcdXMethod.h"): + try: + text = path.read_text(encoding="utf-8") + except Exception: + continue + + if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: + candidates.append(path) + + preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] + + if len(preferred) == 1: + return preferred[0] + if len(candidates) == 1: + return candidates[0] + + detail = "\n".join(f" {p}" for p in candidates) or " " + raise RuntimeError( + "CoreS3 NeoPixelBus patch could not uniquely locate " + "NeoEsp32LcdXMethod.h:\n" + detail + ) + + +def _patch_rmt(target: Path) -> None: + '''Apply the validated ESP32-S3 RMT DMA1024 production patch.''' + text = target.read_text(encoding="utf-8") + + ready = f''' bool IsReadyToUpdate() const + {{ + // {RMT_MARKER} + // ESP-IDF 5.5 emits an error log when timeout=0 is used as a busy poll. + // Update() performs the actual completion wait before transmitting. + // If RMT channel allocation failed, no asynchronous transfer can be + // active. Report ready so WLED can remove/rebuild that failed bus. + return true; + }}''' + + if RMT_MARKER in text: + # Upgrade the earlier production patch in place. The previous guard + # returned false forever when rmt_new_tx_channel() failed, which made + # WLED's BusManager::removeAll() wait forever during a settings save. + old_ready = "return (_channel != nullptr && _led_encoder != nullptr);" + upgraded = False + if old_ready in text: + text = _replace_function( + text, "bool IsReadyToUpdate() const", ready + ) + target.write_text(text, encoding="utf-8", newline="\n") + upgraded = True + print( + "[CoreS3 RMT DMA1024] upgraded failed-channel readiness " + f"guard: {target.name}" + ) + + required = [ + "config.mem_block_symbols = 1024;", + "config.flags.with_dma = true;", + "return true;", + ] + missing = [item for item in required if item not in text] + if missing: + raise RuntimeError( + "CoreS3 RMT patch marker exists but verification failed; " + f"missing: {missing}" + ) + + if not upgraded: + print(f"[CoreS3 RMT DMA1024] patch already present: {target.name}") + return + + destructor = f''' ~NeoEsp32RmtMethodBase() + {{ + // {RMT_MARKER} + if (_channel != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT( + rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS) + ); + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_disable(_channel)); + }} + + if (_led_encoder != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); + _led_encoder = nullptr; + }} + + if (_channel != nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + }} + + gpio_matrix_out(_pin, 0x100, false, false); + pinMode(_pin, INPUT); + + free(_dataEditing); + free(_dataSending); + }}''' + + initialize = f''' void Initialize() + {{ + // {RMT_MARKER} + rmt_tx_channel_config_t config = {{}}; + config.clk_src = RMT_CLK_SRC_DEFAULT; + config.gpio_num = static_cast(_pin); + +#if defined(CONFIG_IDF_TARGET_ESP32S3) + // CoreS3 / ESP32-S3 uses DMA with a 1024-symbol buffer. + // A 32-pixel RGB frame is 768 RMT symbols, so the complete frame + // fits in one DMA buffer and avoids the refill boundary implicated + // in the previously observed tail-pixel anomaly. + config.mem_block_symbols = 1024; +#else + config.mem_block_symbols = 192; +#endif + + config.resolution_hz = T_SPEED::RmtTicksPerSecond; + config.trans_queue_depth = 4; + config.flags.invert_out = T_INVERTED::Inverted; +#if defined(CONFIG_IDF_TARGET_ESP32S3) + config.flags.with_dma = true; +#else + config.flags.with_dma = false; +#endif + + esp_err_t ret = rmt_new_tx_channel(&config, &_channel); + if (ret != ESP_OK || _channel == nullptr) + {{ + _channel = nullptr; + return; + }} + + led_strip_encoder_config_t encoder_config = {{}}; + encoder_config.resolution = T_SPEED::RmtTicksPerSecond; + _tx_config.loop_count = 0; + + ret = rmt_new_led_strip_encoder( + &encoder_config, + &_led_encoder, + T_SPEED::RmtBit0, + T_SPEED::RmtBit1 + ); + + if (ret != ESP_OK || _led_encoder == nullptr) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + _led_encoder = nullptr; + return; + }} + + ret = rmt_enable(_channel); + if (ret != ESP_OK) + {{ + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); + _led_encoder = nullptr; + ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); + _channel = nullptr; + return; + }} + }}''' + + update = f''' void Update(bool maintainBufferConsistency) + {{ + // {RMT_MARKER} + if (_channel == nullptr || _led_encoder == nullptr) + {{ + return; + }} + + // Serialize writes: wait for the previous asynchronous RMT transfer + // to finish before starting the next frame. + if (ESP_OK == ESP_ERROR_CHECK_WITHOUT_ABORT( + rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS))) + {{ + const esp_err_t ret = rmt_transmit( + _channel, + _led_encoder, + _dataEditing, + _sizeData, + &_tx_config + ); + + if (ret != ESP_OK) + {{ + return; + }} + + if (maintainBufferConsistency) + {{ + memcpy(_dataSending, _dataEditing, _sizeData); + }} + + std::swap(_dataSending, _dataEditing); + }} + }}''' + + try: + text = _replace_function( + text, "~NeoEsp32RmtMethodBase()", destructor + ) + text = _replace_function( + text, "bool IsReadyToUpdate() const", ready + ) + text = _replace_function( + text, "void Initialize()", initialize + ) + text = _replace_function( + text, "void Update(bool maintainBufferConsistency)", update + ) + except RuntimeError as exc: + raise RuntimeError( + f"CoreS3 RMT DMA1024 patch failed for {target}: {exc}" + ) from exc + + required = [ + RMT_MARKER, + "config.mem_block_symbols = 1024;", + "config.flags.with_dma = true;", + "return true;", + ] + for item in required: + if item not in text: + raise RuntimeError( + f"CoreS3 RMT DMA1024 verification failed: missing {item}" + ) + + target.write_text(text, encoding="utf-8", newline="\n") + print( + "[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / " + f"1024-symbol patch: {target}" + ) + + +def _patch_lcd_gdma(target: Path) -> None: + '''Apply the validated LCD/GDMA full teardown and remove TEST-only markers.''' + text = target.read_text(encoding="utf-8") + original = text + + # Remove the compile-time TEST banner used during hardware diagnosis. + for pragma in LCD_TEST_PRAGMAS: + text = text.replace(pragma + "\n\n", "") + text = text.replace(pragma + "\r\n\r\n", "") + text = text.replace(pragma + "\n", "") + text = text.replace(pragma + "\r\n", "") + text = text.replace(pragma, "") + + if LCD_MARKER not in text: + destruct = f''' void Destruct() + {{ + if (_dmaItems == nullptr) + {{ + return; + }} + + // {LCD_MARKER} + // + // gdma_reset() alone resets the channel state but does not release + // the LCD peripheral ownership held by the GDMA driver. A runtime + // WLED bus rebuild can therefore fail on the next gdma_connect(). + // + // NeoEsp32LcdXMethodBase waits for the active LCD transfer to finish + // before DeregisterMuxBus() reaches this teardown. + if (_dmaChannel != nullptr) + {{ + esp_err_t dmaResult; + + dmaResult = gdma_stop(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA stop during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_reset(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA reset during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_disconnect(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA disconnect during teardown failed: %d", + (int)dmaResult + ); + }} + + dmaResult = gdma_del_channel(_dmaChannel); + if (dmaResult != ESP_OK) + {{ + log_w( + "LCD GDMA delete during teardown failed: %d", + (int)dmaResult + ); + }} + + _dmaChannel = nullptr; + }} + + periph_module_disable(PERIPH_LCD_CAM_MODULE); + periph_module_reset(PERIPH_LCD_CAM_MODULE); + + heap_caps_free(LcdBuffer); + heap_caps_free(_dmaItems); + + LcdBufferSize = 0; + _dmaItems = nullptr; + LcdBuffer = nullptr; + + MuxMap.Reset(); + }}''' + + try: + text = _replace_function(text, "void Destruct()", destruct) + except RuntimeError as exc: + raise RuntimeError( + f"CoreS3 LCD GDMA teardown patch failed for {target}: {exc}" + ) from exc + + required = [ + LCD_MARKER, + "gdma_stop(_dmaChannel);", + "gdma_reset(_dmaChannel);", + "gdma_disconnect(_dmaChannel);", + "gdma_del_channel(_dmaChannel);", + "_dmaChannel = nullptr;", + ] + for item in required: + if item not in text: + raise RuntimeError( + f"CoreS3 LCD GDMA verification failed: missing {item}" + ) + + for pragma in LCD_TEST_PRAGMAS: + if pragma in text: + raise RuntimeError( + "CoreS3 LCD GDMA production cleanup failed: " + f"TEST pragma still present: {pragma}" + ) + + if text == original: + print(f"[CoreS3 LCD GDMA] patch already present: {target.name}") + return + + target.write_text(text, encoding="utf-8", newline="\n") + print( + "[CoreS3 LCD GDMA] applied full GDMA teardown production patch: " + f"{target}" + ) + + +def _apply_patches() -> None: + rmt_target = _find_rmt_target() + lcd_target = _find_lcd_target(rmt_target) + + _patch_rmt(rmt_target) + _patch_lcd_gdma(lcd_target) + + +if not env.IsIntegrationDump(): + _apply_patches() diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 1a55d965b4..03133c62ec 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -16,6 +16,7 @@ ; This example preserves: ; - the validated CoreS3 Quad PSRAM configuration ; - CoreS3 Power / Display / Audio / AudioReactive integration +; - the validated ESP32-S3 NeoPixelBus RMT DMA1024, failed-channel guard, and LCD/GDMA teardown patches ; ; IMPORTANT: ; Do NOT set lib_archive=yes in this custom environment. @@ -39,8 +40,12 @@ platform = ${esp32_idf_V5.platform_pioarduino} platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} ; Preserve normal WLED build scripts. +; +; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024, failed-channel guard, +; and LCD/GDMA teardown patches. extra_scripts = ${scripts_defaults.extra_scripts} + pre:pio-scripts/cores3_v17_neopixelbus_patch.py custom_usermods = CoreS3_Power diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index ceae73dde0..155cc1c7a0 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -28,6 +28,7 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
- The CoreS3 Power usermod blanks the existing LED output, lets WLED save the new configuration, and then performs a software reboot. The saved configuration is applied normally after reboot; no additional user action is required. +- For the validated three-output configuration, Port A / B / C use the I2S driver. Multiple DMA-backed RMT outputs may exhaust the available ESP32-S3 RMT TX channel resources. - DCDC OVP protection is not disabled. --- @@ -472,7 +523,7 @@ Please refer to the upstream repository for WLED documentation, supported LED ty ## Licensing WLED source in this repository follows the upstream **EUPL v1.2** license.
-NeoPixelBus remains licensed under **LGPL-3.0-or-later**. +NeoPixelBus remains licensed under **LGPL-3.0-or-later**. The CoreS3 build-time patch script modifies the PlatformIO-downloaded NeoPixelBus source while preserving the upstream library license header. Refer to the repository `LICENSE` file and the respective upstream projects for complete license terms. diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index c85ff2d9d4..f4a38f9ddc 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -28,6 +28,7 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
- AXP2101 Power Key を使った Safe Shutdown - 電源OFF前に LED BLACK frame を送信 - Safe Shutdown のキャンセル時は直前の LED 状態を復元 +- ESP32-S3 / NeoPixelBus 向け RMT DMA1024、failed-channel guard、および LCD/GDMA runtime-rebuild 安定化 - ブラウザから現在の LCD 画面を BMP で取得 --- @@ -217,7 +218,7 @@ Audio Reactive 用: ```text UM_AUDIOREACTIVE_ENABLE -SR_DMTYPE=7 +SR_DMTYPE=10 ``` GPIO0 は ES7210 MCLK として使用するため、WLED の物理 Button から除外しています。 @@ -239,7 +240,7 @@ usermods/CoreS3_Display/platformio_override.ini.sample platformio_override.ini ``` -このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、および Audio Reactive 定義が含まれています。 +このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、および NeoPixelBus patch 用 pre-script が含まれています。 ## Build @@ -279,6 +280,53 @@ PlatformIO から `m5stack_cores3` を Upload します。 シリアルモニタなどのプログラムが COM ポートを開いている場合は、Upload 前に閉じてください。 +--- + +## NeoPixelBus / RMT DMA1024 + LCD/GDMA Patch + +ESP32-S3 + NeoPixelBus の RMT 出力では、LED Count より後ろのピクセルが不定期に点灯する問題を実機で確認しました。 + +ESP32-S3 の RMT 出力に対して、CoreS3 Buildでは次の設定を適用します。 + +```text +RMT DMA enabled +mem_block_symbols 1024 +``` + +パッチは `.pio/libdeps` のライブラリを手作業で変更する方式ではありません。 + +```text +pio-scripts/cores3_v17_neopixelbus_patch.py +``` + +を PlatformIO の pre-script として実行し、NeoPixelBus dependency を再取得した場合でも必要な修正を自動的に再適用します。 + +RMT patch では、RMT channel の確保に失敗した場合の lifecycle も保護します。現在検証している ESP32-S3 / ESP-IDF / NeoPixelBus の組み合わせでは、DMA を使用する RMT 出力を複数同時に使用すると、利用可能な RMT TX channel resource が不足する場合があります。そのため、初期化に失敗した RMT Bus は WLED の Bus 再構築を永久待ちさせず、安全に破棄できる状態として扱います。 + +CoreS3 の3出力構成では、Port A / B / C を I2S Driver に設定した状態で実機検証しています。 + +RMT patch 適用時の例: + +```text +[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / 1024-symbol patch: ... +``` + +同じ pre-script では、runtime の LED Bus 再構築に使用する LCD/GDMA teardown 修正も適用します。 + +最後の LCD mux bus を破棄する際、GDMA channel を stop / reset / disconnect / delete してから次の初期化へ進むことで、古い LCD peripheral ownership が残ることによる次のエラーを防止します。 + +```text +gdma: peripheral 5 is already used by another channel +``` + +LCD/GDMA patch 適用時の例: + +```text +[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... +``` + +各 patch は idempotent であり、NeoPixelBus dependency を削除したクリーンな状態からの再 Build でも再適用性を確認しています。また、CoreS3 の3つの LED 出力をすべて I2S に設定した状態で、LED Bus 設定の Save / Bus 再構築 / Software Reboot について実機確認しています。 + --- ## Power Management @@ -408,6 +456,8 @@ CoreS3 対応の中心は次のファイルです。 ```text WLED/ ├─ platformio_override.ini +├─ pio-scripts/ +│ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ @@ -452,8 +502,7 @@ CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S - WLED 標準 Brightness 128 は変更していません。 - CoreS3 では Brightness 64 前後からの使用を推奨します。 - Browser Screenshot は BMP の静止画です。ライブストリームではありません。 -- M5Stack CoreS3 では、**LED & Hardware** で LED バス構成を変更して保存すると、ランタイムでの LED バス再初期化中に GDMA/LCD peripheral conflict のログが一時的に出力される場合があります。
- CoreS3 Power Usermod は旧 LED 出力を消灯して WLED に新しい構成を保存させた後、Software Reboot を実行します。再起動後は保存した構成が正常に適用され、追加操作は不要です。 +- 検証済みの3出力構成では、Port A / B / C に I2S Driver を使用します。DMA を使用する RMT 出力を複数同時に使用すると、ESP32-S3 の利用可能な RMT TX channel resource が不足する場合があります。 - DCDC OVP 保護は無効化していません。 --- @@ -472,7 +521,7 @@ Please also refer to the upstream repository for WLED documentation, supported L ## Licensing このリポジトリの WLED ソースは、upstream と同じ **EUPL v1.2** に従います。
-NeoPixelBus は **LGPL-3.0-or-later** のままです。 +NeoPixelBus は **LGPL-3.0-or-later** のままです。CoreS3 の build-time patch script は PlatformIO が取得した NeoPixelBus ソースへ修正を適用しますが、upstream library のライセンスヘッダーは保持します。 完全なライセンス条件については、リポジトリの `LICENSE` と各 upstream project を参照してください。 From e6943e2e4e4f5f223c944df855738f58e9f5b4d6 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Tue, 29 Sep 2026 12:59:34 +0900 Subject: [PATCH 21/35] Fix CoreS3 display usermods build --- usermods/CoreS3_Display/CoreS3_Display.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index abcb6a5f4a..c4595e5126 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -57,9 +57,11 @@ static const char CORES3_DISPLAY_CONFIG_NAME[] PROGMEM = "CoreS3_Display"; // CoreS3_Power publishes read-only runtime health state. // Display consumes these signals only for user-facing warning UX; it does // not own or modify the power-control implementation. +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) bool coreS3PowerInitializationComplete(); bool coreS3PowerExternal5VReady(); bool coreS3PowerSafeShutdownMonitorReady(); +#endif class CoreS3DisplayUsermod : public Usermod { @@ -1938,6 +1940,7 @@ class CoreS3DisplayUsermod : public Usermod { } RuntimeHealthWarning getNextRuntimeHealthWarning( unsigned long now ) { +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) const bool graceExpired = now - runtimeHealthStartMs >= POWER_SAFETY_WARNING_GRACE_MS; @@ -1958,6 +1961,7 @@ class CoreS3DisplayUsermod : public Usermod { ) { return RUNTIME_HEALTH_WARNING_POWER_SAFETY; } +#endif if ( !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_TOUCH ) && From cc5808dcf5b3c197f55b8ba8082c5136011cdd21 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Tue, 29 Sep 2026 21:56:35 +0900 Subject: [PATCH 22/35] Remove CoreS3 NeoPixelBus patch script --- pio-scripts/cores3_v17_neopixelbus_patch.py | 540 ------------------ .../platformio_override.ini.sample | 5 - usermods/CoreS3_Display/readme.md | 64 +-- usermods/CoreS3_Display/readme_jp.md | 62 +- 4 files changed, 2 insertions(+), 669 deletions(-) delete mode 100644 pio-scripts/cores3_v17_neopixelbus_patch.py diff --git a/pio-scripts/cores3_v17_neopixelbus_patch.py b/pio-scripts/cores3_v17_neopixelbus_patch.py deleted file mode 100644 index b2f23e57aa..0000000000 --- a/pio-scripts/cores3_v17_neopixelbus_patch.py +++ /dev/null @@ -1,540 +0,0 @@ -# M5Stack CoreS3 / WLED V17 NeoPixelBus Production Integration -# -# PlatformIO PRE script for the m5stack_cores3 environment. -# -# This script makes the two hardware-validated NeoPixelBus fixes reproducible: -# -# 1) ESP32-S3 RMT output: -# - DMA enabled -# - 1024-symbol TX buffer -# - non-blocking readiness check without ESP-IDF timeout=0 log noise -# - guarded RMT handle lifecycle -# - failed channel allocation reports ready so WLED can tear down/rebuild -# the failed bus instead of waiting forever in BusManager::removeAll() -# -# 2) ESP32-S3 LCD/GDMA output: -# - fully stop/reset/disconnect/delete the GDMA channel when the last -# LCD mux bus is destroyed -# - prevents stale LCD peripheral ownership across runtime bus rebuilds -# -# The script patches the NeoPixelBus source downloaded by PlatformIO under -# .pio/libdeps. It does not require modified library files to be committed. -# -# NeoPixelBus itself is licensed under LGPL-3.0-or-later. This script keeps -# the upstream license header in the patched files untouched. -# -# The patch is idempotent and is safe to run on every PlatformIO build. - -from pathlib import Path - -Import("env") - -RMT_MARKER = "CoreS3 NeoPixelBus RMT DMA1024 production patch" -LCD_MARKER = "CoreS3 NeoPixelBus LCD GDMA teardown production patch" - -LCD_TEST_PRAGMAS = ( - '#pragma message("=== CORES3 TEST: ACTIVE NeoPixelBus NeoEsp32LcdXMethod.h IS COMPILED ===")', - '#pragma message("=== CORES3 TEST: NeoEsp32LcdXMethod.h IS COMPILED ===")', -) - - -def _replace_function(source: str, signature: str, replacement: str) -> str: - '''Replace one C++ function body by matching balanced braces.''' - sig_pos = source.find(signature) - if sig_pos < 0: - raise RuntimeError(f"signature not found: {signature}") - - brace_start = source.find("{", sig_pos + len(signature)) - if brace_start < 0: - raise RuntimeError(f"opening brace not found: {signature}") - - depth = 0 - in_string = False - in_char = False - escape = False - line_comment = False - block_comment = False - i = brace_start - - while i < len(source): - ch = source[i] - nxt = source[i + 1] if i + 1 < len(source) else "" - - if line_comment: - if ch == "\n": - line_comment = False - i += 1 - continue - - if block_comment: - if ch == "*" and nxt == "/": - block_comment = False - i += 2 - continue - i += 1 - continue - - if escape: - escape = False - i += 1 - continue - - if (in_string or in_char) and ch == "\\": - escape = True - i += 1 - continue - - if not in_string and not in_char: - if ch == "/" and nxt == "/": - line_comment = True - i += 2 - continue - if ch == "/" and nxt == "*": - block_comment = True - i += 2 - continue - - if not in_char and ch == '"': - in_string = not in_string - i += 1 - continue - - if not in_string and ch == "'": - in_char = not in_char - i += 1 - continue - - if not in_string and not in_char: - if ch == "{": - depth += 1 - elif ch == "}": - depth -= 1 - if depth == 0: - return source[:sig_pos] + replacement + source[i + 1:] - - i += 1 - - raise RuntimeError(f"matching closing brace not found: {signature}") - - -def _find_rmt_target() -> Path: - '''Locate the active NeoPixelBus RMT header used by this PIO environment.''' - libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") - - candidates = [] - for path in libdeps_root.rglob("NeoEsp32RmtXMethod.h"): - try: - text = path.read_text(encoding="utf-8") - except Exception: - continue - - if "NeoEsp32RmtMethodBase" in text and "rmt_new_tx_channel" in text: - candidates.append(path) - - # WLED's source-pinned NeoPixelBus package is installed as NeoPixelBus@src-... - # Prefer that copy when PlatformIO also leaves another NeoPixelBus directory. - preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] - - if len(preferred) == 1: - return preferred[0] - if len(candidates) == 1: - return candidates[0] - - detail = "\n".join(f" {p}" for p in candidates) or " " - raise RuntimeError( - "CoreS3 NeoPixelBus patch could not uniquely locate " - "NeoEsp32RmtXMethod.h:\n" + detail - ) - - -def _find_lcd_target(rmt_target: Path) -> Path: - ''' - Locate NeoEsp32LcdXMethod.h from the same active NeoPixelBus package. - - The sibling lookup is intentionally preferred so RMT and LCD patches can - never be applied to different NeoPixelBus copies in .pio/libdeps. - ''' - sibling = rmt_target.with_name("NeoEsp32LcdXMethod.h") - if sibling.is_file(): - try: - text = sibling.read_text(encoding="utf-8") - except Exception as exc: - raise RuntimeError( - f"CoreS3 LCD patch could not read {sibling}: {exc}" - ) from exc - - if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: - return sibling - - libdeps_root = Path(env.subst("$PROJECT_LIBDEPS_DIR")) / env.subst("$PIOENV") - candidates = [] - - for path in libdeps_root.rglob("NeoEsp32LcdXMethod.h"): - try: - text = path.read_text(encoding="utf-8") - except Exception: - continue - - if "NeoEspLcdMonoBuffContext" in text and "gdma_connect" in text: - candidates.append(path) - - preferred = [p for p in candidates if "NeoPixelBus@src-" in str(p)] - - if len(preferred) == 1: - return preferred[0] - if len(candidates) == 1: - return candidates[0] - - detail = "\n".join(f" {p}" for p in candidates) or " " - raise RuntimeError( - "CoreS3 NeoPixelBus patch could not uniquely locate " - "NeoEsp32LcdXMethod.h:\n" + detail - ) - - -def _patch_rmt(target: Path) -> None: - '''Apply the validated ESP32-S3 RMT DMA1024 production patch.''' - text = target.read_text(encoding="utf-8") - - ready = f''' bool IsReadyToUpdate() const - {{ - // {RMT_MARKER} - // ESP-IDF 5.5 emits an error log when timeout=0 is used as a busy poll. - // Update() performs the actual completion wait before transmitting. - // If RMT channel allocation failed, no asynchronous transfer can be - // active. Report ready so WLED can remove/rebuild that failed bus. - return true; - }}''' - - if RMT_MARKER in text: - # Upgrade the earlier production patch in place. The previous guard - # returned false forever when rmt_new_tx_channel() failed, which made - # WLED's BusManager::removeAll() wait forever during a settings save. - old_ready = "return (_channel != nullptr && _led_encoder != nullptr);" - upgraded = False - if old_ready in text: - text = _replace_function( - text, "bool IsReadyToUpdate() const", ready - ) - target.write_text(text, encoding="utf-8", newline="\n") - upgraded = True - print( - "[CoreS3 RMT DMA1024] upgraded failed-channel readiness " - f"guard: {target.name}" - ) - - required = [ - "config.mem_block_symbols = 1024;", - "config.flags.with_dma = true;", - "return true;", - ] - missing = [item for item in required if item not in text] - if missing: - raise RuntimeError( - "CoreS3 RMT patch marker exists but verification failed; " - f"missing: {missing}" - ) - - if not upgraded: - print(f"[CoreS3 RMT DMA1024] patch already present: {target.name}") - return - - destructor = f''' ~NeoEsp32RmtMethodBase() - {{ - // {RMT_MARKER} - if (_channel != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT( - rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS) - ); - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_disable(_channel)); - }} - - if (_led_encoder != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); - _led_encoder = nullptr; - }} - - if (_channel != nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - }} - - gpio_matrix_out(_pin, 0x100, false, false); - pinMode(_pin, INPUT); - - free(_dataEditing); - free(_dataSending); - }}''' - - initialize = f''' void Initialize() - {{ - // {RMT_MARKER} - rmt_tx_channel_config_t config = {{}}; - config.clk_src = RMT_CLK_SRC_DEFAULT; - config.gpio_num = static_cast(_pin); - -#if defined(CONFIG_IDF_TARGET_ESP32S3) - // CoreS3 / ESP32-S3 uses DMA with a 1024-symbol buffer. - // A 32-pixel RGB frame is 768 RMT symbols, so the complete frame - // fits in one DMA buffer and avoids the refill boundary implicated - // in the previously observed tail-pixel anomaly. - config.mem_block_symbols = 1024; -#else - config.mem_block_symbols = 192; -#endif - - config.resolution_hz = T_SPEED::RmtTicksPerSecond; - config.trans_queue_depth = 4; - config.flags.invert_out = T_INVERTED::Inverted; -#if defined(CONFIG_IDF_TARGET_ESP32S3) - config.flags.with_dma = true; -#else - config.flags.with_dma = false; -#endif - - esp_err_t ret = rmt_new_tx_channel(&config, &_channel); - if (ret != ESP_OK || _channel == nullptr) - {{ - _channel = nullptr; - return; - }} - - led_strip_encoder_config_t encoder_config = {{}}; - encoder_config.resolution = T_SPEED::RmtTicksPerSecond; - _tx_config.loop_count = 0; - - ret = rmt_new_led_strip_encoder( - &encoder_config, - &_led_encoder, - T_SPEED::RmtBit0, - T_SPEED::RmtBit1 - ); - - if (ret != ESP_OK || _led_encoder == nullptr) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - _led_encoder = nullptr; - return; - }} - - ret = rmt_enable(_channel); - if (ret != ESP_OK) - {{ - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_encoder(_led_encoder)); - _led_encoder = nullptr; - ESP_ERROR_CHECK_WITHOUT_ABORT(rmt_del_channel(_channel)); - _channel = nullptr; - return; - }} - }}''' - - update = f''' void Update(bool maintainBufferConsistency) - {{ - // {RMT_MARKER} - if (_channel == nullptr || _led_encoder == nullptr) - {{ - return; - }} - - // Serialize writes: wait for the previous asynchronous RMT transfer - // to finish before starting the next frame. - if (ESP_OK == ESP_ERROR_CHECK_WITHOUT_ABORT( - rmt_tx_wait_all_done(_channel, 10000 / portTICK_PERIOD_MS))) - {{ - const esp_err_t ret = rmt_transmit( - _channel, - _led_encoder, - _dataEditing, - _sizeData, - &_tx_config - ); - - if (ret != ESP_OK) - {{ - return; - }} - - if (maintainBufferConsistency) - {{ - memcpy(_dataSending, _dataEditing, _sizeData); - }} - - std::swap(_dataSending, _dataEditing); - }} - }}''' - - try: - text = _replace_function( - text, "~NeoEsp32RmtMethodBase()", destructor - ) - text = _replace_function( - text, "bool IsReadyToUpdate() const", ready - ) - text = _replace_function( - text, "void Initialize()", initialize - ) - text = _replace_function( - text, "void Update(bool maintainBufferConsistency)", update - ) - except RuntimeError as exc: - raise RuntimeError( - f"CoreS3 RMT DMA1024 patch failed for {target}: {exc}" - ) from exc - - required = [ - RMT_MARKER, - "config.mem_block_symbols = 1024;", - "config.flags.with_dma = true;", - "return true;", - ] - for item in required: - if item not in text: - raise RuntimeError( - f"CoreS3 RMT DMA1024 verification failed: missing {item}" - ) - - target.write_text(text, encoding="utf-8", newline="\n") - print( - "[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / " - f"1024-symbol patch: {target}" - ) - - -def _patch_lcd_gdma(target: Path) -> None: - '''Apply the validated LCD/GDMA full teardown and remove TEST-only markers.''' - text = target.read_text(encoding="utf-8") - original = text - - # Remove the compile-time TEST banner used during hardware diagnosis. - for pragma in LCD_TEST_PRAGMAS: - text = text.replace(pragma + "\n\n", "") - text = text.replace(pragma + "\r\n\r\n", "") - text = text.replace(pragma + "\n", "") - text = text.replace(pragma + "\r\n", "") - text = text.replace(pragma, "") - - if LCD_MARKER not in text: - destruct = f''' void Destruct() - {{ - if (_dmaItems == nullptr) - {{ - return; - }} - - // {LCD_MARKER} - // - // gdma_reset() alone resets the channel state but does not release - // the LCD peripheral ownership held by the GDMA driver. A runtime - // WLED bus rebuild can therefore fail on the next gdma_connect(). - // - // NeoEsp32LcdXMethodBase waits for the active LCD transfer to finish - // before DeregisterMuxBus() reaches this teardown. - if (_dmaChannel != nullptr) - {{ - esp_err_t dmaResult; - - dmaResult = gdma_stop(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA stop during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_reset(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA reset during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_disconnect(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA disconnect during teardown failed: %d", - (int)dmaResult - ); - }} - - dmaResult = gdma_del_channel(_dmaChannel); - if (dmaResult != ESP_OK) - {{ - log_w( - "LCD GDMA delete during teardown failed: %d", - (int)dmaResult - ); - }} - - _dmaChannel = nullptr; - }} - - periph_module_disable(PERIPH_LCD_CAM_MODULE); - periph_module_reset(PERIPH_LCD_CAM_MODULE); - - heap_caps_free(LcdBuffer); - heap_caps_free(_dmaItems); - - LcdBufferSize = 0; - _dmaItems = nullptr; - LcdBuffer = nullptr; - - MuxMap.Reset(); - }}''' - - try: - text = _replace_function(text, "void Destruct()", destruct) - except RuntimeError as exc: - raise RuntimeError( - f"CoreS3 LCD GDMA teardown patch failed for {target}: {exc}" - ) from exc - - required = [ - LCD_MARKER, - "gdma_stop(_dmaChannel);", - "gdma_reset(_dmaChannel);", - "gdma_disconnect(_dmaChannel);", - "gdma_del_channel(_dmaChannel);", - "_dmaChannel = nullptr;", - ] - for item in required: - if item not in text: - raise RuntimeError( - f"CoreS3 LCD GDMA verification failed: missing {item}" - ) - - for pragma in LCD_TEST_PRAGMAS: - if pragma in text: - raise RuntimeError( - "CoreS3 LCD GDMA production cleanup failed: " - f"TEST pragma still present: {pragma}" - ) - - if text == original: - print(f"[CoreS3 LCD GDMA] patch already present: {target.name}") - return - - target.write_text(text, encoding="utf-8", newline="\n") - print( - "[CoreS3 LCD GDMA] applied full GDMA teardown production patch: " - f"{target}" - ) - - -def _apply_patches() -> None: - rmt_target = _find_rmt_target() - lcd_target = _find_lcd_target(rmt_target) - - _patch_rmt(rmt_target) - _patch_lcd_gdma(lcd_target) - - -if not env.IsIntegrationDump(): - _apply_patches() diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 03133c62ec..1a55d965b4 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -16,7 +16,6 @@ ; This example preserves: ; - the validated CoreS3 Quad PSRAM configuration ; - CoreS3 Power / Display / Audio / AudioReactive integration -; - the validated ESP32-S3 NeoPixelBus RMT DMA1024, failed-channel guard, and LCD/GDMA teardown patches ; ; IMPORTANT: ; Do NOT set lib_archive=yes in this custom environment. @@ -40,12 +39,8 @@ platform = ${esp32_idf_V5.platform_pioarduino} platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} ; Preserve normal WLED build scripts. -; -; Apply the validated NeoPixelBus ESP32-S3 RMT DMA1024, failed-channel guard, -; and LCD/GDMA teardown patches. extra_scripts = ${scripts_defaults.extra_scripts} - pre:pio-scripts/cores3_v17_neopixelbus_patch.py custom_usermods = CoreS3_Power diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 155cc1c7a0..43a3a22fde 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -28,7 +28,6 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
-NeoPixelBus remains licensed under **LGPL-3.0-or-later**. The CoreS3 build-time patch script modifies the PlatformIO-downloaded NeoPixelBus source while preserving the upstream library license header. - -Refer to the repository `LICENSE` file and the respective upstream projects for complete license terms. - ---- - ## Acknowledgements - WLED project and contributors diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index f4a38f9ddc..69f0756eb6 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -28,7 +28,6 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
- AXP2101 Power Key を使った Safe Shutdown - 電源OFF前に LED BLACK frame を送信 - Safe Shutdown のキャンセル時は直前の LED 状態を復元 -- ESP32-S3 / NeoPixelBus 向け RMT DMA1024、failed-channel guard、および LCD/GDMA runtime-rebuild 安定化 - ブラウザから現在の LCD 画面を BMP で取得 --- @@ -240,7 +239,7 @@ usermods/CoreS3_Display/platformio_override.ini.sample platformio_override.ini ``` -このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、Audio Reactive 定義、および NeoPixelBus patch 用 pre-script が含まれています。 +このサンプルには、CoreS3 Environment、Quad PSRAM 設定、CoreS3 Usermod、および Audio Reactive 定義が含まれています。 ## Build @@ -280,53 +279,6 @@ PlatformIO から `m5stack_cores3` を Upload します。 シリアルモニタなどのプログラムが COM ポートを開いている場合は、Upload 前に閉じてください。 ---- - -## NeoPixelBus / RMT DMA1024 + LCD/GDMA Patch - -ESP32-S3 + NeoPixelBus の RMT 出力では、LED Count より後ろのピクセルが不定期に点灯する問題を実機で確認しました。 - -ESP32-S3 の RMT 出力に対して、CoreS3 Buildでは次の設定を適用します。 - -```text -RMT DMA enabled -mem_block_symbols 1024 -``` - -パッチは `.pio/libdeps` のライブラリを手作業で変更する方式ではありません。 - -```text -pio-scripts/cores3_v17_neopixelbus_patch.py -``` - -を PlatformIO の pre-script として実行し、NeoPixelBus dependency を再取得した場合でも必要な修正を自動的に再適用します。 - -RMT patch では、RMT channel の確保に失敗した場合の lifecycle も保護します。現在検証している ESP32-S3 / ESP-IDF / NeoPixelBus の組み合わせでは、DMA を使用する RMT 出力を複数同時に使用すると、利用可能な RMT TX channel resource が不足する場合があります。そのため、初期化に失敗した RMT Bus は WLED の Bus 再構築を永久待ちさせず、安全に破棄できる状態として扱います。 - -CoreS3 の3出力構成では、Port A / B / C を I2S Driver に設定した状態で実機検証しています。 - -RMT patch 適用時の例: - -```text -[CoreS3 RMT DMA1024] applied ESP32-S3 DMA / 1024-symbol patch: ... -``` - -同じ pre-script では、runtime の LED Bus 再構築に使用する LCD/GDMA teardown 修正も適用します。 - -最後の LCD mux bus を破棄する際、GDMA channel を stop / reset / disconnect / delete してから次の初期化へ進むことで、古い LCD peripheral ownership が残ることによる次のエラーを防止します。 - -```text -gdma: peripheral 5 is already used by another channel -``` - -LCD/GDMA patch 適用時の例: - -```text -[CoreS3 LCD GDMA] applied full GDMA teardown production patch: ... -``` - -各 patch は idempotent であり、NeoPixelBus dependency を削除したクリーンな状態からの再 Build でも再適用性を確認しています。また、CoreS3 の3つの LED 出力をすべて I2S に設定した状態で、LED Bus 設定の Save / Bus 再構築 / Software Reboot について実機確認しています。 - --- ## Power Management @@ -456,8 +408,6 @@ CoreS3 対応の中心は次のファイルです。 ```text WLED/ ├─ platformio_override.ini -├─ pio-scripts/ -│ └─ cores3_v17_neopixelbus_patch.py └─ usermods/ ├─ CoreS3_Power/ ├─ CoreS3_Display/ @@ -502,7 +452,6 @@ CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S - WLED 標準 Brightness 128 は変更していません。 - CoreS3 では Brightness 64 前後からの使用を推奨します。 - Browser Screenshot は BMP の静止画です。ライブストリームではありません。 -- 検証済みの3出力構成では、Port A / B / C に I2S Driver を使用します。DMA を使用する RMT 出力を複数同時に使用すると、ESP32-S3 の利用可能な RMT TX channel resource が不足する場合があります。 - DCDC OVP 保護は無効化していません。 --- @@ -518,15 +467,6 @@ Please also refer to the upstream repository for WLED documentation, supported L --- -## Licensing - -このリポジトリの WLED ソースは、upstream と同じ **EUPL v1.2** に従います。
-NeoPixelBus は **LGPL-3.0-or-later** のままです。CoreS3 の build-time patch script は PlatformIO が取得した NeoPixelBus ソースへ修正を適用しますが、upstream library のライセンスヘッダーは保持します。 - -完全なライセンス条件については、リポジトリの `LICENSE` と各 upstream project を参照してください。 - ---- - ## Acknowledgements - WLED project and contributors From 97a0ff29d70d7bd59c60decdc8620c930ae2d43b Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Wed, 30 Sep 2026 22:51:27 +0900 Subject: [PATCH 23/35] Align ES7210 audio code with WLED style --- usermods/audioreactive/audio_source.h | 47 +++++---------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 348fe9642d..b683638ad7 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -2,7 +2,6 @@ #ifdef ARDUINO_ARCH_ESP32 #include "wled.h" #include - #if defined(CONFIG_IDF_TARGET_ESP32) && (ESP_IDF_VERSION_MAJOR < 5) #include // legacy ADC driver causes bootloops in V5 #endif @@ -254,8 +253,8 @@ class I2SSource : public AudioSource { // Reserve the master clock pin if provided _mclkPin = mclkPin; if (mclkPin != I2S_PIN_NO_CHANGE) { - if(!PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { - DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pin: MCLK=%d\n", mclkPin); + if(!PinManager::allocatePin(mclkPin, true, PinOwner::UM_Audioreactive)) { + DEBUGSR_PRINTF("\nAR: Failed to allocate I2S pin: MCLK=%d\n", mclkPin); return; } else _routeMclk(mclkPin); @@ -437,14 +436,8 @@ class ES7210Source : public I2SSource { uint8_t i2cErr = Wire.endTransmission(); if (i2cErr != 0) { - DEBUGSR_PRINTF( - "AR: ES7210 I2C write failed with error=%d " - "(addr=0x%X, reg=0x%X, val=0x%X).\n", - i2cErr, - ES7210_ADDR, - reg, - value - ); + DEBUGSR_PRINTF("AR: ES7210 I2C write failed with error=%d (addr=0x%X, reg=0x%X, val=0x%X).\n", + i2cErr, ES7210_ADDR, reg, value); return false; } @@ -453,10 +446,7 @@ class ES7210Source : public I2SSource { bool _es7210InitAdc() { if (_sampleRate != 22050) { - DEBUGSR_PRINTF( - "AR: ES7210 requires 22050 Hz sampling; requested %u Hz.\n", - (unsigned)_sampleRate - ); + DEBUGSR_PRINTF("AR: ES7210 requires 22050 Hz sampling; requested %u Hz.\n", (unsigned)_sampleRate); return false; } @@ -543,12 +533,7 @@ class ES7210Source : public I2SSource { } public: - ES7210Source( - SRate_t sampleRate, - int blockSize, - float sampleScale = 1.0f - ) : - I2SSource(sampleRate, blockSize, sampleScale) { + ES7210Source(SRate_t sampleRate, int blockSize, float sampleScale = 1.0f) : I2SSource(sampleRate, blockSize, sampleScale) { /* * ES7210 22.05 kHz uses an 11.2896 MHz master clock. @@ -560,20 +545,11 @@ class ES7210Source : public I2SSource { _config.mclk_multiple = I2S_MCLK_MULTIPLE_512; } - void initialize( - int8_t i2swsPin, - int8_t i2ssdPin, - int8_t i2sckPin, - int8_t mclkPin - ) override { + void initialize(int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) override { DEBUGSR_PRINTLN(F("ES7210Source:: initialize();")); if ((i2sckPin < 0) || (mclkPin < 0)) { - DEBUGSR_PRINTF( - "\nAR: invalid ES7210 I2S pin: SCK=%d, MCLK=%d\n", - i2sckPin, - mclkPin - ); + DEBUGSR_PRINTF("\nAR: invalid ES7210 I2S pin: SCK=%d, MCLK=%d\n", i2sckPin, mclkPin); return; } @@ -589,12 +565,7 @@ class ES7210Source : public I2SSource { * - I2S driver lifecycle * - standard sample acquisition */ - I2SSource::initialize( - i2swsPin, - i2ssdPin, - i2sckPin, - mclkPin - ); + I2SSource::initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); } }; // AI: end From 0411732c6174e4aa0bc96bd142b5d15ffb14aa88 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 1 Oct 2026 00:06:54 +0900 Subject: [PATCH 24/35] Address CoreS3 README review feedback --- usermods/CoreS3_Display/readme.md | 41 +++++++++++----------------- usermods/CoreS3_Display/readme_jp.md | 41 +++++++++++----------------- 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 43a3a22fde..4c4f003a41 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -39,7 +39,7 @@ The following images were captured directly from the CoreS3 LCD using the Browse ### Main

- + CoreS3 main control screen

The MAIN screen provides LED Power, Brightness, Effect, Color, Preset, network status, and battery status. @@ -48,8 +48,8 @@ The MAIN screen provides LED Power, Brightness, Effect, Color, Preset, network s - - + + @@ -64,9 +64,9 @@ Audio Reactive Effects can expose a different parameter set from standard Effect
CoreS3 standard effect screenCoreS3 Audio Reactive effect screen
Standard Effect
- - - + + + @@ -82,9 +82,9 @@ When the current Effect does not use a Color Slot, the UI shows `COLOR NOT USED`
CoreS3 C1 color editing screenCoreS3 C1 and C2 color editing screenCoreS3 color not used screen
C1 Edit
- - - + + + @@ -92,8 +92,8 @@ When the current Effect does not use a Color Slot, the UI shows `COLOR NOT USED` - - + + @@ -210,9 +210,10 @@ CoreS3-specific usermods: ```text CoreS3_Power CoreS3_Display -audioreactive ``` +The generic WLED `audioreactive` usermod is also enabled to provide ES7210 support for the CoreS3 built-in microphones. + Audio Reactive definitions: ```text @@ -403,7 +404,8 @@ Battery status is updated periodically instead of continuously polling I2C. ## Project Structure -The main files involved in CoreS3 support are: +The CoreS3-specific usermods are `CoreS3_Power` and `CoreS3_Display`. +The generic WLED `audioreactive` usermod also contains the ES7210 source used by the CoreS3 configuration. ```text WLED/ @@ -440,7 +442,7 @@ Responsibilities: ### audioreactive -Adds ES7210 support for the CoreS3 built-in microphone through the standard WLED Audio Reactive I2S path. +The generic WLED Audio Reactive usermod includes ES7210 support used by the CoreS3 built-in microphones through the standard WLED Audio Reactive I2S path. --- @@ -456,17 +458,6 @@ Adds ES7210 support for the CoreS3 built-in microphone through the standard WLED --- -## Upstream - -This project is based on WLED: - -https://github.com/wled/WLED - -WLED itself remains the upstream project.
-Please refer to the upstream repository for WLED documentation, supported LED types, API behavior, and licensing. - ---- - ## Acknowledgements - WLED project and contributors diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 69f0756eb6..3e2086ccd3 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -39,7 +39,7 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
### Main

- + CoreS3 メイン操作画面

MAIN 画面では、LED Power、Brightness、Effect、Color、Preset、およびネットワーク / バッテリー状態を確認できます。 @@ -48,8 +48,8 @@ MAIN 画面では、LED Power、Brightness、Effect、Color、Preset、および
CoreS3 preset selection screenCoreS3 preset management screenCoreS3 save new preset screen
PresetSave New
CoreS3 delete preset screenCoreS3 boot preset screen
- - + + @@ -64,9 +64,9 @@ Audio Reactive Effect では、通常 Effect と異なるパラメータ構成
CoreS3 標準エフェクト画面CoreS3 Audio Reactive エフェクト画面
Standard Effect
- - - + + + @@ -82,9 +82,9 @@ Effect が Color Slot を使用しない場合は `COLOR NOT USED` と表示し
CoreS3 C1 カラー編集画面CoreS3 C1・C2 カラー編集画面CoreS3 Color Not Used 画面
C1 Edit
- - - + + + @@ -92,8 +92,8 @@ Effect が Color Slot を使用しない場合は `COLOR NOT USED` と表示し - - + + @@ -210,9 +210,10 @@ CoreS3 固有 Usermod: ```text CoreS3_Power CoreS3_Display -audioreactive ``` +WLED 標準の汎用 `audioreactive` Usermod も有効化し、CoreS3 内蔵マイクの ES7210 対応に使用します。 + Audio Reactive 用: ```text @@ -403,7 +404,8 @@ Battery status は定期更新され、常時 I2C polling し続けないよう ## Project Structure -CoreS3 対応の中心は次のファイルです。 +CoreS3 固有 Usermod は `CoreS3_Power` と `CoreS3_Display` です。 +WLED 標準の汎用 `audioreactive` Usermod には、CoreS3 構成で使用する ES7210 source も含まれます。 ```text WLED/ @@ -440,7 +442,7 @@ WLED/ ### audioreactive -CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S 経路へ追加しています。 +WLED 標準の汎用 Audio Reactive Usermod に含まれる ES7210 対応を、CoreS3 内蔵マイクで使用します。 --- @@ -456,17 +458,6 @@ CoreS3 内蔵マイクの ES7210 対応を、WLED 標準の Audio Reactive I2S --- -## Upstream - -This project is based on WLED: - -https://github.com/wled/WLED - -WLED itself remains the upstream project.
-Please also refer to the upstream repository for WLED documentation, supported LED types, API behavior, and licensing. - ---- - ## Acknowledgements - WLED project and contributors From ee222911b82c666fec361862ea1cb1c07e7df44e Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 1 Oct 2026 11:26:18 +0900 Subject: [PATCH 25/35] Avoid repeated CoreS3 touch initialization --- usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 222b84f341..af401752e0 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -489,7 +489,7 @@ class CoreS3TouchGlobalWire : public lgfx::ITouch { if ( points == nullptr || count == 0 || - ( !_inited && !init() ) + !_inited ) { return 0; } From 188675e58f2dd30a77dac89b28bb7dc2b33f04b5 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 1 Oct 2026 14:16:53 +0900 Subject: [PATCH 26/35] Use global Wire for Core2 diagnostics --- .../M5StackDisplayHardwareBackend.cpp | 25 +++++++++---------- .../M5StackDisplayHardwareBackend.h | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index af401752e0..9d2b3aa512 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -1097,28 +1097,27 @@ void M5StackDisplayHardwareBackend::runDiagnostics() { return; } - TwoWire probeWire( 1 ); - - if ( !probeWire.begin( CORE2_INTERNAL_I2C_SDA, CORE2_INTERNAL_I2C_SCL, CORE2_INTERNAL_I2C_FREQUENCY ) ) { + if ( + i2c_sda != CORE2_INTERNAL_I2C_SDA || + i2c_scl != CORE2_INTERNAL_I2C_SCL + ) { hardwareProbe.state = M5STACK_HARDWARE_PROBE_FAILED; - DEBUG_PRINTLN( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C start failed" ) ); + DEBUG_PRINTLN( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C must use WLED global Wire on GPIO21/22" ) ); return; } delay( 10 ); - hardwareProbe.address34 = probeI2CAddress( probeWire, 0x34 ); - hardwareProbe.address35 = probeI2CAddress( probeWire, 0x35 ); - hardwareProbe.address38 = probeI2CAddress( probeWire, 0x38 ); - hardwareProbe.address40 = probeI2CAddress( probeWire, 0x40 ); - hardwareProbe.address51 = probeI2CAddress( probeWire, 0x51 ); - hardwareProbe.address68 = probeI2CAddress( probeWire, 0x68 ); - - classifyCore2HardwareProbe( probeWire ); + hardwareProbe.address34 = probeI2CAddress( Wire, 0x34 ); + hardwareProbe.address35 = probeI2CAddress( Wire, 0x35 ); + hardwareProbe.address38 = probeI2CAddress( Wire, 0x38 ); + hardwareProbe.address40 = probeI2CAddress( Wire, 0x40 ); + hardwareProbe.address51 = probeI2CAddress( Wire, 0x51 ); + hardwareProbe.address68 = probeI2CAddress( Wire, 0x68 ); - probeWire.end(); + classifyCore2HardwareProbe( Wire ); hardwareProbe.state = M5STACK_HARDWARE_PROBE_COMPLETE; diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h index a3ef44a5bf..df28f0f7be 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h @@ -63,7 +63,6 @@ class M5StackDisplayHardwareBackend { static constexpr int CORE2_INTERNAL_I2C_SDA = 21; static constexpr int CORE2_INTERNAL_I2C_SCL = 22; - static constexpr uint32_t CORE2_INTERNAL_I2C_FREQUENCY = 400000; M5GFX& display; HardwareProbeResult hardwareProbe; From dc1c03cfb1bd8193e4390b13d3fe4799c17febeb Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Thu, 1 Oct 2026 22:57:33 +0900 Subject: [PATCH 27/35] Remove Core2 diagnostic scaffolding --- usermods/CoreS3_Display/CoreS3_Display.cpp | 180 +------ .../M5StackDisplayHardwareBackend.cpp | 476 +----------------- .../M5StackDisplayHardwareBackend.h | 84 +--- 3 files changed, 30 insertions(+), 710 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index c4595e5126..934b6ba5af 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -13,7 +13,7 @@ #include "CoreS3_WLED_Logo.h" // =========================================================== -// M5Stack Display Controller Usermod +// M5Stack CoreS3 Display Controller Usermod // // Current verified runtime // - M5Stack CoreS3 @@ -21,14 +21,6 @@ // - Touch input // - LCD brightness control // -// Prepared hardware profiles -// - M5Stack Core2 -// - M5Stack Core2 for AWS -// -// The Core2-family profiles remain diagnostic-only until their -// Display / Touch / Power paths are implemented and verified on -// real hardware. -// // Responsibilities // - WLED power / brightness control // - Effect and palette navigation @@ -42,9 +34,7 @@ // // Architecture // UI and WLED-state logic are kept separate from the thin -// Display / Touch / brightness hardware-access boundary so the -// same controller behavior can later be reused by Core2-family -// hardware profiles. +// CoreS3 Display / Touch / brightness hardware-access boundary. // // Compatibility // The existing "CoreS3_Display" configuration key and usermod @@ -821,58 +811,6 @@ class CoreS3DisplayUsermod : public Usermod { // regression surface for the hardware-verified CoreS3 UI. // ========================================================= - const char* getHardwareProbeStateName() { - return hardwareBackend.probeStateName(); - } - - const char* getDetectedPmuName() { - return hardwareBackend.detectedPmuName(); - } - - const char* getDetectedImuName() { - return hardwareBackend.detectedImuName(); - } - - const char* getDetectedVariantName() { - return hardwareBackend.detectedVariantName(); - } - - bool isCore2FamilyProfile() { - return hardwareBackend.isCore2FamilyProfile(); - } - - bool isCore2DiagnosticOnlyMode() { - return hardwareBackend.isCore2DiagnosticOnlyMode(); - } - - const char* getHardwareRuntimeModeName() { - return hardwareBackend.runtimeModeName(); - } - - const char* getHardwarePortStatusName() { - return hardwareBackend.portStatusName(); - } - - const char* getDetectedRevisionName() { - return hardwareBackend.detectedRevisionName(); - } - - void runHardwareDiagnostics() { - hardwareBackend.runDiagnostics(); - } - - const char* getHardwareProfileName() { - return hardwareBackend.profileName(); - } - - const char* getHardwareRevisionName() { - return hardwareBackend.revisionName(); - } - - bool isHardwareDisplayRuntimeEnabled() { - return hardwareBackend.isDisplayRuntimeEnabled(); - } - bool initializeDisplayHardware() { return hardwareBackend.initializeDisplay( screenWidth, screenHeight, touchReady ); } @@ -6602,8 +6540,7 @@ class CoreS3DisplayUsermod : public Usermod { // Page-specific runtime display synchronization // // These helpers contain only UI/WLED state synchronization. - // Hardware-specific display/touch access remains outside this layer - // so the same page logic can be reused for Core2 variants later. + // Hardware-specific display/touch access remains isolated in the backend. // ========================================================= bool updateMainPageState( uint8_t effectMode, uint8_t currentSpeed, uint8_t currentIntensity, uint8_t currentPalette, uint32_t primaryColor, bool primaryColorChanged ) { @@ -6942,7 +6879,6 @@ class CoreS3DisplayUsermod : public Usermod { } if ( - isCore2DiagnosticOnlyMode() || !displayReady || screenWidth <= 0 || screenHeight <= 0 @@ -7193,33 +7129,10 @@ class CoreS3DisplayUsermod : public Usermod { registerScreenshotEndpoint(); - DEBUG_PRINTF( - "[CoreS3_Display] " "Hardware: %s, Revision=%s, Runtime=%s\n", - getHardwareProfileName(), - getHardwareRevisionName(), - getHardwareRuntimeModeName() - ); - - DEBUG_PRINTF( - "[CoreS3_Display] " "Port status: %s\n", - getHardwarePortStatusName() - ); + DEBUG_PRINTLN( F( "[CoreS3_Display] Hardware: M5Stack CoreS3" ) ); DEBUG_PRINTF( "[CoreS3_Display] " "Settings: " "Sleep=%u sec, " "LCD=%u, " "Fade=%s, " "FadeDuration=%u ms\n", sleepTimeoutSec, lcdBrightness, fadeEnabled ? "ON" : "OFF", fadeDurationMs ); - runHardwareDiagnostics(); - - if ( isCore2DiagnosticOnlyMode() ) { - initDone = true; - - DEBUG_PRINTLN( F( "[CoreS3_Display] Core2 diagnostic-only runtime complete" ) ); - DEBUG_PRINTLN( F( "[CoreS3_Display] Display/Touch/LCD brightness initialization intentionally skipped" ) ); - DEBUG_PRINTLN( F( "[CoreS3_Display] Capture the hardware probe result before enabling Core2 UI/Power support" ) ); - DEBUG_PRINTLN(""); - - return; - } - if ( !initializeDisplayHardware() ) { return; } @@ -7436,71 +7349,9 @@ class CoreS3DisplayUsermod : public Usermod { user = root.createNestedObject( "u" ); } - JsonArray hardwareProfileInfo = user.createNestedArray( "M5Stack Hardware Profile" ); - - hardwareProfileInfo.add( getHardwareProfileName() ); - - JsonArray hardwareRevisionInfo = user.createNestedArray( "M5Stack Hardware Revision" ); - - hardwareRevisionInfo.add( getHardwareRevisionName() ); - - JsonArray hardwareRuntimeInfo = user.createNestedArray( "M5Stack Runtime Mode" ); - - hardwareRuntimeInfo.add( getHardwareRuntimeModeName() ); - - JsonArray hardwarePortStatusInfo = user.createNestedArray( "M5Stack Porting Status" ); - - hardwarePortStatusInfo.add( getHardwarePortStatusName() ); - - JsonArray hardwareProbeInfo = user.createNestedArray( "M5Stack Hardware Probe" ); - - hardwareProbeInfo.add( getHardwareProbeStateName() ); - - JsonArray hardwareVariantInfo = user.createNestedArray( "M5Stack Detected Variant" ); - - hardwareVariantInfo.add( getDetectedVariantName() ); - - JsonArray hardwareDetectedRevisionInfo = user.createNestedArray( "M5Stack Detected Revision" ); - - hardwareDetectedRevisionInfo.add( getDetectedRevisionName() ); - - JsonArray hardwarePmuInfo = user.createNestedArray( "M5Stack Detected PMU" ); - - hardwarePmuInfo.add( getDetectedPmuName() ); - - JsonArray hardwareImuInfo = user.createNestedArray( "M5Stack Detected IMU" ); - - hardwareImuInfo.add( getDetectedImuName() ); - - JsonArray hardwareCore2I2cInfo = user.createNestedArray( "M5Stack Core2 I2C Signature" ); - - if ( hardwareBackend.isProbeComplete() ) { - char signature[64]; - - snprintf( - signature, - sizeof(signature), - "34:%c 35:%c 38:%c 40:%c 51:%c 68:%c", - hardwareBackend.hasI2CAddress( 0x34 ) ? 'Y' : 'N', - hardwareBackend.hasI2CAddress( 0x35 ) ? 'Y' : 'N', - hardwareBackend.hasI2CAddress( 0x38 ) ? 'Y' : 'N', - hardwareBackend.hasI2CAddress( 0x40 ) ? 'Y' : 'N', - hardwareBackend.hasI2CAddress( 0x51 ) ? 'Y' : 'N', - hardwareBackend.hasI2CAddress( 0x68 ) ? 'Y' : 'N' - ); - - hardwareCore2I2cInfo.add( signature ); - } - else { - hardwareCore2I2cInfo.add( "Not probed" ); - } - JsonArray displayInfo = user.createNestedArray( "CoreS3 Display" ); - if ( isCore2DiagnosticOnlyMode() ) { - displayInfo.add( "DIAGNOSTIC ONLY - NOT INITIALIZED" ); - } - else if (displayReady) { + if ( displayReady ) { char text[32]; snprintf( text, sizeof(text), "READY (%d x %d)", screenWidth, screenHeight ); @@ -7513,10 +7364,7 @@ class CoreS3DisplayUsermod : public Usermod { JsonArray screenshotInfo = user.createNestedArray( "CoreS3 Display Screenshot" ); - if ( isCore2DiagnosticOnlyMode() ) { - screenshotInfo.add( "UNAVAILABLE - DIAGNOSTIC ONLY" ); - } - else if ( displayReady ) { + if ( displayReady ) { screenshotInfo.add( "/cores3/screenshot.bmp" ); } else { @@ -7525,21 +7373,7 @@ class CoreS3DisplayUsermod : public Usermod { JsonArray touchInfo = user.createNestedArray( "CoreS3 Display Touch" ); - if ( isCore2DiagnosticOnlyMode() ) { - if ( hardwareBackend.isProbeComplete() ) { - touchInfo.add( - hardwareBackend.hasI2CAddress( 0x38 ) - ? "I2C 0x38 DETECTED - NOT INITIALIZED" - : "I2C 0x38 NOT DETECTED" - ); - } - else { - touchInfo.add( "DIAGNOSTIC PROBE UNAVAILABLE" ); - } - } - else { - touchInfo.add( touchReady ? "READY" : "NOT FOUND" ); - } + touchInfo.add( touchReady ? "READY" : "NOT FOUND" ); JsonArray wifiInfo = user.createNestedArray( "CoreS3 Display WiFi" ); diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 9d2b3aa512..750a7b6ab1 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -2,6 +2,7 @@ #include "M5StackDisplayHardwareBackend.h" +#include #include #include @@ -653,133 +654,8 @@ coreS3DisplayGlobalWireRuntime() { #endif -// =========================================================== -// Compile-time M5Stack hardware profile -// -// CoreS3 remains the default verified runtime. -// Core2 / Core2 for AWS remain diagnostic-only until their -// Display / Touch / Power paths are verified on real hardware. -// =========================================================== - -#define WLED_M5STACK_DISPLAY_PROFILE_CORES3 0 -#define WLED_M5STACK_DISPLAY_PROFILE_CORE2 1 -#define WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS 2 - -#define WLED_M5STACK_DISPLAY_REVISION_UNKNOWN 0 -#define WLED_M5STACK_DISPLAY_REVISION_V1_0 10 -#define WLED_M5STACK_DISPLAY_REVISION_V1_1 11 -#define WLED_M5STACK_DISPLAY_REVISION_V1_3 13 - -#ifndef WLED_M5STACK_DISPLAY_PROFILE - #define WLED_M5STACK_DISPLAY_PROFILE WLED_M5STACK_DISPLAY_PROFILE_CORES3 -#endif - -#ifndef WLED_M5STACK_DISPLAY_REVISION - #define WLED_M5STACK_DISPLAY_REVISION WLED_M5STACK_DISPLAY_REVISION_UNKNOWN -#endif - -#ifndef WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY - #define WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY 0 -#endif - -static_assert( - WLED_M5STACK_DISPLAY_PROFILE >= WLED_M5STACK_DISPLAY_PROFILE_CORES3 && - WLED_M5STACK_DISPLAY_PROFILE <= WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS, - "Invalid WLED_M5STACK_DISPLAY_PROFILE" -); - -static_assert( - WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_UNKNOWN || - WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_0 || - WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_1 || - WLED_M5STACK_DISPLAY_REVISION == WLED_M5STACK_DISPLAY_REVISION_V1_3, - "Invalid WLED_M5STACK_DISPLAY_REVISION" -); - -static_assert( - WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 0 || - WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1, - "WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY must be 0 or 1" -); - -static_assert( - WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORES3 || - WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1, - "Core2-family profiles require WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY=1" -); - -static_assert( - WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 0 || - WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORE2 || - WLED_M5STACK_DISPLAY_PROFILE == WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS, - "Diagnostic-only mode is reserved for Core2-family profiles" -); - -enum M5StackDisplayHardwareProfile : uint8_t { - M5STACK_DISPLAY_HARDWARE_CORES3 = WLED_M5STACK_DISPLAY_PROFILE_CORES3, - M5STACK_DISPLAY_HARDWARE_CORE2 = WLED_M5STACK_DISPLAY_PROFILE_CORE2, - M5STACK_DISPLAY_HARDWARE_CORE2_AWS = WLED_M5STACK_DISPLAY_PROFILE_CORE2_AWS -}; - -enum M5StackDisplayHardwareRevision : uint8_t { - M5STACK_DISPLAY_REVISION_UNKNOWN = WLED_M5STACK_DISPLAY_REVISION_UNKNOWN, - M5STACK_DISPLAY_REVISION_V1_0 = WLED_M5STACK_DISPLAY_REVISION_V1_0, - M5STACK_DISPLAY_REVISION_V1_1 = WLED_M5STACK_DISPLAY_REVISION_V1_1, - M5STACK_DISPLAY_REVISION_V1_3 = WLED_M5STACK_DISPLAY_REVISION_V1_3 -}; - -static constexpr M5StackDisplayHardwareProfile ACTIVE_M5STACK_DISPLAY_PROFILE = - static_cast( WLED_M5STACK_DISPLAY_PROFILE ); - -static constexpr M5StackDisplayHardwareRevision ACTIVE_M5STACK_DISPLAY_REVISION = - static_cast( WLED_M5STACK_DISPLAY_REVISION ); - -static constexpr bool ACTIVE_M5STACK_CORE2_DIAGNOSTIC_ONLY = - ( WLED_M5STACK_CORE2_DIAGNOSTIC_ONLY == 1 ); - -struct M5StackDisplayHardwareCapabilities { - const char* name; - uint8_t displayRotation; - bool displayRuntimeEnabled; - bool touchRuntimeEnabled; - bool brightnessRuntimeEnabled; - bool batteryRuntimeEnabled; - bool diagnosticProbeEnabled; -}; - -static constexpr M5StackDisplayHardwareCapabilities M5STACK_HARDWARE_CAPABILITIES[] = { - { - "M5Stack CoreS3", - 1, - true, - true, - true, - true, - false - }, - { - "M5Stack Core2", - 1, - false, - false, - false, - false, - true - }, - { - "M5Stack Core2 for AWS", - 1, - false, - false, - false, - false, - true - } -}; - -static constexpr const M5StackDisplayHardwareCapabilities& - ACTIVE_M5STACK_HARDWARE_CAPABILITIES = - M5STACK_HARDWARE_CAPABILITIES[WLED_M5STACK_DISPLAY_PROFILE]; +// CoreS3 is the only hardware target in this usermod. +static constexpr uint8_t CORES3_DISPLAY_ROTATION = 1; // =========================================================== // CoreS3 runtime battery telemetry @@ -818,323 +694,11 @@ static bool readCoreS3Axp2101Register( uint8_t reg, uint8_t& value ) { return true; } -bool M5StackDisplayHardwareBackend::probeI2CAddress( TwoWire& wire, uint8_t address ) { - wire.beginTransmission( address ); - - return wire.endTransmission() == 0; - } - -bool M5StackDisplayHardwareBackend::readI2CRegister8( TwoWire& wire, uint8_t address, uint8_t reg, uint8_t& value ) { - wire.beginTransmission( address ); - wire.write( reg ); - - if ( wire.endTransmission( false ) != 0 ) { - return false; - } - - size_t received = wire.requestFrom( address, (size_t)1, true ); - - if ( received != 1 || !wire.available() ) { - return false; - } - - value = (uint8_t)wire.read(); - - return true; - } - -void M5StackDisplayHardwareBackend::classifyCore2HardwareProbe( TwoWire& wire ) { - if ( hardwareProbe.address68 ) { - uint8_t chipId = 0; - - // BMI270 CHIP_ID register 0x00 returns 0x24. - if ( readI2CRegister8( wire, 0x68, 0x00, chipId ) && chipId == 0x24 ) { - hardwareProbe.imu = M5STACK_DETECTED_IMU_BMI270; - hardwareProbe.imuChipId = chipId; - } - else { - // MPU6886 WHO_AM_I register 0x75 returns 0x19. - if ( readI2CRegister8( wire, 0x68, 0x75, chipId ) && chipId == 0x19 ) { - hardwareProbe.imu = M5STACK_DETECTED_IMU_MPU6886; - hardwareProbe.imuChipId = chipId; - } - } - } - - if ( ACTIVE_M5STACK_DISPLAY_PROFILE == M5STACK_DISPLAY_HARDWARE_CORE2_AWS ) { - if ( hardwareProbe.address34 ) { - // Both documented Core2 for AWS generations use AXP192. - hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP192; - } - - if ( hardwareProbe.imu == M5STACK_DETECTED_IMU_BMI270 ) { - hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3; - } - else if ( hardwareProbe.imu == M5STACK_DETECTED_IMU_MPU6886 ) { - // Official legacy Core2 for AWS documentation identifies MPU6886, - // but does not assign the v1.0 / v1.1 name here. Keep it generic. - hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY; - } - } - else if ( ACTIVE_M5STACK_DISPLAY_PROFILE == M5STACK_DISPLAY_HARDWARE_CORE2 ) { - if ( hardwareProbe.address34 && hardwareProbe.address40 ) { - // Core2 v1.1 signature: AXP2101 + INA3221. - hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP2101; - hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_V1_1; - } - else if ( hardwareProbe.address34 ) { - hardwareProbe.pmu = M5STACK_DETECTED_PMU_AXP192; - hardwareProbe.variant = M5STACK_DETECTED_VARIANT_CORE2_LEGACY; - } - } - } - -void M5StackDisplayHardwareBackend::printCore2HardwareProbeResult() { - DEBUG_PRINTF( - "[CoreS3_Display] Hardware probe: %s, Variant=%s, PMU=%s, IMU=%s", - probeStateName(), - detectedVariantName(), - detectedPmuName(), - detectedImuName() - ); - - if ( hardwareProbe.imuChipId > 0 ) { - DEBUG_PRINTF( " (ID=0x%02X)", hardwareProbe.imuChipId ); - } - - DEBUG_PRINTLN(""); - - DEBUG_PRINTF( - "[CoreS3_Display] Detected revision: %s\n", - detectedRevisionName() - ); - - DEBUG_PRINTF( - "[CoreS3_Display] Core2 I2C signature: " - "34=%s 35=%s 38=%s 40=%s 51=%s 68=%s\n", - hardwareProbe.address34 ? "YES" : "NO", - hardwareProbe.address35 ? "YES" : "NO", - hardwareProbe.address38 ? "YES" : "NO", - hardwareProbe.address40 ? "YES" : "NO", - hardwareProbe.address51 ? "YES" : "NO", - hardwareProbe.address68 ? "YES" : "NO" - ); - } - M5StackDisplayHardwareBackend::M5StackDisplayHardwareBackend( M5GFX& displayRef ) : display( displayRef ) { } -const char* M5StackDisplayHardwareBackend::profileName() const { - return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.name; - } - -const char* M5StackDisplayHardwareBackend::revisionName() const { - switch ( ACTIVE_M5STACK_DISPLAY_REVISION ) { - case M5STACK_DISPLAY_REVISION_V1_0: - return "v1.0"; - - case M5STACK_DISPLAY_REVISION_V1_1: - return "v1.1"; - - case M5STACK_DISPLAY_REVISION_V1_3: - return "v1.3"; - - case M5STACK_DISPLAY_REVISION_UNKNOWN: - default: - return "UNKNOWN"; - } - } - -bool M5StackDisplayHardwareBackend::isDisplayRuntimeEnabled() const { - return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled; - } - -bool M5StackDisplayHardwareBackend::isCore2FamilyProfile() const { - return ACTIVE_M5STACK_HARDWARE_CAPABILITIES.diagnosticProbeEnabled; - } - -bool M5StackDisplayHardwareBackend::isCore2DiagnosticOnlyMode() const { - return isCore2FamilyProfile() && ACTIVE_M5STACK_CORE2_DIAGNOSTIC_ONLY; - } - -const char* M5StackDisplayHardwareBackend::runtimeModeName() const { - return isCore2DiagnosticOnlyMode() ? "CORE2 DIAGNOSTIC ONLY" : "DISPLAY ACTIVE"; - } - -const char* M5StackDisplayHardwareBackend::portStatusName() const { - if ( ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled ) { - return "CORES3 VERIFIED DISPLAY RUNTIME"; - } - - if ( isCore2DiagnosticOnlyMode() ) { - return "CORE2 PORT PREPARED - DIAGNOSTIC ONLY"; - } - - return "HARDWARE RUNTIME BLOCKED"; - } - -const char* M5StackDisplayHardwareBackend::probeStateName() const { - switch ( hardwareProbe.state ) { - case M5STACK_HARDWARE_PROBE_NOT_REQUIRED: - return "NOT REQUIRED"; - - case M5STACK_HARDWARE_PROBE_COMPLETE: - return "COMPLETE"; - - case M5STACK_HARDWARE_PROBE_FAILED: - return "FAILED"; - - case M5STACK_HARDWARE_PROBE_NOT_RUN: - default: - return "NOT RUN"; - } - } - -const char* M5StackDisplayHardwareBackend::detectedPmuName() const { - switch ( hardwareProbe.pmu ) { - case M5STACK_DETECTED_PMU_AXP192: - return "AXP192 signature"; - - case M5STACK_DETECTED_PMU_AXP2101: - return "AXP2101 signature"; - - case M5STACK_DETECTED_PMU_UNKNOWN: - default: - return "UNKNOWN"; - } - } - -const char* M5StackDisplayHardwareBackend::detectedImuName() const { - switch ( hardwareProbe.imu ) { - case M5STACK_DETECTED_IMU_MPU6886: - return "MPU6886"; - - case M5STACK_DETECTED_IMU_BMI270: - return "BMI270"; - - case M5STACK_DETECTED_IMU_UNKNOWN: - default: - return "UNKNOWN"; - } - } - -const char* M5StackDisplayHardwareBackend::detectedVariantName() const { - switch ( hardwareProbe.variant ) { - case M5STACK_DETECTED_VARIANT_CORE2_LEGACY: - return "Core2 legacy signature"; - - case M5STACK_DETECTED_VARIANT_CORE2_V1_1: - return "Core2 v1.1 signature"; - - case M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY: - return "Core2 for AWS MPU6886 generation"; - - case M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3: - return "Core2 for AWS v1.3 signature"; - - case M5STACK_DETECTED_VARIANT_UNKNOWN: - default: - return "UNKNOWN"; - } - } - -const char* M5StackDisplayHardwareBackend::detectedRevisionName() const { - switch ( hardwareProbe.variant ) { - case M5STACK_DETECTED_VARIANT_CORE2_V1_1: - return "v1.1 signature"; - - case M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3: - return "v1.3 signature"; - - case M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY: - return "Legacy MPU6886 generation; exact revision unknown"; - - case M5STACK_DETECTED_VARIANT_CORE2_LEGACY: - return "Legacy generation; exact revision unknown"; - - case M5STACK_DETECTED_VARIANT_UNKNOWN: - default: - return "UNKNOWN"; - } - } - -bool M5StackDisplayHardwareBackend::isProbeComplete() const { - return hardwareProbe.state == M5STACK_HARDWARE_PROBE_COMPLETE; - } - -bool M5StackDisplayHardwareBackend::hasI2CAddress( uint8_t address ) const { - switch ( address ) { - case 0x34: - return hardwareProbe.address34; - - case 0x35: - return hardwareProbe.address35; - - case 0x38: - return hardwareProbe.address38; - - case 0x40: - return hardwareProbe.address40; - - case 0x51: - return hardwareProbe.address51; - - case 0x68: - return hardwareProbe.address68; - - default: - return false; - } - } - -void M5StackDisplayHardwareBackend::runDiagnostics() { - hardwareProbe = HardwareProbeResult(); - - if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.diagnosticProbeEnabled ) { - hardwareProbe.state = M5STACK_HARDWARE_PROBE_NOT_REQUIRED; - - return; - } - - if ( - i2c_sda != CORE2_INTERNAL_I2C_SDA || - i2c_scl != CORE2_INTERNAL_I2C_SCL - ) { - hardwareProbe.state = M5STACK_HARDWARE_PROBE_FAILED; - - DEBUG_PRINTLN( F( "[CoreS3_Display] ERROR: Core2 diagnostic I2C must use WLED global Wire on GPIO21/22" ) ); - - return; - } - - delay( 10 ); - - hardwareProbe.address34 = probeI2CAddress( Wire, 0x34 ); - hardwareProbe.address35 = probeI2CAddress( Wire, 0x35 ); - hardwareProbe.address38 = probeI2CAddress( Wire, 0x38 ); - hardwareProbe.address40 = probeI2CAddress( Wire, 0x40 ); - hardwareProbe.address51 = probeI2CAddress( Wire, 0x51 ); - hardwareProbe.address68 = probeI2CAddress( Wire, 0x68 ); - - classifyCore2HardwareProbe( Wire ); - - hardwareProbe.state = M5STACK_HARDWARE_PROBE_COMPLETE; - - printCore2HardwareProbeResult(); - } - bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int16_t& screenHeight, bool& touchReady ) { - if ( !isDisplayRuntimeEnabled() ) { - DEBUG_PRINTF( - "[CoreS3_Display] Hardware profile not enabled for Display runtime: %s (%s)\n", - profileName(), - revisionName() - ); - - return false; - } - #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) CoreS3DisplayGlobalWireRuntime& coreS3Runtime = coreS3DisplayGlobalWireRuntime(); @@ -1160,7 +724,7 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int return false; #endif - display.setRotation( ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRotation ); + display.setRotation( CORES3_DISPLAY_ROTATION ); screenWidth = display.width(); screenHeight = display.height(); @@ -1179,19 +743,22 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int } bool M5StackDisplayHardwareBackend::readTouch( int16_t& touchX, int16_t& touchY ) { - if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.touchRuntimeEnabled ) { - return false; - } - +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) return ( display.getTouch( &touchX, &touchY ) > 0 ); +#else + (void)touchX; + (void)touchY; + + return false; +#endif } void M5StackDisplayHardwareBackend::writeBrightness( uint8_t value ) { - if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.brightnessRuntimeEnabled ) { - return; - } - +#if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) display.setBrightness( value ); +#else + (void)value; +#endif } @@ -1201,7 +768,6 @@ bool M5StackDisplayHardwareBackend::readDisplayRgb565( int16_t height ) { if ( - !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.displayRuntimeEnabled || pixels == nullptr || width <= 0 || height <= 0 @@ -1228,14 +794,9 @@ bool M5StackDisplayHardwareBackend::readDisplayRgb565( bool M5StackDisplayHardwareBackend::readBatteryStatus( M5StackBatteryStatus& status ) { status = M5StackBatteryStatus(); - if ( !ACTIVE_M5STACK_HARDWARE_CAPABILITIES.batteryRuntimeEnabled ) { - return false; - } - - if ( ACTIVE_M5STACK_DISPLAY_PROFILE != M5STACK_DISPLAY_HARDWARE_CORES3 ) { - return false; - } - +#if !defined(WLED_M5STACK_CORES3) || !defined(CONFIG_IDF_TARGET_ESP32S3) + return false; +#else uint8_t status1 = 0; uint8_t status2 = 0; uint8_t batteryPercent = 0; @@ -1264,4 +825,5 @@ bool M5StackDisplayHardwareBackend::readBatteryStatus( M5StackBatteryStatus& sta status.level = batteryPercent; return true; +#endif } diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h index df28f0f7be..7f90098f80 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h @@ -1,13 +1,11 @@ #pragma once #include -#include -// M5Stack display hardware boundary. +// CoreS3 display hardware boundary. // -// This class contains only board-dependent Display / Touch / brightness -// access and Core2-family read-only hardware diagnostics. -// UI/WLED behavior remains in CoreS3_Display.cpp. +// This class contains only CoreS3 board-dependent Display / Touch / +// brightness / battery access. UI/WLED behavior remains in CoreS3_Display.cpp. struct M5StackBatteryStatus { bool available = false; bool present = false; @@ -18,86 +16,12 @@ struct M5StackBatteryStatus { class M5StackDisplayHardwareBackend { private: - enum M5StackHardwareProbeState : uint8_t { - M5STACK_HARDWARE_PROBE_NOT_REQUIRED = 0, - M5STACK_HARDWARE_PROBE_NOT_RUN, - M5STACK_HARDWARE_PROBE_COMPLETE, - M5STACK_HARDWARE_PROBE_FAILED - }; - - enum M5StackDetectedPmu : uint8_t { - M5STACK_DETECTED_PMU_UNKNOWN = 0, - M5STACK_DETECTED_PMU_AXP192, - M5STACK_DETECTED_PMU_AXP2101 - }; - - enum M5StackDetectedImu : uint8_t { - M5STACK_DETECTED_IMU_UNKNOWN = 0, - M5STACK_DETECTED_IMU_MPU6886, - M5STACK_DETECTED_IMU_BMI270 - }; - - enum M5StackDetectedVariant : uint8_t { - M5STACK_DETECTED_VARIANT_UNKNOWN = 0, - M5STACK_DETECTED_VARIANT_CORE2_LEGACY, - M5STACK_DETECTED_VARIANT_CORE2_V1_1, - M5STACK_DETECTED_VARIANT_CORE2_AWS_LEGACY, - M5STACK_DETECTED_VARIANT_CORE2_AWS_V1_3 - }; - - struct HardwareProbeResult { - M5StackHardwareProbeState state = M5STACK_HARDWARE_PROBE_NOT_RUN; - M5StackDetectedPmu pmu = M5STACK_DETECTED_PMU_UNKNOWN; - M5StackDetectedImu imu = M5STACK_DETECTED_IMU_UNKNOWN; - M5StackDetectedVariant variant = M5STACK_DETECTED_VARIANT_UNKNOWN; - - bool address34 = false; - bool address35 = false; - bool address38 = false; - bool address40 = false; - bool address51 = false; - bool address68 = false; - - uint8_t imuChipId = 0; - }; - - static constexpr int CORE2_INTERNAL_I2C_SDA = 21; - static constexpr int CORE2_INTERNAL_I2C_SCL = 22; - M5GFX& display; - HardwareProbeResult hardwareProbe; - - bool probeI2CAddress( TwoWire& wire, uint8_t address ); - bool readI2CRegister8( TwoWire& wire, uint8_t address, uint8_t reg, uint8_t& value ); - - void classifyCore2HardwareProbe( TwoWire& wire ); - void printCore2HardwareProbeResult(); public: explicit M5StackDisplayHardwareBackend( M5GFX& displayRef ); - const char* profileName() const; - const char* revisionName() const; - - bool isDisplayRuntimeEnabled() const; - bool isCore2FamilyProfile() const; - bool isCore2DiagnosticOnlyMode() const; - - const char* runtimeModeName() const; - const char* portStatusName() const; - - const char* probeStateName() const; - const char* detectedPmuName() const; - const char* detectedImuName() const; - const char* detectedVariantName() const; - const char* detectedRevisionName() const; - - bool isProbeComplete() const; - bool hasI2CAddress( uint8_t address ) const; - - void runDiagnostics(); - bool initializeDisplay( int16_t& screenWidth, int16_t& screenHeight, @@ -116,7 +40,7 @@ class M5StackDisplayHardwareBackend { int16_t height ); - // Read battery state through the active board-specific hardware backend. + // Read battery state through the CoreS3 hardware backend. // The UI consumes only this generic status object; PMIC/register details // stay outside CoreS3_Display.cpp. bool readBatteryStatus( M5StackBatteryStatus& status ); From ab934ea0b40bc2faf9b0aa75b199200b67d87971 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Fri, 2 Oct 2026 22:47:59 +0900 Subject: [PATCH 28/35] Avoid strip suspend ownership and mark AI-generated CoreS3 code --- usermods/CoreS3_Display/CoreS3_Display.cpp | 4 + .../M5StackDisplayHardwareBackend.cpp | 4 + .../M5StackDisplayHardwareBackend.h | 4 + .../M5StackDisplayTouchContext.h | 4 + .../M5StackDisplayTouchHelpers.h | 4 + .../CoreS3_Display/M5StackDisplayTouchState.h | 4 + .../M5StackDisplayTouchStateMachine.inc | 4 + usermods/CoreS3_Display/M5StackDisplayUI.h | 4 + usermods/CoreS3_Power/CoreS3_Power.cpp | 79 +++++++++++++------ 9 files changed, 85 insertions(+), 26 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 934b6ba5af..01f63ae2e8 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -12,6 +12,8 @@ #include "CoreS3_WLED_Logo.h" +// AI: below section was generated by an AI + // =========================================================== // M5Stack CoreS3 Display Controller Usermod // @@ -7613,3 +7615,5 @@ class CoreS3DisplayUsermod : public Usermod { static CoreS3DisplayUsermod coreS3DisplayUsermod; REGISTER_USERMOD(coreS3DisplayUsermod); + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 750a7b6ab1..93359a5194 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -7,6 +7,8 @@ #include +// AI: below section was generated by an AI + #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) #include #include @@ -827,3 +829,5 @@ bool M5StackDisplayHardwareBackend::readBatteryStatus( M5StackBatteryStatus& sta return true; #endif } + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h index 7f90098f80..c3fa3430f5 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h @@ -2,6 +2,8 @@ #include +// AI: below section was generated by an AI + // CoreS3 display hardware boundary. // // This class contains only CoreS3 board-dependent Display / Touch / @@ -45,3 +47,5 @@ class M5StackDisplayHardwareBackend { // stay outside CoreS3_Display.cpp. bool readBatteryStatus( M5StackBatteryStatus& status ); }; + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchContext.h b/usermods/CoreS3_Display/M5StackDisplayTouchContext.h index d9722870f2..804a9ec53a 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchContext.h +++ b/usermods/CoreS3_Display/M5StackDisplayTouchContext.h @@ -2,6 +2,8 @@ #include "M5StackDisplayTouchState.h" +// AI: below section was generated by an AI + // Explicit input boundary for the Touch state-machine entry points. // // The contexts do not own data. They only reference the existing @@ -18,3 +20,5 @@ struct M5StackTouchReleaseContext { M5StackTouchRuntimeState& state; unsigned long now; }; + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h index 008586af43..2326184410 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h +++ b/usermods/CoreS3_Display/M5StackDisplayTouchHelpers.h @@ -3,6 +3,8 @@ #include "M5StackDisplayUI.h" #include "M5StackDisplayTouchState.h" +// AI: below section was generated by an AI + // Stateless helpers shared by the M5Stack controller Touch layer. // // These functions do not own runtime state, access hardware, draw UI, @@ -83,3 +85,5 @@ inline bool isTouchTargetPair( M5StackTouchTarget target, M5StackTouchTarget fir } } // namespace M5StackDisplayTouchHelpers + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchState.h b/usermods/CoreS3_Display/M5StackDisplayTouchState.h index db28a4e595..7b20949721 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchState.h +++ b/usermods/CoreS3_Display/M5StackDisplayTouchState.h @@ -2,6 +2,8 @@ #include +// AI: below section was generated by an AI + // Common M5Stack controller Touch state types. // // These types contain no hardware access and no WLED operations. @@ -191,3 +193,5 @@ struct M5StackTouchRuntimeState { // before the parser-sensitive final position. bool intellisenseTailGuard = false; }; + +// AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc b/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc index 5b4bef126d..5891934f3a 100644 --- a/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc +++ b/usermods/CoreS3_Display/M5StackDisplayTouchStateMachine.inc @@ -1,3 +1,5 @@ + // AI: below section was generated by an AI + // ========================================================= // Shared paired-touch helpers // ========================================================= @@ -1718,3 +1720,5 @@ handleTouchRelease( releaseContext ); } + + // AI: end diff --git a/usermods/CoreS3_Display/M5StackDisplayUI.h b/usermods/CoreS3_Display/M5StackDisplayUI.h index e5e125ddf0..99c65725d9 100644 --- a/usermods/CoreS3_Display/M5StackDisplayUI.h +++ b/usermods/CoreS3_Display/M5StackDisplayUI.h @@ -2,6 +2,8 @@ #include +// AI: below section was generated by an AI + // Common M5Stack controller UI geometry. // // The 320 x 240 hit areas below are the hardware-verified CoreS3 @@ -66,3 +68,5 @@ static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_OPEN = { 48, 186, static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_PREV = { 8, 116, 80, 52 }; static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_NEXT = { 232, 116, 80, 52 }; static constexpr M5StackTouchRect M5STACK_TOUCH_BOOT_HOLD = { 48, 184, 224, 56 }; + +// AI: end diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index b20477129b..d430505bfd 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -1,5 +1,7 @@ #include "wled.h" +// AI: below section was generated by an AI + #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) #include @@ -13,7 +15,7 @@ * CoreS3 LED settings save guard: * - Intercepts POST /settings/leds before WLED registers its generic handler. * - Defers the request so BLACK is rendered from the normal WLED loop context. - * - Keeps the old bus suspended after BLACK while WLED parses the new settings. + * - Keeps the old bus physically BLACK without taking ownership of WLED's strip suspend state. * - Lets the standard WLED bus rebuild/config serialization path run unchanged. * - Arms a standard WLED software reboot from the following loop iteration. * @@ -21,7 +23,7 @@ * - AW9523B external-5V enable writes (BOOST then BUS). * - Runtime External 5V re-assert after M5GFX initialization. * - AXP2101 power-key IRQ handling for Safe Shutdown. - * - Safe Physical Shutdown LED BLACK / suspend / cancel-and-restore logic. + * - Safe Physical Shutdown LED BLACK / cancel-and-restore logic. * - AXP2101 DCDC3 Always-PWM stability setting. * - Concise boot reset / PWRON / PWROFF cause reporting. */ @@ -177,7 +179,6 @@ class CoreS3PowerUsermod : public Usermod }; volatile LedSettingsSaveState ledSettingsSaveState = LedSettingsSaveState::IDLE; - uint8_t ledSettingsSavedStripBrightness = 0; bool ledSettingsHandlerRegistered = false; @@ -552,35 +553,41 @@ class CoreS3PowerUsermod : public Usermod case LedSettingsSaveState::BLACK_PENDING: { - // Physical shutdown already owns the LED output. If this ever overlaps + // Physical shutdown already owns the BLACK output. If this overlaps // with a settings save, keep the request deferred until that sequence // has finished or the device powers off. if (safeShutdownBlankActive) return; - ledSettingsSavedStripBrightness = strip.getBrightness(); + const uint8_t previousStripBrightness = strip.getBrightness(); DEBUG_PRINTF( "[CoreS3_Power][LED] Settings save: BLACK old bus before re-init (len=%u, bri=%u)\n", strip.getLengthPhysical(), - ledSettingsSavedStripBrightness + previousStripBrightness ); - // Use the same physical BLACK path already validated by Safe Shutdown. - // Keep WLED's logical bri untouched; only the physical strip brightness - // is forced to zero for this final old-bus frame. + // Keep the old physical bus BLACK without touching WS2812FX suspend + // state. WLED owns strip.suspend()/resume() for its own configuration + // and segment lifecycle operations. + // + // WLED's logical bri remains unchanged. A physical brightness of zero + // also keeps normal strip.service() output BLACK until the bus is + // rebuilt or the old output is explicitly restored. strip.waitForIt(); strip.setBrightness(0, true); strip.show(); waitForLedOutputComplete(); - strip.suspend(); - strip.waitForIt(); ledSettingsSaveState = LedSettingsSaveState::BLACK_READY; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: old bus BLACK and suspended")); + DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: old bus BLACK; waiting for settings parse/re-init")); return; } case LedSettingsSaveState::REBOOT_ARM_PENDING: + // The bus may already have been rebuilt by WLED::loop(). Keep its + // physical output BLACK until the standard WLED reboot path runs. + strip.setBrightness(0, true); + // Do not set doReboot in the AsyncWebServer callback. Arming it here // guarantees WLED's main loop gets a chance to consume doInitBusses and // serialize the new bus configuration before the standard reset check. @@ -590,18 +597,27 @@ class CoreS3PowerUsermod : public Usermod return; case LedSettingsSaveState::RESTORE_PENDING: - // A valid LED settings POST normally creates pending bus configs. If it - // did not, no re-init/reboot is needed, so restore the old physical - // output that was blanked while the request was deferred. - strip.resume(); - strip.setBrightness(ledSettingsSavedStripBrightness, true); + { + // Safe shutdown owns the BLACK output while active. If it overlaps a + // settings save that does not require bus re-init, defer restoration + // until the physical power-key sequence has completed or been canceled. + if (safeShutdownBlankActive) return; + + // No bus rebuild was requested. Restore the current logical WLED + // brightness directly; this usermod does not own WS2812FX suspend state. + const uint8_t restoreBrightness = bri; + + strip.setBrightness(restoreBrightness, true); strip.show(); waitForLedOutputComplete(); - strip.trigger(); ledSettingsSaveState = LedSettingsSaveState::IDLE; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: no bus re-init; old output restored")); + DEBUG_PRINTF( + "[CoreS3_Power][LED] Settings save: no bus re-init; old output restored (bri=%u)\n", + restoreBrightness + ); return; + } } } @@ -654,8 +670,8 @@ class CoreS3PowerUsermod : public Usermod return; case LedSettingsSaveState::BLACK_READY: - // The old physical bus is now BLACK and suspended. Delegate parsing - // and validation to the unchanged WLED settings implementation. + // The old physical bus is now BLACK. Delegate parsing and validation + // to the unchanged WLED settings implementation. serveSettings(request, true); if (doInitBusses) { @@ -703,17 +719,17 @@ class CoreS3PowerUsermod : public Usermod strip.getBrightness() ); + // Keep physical output BLACK without taking ownership of WLED's strip + // suspend state. strip.waitForIt(); strip.setBrightness(0, true); strip.show(); waitForLedOutputComplete(); - strip.suspend(); - strip.waitForIt(); safeShutdownBlankActive = true; lastShutdownBlackRefresh = millis(); - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: BLACK frame sent and strip suspended")); + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: BLACK frame sent; physical brightness held at 0")); } void maintainSafeShutdownBlank(unsigned long now) @@ -731,8 +747,18 @@ class CoreS3PowerUsermod : public Usermod { if (!safeShutdownBlankActive) return; + // If an LED settings save is already in progress, that state machine owns + // the requirement to keep the physical output BLACK. Do not restore + // brightness here. + if (ledSettingsSaveState != LedSettingsSaveState::IDLE) { + safeShutdownBlankActive = false; + safeShutdownLastCanceled = true; + + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: LED settings save keeps output BLACK")); + return; + } + DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); - strip.resume(); uint8_t restoreBrightness = bri; if (restoreBrightness == 0 && savedLogicalBrightness > 0) { @@ -745,7 +771,6 @@ class CoreS3PowerUsermod : public Usermod safeShutdownBlankActive = false; safeShutdownLastCanceled = true; - strip.trigger(); DEBUG_PRINTF("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); } @@ -1050,3 +1075,5 @@ static CoreS3PowerUsermod coreS3PowerUsermod; REGISTER_USERMOD(coreS3PowerUsermod); #endif // WLED_M5STACK_CORES3 && CONFIG_IDF_TARGET_ESP32S3 + +// AI: end From 293139d646310122d4755bc481e38f28dc65dfd3 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sat, 3 Oct 2026 23:54:16 +0900 Subject: [PATCH 29/35] Use default Tasmota platform for CoreS3 --- usermods/CoreS3_Display/platformio_override.ini.sample | 4 ---- 1 file changed, 4 deletions(-) diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 1a55d965b4..9380a65988 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -34,10 +34,6 @@ extends = env:esp32s3dev_16MB_opi board_build.arduino.memory_type = qio_qspi board_build.flash_mode = qio -; Current WLED pioarduino / ESP-IDF 5 build family. -platform = ${esp32_idf_V5.platform_pioarduino} -platform_packages = ${esp32_idf_V5.platform_packages_pioarduino} - ; Preserve normal WLED build scripts. extra_scripts = ${scripts_defaults.extra_scripts} From bb2a6a5c79e7710075eda868b5b72ddf1417a0b8 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Mon, 5 Oct 2026 00:05:57 +0900 Subject: [PATCH 30/35] Remove CoreS3 LED settings save workaround --- usermods/CoreS3_Power/CoreS3_Power.cpp | 195 ------------------------- 1 file changed, 195 deletions(-) diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index d430505bfd..e2cb25fecc 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -12,13 +12,6 @@ * * Production baseline for M5Stack CoreS3 power handling. * - * CoreS3 LED settings save guard: - * - Intercepts POST /settings/leds before WLED registers its generic handler. - * - Defers the request so BLACK is rendered from the normal WLED loop context. - * - Keeps the old bus physically BLACK without taking ownership of WLED's strip suspend state. - * - Lets the standard WLED bus rebuild/config serialization path run unchanged. - * - Arms a standard WLED software reboot from the following loop iteration. - * * Preserved validated power behavior: * - AW9523B external-5V enable writes (BOOST then BUS). * - Runtime External 5V re-assert after M5GFX initialization. @@ -59,7 +52,6 @@ class CoreS3PowerUsermod : public Usermod static constexpr uint8_t AW9523B_ADDR = 0x58; static constexpr uint8_t AXP2101_ADDR = 0x34; - static constexpr uint8_t AXP2101_REG_CHIP_ID = 0x03; static constexpr uint8_t AXP2101_REG_PWRON_STATUS = 0x20; static constexpr uint8_t AXP2101_REG_PWROFF_STATUS = 0x21; @@ -165,23 +157,6 @@ class CoreS3PowerUsermod : public Usermod uint8_t dcdcModeBefore = 0; uint8_t dcdcModeAfter = 0; - // LED & Hardware settings save guard. - // - // The HTTP callback only changes this small state machine and delegates back - // to WLED. Physical LED I/O is kept in the normal WLED loop context. - enum class LedSettingsSaveState : uint8_t { - IDLE = 0, - BLACK_PENDING, - BLACK_READY, - REBOOT_ARM_PENDING, - WAIT_REBOOT, - RESTORE_PENDING - }; - - volatile LedSettingsSaveState ledSettingsSaveState = LedSettingsSaveState::IDLE; - bool ledSettingsHandlerRegistered = false; - - const char* resetReasonText(esp_reset_reason_t reason) { switch (reason) { @@ -541,162 +516,6 @@ class CoreS3PowerUsermod : public Usermod } } - // Process the LED settings save guard only from WLED's normal loop context. - // This keeps physical strip I/O out of the AsyncWebServer callback context. - void serviceLedSettingsSaveGuard() - { - switch (ledSettingsSaveState) { - case LedSettingsSaveState::IDLE: - case LedSettingsSaveState::BLACK_READY: - case LedSettingsSaveState::WAIT_REBOOT: - return; - - case LedSettingsSaveState::BLACK_PENDING: - { - // Physical shutdown already owns the BLACK output. If this overlaps - // with a settings save, keep the request deferred until that sequence - // has finished or the device powers off. - if (safeShutdownBlankActive) return; - - const uint8_t previousStripBrightness = strip.getBrightness(); - - DEBUG_PRINTF( - "[CoreS3_Power][LED] Settings save: BLACK old bus before re-init (len=%u, bri=%u)\n", - strip.getLengthPhysical(), - previousStripBrightness - ); - - // Keep the old physical bus BLACK without touching WS2812FX suspend - // state. WLED owns strip.suspend()/resume() for its own configuration - // and segment lifecycle operations. - // - // WLED's logical bri remains unchanged. A physical brightness of zero - // also keeps normal strip.service() output BLACK until the bus is - // rebuilt or the old output is explicitly restored. - strip.waitForIt(); - strip.setBrightness(0, true); - strip.show(); - waitForLedOutputComplete(); - - ledSettingsSaveState = LedSettingsSaveState::BLACK_READY; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save: old bus BLACK; waiting for settings parse/re-init")); - return; - } - - case LedSettingsSaveState::REBOOT_ARM_PENDING: - // The bus may already have been rebuilt by WLED::loop(). Keep its - // physical output BLACK until the standard WLED reboot path runs. - strip.setBrightness(0, true); - - // Do not set doReboot in the AsyncWebServer callback. Arming it here - // guarantees WLED's main loop gets a chance to consume doInitBusses and - // serialize the new bus configuration before the standard reset check. - doReboot = true; - ledSettingsSaveState = LedSettingsSaveState::WAIT_REBOOT; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] Bus re-init detected; safe reboot armed for post-save reset")); - return; - - case LedSettingsSaveState::RESTORE_PENDING: - { - // Safe shutdown owns the BLACK output while active. If it overlaps a - // settings save that does not require bus re-init, defer restoration - // until the physical power-key sequence has completed or been canceled. - if (safeShutdownBlankActive) return; - - // No bus rebuild was requested. Restore the current logical WLED - // brightness directly; this usermod does not own WS2812FX suspend state. - const uint8_t restoreBrightness = bri; - - strip.setBrightness(restoreBrightness, true); - strip.show(); - waitForLedOutputComplete(); - - ledSettingsSaveState = LedSettingsSaveState::IDLE; - DEBUG_PRINTF( - "[CoreS3_Power][LED] Settings save: no bus re-init; old output restored (bri=%u)\n", - restoreBrightness - ); - return; - } - } - } - - // Local copy of WLED's settings-origin check. The upstream helpers live as - // static functions in wled_server.cpp, so a separately compiled usermod - // cannot call them directly. Keep this scoped to the LED settings guard. - bool isLocalLedSettingsClient(const IPAddress& client) - { - auto inSubnetLocal = [](const IPAddress& ip, const IPAddress& subnet, const IPAddress& mask) { - return (((uint32_t)ip & (uint32_t)mask) == ((uint32_t)subnet & (uint32_t)mask)); - }; - - return - inSubnetLocal(client, IPAddress(10, 0, 0, 0), IPAddress(255, 0, 0, 0)) || - inSubnetLocal(client, IPAddress(192, 168, 0, 0), IPAddress(255, 255, 0, 0)) || - inSubnetLocal(client, IPAddress(172, 16, 0, 0), IPAddress(255, 240, 0, 0)) || - (inSubnetLocal(client, IPAddress(4, 3, 2, 0), IPAddress(255, 255, 255, 0)) && apActive) || - inSubnetLocal(client, WLEDNetwork.localIP(), WLEDNetwork.subnetMask()); - } - - // Register before WLED::initServer(). UsermodManager::setup() runs earlier in - // WLED::setup(), so this exact /settings/leds POST handler gets first chance - // to safely blank the old CoreS3 LED bus. WLED's own serveSettings() remains - // the single parser/validator for the actual settings request. - void registerLedSettingsSaveHandler() - { - if (ledSettingsHandlerRegistered) return; - ledSettingsHandlerRegistered = true; - - server.on(F("/settings/leds"), HTTP_POST, [this](AsyncWebServerRequest* request) { - // Preserve WLED's normal access-control/PIN behavior. Do not blank LEDs - // for a request that cannot yet be treated as a real LED settings save. - if ( - !isLocalLedSettingsClient(request->client()->remoteIP()) || - (!correctPIN && strlen(settingsPIN) > 0) - ) { - serveSettings(request, true); - return; - } - - switch (ledSettingsSaveState) { - case LedSettingsSaveState::IDLE: - ledSettingsSaveState = LedSettingsSaveState::BLACK_PENDING; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] LED settings POST detected; deferring for old-bus BLACK")); - request->deferResponse(); - return; - - case LedSettingsSaveState::BLACK_PENDING: - request->deferResponse(); - return; - - case LedSettingsSaveState::BLACK_READY: - // The old physical bus is now BLACK. Delegate parsing and validation - // to the unchanged WLED settings implementation. - serveSettings(request, true); - - if (doInitBusses) { - // Arm reboot from Usermod::loop(), never from this callback. This - // avoids racing WLED's doInitBusses/configNeedsWrite main-loop path. - ledSettingsSaveState = LedSettingsSaveState::REBOOT_ARM_PENDING; - DEBUG_PRINTLN(F("[CoreS3_Power][LED] LED settings accepted; standard bus re-init pending")); - } else { - ledSettingsSaveState = LedSettingsSaveState::RESTORE_PENDING; - } - return; - - case LedSettingsSaveState::REBOOT_ARM_PENDING: - case LedSettingsSaveState::WAIT_REBOOT: - case LedSettingsSaveState::RESTORE_PENDING: - // A previous save is still being completed. Keep any duplicate POST - // deferred rather than allowing overlapping bus lifecycle operations. - request->deferResponse(); - return; - } - }); - - DEBUG_PRINTLN(F("[CoreS3_Power][LED] Settings save guard: ARMED")); - } - void beginSafeShutdownBlank(uint8_t triggerStatus) { if (safeShutdownBlankActive) return; @@ -747,17 +566,6 @@ class CoreS3PowerUsermod : public Usermod { if (!safeShutdownBlankActive) return; - // If an LED settings save is already in progress, that state machine owns - // the requirement to keep the physical output BLACK. Do not restore - // brightness here. - if (ledSettingsSaveState != LedSettingsSaveState::IDLE) { - safeShutdownBlankActive = false; - safeShutdownLastCanceled = true; - - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: LED settings save keeps output BLACK")); - return; - } - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); uint8_t restoreBrightness = bri; @@ -907,8 +715,6 @@ class CoreS3PowerUsermod : public Usermod DEBUG_PRINTF("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); DEBUG_PRINTF("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); - registerLedSettingsSaveHandler(); - coreS3PowerInitializationCompleteState = true; DEBUG_PRINTLN(F("[CoreS3_Power] Initialization complete")); @@ -917,7 +723,6 @@ class CoreS3PowerUsermod : public Usermod void loop() override { - serviceLedSettingsSaveGuard(); servicePhysicalPowerKey(); serviceRuntimeExternal5VEnable(); serviceDcdc3StabilityMode(); From 05ae69d81e4b65853133b52ee9912f8664ec6489 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Wed, 7 Oct 2026 23:12:36 +0900 Subject: [PATCH 31/35] Remove CoreS3 browser screenshot endpoint --- usermods/CoreS3_Display/CoreS3_Display.cpp | 329 ------------------ .../M5StackDisplayHardwareBackend.cpp | 29 -- .../M5StackDisplayHardwareBackend.h | 9 - usermods/CoreS3_Display/readme.md | 27 +- usermods/CoreS3_Display/readme_jp.md | 27 +- 5 files changed, 2 insertions(+), 419 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 01f63ae2e8..868f451eac 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -1,8 +1,6 @@ #include "wled.h" #include #include -#include -#include #include "M5StackDisplayHardwareBackend.h" #include "M5StackDisplayUI.h" @@ -32,7 +30,6 @@ // - Startup animation // - Display sleep / wake // - Runtime synchronization with WLED state -// - Browser screenshot endpoint (/cores3/screenshot.bmp) // // Architecture // UI and WLED-state logic are kept separate from the thin @@ -75,27 +72,6 @@ class CoreS3DisplayUsermod : public Usermod { unsigned long lastUpdate = 0; - // ========================================================= - // Browser Screenshot - // ========================================================= - // - // A GET request to /cores3/screenshot.bmp returns a one-shot - // 24-bit BMP capture of the current 320 x 240 LCD contents. - // - // The frame and BMP buffers are allocated only while a request - // is active and are placed in PSRAM. The finished BMP buffer is - // retained by the asynchronous HTTP response until transmission - // completes, then released automatically. - // - // This is intentionally a still-image endpoint. It does not - // continuously stream frames and therefore does not add normal - // runtime Display/Wi-Fi load when unused. - // ========================================================= - - bool screenshotCaptureInProgress = false; - - static constexpr size_t SCREENSHOT_BMP_HEADER_SIZE = 54; - // ========================================================= // Network access state // ========================================================= @@ -6722,300 +6698,6 @@ class CoreS3DisplayUsermod : public Usermod { } } - static void writeBmp16( uint8_t* destination, uint16_t value ) { - destination[0] = (uint8_t)( value & 0xFF ); - destination[1] = (uint8_t)( ( value >> 8 ) & 0xFF ); - } - - static void writeBmp32( uint8_t* destination, uint32_t value ) { - destination[0] = (uint8_t)( value & 0xFF ); - destination[1] = (uint8_t)( ( value >> 8 ) & 0xFF ); - destination[2] = (uint8_t)( ( value >> 16 ) & 0xFF ); - destination[3] = (uint8_t)( ( value >> 24 ) & 0xFF ); - } - - bool buildScreenshotBmp( - uint8_t*& bmpData, - size_t& bmpSize - ) { - bmpData = nullptr; - bmpSize = 0; - - if ( !displayReady || screenWidth <= 0 || screenHeight <= 0 ) { - return false; - } - - const size_t width = (size_t)screenWidth; - const size_t height = (size_t)screenHeight; - const size_t pixelCount = width * height; - const size_t frameBytes = pixelCount * sizeof(uint16_t); - - // 24-bit BMP rows are aligned to 4-byte boundaries. - const size_t rowBytes = width * 3; - const size_t rowStride = ( rowBytes + 3 ) & ~((size_t)3); - const size_t imageBytes = rowStride * height; - const size_t totalBytes = SCREENSHOT_BMP_HEADER_SIZE + imageBytes; - - uint16_t* frameBuffer = static_cast( - heap_caps_malloc( - frameBytes, - MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT - ) - ); - - if ( frameBuffer == nullptr ) { - return false; - } - - uint8_t* outputBuffer = static_cast( - heap_caps_malloc( - totalBytes, - MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT - ) - ); - - if ( outputBuffer == nullptr ) { - heap_caps_free( frameBuffer ); - return false; - } - - if ( - !hardwareBackend.readDisplayRgb565( - frameBuffer, - screenWidth, - screenHeight - ) - ) { - heap_caps_free( outputBuffer ); - heap_caps_free( frameBuffer ); - - return false; - } - - memset( outputBuffer, 0, totalBytes ); - - // ------------------------------------------------------- - // BITMAPFILEHEADER (14 bytes) - // ------------------------------------------------------- - - outputBuffer[0] = 'B'; - outputBuffer[1] = 'M'; - - writeBmp32( outputBuffer + 2, (uint32_t)totalBytes ); - writeBmp32( outputBuffer + 10, (uint32_t)SCREENSHOT_BMP_HEADER_SIZE ); - - // ------------------------------------------------------- - // BITMAPINFOHEADER (40 bytes) - // - // Negative height selects top-down row order. This lets us - // preserve the LCD's natural 0..height-1 scan order without - // reversing the captured frame. - // ------------------------------------------------------- - - writeBmp32( outputBuffer + 14, 40 ); - writeBmp32( outputBuffer + 18, (uint32_t)screenWidth ); - writeBmp32( - outputBuffer + 22, - (uint32_t)(int32_t)( -screenHeight ) - ); - - writeBmp16( outputBuffer + 26, 1 ); - writeBmp16( outputBuffer + 28, 24 ); - - writeBmp32( outputBuffer + 30, 0 ); - writeBmp32( outputBuffer + 34, (uint32_t)imageBytes ); - - // 72 DPI, expressed as pixels per metre. - writeBmp32( outputBuffer + 38, 2835 ); - writeBmp32( outputBuffer + 42, 2835 ); - - for ( size_t y = 0; y < height; y++ ) { - const uint16_t* sourceRow = frameBuffer + ( y * width ); - uint8_t* destinationRow = - outputBuffer + SCREENSHOT_BMP_HEADER_SIZE + ( y * rowStride ); - - for ( size_t x = 0; x < width; x++ ) { - const uint16_t rawPixel = sourceRow[x]; - - // M5GFX readRect() returns the CoreS3 panel RGB565 word with - // the two bytes swapped relative to the logical TFT color value. - // - // Example: - // TFT_CYAN = 0x07FF - // readRect = 0xFF07 - // - // Restore the logical RGB565 word before expanding to RGB888. - const uint16_t pixel = (uint16_t)( - ( rawPixel >> 8 ) | - ( rawPixel << 8 ) - ); - - const uint8_t red5 = (uint8_t)( ( pixel >> 11 ) & 0x1F ); - const uint8_t green6 = (uint8_t)( ( pixel >> 5 ) & 0x3F ); - const uint8_t blue5 = (uint8_t)( pixel & 0x1F ); - - const uint8_t red8 = (uint8_t)( ( red5 << 3 ) | ( red5 >> 2 ) ); - const uint8_t green8 = (uint8_t)( ( green6 << 2 ) | ( green6 >> 4 ) ); - const uint8_t blue8 = (uint8_t)( ( blue5 << 3 ) | ( blue5 >> 2 ) ); - - uint8_t* destinationPixel = destinationRow + ( x * 3 ); - - // BMP 24-bit pixel byte order is B, G, R. - destinationPixel[0] = blue8; - destinationPixel[1] = green8; - destinationPixel[2] = red8; - } - } - - heap_caps_free( frameBuffer ); - - bmpData = outputBuffer; - bmpSize = totalBytes; - - return true; - } - - void handleScreenshotRequest( AsyncWebServerRequest* request ) { - if ( request == nullptr ) { - return; - } - - if ( - !displayReady || - screenWidth <= 0 || - screenHeight <= 0 - ) { - request->send( - 503, - F("text/plain"), - F("CoreS3 display screenshot is unavailable.\n") - ); - - return; - } - - // Keep screenshot memory use bounded and avoid overlapping LCD reads. - if ( screenshotCaptureInProgress ) { - request->send( - 429, - F("text/plain"), - F("A CoreS3 screenshot request is already in progress.\n") - ); - - return; - } - - screenshotCaptureInProgress = true; - - uint8_t* bmpData = nullptr; - size_t bmpSize = 0; - - if ( !buildScreenshotBmp( bmpData, bmpSize ) ) { - screenshotCaptureInProgress = false; - - DEBUG_PRINTLN( - F( "[CoreS3_Display] Screenshot: capture/buffer allocation failed" ) - ); - - request->send( - 503, - F("text/plain"), - F("CoreS3 screenshot capture failed. PSRAM may be unavailable.\n") - ); - - return; - } - - // The async response outlives this request handler. Keep the BMP buffer - // alive with shared ownership until the response itself is destroyed. - std::shared_ptr bmpOwner( - bmpData, - [this]( uint8_t* pointer ) { - if ( pointer != nullptr ) { - heap_caps_free( pointer ); - } - - screenshotCaptureInProgress = false; - } - ); - - const size_t responseSize = bmpSize; - - AsyncWebServerResponse* response = request->beginResponse( - F("image/bmp"), - responseSize, - [bmpOwner, responseSize]( - uint8_t* buffer, - size_t maxLength, - size_t index - ) -> size_t { - if ( buffer == nullptr || index >= responseSize ) { - return 0; - } - - const size_t remaining = responseSize - index; - const size_t copyLength = - ( maxLength < remaining ) ? maxLength : remaining; - - memcpy( - buffer, - bmpOwner.get() + index, - copyLength - ); - - return copyLength; - } - ); - - if ( response == nullptr ) { - screenshotCaptureInProgress = false; - - request->send( - 503, - F("text/plain"), - F("CoreS3 screenshot response could not be created.\n") - ); - - return; - } - - response->addHeader( - F("Cache-Control"), - F("no-store, no-cache, must-revalidate, max-age=0") - ); - - response->addHeader( F("Pragma"), F("no-cache") ); - response->addHeader( F("Expires"), F("0") ); - - response->addHeader( - F("Content-Disposition"), - F("inline; filename=\"cores3-screen.bmp\"") - ); - - request->send( response ); - - DEBUG_PRINTF( - "[CoreS3_Display] Screenshot: %d x %d BMP (%u bytes)\n", - screenWidth, - screenHeight, - (unsigned)responseSize - ); - } - - void registerScreenshotEndpoint() { - server.on( - F("/cores3/screenshot.bmp"), - HTTP_GET, - [this]( AsyncWebServerRequest* request ) { - handleScreenshotRequest( request ); - } - ); - - DEBUG_PRINTLN( - F( "[CoreS3_Display] Screenshot endpoint: /cores3/screenshot.bmp" ) - ); - } - public: CoreS3DisplayUsermod() @@ -7129,8 +6811,6 @@ class CoreS3DisplayUsermod : public Usermod { DEBUG_PRINTLN( F( "[CoreS3_Display][BUILD] CoreS3 Display v0.1.0" ) ); DEBUG_PRINTLN( F( "[CoreS3_Display] Initialization start" ) ); - registerScreenshotEndpoint(); - DEBUG_PRINTLN( F( "[CoreS3_Display] Hardware: M5Stack CoreS3" ) ); DEBUG_PRINTF( "[CoreS3_Display] " "Settings: " "Sleep=%u sec, " "LCD=%u, " "Fade=%s, " "FadeDuration=%u ms\n", sleepTimeoutSec, lcdBrightness, fadeEnabled ? "ON" : "OFF", fadeDurationMs ); @@ -7364,15 +7044,6 @@ class CoreS3DisplayUsermod : public Usermod { displayInfo.add( "FAILED" ); } - JsonArray screenshotInfo = user.createNestedArray( "CoreS3 Display Screenshot" ); - - if ( displayReady ) { - screenshotInfo.add( "/cores3/screenshot.bmp" ); - } - else { - screenshotInfo.add( "UNAVAILABLE" ); - } - JsonArray touchInfo = user.createNestedArray( "CoreS3 Display Touch" ); touchInfo.add( touchReady ? "READY" : "NOT FOUND" ); diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 93359a5194..2ce48a9442 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -764,35 +764,6 @@ void M5StackDisplayHardwareBackend::writeBrightness( uint8_t value ) { } -bool M5StackDisplayHardwareBackend::readDisplayRgb565( - uint16_t* pixels, - int16_t width, - int16_t height -) { - if ( - pixels == nullptr || - width <= 0 || - height <= 0 - ) { - return false; - } - - if ( width != display.width() || height != display.height() ) { - return false; - } - - display.readRect( - 0, - 0, - width, - height, - pixels - ); - - return true; - } - - bool M5StackDisplayHardwareBackend::readBatteryStatus( M5StackBatteryStatus& status ) { status = M5StackBatteryStatus(); diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h index c3fa3430f5..e3ba9e7b24 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.h @@ -33,15 +33,6 @@ class M5StackDisplayHardwareBackend { bool readTouch( int16_t& touchX, int16_t& touchY ); void writeBrightness( uint8_t value ); - // Read the current physical LCD contents as RGB565. - // The caller owns the buffer; this backend keeps panel access out of the - // UI/WLED-state layer. - bool readDisplayRgb565( - uint16_t* pixels, - int16_t width, - int16_t height - ); - // Read battery state through the CoreS3 hardware backend. // The UI consumes only this generic status object; PMIC/register details // stay outside CoreS3_Display.cpp. diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 4c4f003a41..0397b59fdf 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -28,13 +28,12 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
/cores3/screenshot.bmp -``` - -Example: - -```text -http://192.168.1.100/cores3/screenshot.bmp -``` - -The endpoint returns a still image.
-Refreshing the URL captures the current 320 × 240 LCD contents again. - -The LCD is read as RGB565 and converted to a 24-bit BMP.
-Large screenshot buffers are not kept resident during normal operation; they are allocated in PSRAM only while a request is active. - -The PNG images included in this documentation were also created by converting BMP files captured with this Browser Screenshot endpoint on the PC side. - --- ## Tested Hardware @@ -438,7 +415,6 @@ Responsibilities: - Battery display - Wi-Fi status - Preset UI -- Browser Screenshot ### audioreactive @@ -453,7 +429,6 @@ The generic WLED Audio Reactive usermod includes ES7210 support used by the Core - Directly powering 120 LEDs from CoreS3 at high brightness is not recommended. - The standard WLED Brightness default of 128 is not modified. - A starting Brightness around 64 is recommended for CoreS3. -- Browser Screenshot returns a still BMP image; it is not a live stream. - DCDC OVP protection is not disabled. --- diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 3e2086ccd3..813070e596 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -28,13 +28,12 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
- AXP2101 Power Key を使った Safe Shutdown - 電源OFF前に LED BLACK frame を送信 - Safe Shutdown のキャンセル時は直前の LED 状態を復元 -- ブラウザから現在の LCD 画面を BMP で取得 --- ## Screenshots -以下は、CoreS3 本体の LCD を Browser Screenshot 機能で直接取得した実画面です。 +以下は、CoreS3 本体の LCD ユーザーインターフェースの実画面です。 ### Main @@ -105,28 +104,6 @@ Effect が Color Slot を使用しない場合は `COLOR NOT USED` と表示し Preset の呼び出しだけでなく、SAVE NEW / OVERWRITE / DELETE / BOOT PRESET まで CoreS3 から操作できます。 -### Browser Screenshot - -CoreS3 の現在の LCD 表示は、ブラウザから次の URL で取得できます。 - -```text -http:///cores3/screenshot.bmp -``` - -例: - -```text -http://192.168.1.100/cores3/screenshot.bmp -``` - -Screenshot は静止画です。
-URL を更新すると、その時点の 320 × 240 LCD 内容を再取得します。 - -BMP は CoreS3 の LCD から RGB565 で読み出し、24-bit BMP に変換して返します。
-大きな Screenshot バッファは通常時には常駐せず、要求時のみ PSRAM に確保します。 - -このドキュメントに掲載している PNG 画像も、Browser Screenshot で取得した BMP を PC 側で PNG に変換したものです。 - --- ## Tested Hardware @@ -438,7 +415,6 @@ WLED/ - Battery display - Wi-Fi status - Preset UI -- Browser Screenshot ### audioreactive @@ -453,7 +429,6 @@ WLED 標準の汎用 Audio Reactive Usermod に含まれる ES7210 対応を、C - 120 LED を CoreS3 から直接高輝度で給電する構成は推奨しません。 - WLED 標準 Brightness 128 は変更していません。 - CoreS3 では Brightness 64 前後からの使用を推奨します。 -- Browser Screenshot は BMP の静止画です。ライブストリームではありません。 - DCDC OVP 保護は無効化していません。 --- From 72ab4f1c43e17f68e0176310b5d6a8d42167a65d Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Fri, 9 Oct 2026 18:09:37 +0900 Subject: [PATCH 32/35] Use WLED power transitions for CoreS3 power key --- usermods/CoreS3_Display/CoreS3_Display.cpp | 28 ++-- usermods/CoreS3_Power/CoreS3_Power.cpp | 161 +++++++++------------ 2 files changed, 79 insertions(+), 110 deletions(-) diff --git a/usermods/CoreS3_Display/CoreS3_Display.cpp b/usermods/CoreS3_Display/CoreS3_Display.cpp index 868f451eac..68d52f446a 100644 --- a/usermods/CoreS3_Display/CoreS3_Display.cpp +++ b/usermods/CoreS3_Display/CoreS3_Display.cpp @@ -49,7 +49,7 @@ static const char CORES3_DISPLAY_CONFIG_NAME[] PROGMEM = "CoreS3_Display"; #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) bool coreS3PowerInitializationComplete(); bool coreS3PowerExternal5VReady(); -bool coreS3PowerSafeShutdownMonitorReady(); +bool coreS3PowerKeyMonitorReady(); #endif @@ -127,7 +127,7 @@ class CoreS3DisplayUsermod : public Usermod { enum RuntimeHealthWarning : uint8_t { RUNTIME_HEALTH_WARNING_NONE = 0, RUNTIME_HEALTH_WARNING_LED_POWER, - RUNTIME_HEALTH_WARNING_POWER_SAFETY, + RUNTIME_HEALTH_WARNING_POWER_KEY, RUNTIME_HEALTH_WARNING_TOUCH }; @@ -139,12 +139,12 @@ class CoreS3DisplayUsermod : public Usermod { unsigned long runtimeHealthWarningStartMs = 0; static constexpr uint8_t RUNTIME_HEALTH_SHOWN_LED_POWER = 0x01; - static constexpr uint8_t RUNTIME_HEALTH_SHOWN_POWER_SAFETY = 0x02; + static constexpr uint8_t RUNTIME_HEALTH_SHOWN_POWER_KEY = 0x02; static constexpr uint8_t RUNTIME_HEALTH_SHOWN_TOUCH = 0x04; // Give the deferred global Wire / I2C0 power-key monitor time to ARM before - // declaring Safe Shutdown unavailable. - static constexpr unsigned long POWER_SAFETY_WARNING_GRACE_MS = 5000; + // declaring the power-key monitor unavailable. + static constexpr unsigned long POWER_KEY_WARNING_GRACE_MS = 5000; static constexpr unsigned long RUNTIME_HEALTH_WARNING_HOLD_MS = 2200; // ========================================================= @@ -1843,8 +1843,8 @@ class CoreS3DisplayUsermod : public Usermod { case RUNTIME_HEALTH_WARNING_LED_POWER: return RUNTIME_HEALTH_SHOWN_LED_POWER; - case RUNTIME_HEALTH_WARNING_POWER_SAFETY: - return RUNTIME_HEALTH_SHOWN_POWER_SAFETY; + case RUNTIME_HEALTH_WARNING_POWER_KEY: + return RUNTIME_HEALTH_SHOWN_POWER_KEY; case RUNTIME_HEALTH_WARNING_TOUCH: return RUNTIME_HEALTH_SHOWN_TOUCH; @@ -1858,7 +1858,7 @@ class CoreS3DisplayUsermod : public Usermod { RuntimeHealthWarning getNextRuntimeHealthWarning( unsigned long now ) { #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) const bool graceExpired = - now - runtimeHealthStartMs >= POWER_SAFETY_WARNING_GRACE_MS; + now - runtimeHealthStartMs >= POWER_KEY_WARNING_GRACE_MS; if ( !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_LED_POWER ) && @@ -1871,11 +1871,11 @@ class CoreS3DisplayUsermod : public Usermod { } if ( - !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_POWER_SAFETY ) && + !( runtimeHealthWarningsShownMask & RUNTIME_HEALTH_SHOWN_POWER_KEY ) && graceExpired && - !coreS3PowerSafeShutdownMonitorReady() + !coreS3PowerKeyMonitorReady() ) { - return RUNTIME_HEALTH_WARNING_POWER_SAFETY; + return RUNTIME_HEALTH_WARNING_POWER_KEY; } #endif @@ -1909,14 +1909,14 @@ class CoreS3DisplayUsermod : public Usermod { return; } - if ( warning == RUNTIME_HEALTH_WARNING_POWER_SAFETY ) { + if ( warning == RUNTIME_HEALTH_WARNING_POWER_KEY ) { display.setTextColor( TFT_YELLOW, TFT_BLACK ); display.setTextSize( 2 ); - display.drawString( "POWER SAFETY WARNING", screenWidth / 2, 82 ); + display.drawString( "POWER KEY WARNING", screenWidth / 2, 82 ); display.setTextColor( TFT_WHITE, TFT_BLACK ); display.setTextSize( 1 ); - display.drawString( "Safe shutdown unavailable", screenWidth / 2, 122 ); + display.drawString( "Power key monitor unavailable", screenWidth / 2, 122 ); display.setTextColor( TFT_DARKGREY, TFT_BLACK ); display.drawString( "Use WLED power control", screenWidth / 2, 151 ); diff --git a/usermods/CoreS3_Power/CoreS3_Power.cpp b/usermods/CoreS3_Power/CoreS3_Power.cpp index e2cb25fecc..0e427ffe02 100644 --- a/usermods/CoreS3_Power/CoreS3_Power.cpp +++ b/usermods/CoreS3_Power/CoreS3_Power.cpp @@ -15,8 +15,8 @@ * Preserved validated power behavior: * - AW9523B external-5V enable writes (BOOST then BUS). * - Runtime External 5V re-assert after M5GFX initialization. - * - AXP2101 power-key IRQ handling for Safe Shutdown. - * - Safe Physical Shutdown LED BLACK / cancel-and-restore logic. + * - AXP2101 power-key IRQ handling. + * - WLED standard power transitions for long-press and canceled hold. * - AXP2101 DCDC3 Always-PWM stability setting. * - Concise boot reset / PWRON / PWROFF cause reporting. */ @@ -29,7 +29,7 @@ // ============================================================ static volatile bool coreS3PowerInitializationCompleteState = false; static volatile bool coreS3PowerExternal5VReadyState = false; -static volatile bool coreS3PowerSafeShutdownMonitorReadyState = false; +static volatile bool coreS3PowerKeyMonitorReadyState = false; bool coreS3PowerInitializationComplete() { @@ -41,9 +41,9 @@ bool coreS3PowerExternal5VReady() return coreS3PowerExternal5VReadyState; } -bool coreS3PowerSafeShutdownMonitorReady() +bool coreS3PowerKeyMonitorReady() { - return coreS3PowerSafeShutdownMonitorReadyState; + return coreS3PowerKeyMonitorReadyState; } class CoreS3PowerUsermod : public Usermod @@ -86,9 +86,7 @@ class CoreS3PowerUsermod : public Usermod AXP2101_PKEY_LONG_MASK; static constexpr unsigned long POWER_KEY_POLL_INTERVAL_MS = 20; - static constexpr unsigned long SAFE_SHUTDOWN_FALLBACK_HOLD_MS = 1500; - static constexpr unsigned long SAFE_SHUTDOWN_BLACK_REFRESH_MS = 100; - static constexpr unsigned long SAFE_SHUTDOWN_SHOW_WAIT_MS = 150; + static constexpr unsigned long POWER_KEY_FALLBACK_HOLD_MS = 1500; static constexpr unsigned long RUNTIME_POWER_HEALTH_POLL_MS = 10000; static constexpr uint8_t REG_OUTPUT_P0 = 0x02; @@ -130,19 +128,18 @@ class CoreS3PowerUsermod : public Usermod bool runtimePowerKeyMonitorAttempted = false; bool runtimePowerKeyBusReadyLogged = false; bool powerKeyPressed = false; - bool safeShutdownBlankActive = false; - bool safeShutdownEverTriggered = false; - bool safeShutdownLastCanceled = false; + bool powerKeyHoldHandled = false; + bool powerKeyEverTriggered = false; + bool powerKeyLastCanceled = false; uint8_t axpIrqEnableBefore = 0; uint8_t axpIrqEnableAfter = 0; uint8_t lastPowerKeyStatus = 0; - uint8_t lastSafeShutdownTriggerStatus = 0; - uint8_t savedLogicalBrightness = 0; + uint8_t lastPowerKeyOffTriggerStatus = 0; + uint8_t brightnessBeforePowerKeyHold = 0; unsigned long powerKeyPressedAt = 0; unsigned long lastPowerKeyPoll = 0; - unsigned long lastShutdownBlackRefresh = 0; unsigned long lastRuntimeI2CFailureLog = 0; esp_reset_reason_t bootResetReason = ESP_RST_UNKNOWN; @@ -464,7 +461,7 @@ class CoreS3PowerUsermod : public Usermod void serviceRuntimePowerHealth() { - if (!powerKeyMonitorReady || safeShutdownBlankActive) return; + if (!powerKeyMonitorReady || powerKeyHoldHandled) return; const unsigned long now = millis(); if ( @@ -508,79 +505,55 @@ class CoreS3PowerUsermod : public Usermod } } - void waitForLedOutputComplete() + void beginPowerKeyWledOff(uint8_t triggerStatus) { - unsigned long waitStart = millis(); - while (strip.isUpdating() && millis() - waitStart < SAFE_SHUTDOWN_SHOW_WAIT_MS) { - delay(1); - } - } - - void beginSafeShutdownBlank(uint8_t triggerStatus) - { - if (safeShutdownBlankActive) return; + if (powerKeyHoldHandled) return; - safeShutdownLastCanceled = false; - safeShutdownEverTriggered = true; - lastSafeShutdownTriggerStatus = triggerStatus; - savedLogicalBrightness = bri; + powerKeyLastCanceled = false; + powerKeyEverTriggered = true; + lastPowerKeyOffTriggerStatus = triggerStatus; + brightnessBeforePowerKeyHold = bri; DEBUG_PRINTF( - "[CoreS3_Power] Safe shutdown trigger: IRQ=0x%02X%s%s\n", + "[CoreS3_Power] Power key WLED off trigger: IRQ=0x%02X%s%s bri=%u\n", triggerStatus, (triggerStatus & AXP2101_PKEY_LONG_MASK) ? " LONG" : "", - (triggerStatus & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "" + (triggerStatus & AXP2101_PKEY_NEGATIVE_MASK) ? " PRESS" : "", + brightnessBeforePowerKeyHold ); - DEBUG_PRINTF( - "[CoreS3_Power] Safe shutdown: WLED bri=%u, strip=%u -> physical 0\n", - savedLogicalBrightness, - strip.getBrightness() - ); - - // Keep physical output BLACK without taking ownership of WLED's strip - // suspend state. - strip.waitForIt(); - strip.setBrightness(0, true); - strip.show(); - waitForLedOutputComplete(); - - safeShutdownBlankActive = true; - lastShutdownBlackRefresh = millis(); - - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: BLACK frame sent; physical brightness held at 0")); - } - - void maintainSafeShutdownBlank(unsigned long now) - { - if (!safeShutdownBlankActive || !powerKeyPressed) return; - if (now - lastShutdownBlackRefresh < SAFE_SHUTDOWN_BLACK_REFRESH_MS) return; + if (bri > 0) { + toggleOnOff(); + stateUpdated(CALL_MODE_BUTTON); + DEBUG_PRINTLN(F("[CoreS3_Power] WLED power-off requested")); + } + else { + DEBUG_PRINTLN(F("[CoreS3_Power] WLED already off; no power-off request needed")); + } - lastShutdownBlackRefresh = now; - strip.setBrightness(0, true); - strip.show(); - waitForLedOutputComplete(); + powerKeyHoldHandled = true; } - void cancelSafeShutdownBlank() + void cancelPowerKeyWledOff() { - if (!safeShutdownBlankActive) return; + if (!powerKeyHoldHandled) return; - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown canceled: restoring LED output")); + DEBUG_PRINTLN(F("[CoreS3_Power] Power key hold canceled")); - uint8_t restoreBrightness = bri; - if (restoreBrightness == 0 && savedLogicalBrightness > 0) { - restoreBrightness = savedLogicalBrightness; + if (brightnessBeforePowerKeyHold > 0 && bri == 0) { + toggleOnOff(); + stateUpdated(CALL_MODE_BUTTON); + DEBUG_PRINTLN(F("[CoreS3_Power] WLED power-on requested")); + } + else if (bri > 0) { + DEBUG_PRINTLN(F("[CoreS3_Power] WLED already on; no restore request needed")); + } + else { + DEBUG_PRINTLN(F("[CoreS3_Power] WLED was already off before power-key hold")); } - strip.setBrightness(restoreBrightness, true); - strip.show(); - waitForLedOutputComplete(); - - safeShutdownBlankActive = false; - safeShutdownLastCanceled = true; - - DEBUG_PRINTF("[CoreS3_Power] Safe shutdown canceled: restored bri=%u\n", restoreBrightness); + powerKeyHoldHandled = false; + powerKeyLastCanceled = true; } void servicePhysicalPowerKey() @@ -591,7 +564,7 @@ class CoreS3PowerUsermod : public Usermod if (!runtimePowerKeyMonitorAttempted || now - lastRuntimeI2CFailureLog >= 1000) { if (configureRuntimePowerKeyMonitor()) { powerKeyMonitorReady = true; - coreS3PowerSafeShutdownMonitorReadyState = true; + coreS3PowerKeyMonitorReadyState = true; } else { lastRuntimeI2CFailureLog = now; @@ -601,7 +574,6 @@ class CoreS3PowerUsermod : public Usermod } if (now - lastPowerKeyPoll < POWER_KEY_POLL_INTERVAL_MS) { - maintainSafeShutdownBlank(now); return; } @@ -613,7 +585,6 @@ class CoreS3PowerUsermod : public Usermod lastRuntimeI2CFailureLog = now; DEBUG_PRINTLN(F("[CoreS3_Power] Runtime PKEY status read FAILED on global Wire / I2C0")); } - maintainSafeShutdownBlank(now); return; } @@ -639,30 +610,28 @@ class CoreS3PowerUsermod : public Usermod powerKeyPressedAt = now; } - if ((powerKeyStatus & AXP2101_PKEY_LONG_MASK) && !safeShutdownBlankActive) { + if ((powerKeyStatus & AXP2101_PKEY_LONG_MASK) && !powerKeyHoldHandled) { powerKeyPressed = true; if (powerKeyPressedAt == 0) powerKeyPressedAt = now; - beginSafeShutdownBlank(powerKeyStatus); + beginPowerKeyWledOff(powerKeyStatus); } if ( powerKeyPressed && - !safeShutdownBlankActive && + !powerKeyHoldHandled && powerKeyPressedAt > 0 && - now - powerKeyPressedAt >= SAFE_SHUTDOWN_FALLBACK_HOLD_MS + now - powerKeyPressedAt >= POWER_KEY_FALLBACK_HOLD_MS ) { - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: PRESS timer fallback")); - beginSafeShutdownBlank(AXP2101_PKEY_NEGATIVE_MASK); + DEBUG_PRINTLN(F("[CoreS3_Power] Power key: PRESS timer fallback")); + beginPowerKeyWledOff(AXP2101_PKEY_NEGATIVE_MASK); } if (powerKeyStatus & AXP2101_PKEY_POSITIVE_MASK) { powerKeyPressed = false; powerKeyPressedAt = 0; - cancelSafeShutdownBlank(); + cancelPowerKeyWledOff(); return; } - - maintainSafeShutdownBlank(now); } public: @@ -670,7 +639,7 @@ class CoreS3PowerUsermod : public Usermod { coreS3PowerInitializationCompleteState = false; coreS3PowerExternal5VReadyState = false; - coreS3PowerSafeShutdownMonitorReadyState = false; + coreS3PowerKeyMonitorReadyState = false; bootResetReason = esp_reset_reason(); @@ -711,8 +680,8 @@ class CoreS3PowerUsermod : public Usermod runtimePowerKeyMonitorAttempted = false; DEBUG_PRINTLN(F("[CoreS3_Power] Power key monitor: DEFERRED to runtime loop on global Wire / I2C0")); - DEBUG_PRINTLN(F("[CoreS3_Power] Safe shutdown: AXP2101 LONG IRQ primary trigger")); - DEBUG_PRINTF("[CoreS3_Power] Safe shutdown: PRESS fallback >= %lu ms\n", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); + DEBUG_PRINTLN(F("[CoreS3_Power] Power key: AXP2101 LONG IRQ primary trigger")); + DEBUG_PRINTF("[CoreS3_Power] Power key: PRESS fallback >= %lu ms\n", POWER_KEY_FALLBACK_HOLD_MS); DEBUG_PRINTF("[CoreS3_Power] External 5V: %s\n", external5VEnableSuccess ? "ENABLED" : "FAILED"); coreS3PowerInitializationCompleteState = true; @@ -759,18 +728,18 @@ class CoreS3PowerUsermod : public Usermod pwmInfo.add("OVP protection unchanged"); } - JsonArray shutdownInfo = user.createNestedArray("CoreS3 Safe Shutdown"); + JsonArray shutdownInfo = user.createNestedArray("CoreS3 Power Key"); if (!powerKeyMonitorReady) { shutdownInfo.add(runtimePowerKeyMonitorAttempted ? "Runtime Wire / I2C0 monitor unavailable" : "Runtime Wire / I2C0 monitor pending"); } - else if (safeShutdownBlankActive) { - shutdownInfo.add("BLACK output active - waiting for PMIC off"); + else if (powerKeyHoldHandled) { + shutdownInfo.add("Power key hold active - waiting for PMIC off"); } - else if (safeShutdownLastCanceled) { - shutdownInfo.add("ARMED - last shutdown hold canceled"); + else if (powerKeyLastCanceled) { + shutdownInfo.add("ARMED - last power-key hold canceled"); } - else if (safeShutdownEverTriggered) { - shutdownInfo.add("ARMED - shutdown BLACK previously triggered"); + else if (powerKeyEverTriggered) { + shutdownInfo.add("ARMED - WLED power-off previously triggered"); } else { shutdownInfo.add("ARMED"); @@ -779,7 +748,7 @@ class CoreS3PowerUsermod : public Usermod shutdownInfo.add("Trigger: AXP2101 Long Press IRQ"); char fallbackText[48]; - snprintf(fallbackText, sizeof(fallbackText), "PRESS fallback: %lu ms", SAFE_SHUTDOWN_FALLBACK_HOLD_MS); + snprintf(fallbackText, sizeof(fallbackText), "PRESS fallback: %lu ms", POWER_KEY_FALLBACK_HOLD_MS); shutdownInfo.add(fallbackText); char lastIrqText[40]; @@ -787,7 +756,7 @@ class CoreS3PowerUsermod : public Usermod shutdownInfo.add(lastIrqText); char triggerIrqText[40]; - snprintf(triggerIrqText, sizeof(triggerIrqText), "Last BLACK trigger: 0x%02X", lastSafeShutdownTriggerStatus); + snprintf(triggerIrqText, sizeof(triggerIrqText), "Last power-off trigger: 0x%02X", lastPowerKeyOffTriggerStatus); shutdownInfo.add(triggerIrqText); char irqText[48]; From d06da162ccf37f18013db11a77adfb4469f69b4e Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Fri, 9 Oct 2026 18:22:37 +0900 Subject: [PATCH 33/35] Update CoreS3 power key documentation --- usermods/CoreS3_Display/readme.md | 24 ++++++++++++++---------- usermods/CoreS3_Display/readme_jp.md | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/usermods/CoreS3_Display/readme.md b/usermods/CoreS3_Display/readme.md index 0397b59fdf..b781a45ce8 100644 --- a/usermods/CoreS3_Display/readme.md +++ b/usermods/CoreS3_Display/readme.md @@ -3,7 +3,7 @@ [日本語](readme_jp.md) This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
-**local touch control / built-in microphone Audio Reactive / battery status / safe power-off / LED control** in a single device. +**local touch control / built-in microphone Audio Reactive / battery status / physical Power Key handling / LED control** in a single device. > **Status:** Hardware-validated CoreS3 implementation
> **Base:** WLED 17.0.0-devV5
@@ -25,9 +25,9 @@ This project runs the WLED v17 series natively on M5Stack CoreS3 and combines
This does not claim that DCDC3 itself was proven to be the sole hardware root cause.
> In this project, DCDC3 Always-PWM is treated as a stabilization measure that has been strongly validated on real hardware. -### Safe Shutdown +### Power Key Handling -Holding the physical CoreS3 Power Key sends a BLACK frame to the LED Strip before the PMIC hard power-off. +Holding the physical CoreS3 Power Key requests the normal WLED power-off path before the AXP2101 performs the final PMIC hard power-off. + +The CoreS3 usermod does not force LED output directly. It uses WLED's standard power transition handling so brightness state, transition reversal, and other WLED power behavior remain under core control. Processing sequence: ```text Power Key long press ↓ -LED BLACK frame +WLED standard OFF transition ↓ -Strip suspend +LED output reaches OFF ↓ AXP2101 hard power-off ``` -If the Power Key is released after BLACK but before the final hard power-off, shutdown is canceled and the previous LED output and Brightness are restored. +If the Power Key is released before the final PMIC hard power-off, the usermod requests the normal WLED power-on path when appropriate. + +If WLED is turned back on from the Web UI while the power-off transition is still running, the standard WLED transition can reverse and restore the active effect without CoreS3-specific LED output overriding it. --- @@ -402,7 +406,7 @@ Responsibilities: - External 5V - DCDC3 Always-PWM - Power Key -- Safe Shutdown +- WLED Power Transition - Power Health ### CoreS3_Display diff --git a/usermods/CoreS3_Display/readme_jp.md b/usermods/CoreS3_Display/readme_jp.md index 813070e596..45e9cef4ae 100644 --- a/usermods/CoreS3_Display/readme_jp.md +++ b/usermods/CoreS3_Display/readme_jp.md @@ -3,7 +3,7 @@ [English](readme.md) M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
-**タッチディスプレイ / 内蔵マイク Audio Reactive / バッテリー表示 / 安全な電源OFF / LED制御** を1台にまとめるプロジェクトです。 +**タッチディスプレイ / 内蔵マイク Audio Reactive / バッテリー表示 / 物理 Power Key 連携 / LED制御** を1台にまとめるプロジェクトです。 > **Status:** CoreS3 実機検証済み実装
> **Base:** WLED 17.0.0-devV5
@@ -25,9 +25,9 @@ M5Stack CoreS3 上で WLED v17 系をネイティブ動作させ、
- バッテリー残量表示 - Display Sleep / Wake - Wi-Fi Offline / Recovery AP UX -- AXP2101 Power Key を使った Safe Shutdown -- 電源OFF前に LED BLACK frame を送信 -- Safe Shutdown のキャンセル時は直前の LED 状態を復元 +- AXP2101 物理 Power Key の処理 +- 長押しおよびキャンセル時に WLED 標準の Power OFF / ON transition を使用 +- PMIC hard-off 前に WLED が再度 ON になった場合も標準 transition で自然に復帰 --- @@ -278,23 +278,27 @@ OVP protection: unchanged / enabled > DCDC3 がハードウェア上の絶対的な根本原因だった、と断定しているわけではありません。
> 本プロジェクトでは「実機で強く検証された安定化策」として扱っています。 -### Safe Shutdown +### Power Key Handling -CoreS3 の物理 Power Key 長押しでは、PMIC hard-off の前に LED Strip へ BLACK frame を送信します。 +CoreS3 の物理 Power Key を長押しすると、AXP2101 が最終的な PMIC hard-off を行う前に、WLED 標準の Power OFF 処理を要求します。 + +CoreS3 usermod 側では LED 出力を直接強制せず、WLED 標準の Power transition を使用します。これにより Brightness 状態、transition の反転、その他の WLED Power 処理は WLED Core 側で管理されます。 処理の概要: ```text Power Key long press ↓ -LED BLACK frame +WLED standard OFF transition ↓ -Strip suspend +LED output reaches OFF ↓ AXP2101 hard power-off ``` -BLACK 送信後に Power Key を離して Shutdown をキャンセルした場合は、直前の LED 出力と Brightness を復元します。 +最終的な PMIC hard-off の前に Power Key を離した場合は、必要に応じて WLED 標準の Power ON 処理を要求します。 + +Power OFF transition の途中で Web UI から WLED を再度 ON にした場合も、CoreS3 固有の LED 出力強制を行わず、WLED 標準 transition が反転して実行中のエフェクトへ復帰します。 --- @@ -402,7 +406,7 @@ WLED/ - External 5V - DCDC3 Always-PWM - Power Key -- Safe Shutdown +- WLED Power Transition - Power Health ### CoreS3_Display From b90b859885ad14e26a5688902de001a56712e0b4 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Fri, 9 Oct 2026 18:46:47 +0900 Subject: [PATCH 34/35] Reserve CoreS3 display pins with PinManager --- .../M5StackDisplayHardwareBackend.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp index 2ce48a9442..d9b110ee5a 100644 --- a/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp +++ b/usermods/CoreS3_Display/M5StackDisplayHardwareBackend.cpp @@ -702,6 +702,32 @@ M5StackDisplayHardwareBackend::M5StackDisplayHardwareBackend( M5GFX& displayRef bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int16_t& screenHeight, bool& touchReady ) { #if defined(WLED_M5STACK_CORES3) && defined(CONFIG_IDF_TARGET_ESP32S3) + static const managed_pin_type coreS3DisplayPins[] = { + { 3, true }, // LCD CS + { 35, true }, // LCD MISO / D/C shared + { 36, true }, // LCD SCLK + { 37, true }, // LCD MOSI + { 21, false } // Touch INT + }; + + static bool coreS3DisplayPinsAllocated = false; + + if ( !coreS3DisplayPinsAllocated ) { + if ( !PinManager::allocateMultiplePins( + coreS3DisplayPins, + sizeof( coreS3DisplayPins ) / sizeof( coreS3DisplayPins[0] ), + PinOwner::UM_Unspecified + ) ) { + DEBUG_PRINTLN( + F( "[CoreS3_Display] ERROR: display/touch pins already in use" ) + ); + + return false; + } + + coreS3DisplayPinsAllocated = true; + } + CoreS3DisplayGlobalWireRuntime& coreS3Runtime = coreS3DisplayGlobalWireRuntime(); @@ -714,6 +740,14 @@ bool M5StackDisplayHardwareBackend::initializeDisplay( int16_t& screenWidth, int F( "[CoreS3_Display] ERROR: CoreS3 global Wire display initialization failed" ) ); + PinManager::deallocateMultiplePins( + coreS3DisplayPins, + sizeof( coreS3DisplayPins ) / sizeof( coreS3DisplayPins[0] ), + PinOwner::UM_Unspecified + ); + + coreS3DisplayPinsAllocated = false; + return false; } From 824cdaaed527af00702cff6360ffae5ea20da610 Mon Sep 17 00:00:00 2001 From: t_mac1116 Date: Sat, 10 Oct 2026 00:01:39 +0900 Subject: [PATCH 35/35] Update CoreS3 sample runtime basis --- usermods/CoreS3_Display/platformio_override.ini.sample | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/usermods/CoreS3_Display/platformio_override.ini.sample b/usermods/CoreS3_Display/platformio_override.ini.sample index 9380a65988..35711ac524 100644 --- a/usermods/CoreS3_Display/platformio_override.ini.sample +++ b/usermods/CoreS3_Display/platformio_override.ini.sample @@ -8,9 +8,9 @@ ; ; Validated runtime basis: ; WLED 17.0.0-devV5 / current main-derived tree -; pioarduino 55.03.39 -; Arduino Core 3.3.9 -; ESP-IDF 5.5.4 +; Tasmota Platform 2026.05.50 +; Arduino Core 3.3.8.260506 +; ESP-IDF 5.5.4.260407 ; M5Stack CoreS3: 16MB Flash + 8MB QUAD PSRAM ; ; This example preserves:
CoreS3 Preset 選択画面CoreS3 Preset 管理画面CoreS3 新規 Preset 保存画面
PresetSave New
CoreS3 Preset 削除画面CoreS3 Boot Preset 設定画面