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