memory allocation failed for neopixel

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: memory allocation failed for neopixel

Post by jerryn »

Regarding the error messages regarding nvm. nvm is not implented on the trinket M0 - it is only implemented for the "M0 Express" boards. see: http://circuitpython.readthedocs.io/en/ ... hlight=nvm

I ran a test on a feather_m0_express:
I was able to write to the bytes individually.
microcontroller.nvm[0]=1
microcontroller.nvm[1]=2
But - I was unable to write to the nvm with: microcontroller.nvm=bytearray([1,2,3])
that still yields: AttributeError: 'module' object has no attribute 'nvm'

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: memory allocation failed for neopixel

Post by tannewt »

nvm isn't enabled for the Trinket M0 because it uses flash space. Its not actually a separate memory area.

The config for it is here.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

Thanks alot!!!

Now all alphanumerics and some special characters fit in. I have updated the Trinket M0 to version 2.1. Than used the cross compiler to compile:

https://github.com/Meresmata/NeoText/bl ... t/MAINX.PY

to a .mpy...

Fantastic... now I can try and find out, whether the characters fit in and look well! :) Do you know, I it works now?

Then I wanted to be able to scroll the characters non-blocking. I thought that an Timer interrupt would be good. Any other attempt has to be handle by the user?

Thanks alot. with kind regards!

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: memory allocation failed for neopixel

Post by danhalbert »

Glad `mpy-cross` helped! As for interrupts, that kind of thing is on our long-term list of desirable features, but we have no imminent plans, unfortunately.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

Do you have any other idea not to block, while the text scrolls on the neopixels, in a way the user has not to care about?

Processes, threads or conroutines are not implemented, right?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: memory allocation failed for neopixel

Post by tannewt »

Yeah, I think we could make neopixel_write asynchronous so that it only blocks if its called really quickly. The tricky part is that it'd need to copy the buffer to output in case it gets mutated.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

DSC_0008.jpg
DSC_0008.jpg (341.01 KiB) Viewed 1286 times
Hi,

now as far the the project... as I could get. I can write, text, numbers, special characters, and raw pixels in (x,y)-Cooridinates.

Code can be found at:

https://github.com/Meresmata/NeoText

Am am currently not so happy with the speed of scrolling. I was not able to solve this until now. I would be glad for a speed up of a factor of 2 til 5. Seams like mircropythons native code emitter can not be used in circuit python? Could type anotations lead to a speed up? Can type anotations be used? Any idead where I could get that speed up from?


A question about assertions in circruit python. If I am right there are always on, also in in .mpy files. Is their a way to turn them of?

With kind regards!
Another current problem is that scrolling currently blocks anything else. Hence I currently put it into a while loop.

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: memory allocation failed for neopixel

Post by danhalbert »

Somespeed ideas:
`_square(x)` could be replaced by `x*x`. Does x start as an integer or a float? If the latter, `int(x*x)`

`write_raw()` does a lot of appending. Could you allocate a fixed-size array in advance? (Not sure this would affect scrolling speed.)

We talked about using bytes or bytearrays like bytes('\x01\x03x05') instead of array.array("B", (1,3,5)). That would save some time in the initial writing, I believe.

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: memory allocation failed for neopixel

Post by danhalbert »

Assertions are always on. So just comment them out if your code seems OK.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

Thanks for the tip with assertion. Well, type hinting seams not to work?

I reduced the call hirachy and deleted the square function and copied the map_led function into the the scroll function. By the way. There are no inline functions in python? Or am I wrong?

I also tried yesteryday to reduce the two llops to one, but the indixes are to differrent to work well. I also reduced the statement

Code: Select all

self.orientation == "ZIGZAG"
to

Code: Select all

self.orientation 
from string compare to boolean.
I test the scrolling speed with "ABCDEF(heart symbol)". I I got a speed up from 56 columns in 30 s to 58 colums in 30 s. SO this is only a slight difference.

What I did was as profile with CPython 3.6 an a Windows 64 bit maschine with Pycharm. But I hope, that still this profile may be a good hint. Is there a way to run, circuitpython on the PC? Than I could use this one to test my code instead of Anaconda.
profile.png
profile.png (82.04 KiB) Viewed 1264 times


As you can see in the picture.

So the scroll method spends:
3.2 % of 12.1% in the scroll method itself. (26,5 %)
2.1 % of 12.1 % in the dummy neopixels.__set_item__ (17,4 %)
5,9 % of 12.1 % in the dummy neopixels._set_item (48,8 %)
0.8 % of 12.1 % in .isinstanceof (6,6 %)

