Improve Drawing Via MVMSB For adafruit_framebuf
I'm playing with a LCD screen built inside the control panel I salvaged from a Canon Pixma MX340 multi-function inkjet. I tried using CircuitPython's adafruit_framebuf
library for drawing operations. Out of all supported frame buffer formats, the closest is MVLSB
but those bits are in reversed order. I added code to reverse bits, which put a picture on screen but the real solution is to add proper buffer format support.
I was encouraged by adafruit_framebuf
's description in its documentation: "CircuitPython pure-python framebuf
module, based on the micropython framebuf
module." If this library was itself implemented in Python, I should be able to figure something out. Clicking "Edit on GitHub" link on the upper right, I was brought to the GitHub repository for adafruit_framebuf
and I quickly found source code file for the module.
At the top of adafruit_framebuf.py
are a series of classes, each representing one of the supported buffer formats and implements four static methods: set_pixel()
, get_pixel()
, fill()
, and fill_rect()
. I found the implementation for MVLSBFormat
and it looks like it's within my skill level to copy it and reverse its bit manipulation operators to create a MVMSBFormat
counterpart. The next problem is: how do I use my custom MVMSBFormat
class with the FrameBuffer
class? FrameBuffer
's __init__()
assigns a format class in response to initialization parameter, and it raises a ValueError()
if it doesn't recognize the parameter.
That's when I remembered I am still thinking like a C/C++ programmer and Python doesn't have a concept of private properties. I could create an instance of FrameBuffer
for MVLSB
format, and then immediately replace the .format
property to point to my MVMSBFormat
class. As a C/C++ programmer, this feels extremely icky. Is this the correct Python-ic approach? I doubt it, but I don't know the right way. So right or wrong this is what I have now.
With my FrameBuffer.format
pointing to my MVMSBFormat
class, adafruit_framebuf
drawing operations render correctly on screen without the need for bit reversal utilities. As a bonus, eliminating bit reversal overhead also meant I could draw text again without encountering "RuntimeError: pystack exhausted
". I think that's good enough to declare victory with basic bitmap operations and move on to a sprite compositing engine.