Since you need to use I2C to talk to the touch sensor, I would be inclined to use the pseudo multi-tasking stuff that uses the generators.
See: http://forum.micropython.org/viewtopic. ... sking#p208
and see this thread as well: http://forum.micropython.org/viewtopic.php?f=2&t=269
Use the timer to set a variable that will wake up your "thread", and have the "thread" do the I2C commands. Then the callback is made on the main thread and you're allowed to allocate memory.
Playing with Interrupts
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Playing with Interrupts
I agree. Interrupts are vital where a rapid response to an event is paramount, but because they involve true concurrency there are a number of gonk traps lurking in their use. Threaded code, using cooperative mult-tasking, avoids most of these and (if used properly) is fast enough for devices supporting human interaction. Threads run pure Python with no restriction. Crucially things won't change in the middle of your code which can happen if an interrupt service routine alters them behind your back. This can lead to very subtle bugs.
Using threaded code where possible makes most embedded systems far easier to write in my experience.
Using threaded code where possible makes most embedded systems far easier to write in my experience.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.