Slice list in neopixel object

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
djelau
Posts: 4
Joined: Thu Mar 25, 2021 10:09 am

Slice list in neopixel object

Post by djelau » Thu Mar 25, 2021 10:31 am

Hi everyone,

I am new to the forum. I'm very new in Python and more in micropython. Perhaps my question is an evidence for some of you, I hope.

I use embedded NeoPixel lib of micropython for ESP8266. I create a neopxl object.
As much as I can read the full list of this object

Code: Select all

>>> list(neopxl)
[(0, 0, 0), (0, 2, 0), (0, 2, 0), (0, 2, 0), (0, 2, 0), (0, 2, 0), (0, 0, 0)]
so much I can't slice (or update) some elements in this list:

Code: Select all

>>> neopxl[1:5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 24, in __getitem__
TypeError: unsupported types for __mul__: 'slice', 'int'
I understood that my request neopxl[1:6] couln't work because neopxl is an object not a list.
Then, how can I slice this list included in this object ?

cookie
Posts: 3
Joined: Fri Mar 26, 2021 2:59 pm

Re: Slice list in neopixel object

Post by cookie » Fri Mar 26, 2021 3:33 pm

What if you convert the object to a list then do the slice?

Code: Select all

neopxlList = list(neopxl)
neopxlList[1:5]  # do stuff to sublist
To update the elements in the list, I think you've got to replace them as entire tuples, since tuples are immutable.
Then apply your updates to the neopixel object to get the changes:

Code: Select all

# iterate over your changed indices and update their values on the neopixel object
neopxl[index] = newValue  # as a tuple!
neopxl.write() # to apply your changes

djelau
Posts: 4
Joined: Thu Mar 25, 2021 10:09 am

Re: Slice list in neopixel object

Post by djelau » Sat Mar 27, 2021 12:59 am

Thank for your answer.
Here, this is a list of tuple thus I should be able to change the position of each element in this list. Isn't it ? Of course I couldn't change the content of each element since this is tuple (but I don't want to).
Create a copy like that works

Code: Select all

neopxlList = list(neopxl)
and I can do the stuff to read. But this copy requests a huge quantity of RAM: I've actually 512 pixels and gc.mem_free() indicates<5k after that (30k before).
Additionally, to iterate to write each pixel is not the most efficient (not fast). Whereas writing a slice of these pixels would be much more faster I guess.
Anyway, if no other solution, I will try to accomodate my needs.

djelau
Posts: 4
Joined: Thu Mar 25, 2021 10:09 am

Re: Slice list in neopixel object

Post by djelau » Mon Mar 29, 2021 10:50 pm

Finally the following gave me what I want:

Code: Select all

>>> neopxl.buf
bytearray(b'\x00\x00\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
or

Code: Select all

>>>neopxl.buf[0:6]
bytearray(b'\x00\x00\x00\x00\x01\x00')

Post Reply