Skip to content

throw error if color tuple/pixel_order mismatch #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2018
Merged

throw error if color tuple/pixel_order mismatch #37

merged 3 commits into from
Oct 30, 2018

Conversation

jerryneedell
Copy link
Contributor

Is this what you had in mind to address #34 ?

Adafruit CircuitPython 4.0.0-alpha.2-84-gbd79c0c0d-dirty on 2018-10-28; Adafruit Metro M4 Express with samd51j19
>>>
>>> import board,neopixel
>>> pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, pixel_order=neopixel.RGB)
>>> pixels[0] = (255,0,0)
>>> pixels[0] = (255,0,0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 170, in __setitem__
  File "neopixel.py", line 151, in _set_item
ValueError: Color tuple size does not match pixel_order.
>>>
Adafruit CircuitPython 4.0.0-alpha.2-84-gbd79c0c0d-dirty on 2018-10-28; Adafruit Metro M4 Express with samd51j19
>>> import board,neopixel
>>> pixels = neopixel.NeoPixel(board.A2, 8, pixel_order=neopixel.RGBW)
>>> pixels = neopixel.NeoPixel(board.NEOPIXEL,1, pixel_order=neopixel.RGBW)
>>> pixels[0]=(255,0,0,0)
>>> pixels[0]=(255,0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 170, in __setitem__
  File "neopixel.py", line 151, in _set_item
ValueError: Color tuple size does not match pixel_order.
>>> 

@caternuson
Copy link
Contributor

That is what I would suggest. But looking at the logic for when an int is passed in, I'm not sure if there's some reasoning to provide automatic behavior for when RGBW pixels are used but only a RGB 3 tuple is supplied. I've added a comment in the issue thread so we can discuss there and nail down what we want to do.

@jerryneedell
Copy link
Contributor Author

updated PR to capture discussion - I hope:

>>> import board,neopixel
>>> pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, pixel_order=neopixel.RGB)
>>> pixels[0] = ( 10,0,10)
>>> pixels[0] = ( 10,0,10,10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 172, in __setitem__
  File "neopixel.py", line 153, in _set_item
ValueError: Color tuple size does not match pixel_order.
>>> pixels[0] = 0xffffff
>>> pixels[0] = 0x1ffffff
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 172, in __setitem__
  File "neopixel.py", line 135, in _set_item
ValueError: only bits 0->23 valid for integer input


Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-alpha.2-84-gbd79c0c0d-dirty on 2018-10-28; Adafruit Metro M4 Express with samd51j19
>>> import board,neopixel
>>> pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, pixel_order=neopixel.RGBW)
>>> pixels[0] = (255,255,0)
>>> pixels[0] = (255,255,0,0)
>>> pixels[0] = 0xff0000
>>> pixels[0] = 0x1ff0000
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 172, in __setitem__
  File "neopixel.py", line 135, in _set_item
ValueError: only bits 0->23 valid for integer input
>>> 

neopixel.py Outdated
r, g, b = value
elif (len(value) == self.bpp) or ((len(value) == 3) and (self.bpp == 4)):
if len(value) == 3:
r, g, b = value[0:3]
else:
r, g, b, w = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if/else could be replaced with a ternary:

r, g, b, w = value if len(value)==4 else value+(0,)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! Thanks.

@caternuson caternuson merged commit 72e8f38 into adafruit:master Oct 30, 2018
@jerryneedell jerryneedell deleted the jerryn_order branch October 30, 2018 01:33
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 30, 2018
Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel to 3.3.4 from 3.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#37 from jerryneedell/jerryn_order
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#36 from adafruit/ladyada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#35 from kattni/rpi-example

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_NeoTrellis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants