Why does FrameBuffer not accept bytes objects?
Posted: Mon Apr 19, 2021 11:48 am
I am working on a simple font engine for a Raspberry Pi Pico using MicroPython. The display driver I use is based on framebuf. So, I was searching for the best way to transfer bitmaps from flash memory into the frame buffer of the display. Ideally avoiding any unnecessary memory allocations.
I hope I got this right, but if I create a bytes object in a precompiled pym file, data from this object is read directly from flash and not allocated in RAM.
So in file fontengine.py:
Reading from this data block should be very efficient?
In order to transfer e.g. a single letter into the target frame buffer, I tried to wrap this memory segment in a FrameBuffer:
This does not work, but raises the exception: "TypeError: object with buffer protocol required". I have to wrap the bytes object into a bytearray, which is, as far as I understand, very inefficient because it will create a copy of the data on the heap.
This created several questions where I did not find any answers for:
- Is this actually the most efficient way to render letters using the built-in framebuf module?
- Shouldn't bytes implement the buffer protocol?
- Is this an implementation problem of the framebuf module?
I think, reimplementing the bit mapping functions to transfer the pixels into the target frame buffer seems not the right way to go, because I only recreate slower versions of already existing functions.
I hope I got this right, but if I create a bytes object in a precompiled pym file, data from this object is read directly from flash and not allocated in RAM.
So in file fontengine.py:
Code: Select all
_font_data = b'\xff\x00..........'
In order to transfer e.g. a single letter into the target frame buffer, I tried to wrap this memory segment in a FrameBuffer:
Code: Select all
fba = framebuf.FrameBuffer(_font_data, 8, 8, framebuf.MONO_VLSB)
Code: Select all
fba = framebuf.FrameBuffer(bytearray(_font_data), 8, 8, framebuf.MONO_VLSB)
- Is this actually the most efficient way to render letters using the built-in framebuf module?
- Shouldn't bytes implement the buffer protocol?
- Is this an implementation problem of the framebuf module?
I think, reimplementing the bit mapping functions to transfer the pixels into the target frame buffer seems not the right way to go, because I only recreate slower versions of already existing functions.