From 717582874679f16ccd2eb5438eee7bfd107ddefd Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Fri, 13 Oct 2023 10:47:18 +0100 Subject: [PATCH] Fixed issue with LED strip type checks --- examples/modules/led_strip/multiple_strips.py | 9 ++++----- examples/modules/led_strip/single_strip.py | 4 ++-- lib/pimoroni_yukon/modules/led_strip.py | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/modules/led_strip/multiple_strips.py b/examples/modules/led_strip/multiple_strips.py index 8e7a620..ecd267b 100644 --- a/examples/modules/led_strip/multiple_strips.py +++ b/examples/modules/led_strip/multiple_strips.py @@ -11,7 +11,7 @@ # Constants STRIP_TYPE = LEDStripModule.NEOPIXEL # Change to LEDStripModule.DOTSTAR for APA102 style strips # Two Neopixel strips can be driven too, by using LEDStripModule.DUAL_NEOPIXEL -LEDS_PER_STRIP = 60 # How many LEDs are on the strip. If using DUAL_NEOPIXEL this can be a tuple +LEDS_PER_STRIP = 60 # How many LEDs are on the strip. If using DUAL_NEOPIXEL this can be a single value or a list or tuple BRIGHTNESS = 1.0 # The max brightness of the LEDs (only supported by APA102s) SLEEP = 0.02 # The time to sleep between each update SPEED = 0.01 # How much to advance the rainbow hue offset by each update @@ -58,8 +58,7 @@ def pio_and_sm_generator(): # Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) try: - - # Find out which slots of Yukon has LEDStripModules attached + # Find out which slots of Yukon have LEDStripModules attached for slot in yukon.find_slots_with(LEDStripModule): pio, sm = next(pio_and_sm) # Get the next PIO and State Machine numbers module = LEDStripModule(STRIP_TYPE, # Create a LEDStripModule object, with the details of the attached strip(s) @@ -74,7 +73,7 @@ def pio_and_sm_generator(): yukon.enable_main_output() # Turn on power to the module slots for module in modules: - module.enable() # Enable each LEDStripModule's onboard 5V regulator + module.enable() # Enable each LEDStripModule's onboard regulator current_time = ticks_ms() # Record the start time of the program loop @@ -90,7 +89,7 @@ def pio_and_sm_generator(): # Otherwise, just update the single Neopixel or Dotstar strip update_rainbow(module.strip, hue_offset) - # Advance the rainbow offset, wrapping if it exceeds 1.0 + # Advance the hue offset, wrapping if it exceeds 1.0 hue_offset += SPEED if hue_offset >= 1.0: hue_offset -= 1.0 diff --git a/examples/modules/led_strip/single_strip.py b/examples/modules/led_strip/single_strip.py index fc724f2..a7bdec6 100644 --- a/examples/modules/led_strip/single_strip.py +++ b/examples/modules/led_strip/single_strip.py @@ -14,7 +14,7 @@ # Two Neopixel strips can be driven too, by using LEDStripModule.DUAL_NEOPIXEL STRIP_PIO = 0 # The PIO system to use (0 or 1) to drive the strip(s) STRIP_SM = 0 # The State Machines (SM) to use to drive the strip(s) -LEDS_PER_STRIP = 60 # How many LEDs are on the strip. If using DUAL_NEOPIXEL this can be a tuple +LEDS_PER_STRIP = 60 # How many LEDs are on the strip. If using DUAL_NEOPIXEL this can be a single value or a list or tuple BRIGHTNESS = 1.0 # The max brightness of the LEDs (only supported by APA102s) SLEEP = 0.02 # The time to sleep between each update SPEED = 0.01 # How much to advance the rainbow hue offset by each update @@ -62,7 +62,7 @@ def update_rainbow(strip, offset): # Otherwise, just update the single Neopixel or Dotstar strip update_rainbow(module.strip, hue_offset) - # Advance the rainbow offset, wrapping if it exceeds 1.0 + # Advance the hue offset, wrapping if it exceeds 1.0 hue_offset += SPEED if hue_offset >= 1.0: hue_offset -= 1.0 diff --git a/lib/pimoroni_yukon/modules/led_strip.py b/lib/pimoroni_yukon/modules/led_strip.py index eb31abd..1d36356 100644 --- a/lib/pimoroni_yukon/modules/led_strip.py +++ b/lib/pimoroni_yukon/modules/led_strip.py @@ -35,8 +35,18 @@ def __init__(self, strip_type, pio, sm, num_leds, brightness=1.0, halt_on_not_pg if sm < 0 or sm > 3: raise ValueError("sm out of range. Expected 0 to 3") - if num_leds <= 0: - raise ValueError("num_leds out of range. Expected greater than 0") + if strip_type == self.DUAL_NEOPIXEL and isinstance(num_leds, (list, tuple)): + if len(num_leds) != 2: + raise ValueError("num_leds list or tuple should only contain two values") + + if num_leds[0] <= 0 or num_leds[1] <= 0: + raise ValueError("value in num_leds list or tuple out of range. Expected greater than 0") + else: + if isinstance(num_leds, (list, tuple)): + raise ValueError("num_leds should not be a list or tuple for the NEOPIXEL and DOTSTAR strip types") + + if num_leds <= 0: + raise ValueError("num_leds out of range. Expected greater than 0") if strip_type == self.DOTSTAR and (brightness < 0.0 or brightness > 1.0): raise ValueError("brightness out of range. Expected 0.0 to 1.0")