I’m building a user interface for a project I’m working on, which requires regular writing to a display over SPI. The challenge is that after some time I start to get memory allocation errors, probably due to fragmentation happening with creating and deleting buffers.
To tackle this issue I'm working where possible with binary buffers (taking 1/16th of a RGB565 buffer), which only just before writing it to the screen is translated into a RGB565 buffer (taking 2 bytes per pixel). For large blocks of data I break it into pieces with a set maximum size (which does slow down writing to the screen, but is still much faster than writing pixel by pixel).
Another step I’ve taken to try to reduce fragmentation is reusing a single bytearray as buffer, which is set at the beginning of the code:I’m using the FrameBuffer class to fill the buffer, each time using the same bytearray, which works perfectly as long as w * h * 2 <= MAX_BUFFER_SIZE: The last challenge is writing this buffer to the display using SPI.write(). doesn’t work. doesn’t work either.
What does work is:The problem is that in this way it seems that the indicated part of the bytearray is copied into a new one before it’s passed to spi.write(), which means it still leads to the memory allocation problems due to fragmentation.
So my now my question is: is there a way to pass on a part of byte_array, while keeping the reference to the original bytearray (so without creating a copy in memory)?
It seems that NumPy arrays can do that, but those are not available in MicroPython. Any other solution?
To tackle this issue I'm working where possible with binary buffers (taking 1/16th of a RGB565 buffer), which only just before writing it to the screen is translated into a RGB565 buffer (taking 2 bytes per pixel). For large blocks of data I break it into pieces with a set maximum size (which does slow down writing to the screen, but is still much faster than writing pixel by pixel).
Another step I’ve taken to try to reduce fragmentation is reusing a single bytearray as buffer, which is set at the beginning of the code:
Code:
MAX_BUFFER_SIZE = const(15488) # 1/5 screen - I would like to go biggerbyte_buffer = bytearray(MAX_BUFFER_SIZE)
Code:
buffer = framebuf.FrameBuffer(byte_buffer, w, h, framebuf.RGB565)
Code:
spi.write(buffer)
Code:
spi.write(byte_buffer)
What does work is:
Code:
spi.write(byte_buffer[0:w * h * 2])
So my now my question is: is there a way to pass on a part of byte_array, while keeping the reference to the original bytearray (so without creating a copy in memory)?
It seems that NumPy arrays can do that, but those are not available in MicroPython. Any other solution?
Statistics: Posted by harmlammers — Fri May 03, 2024 9:31 pm