Is their a trick to increase the speed of setting the pixels? Is their a difference, using integer compared to tupels? I use a frozen module (.mpy) . I guess neopixel support is not build into circruitpython in newer versions of circuit python of the trinket M0? DO you habe another idea to increase the speed?

I also made a coverage:
Coverage.png
Coverage.png (21.09 KiB) Viewed 1264 times
It seams as a lot of code is not neaded for me personally. Therefore I could fork neopixel and comment out a lot of if statements. But it is not the steep that I should take, to make the module usable for a wider audience. Is there a way to promote this module if it is 100 % ready. Because I hope that this could be useful for a wider audience.

With kind regard.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

One short update: commenting out the unessiseary parts in neopixel. Brought a speed up to 125 %. But comes out with the text I wanna write I also get a ram problem. With the test text I had no proble. I guess I have to use neopixel_write itself. Because else I can not easily get rid of the doubled buffers. One in neotext for easier writing and one in neopixel without losing extra speed for local conversion for calcuation first and than back for writing.

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: memory allocation failed for neopixel

Post by danhalbert »

Wow, that's an impressive amount of profiling!

You can build CircuitPython for unix, but you can't actually do I/O with it, so you'd have to do dummy I/O as you are now. No type hinting sorry, and no inlines.

Creating tuples will always be more expensive than passing around integers, so if you can use integers for the colors, you'll save time. Also, note that using brightness=1.0 saves time and space.

There is a lot of runtime checking for RGBW vs. RGB, handling the RGB order, etc. that could be optimized if you're working with a known kind of NeoPixels. For your purposes, just forking our general-purpose library and optimizing it is probably the best idea.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

The problem with the fork is, that if someone uses this lib, that it would not be working as expected with the standard neopixel lib, also I could not address the ram space problem and the speed problem at the same time, at least not in python code.

Hence I tried out to write my own show method... With the problem, that no led is lit...

I use neopixel_write and want to pass a bytearray of the size 3 * 64 to 3 * 256 elements. I tested it with 3 * 256 elements. and nothing happens...

I checked some examples of the bytearray. They are not empty! The code is:

Code: Select all

    def show(self, back_color=Color(COLORS.BLACK)):

        byte_buf = bytearray(3 * self.num_pixels)
        for r1 in range(self.num_pixels):
            buf = self.get_color(r1) if self.__get_on_list(r1) else back_color.to_tuple()
            for r2 in range(3):
                byte_buf[3 * r1 + r2] = (buf[r2])

        neopixel_write.neopixel_write(self.pin, byte_buf)
self.__get_on_list(i) returns the BIT i the array on_list, hence shows whether the given pixel is used
self.get_color(r1) - returns the colour of the given pixel, via its (x,y)-Coordinates and searches an array("B", (...)) with the x, y and rgb code for the colour of the position)
With self.num_pixels as number of the pixel of the matrices.

I couln't find the answer in the neopixel_write.c, nor in the neopixel.py

Do you have any hint why nothing lits up?

Thanks alot.

User avatar
Mummel
 
Posts: 23
Joined: Sun Aug 23, 2015 9:12 am

Re: memory allocation failed for neopixel

Post by Mummel »

Did some optimisation and used a heuristic to reduce calculation speed, but:

Problem of the new version is:
- only few percent faster
- show method does not show anything

+ reduced RAM usage. (massive)

I could rewrite the neotext lib... to reduce the ram usage... (while using neopixel it can not hould all the used Text into the ram).
Adafruit CircuitPython 2.2.4 on 2018-03-07; Adafruit Trinket M0 with samd21e18
>>> import neotext
>>> neotext.run_test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./neotext.py", line 736, in run_test
File "./neotext.py", line 209, in write
File "./neotext.py", line 247, in write_raw
MemoryError: memory allocation failed, allocating 696 bytes
Do you know some good methods to safe ram?

Else I have the remove all text show by the neopixels from the neotext lib and than reload it from the neopixel.buf when needed... Hence I would need some extra computation time.
Are there boards with the neopixel lib backed in? Or is there a method I can do that myself?

Hope you've had some good holidays!

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: memory allocation failed for neopixel

Post by danhalbert »

The firmware for the Circuit Playground Express has the NeoPixel library built into the firmware. It can be added to other boards in the same manner. We don't have an up-to-date "how to build CircuitPython" guide right now, but if you hang out in the #circuitpython discord channel, we could help get you started.

This will save RAM, but not time.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”