Fix rainbow pattern synchronization in LED bar

- Use controller's step for synchronization instead of internal step counter
- Rainbow pattern now syncs with controller timing like n_chase pattern
- Prevents rainbow from running independently and out of sync
- Uses beat_index % 256 for full color wheel cycling
This commit is contained in:
2025-09-19 01:29:48 +12:00
parent d715af4344
commit 355d113e32

View File

@@ -15,6 +15,7 @@ class Patterns(PatternBase): # Inherit from PatternBase
self.n3 = 1 # Default step factor
self.oneshot = False # New: One-shot flag for patterns like fill_range
self.patterns = {
"on": self.on,
"off": self.off,
"flicker": self.flicker,
"fill_range": self.fill_range,
@@ -37,6 +38,12 @@ class Patterns(PatternBase): # Inherit from PatternBase
}
self.step = 0
def on(self):
"""Turn on all LEDs with current color"""
self.fill(self.apply_brightness(self.colors[0]))
self.n.write()
return self.delay
def off(self):
"""Turn off all LEDs"""
self.fill((0, 0, 0))
@@ -89,21 +96,19 @@ class Patterns(PatternBase): # Inherit from PatternBase
self.last_update = current_time
return self.delay
# Use pattern_step for internal chasing, not the controller's step
if not hasattr(self, 'pattern_step'):
self.pattern_step = 0
# Use controller's step for synchronization, but scale it for chasing
chase_step = (self.step * step_rate) % self.num_leds
for i in range(self.num_leds):
# Calculate position relative to the chase head
pos_from_head = (i - self.pattern_step) % self.num_leds
pos_from_head = (i - chase_step) % self.num_leds
if pos_from_head < self.n1:
self.n[i] = self.apply_brightness(self.colors[0])
else:
self.n[i] = (0, 0, 0)
self.n.write()
# Update internal pattern step for chasing
self.pattern_step = (self.pattern_step + step_rate) % self.num_leds
# Don't update internal step - use controller's step for sync
self.last_update = current_time
return self.delay
@@ -196,11 +201,15 @@ class Patterns(PatternBase): # Inherit from PatternBase
return (0, pos * 3, 255 - pos * 3)
step_rate = max(1, int(self.n3))
# Use controller's step for synchronization, scaled for rainbow cycling
rainbow_step = (self.step * step_rate) % 256
for i in range(self.num_leds):
rc_index = (i * 256 // max(1, self.num_leds)) + self.step
rc_index = (i * 256 // max(1, self.num_leds)) + rainbow_step
self.n[i] = self.apply_brightness(wheel(rc_index & 255))
self.n.write()
self.step = (self.step + step_rate) % 256
# Don't update internal step - use controller's step for sync
return max(1, int(self.delay // 5))
def specto(self):