adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.13k stars 1.22k forks source link

Feature request: I2C interrupt handling #6568

Open kmatch98 opened 2 years ago

kmatch98 commented 2 years ago

Following on the previous discussion regarding interrupts, I am considering how to improve display response to capacitive touchscreen events.

I am considering whether it is [useful, possible] to create an event queue for I2C interrupt events.

I don't know anything about interrupt handling in CircuitPython, or CircuitPython does event scheduling, so this is just a first attempt at proposing a behavior and structure. I'm open to feedback, suggestions, clarification about what it will take to implement and the challenges to be faced.

Here's my basic thoughts on how it would work for a capacitive touchscreen:

My desired usage: I want to access a time-stamped list of touchscreen events [x, y, touch status (touch down, still-touched, touch up)].
I will pop off an touchscreen event from the queue and respond to it with a graphical change on the display (for example, reposition a rectangle on the screen).

Definition:

Action:

  1. When the interrupt pin triggers, CircuitPython queues an I2C data request.
  2. I2C data is requested (is this scheduled?).
  3. I2C data is received.
  4. Data is parsed and stuffed into the Event Queue for use on the Python side.

Event Queue elements:

tannewt commented 2 years ago

This will be tough because the user code could also be accessing I2C at the same time.

kmatch98 commented 2 years ago

Hmm.. does this mean that the I2C code would have to be rewritten to schedule the communications?

tannewt commented 2 years ago

You can probably do it using a lock where the background code will wait until user code isn't using I2C.