Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97df6e3cbe | ||
|
|
08d7277204 |
No files matched your search
+17
-1
@@ -5,6 +5,7 @@ MSG_BEAT = 0x25
|
||||
FRAME_LEN = 8 # [4C][25][flags][idx u16][period u16][fire_in]
|
||||
MIN_GUARD_MS = 30
|
||||
FREERUN_MISS_MAX = 4
|
||||
RESTART_BEATS = 3 # consecutive beats that all look old mean the controller restarted its counter
|
||||
|
||||
|
||||
def _diff(a, b):
|
||||
@@ -27,6 +28,8 @@ class BeatSync:
|
||||
self.downbeat = False
|
||||
self.bar = 0
|
||||
self.missed = 0
|
||||
self.stale_idx = None # newest index seen while every frame looked old
|
||||
self.stale_n = 0
|
||||
self.freerun_fire = False # True when the last fire was invented, not triggered by a frame
|
||||
# fires: beats acted on; dups: second copies dropped; late: too-soon or old frames;
|
||||
# freerun: beats the driver made up because no frame arrived
|
||||
@@ -42,11 +45,24 @@ class BeatSync:
|
||||
self.stats["dups"] += 1
|
||||
return False # second copy of a beat we already took
|
||||
guard = max(MIN_GUARD_MS, self.period // 2)
|
||||
if _diff(idx, self.last_idx) >= 0x8000 or (
|
||||
if _diff(idx, self.last_idx) >= 0x8000:
|
||||
# Behind us. A late copy is one or two frames; a controller that rebooted and
|
||||
# restarted its counter sends a run of consecutive indices, so follow it.
|
||||
if idx == self.stale_idx:
|
||||
self.stats["late"] += 1
|
||||
return False
|
||||
self.stale_n = self.stale_n + 1 if self.stale_idx is not None and _diff(idx, self.stale_idx) == 1 else 1
|
||||
self.stale_idx = idx
|
||||
if self.stale_n < RESTART_BEATS:
|
||||
self.stats["late"] += 1
|
||||
return False
|
||||
elif (
|
||||
self.fire_at is None and now - self.last_fire < guard and self.period
|
||||
):
|
||||
self.stats["late"] += 1
|
||||
return False # late copy of an older beat, or too soon after the last one
|
||||
self.stale_idx = None
|
||||
self.stale_n = 0
|
||||
self.last_idx = idx
|
||||
self.period = period
|
||||
self.downbeat = bool(flags & 1)
|
||||
|
||||
@@ -178,8 +178,20 @@ def apply_device_config(data, settings, presets):
|
||||
settings["reversed"] = flag
|
||||
presets.reversed = flag
|
||||
meta_changed = True
|
||||
max_changed = False
|
||||
if "max_brightness" in dc:
|
||||
try:
|
||||
mb = max(0, min(255, int(dc["max_brightness"])))
|
||||
max_changed = presets.max_b != mb
|
||||
settings["max_brightness"] = mb
|
||||
presets.max_b = mb
|
||||
meta_changed = True
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
if not strip_changed and not meta_changed:
|
||||
return
|
||||
if max_changed and not strip_changed and presets.selected in presets.presets:
|
||||
presets.select(presets.selected) # redraw static patterns under the new ceiling
|
||||
if strip_changed:
|
||||
prev = presets.selected
|
||||
try:
|
||||
|
||||
@@ -33,6 +33,7 @@ esp = init_espnow(settings)
|
||||
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||
presets.load(settings)
|
||||
presets.b = settings.get("brightness", 255)
|
||||
presets.max_b = settings.get("max_brightness", 255)
|
||||
presets.debug = bool(settings.get("debug", False))
|
||||
gc.collect()
|
||||
|
||||
|
||||
+3
-1
@@ -23,6 +23,8 @@ class Presets:
|
||||
self.step = 0
|
||||
# Global brightness (0–255), controlled via ESPNow {"b": <value>}
|
||||
self.b = 255
|
||||
# Ceiling for the effective brightness (0–255): neither the global nor a preset's brightness goes above it
|
||||
self.max_b = 255
|
||||
self.reversed = False
|
||||
|
||||
self.generator = None
|
||||
@@ -265,7 +267,7 @@ class Presets:
|
||||
# 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)
|
||||
effective_brightness = min(int(local * self.b / 255), self.max_b)
|
||||
return tuple(int(c * effective_brightness / 255) for c in color)
|
||||
|
||||
def fill(self, color=None):
|
||||
|
||||
@@ -21,6 +21,7 @@ def initialize_runtime():
|
||||
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||
presets.load(settings)
|
||||
presets.b = settings.get("brightness", 255)
|
||||
presets.max_b = settings.get("max_brightness", 255)
|
||||
presets.debug = bool(settings.get("debug", False))
|
||||
gc.collect()
|
||||
|
||||
|
||||
@@ -84,6 +84,27 @@ def test_stats_count_dups_and_freerun():
|
||||
assert b.stats["freerun"] == 1 and b.stats["fires"] == 2
|
||||
|
||||
|
||||
def test_controller_restart_is_followed():
|
||||
b = BeatSync()
|
||||
assert b.on_frame(frame(29000), 0)
|
||||
assert not b.on_frame(frame(1), 500) # looks old
|
||||
assert not b.on_frame(frame(1), 502) # second copy of the same beat
|
||||
assert not b.on_frame(frame(2), 1000)
|
||||
assert b.on_frame(frame(3), 1500) # third consecutive: the counter really restarted
|
||||
assert not b.on_frame(frame(3), 1502)
|
||||
assert b.on_frame(frame(4), 2000)
|
||||
|
||||
|
||||
def test_stray_old_frames_do_not_reset():
|
||||
b = BeatSync()
|
||||
assert b.on_frame(frame(100), 0)
|
||||
assert not b.on_frame(frame(90), 100)
|
||||
assert b.on_frame(frame(101), 500)
|
||||
assert not b.on_frame(frame(99), 600)
|
||||
assert not b.on_frame(frame(98), 700) # not consecutive upward
|
||||
assert b.on_frame(frame(102), 1000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for name, fn in list(globals().items()):
|
||||
if name.startswith("test_"):
|
||||
|
||||
Reference in new issue
Block a user