From 86ecf4e5beb1c8cd36fdeb1c6010d04364988f13 Mon Sep 17 00:00:00 2001 From: chriz Date: Tue, 27 Oct 2026 22:41:03 +0100 Subject: [PATCH] ESA energy enable patch during build. --- environments.ini | 5 +++ scripts/enable_rtl433_decoders.py | 73 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 scripts/enable_rtl433_decoders.py diff --git a/environments.ini b/environments.ini index 0013819..eb3ec1b 100644 --- a/environments.ini +++ b/environments.ini @@ -1051,6 +1051,11 @@ build_flags = custom_description = Gateway using RTL_433_ESP and RadioLib custom_hardware = ESP32 HELTEC LORA32 V2 custom_img = img/boards/heltec-wifi-lora-32.png +; *** Re-enable rtl_433_ESP decoders disabled by default in the library *** +extra_scripts = + ${env.extra_scripts} + scripts/enable_rtl433_decoders.py +custom_rtl_433_enable_decoders = esa_energy [env:heltec-rtl_433-fsk] platform = ${com.esp32_platform} diff --git a/scripts/enable_rtl433_decoders.py b/scripts/enable_rtl433_decoders.py new file mode 100644 index 0000000..8b37b9f --- /dev/null +++ b/scripts/enable_rtl433_decoders.py @@ -0,0 +1,73 @@ +# Re-enable rtl_433_ESP decoders that are disabled by default in the library. +# +# Newer rtl_433_ESP releases mark some decoders with `.disabled = 1` (e.g. +# `esa_energy` since v0.6.x) and there is no build flag to turn them back on. +# This script patches the decoder definitions inside the downloaded library in +# .pio/libdeps// before compilation. +# +# Usage (in the environment section): +# extra_scripts = +# ${env.extra_scripts} +# scripts/enable_rtl433_decoders.py +# custom_rtl_433_enable_decoders = esa_energy +# +# Must run as a normal (post) script: lib_deps are installed while the platform +# builder script runs, so a "pre:" script would not find the library on a clean +# build. Sources are only compiled after all scripts have been read, and SCons +# recompiles patched files because their content changed. +import glob +import os +import re + +Import("env") + + +def enable_decoders(libdeps_dir, decoders): + """Patch `.disabled = N` to `.disabled = 0` for the given r_device names. + + Returns a dict decoder -> status ("enabled", "already enabled", "not found"). + """ + device_files = glob.glob( + os.path.join(libdeps_dir, "**", "src", "rtl_433", "devices", "*.c"), + recursive=True, + ) + result = {name: "not found" for name in decoders} + for path in device_files: + with open(path, "r", encoding="utf-8", newline="") as f: + content = f.read() + changed = False + for name in decoders: + block_re = re.compile( + r"(r_device\s+const\s+" + re.escape(name) + r"\s*=\s*\{)(.*?)(\};)", + re.DOTALL, + ) + match = block_re.search(content) + if not match: + continue + body = match.group(2) + new_body, count = re.subn( + r"(\.disabled\s*=\s*)[1-9]\d*", r"\g<1>0", body + ) + if count: + content = content[: match.start(2)] + new_body + content[match.end(2):] + changed = True + result[name] = "enabled" + else: + result[name] = "already enabled" + if changed: + with open(path, "w", encoding="utf-8", newline="") as f: + f.write(content) + return result + + +decoders = env.GetProjectOption("custom_rtl_433_enable_decoders", "").split() +if decoders: + libdeps_dir = os.path.join(env.subst("$PROJECT_LIBDEPS_DIR"), env.subst("$PIOENV")) + status = enable_decoders(libdeps_dir, decoders) + for name, state in status.items(): + print("rtl_433_ESP decoder %s: %s" % (name, state)) + missing = [name for name, state in status.items() if state == "not found"] + if missing: + print("Error: rtl_433_ESP decoder(s) not found in %s: %s" % (libdeps_dir, ", ".join(missing))) + env.Exit(1) +