adafruit / Adafruit_CircuitPython_NeoPixel

CircuitPython drivers for neopixels.
MIT License
304 stars 99 forks source link

throw error if color tuple/pixel_order mismatch #37

Closed jerryneedell closed 5 years ago

jerryneedell commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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
>>>