Event construction in micropython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
pulkin
Posts: 49
Joined: Tue Feb 19, 2019 10:22 pm

Event construction in micropython

Post by pulkin » Thu Oct 22, 2020 9:55 pm

Hi there,

I am looking for a good event-driven architecture to develop GUI on ESP32 board. My basic task is to, for example, react on touch screen and other interrupts without writing much code. The payload I am looking into may run for a while so I am probably looking at uasyncio. Can anybody point at a good library/example?


pulkin
Posts: 49
Joined: Tue Feb 19, 2019 10:22 pm

Re: Event construction in micropython

Post by pulkin » Fri Oct 23, 2020 8:25 pm

The second one is a monster library compared to my expectations. It looks like it polls touch hardware in a loop: pin_irq is not even used! Besides, it is hard-nailed to pyboard.

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

Re: Event construction in micropython

Post by pythoncoder » Sat Oct 24, 2020 10:26 am

This GUI repo for the official display is capable of running on an ESP32. It uses uasyncio internally: applications can use it if required or simply respond to callbacks. For larger displays there is this one which is also designed for portability.

Polling touch hardware actually works well. IRQ's are great when you need sub-millisecond response times but touch GUI's simply aren't in that category. You simply require a visibly quick response.

As for size, any GUI is likely to involve a significant amount of code. This one has been designed as a Python package so you only import what you need: this minimises RAM use.
Peter Hinch
Index to my micropython libraries.

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

Re: Event construction in micropython

Post by pythoncoder » Mon Oct 26, 2020 3:59 pm

pulkin wrote:
Fri Oct 23, 2020 8:25 pm
...It looks like it polls touch hardware in a loop: pin_irq is not even used! Besides, it is hard-nailed to pyboard.
I have appended this section to the uasyncio tutorial to justify the use of polling in such applications.

tl;dr With a cooperative scheduler, if you want to trigger a coroutine from a hardware change, interrupts offer no performance gain compared to polling the hardware.
Peter Hinch
Index to my micropython libraries.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Event construction in micropython

Post by jimmo » Mon Oct 26, 2020 10:48 pm

pythoncoder wrote:
Mon Oct 26, 2020 3:59 pm
I have appended this section to the uasyncio tutorial to justify the use of polling in such applications.

tl;dr With a cooperative scheduler, if you want to trigger a coroutine from a hardware change, interrupts offer no performance gain compared to polling the hardware.
Yes, 100% agree. Great explanation.

pulkin
Posts: 49
Joined: Tue Feb 19, 2019 10:22 pm

Re: Event construction in micropython

Post by pulkin » Wed Nov 18, 2020 5:24 pm

I have to admit I ended up with the same kind of an infinite loop polling event queue. Events are added via interrupts. I also have to admit that having a single event slot per type instead of a queue seems to be a reasonable choice for touch events at least.

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

Re: Event construction in micropython

Post by pythoncoder » Thu Nov 19, 2020 8:33 am

I have updated the touch GUI for the official display to simplify deployment to the ESP32, and also added new widgets.
Peter Hinch
Index to my micropython libraries.

Post Reply