Playing with Interrupts

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Playing with Interrupts

Post by dhylands » Tue Oct 21, 2014 9:13 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Playing with Interrupts

Post by pythoncoder » Wed Oct 22, 2014 7:15 am

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.
Peter Hinch
Index to my micropython libraries.

Post Reply