Add second strip

This commit is contained in:
jimmy 2025-05-24 13:56:57 +12:00
parent 8902adf18c
commit f2f0b37d63
1 changed files with 43 additions and 24 deletions

View File

@ -4,8 +4,11 @@ import utime
import random
class Patterns:
def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100):
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
def __init__(self, pin1, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100):
# Initialize both NeoPixel strips
self.n = NeoPixel(Pin(9, Pin.OUT), num_leds)
self.n2 = NeoPixel(Pin(10, Pin.OUT), num_leds)
self.num_leds = num_leds
self.pattern_step = 0
self.last_update = utime.ticks_ms()
@ -16,14 +19,9 @@ class Patterns:
"on" : self.on,
"color_wipe": self.color_wipe_step,
"rainbow_cycle": self.rainbow_cycle_step,
"theater_chase": self.theater_chase_step,
"blink": self.blink_step,
"random_color_wipe": self.random_color_wipe_step,
"random_rainbow_cycle": self.random_rainbow_cycle_step,
"random_theater_chase": self.random_theater_chase_step,
"random_blink": self.random_blink_step,
"color_transition": self.color_transition_step,
"external": None
}
self.selected = selected
self.color1 = color1
@ -40,8 +38,10 @@ class Patterns:
if self.patterns[self.selected]:
self.patterns[self.selected]()
def update_num_leds(self, pin, num_leds):
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
def update_num_leds(self, pin1, pin2, num_leds):
# Re-initialize both NeoPixel strips with the new number of LEDs
self.n = NeoPixel(Pin(pin1, Pin.OUT), num_leds)
self.n2 = NeoPixel(Pin(pin2, Pin.OUT), num_leds)
self.num_leds = num_leds
self.pattern_step = 0
@ -84,16 +84,22 @@ class Patterns:
return False
def set(self, i, color):
# Set the color for the specified LED on both strips
self.n[i] = color
self.n2[i] = color
def write(self):
# Write the color data to both strips
self.n.write()
self.n2.write()
def fill(self, color=None):
fill_color = color if color is not None else self.color1
# Fill both strips with the specified color
for i in range(self.num_leds):
self.n[i] = fill_color
self.n.write()
self.n2[i] = fill_color
self.write() # Call write after filling both
def off(self):
self.fill((0, 0, 0))
@ -109,8 +115,10 @@ class Patterns:
if self.pattern_step < self.num_leds:
for i in range(self.num_leds):
self.n[i] = (0, 0, 0)
self.n2[i] = (0, 0, 0) # Clear the second strip as well
self.n[self.pattern_step] = self.apply_brightness(color)
self.n.write()
self.n2[self.pattern_step] = self.apply_brightness(color) # Set the LED on the second strip
self.write() # Write to both strips
self.pattern_step += 1
else:
self.pattern_step = 0
@ -131,8 +139,10 @@ class Patterns:
for i in range(self.num_leds):
rc_index = (i * 256 // self.num_leds) + self.pattern_step
self.n[i] = self.apply_brightness(wheel(rc_index & 255))
self.n.write()
color = self.apply_brightness(wheel(rc_index & 255))
self.n[i] = color
self.n2[i] = color # Set the color on the second strip
self.write() # Write to both strips
self.pattern_step = (self.pattern_step + 1) % 256
self.last_update = current_time
@ -141,10 +151,12 @@ class Patterns:
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
for i in range(self.num_leds):
if (i + self.pattern_step) % 3 == 0:
self.n[i] = self.apply_brightness(self.color1)
color = self.apply_brightness(self.color1)
else:
self.n[i] = (0, 0, 0)
self.n.write()
color = (0, 0, 0)
self.n[i] = color
self.n2[i] = color # Set the color on the second strip
self.write() # Write to both strips
self.pattern_step = (self.pattern_step + 1) % 3
self.last_update = current_time
@ -165,8 +177,10 @@ class Patterns:
if self.pattern_step < self.num_leds:
for i in range(self.num_leds):
self.n[i] = (0, 0, 0)
self.n2[i] = (0, 0, 0) # Clear the second strip as well
self.n[self.pattern_step] = self.apply_brightness(color)
self.n.write()
self.n2[self.pattern_step] = self.apply_brightness(color) # Set the LED on the second strip
self.write() # Write to both strips
self.pattern_step += 1
else:
self.pattern_step = 0
@ -188,8 +202,10 @@ class Patterns:
random_offset = random.randint(0, 255)
for i in range(self.num_leds):
rc_index = (i * 256 // self.num_leds) + self.pattern_step + random_offset
self.n[i] = self.apply_brightness(wheel(rc_index & 255))
self.n.write()
color = self.apply_brightness(wheel(rc_index & 255))
self.n[i] = color
self.n2[i] = color # Set the color on the second strip
self.write() # Write to both strips
self.pattern_step = (self.pattern_step + 1) % 256
self.last_update = current_time
@ -199,10 +215,12 @@ class Patterns:
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for i in range(self.num_leds):
if (i + self.pattern_step) % 3 == 0:
self.n[i] = self.apply_brightness(color)
pixel_color = self.apply_brightness(color)
else:
self.n[i] = (0, 0, 0)
self.n.write()
pixel_color = (0, 0, 0)
self.n[i] = pixel_color
self.n2[i] = pixel_color # Set the color on the second strip
self.write() # Write to both strips
self.pattern_step = (self.pattern_step + 1) % 3
self.last_update = current_time
@ -240,12 +258,13 @@ class Patterns:
interpolated_color = self.interpolate_color(self.color1, self.color2, factor)
self.current_color = self.apply_brightness(interpolated_color)
# Fill the LEDs with the current interpolated color
# Fill the LEDs on both strips with the current interpolated color
self.fill(self.current_color)
if __name__ == "__main__":
p = Patterns(4, 180)
# Use different pins for the two strips
p = Patterns(9, 10, 180)
p.set_color1((255,0,0))
p.set_color2((0,255,0))
#p.set_delay(10)