MX340 CircuitPython Key Events
My CircuitPython learning project was to communicate with the K13988 chip on board a salvaged Canon MX340 multi-function inkjet control panel. The follow-up project, currently in progress, is to convert my jumble of experimental and exploratory code into something resembling an usable library. It looks pretty good after I rearranged all the setup/cleanup into context manager syntax, but then I realized I was missing a big piece: key press events. I had been printing key codes to serial terminal as they come in, but that was no way to write an API!
For precedence I looked into CircuitPython keypad
class. I had expected this to be an opportunity to learn about Python events and event listeners, so I was surprised (and mildly disappointed) to find an event queue that client code is expected to poll on a regular basis. Well, at least I know how to implement such a thing, and Python standard libraries should make it pretty easy to implement.
I could reuse their Event
class to report my key press events, but the EventQueue
class itself is locked away inside Adafruit implementation and not available to me. I went looking for an existing Python standard library queue and the first search result seemed to be far more complex than I had expected. Turns out that particular queue
was designed for coordination across threads, overkill for this purpose. I just need the basic deque
(double-ended queue) class to emulate most of Adafruit's EventQueue
.
For my first draft I decided against implementing a separate object to expose keyboard event queue. I'll expose a get_key_event()
method which should serve basic scenarios well enough until I see justification to do more.