Add global brightness support to driver

Handle per-device global brightness via ESPNow messages and apply it alongside per-preset brightness in all patterns.
This commit is contained in:
2026-01-29 00:02:28 +13:00
parent 337e8c9906
commit f35d8f7084
2 changed files with 15 additions and 5 deletions

View File

@@ -28,9 +28,16 @@ while True:
if e.any():
host, msg = e.recv()
data = json.loads(msg)
if data["v"] != "1":
# Only handle messages with the expected version.
if data.get("v") != "1":
continue
print(data)
# Global brightness (0255) for this device
if "b" in data:
try:
patterns.b = max(0, min(255, int(data["b"])))
except (TypeError, ValueError):
pass
if "presets" in data:
for name, preset_data in data["presets"].items():
# Convert hex color strings to RGB tuples and reorder based on device color order
@@ -44,4 +51,3 @@ while True:
preset_name = select_list[0]
step = select_list[1] if len(select_list) > 1 else None
patterns.select(preset_name, step=step)

View File

@@ -52,7 +52,8 @@ class Patterns:
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
self.num_leds = num_leds
self.step = 0
# Global brightness (0255), controlled via ESPNow {"b": <value>}
self.b = 255
self.generator = None
self.presets = {}
@@ -122,7 +123,10 @@ class Patterns:
self.num_leds = num_leds
def apply_brightness(self, color, brightness_override=None):
effective_brightness = brightness_override if brightness_override is not None else self.brightness
# Combine per-preset brightness (override) with global brightness self.b
local = brightness_override if brightness_override is not None else 255
# Scale preset brightness by global brightness
effective_brightness = int(local * self.b / 255)
return tuple(int(c * effective_brightness / 255) for c in color)
def fill(self, color=None):
@@ -444,7 +448,7 @@ class Patterns:
phase = "growing" # "growing", "shrinking", or "off"
colors = preset.colors
colors = preset.c
color = self.apply_brightness(colors[0] if colors else (255, 255, 255), preset.b)
while True: