Hi lads,
I am planning on writing a graphical user interface with 3-button control for the ESP32-based M5Stack.
At the same time, two background processes will have to run at 3 and 15 minute intervals.
The GUI and background processes should not mutually block one another.
However, they do need to interchange at least a few values with one another.
I will most probably employ the Loboris MicroPython port for ESP32 with psram support, if not the M5Stack MicroPython port without the m5cloud module.
This implies that uasyncio will be unavailable.
So how do I go about? What would be the best programmatic skeleton for such an application?
How do I set up timers, threads and button interrupts?
Thank you in advance for your suggestions.
A pointer to some sample code would also do.
Software architecture ESP32 GUI with background processes
Re: Software architecture ESP32 GUI with background processes
You may look at the Thread Wiki.
There are couple of ways to exchange the data between threads. String messages of unlimited size (for example json strings) can be exchanged between threads, and several methods for suspending the thread until needed are available (thread synchronization).
Look at the Recommended thread function template in The Wiki for example how the tipical thread function should be defined.
Simple button thread. While utime.sleep_ms(50) is executed, the thread does not use any CPU or MPy resources.
Similary, dest_th should be defined which can check for the message from Button thread and take some acction.
There are couple of ways to exchange the data between threads. String messages of unlimited size (for example json strings) can be exchanged between threads, and several methods for suspending the thread until needed are available (thread synchronization).
Look at the Recommended thread function template in The Wiki for example how the tipical thread function should be defined.
Simple button thread. While utime.sleep_ms(50) is executed, the thread does not use any CPU or MPy resources.
Code: Select all
import machine, _thread, utime
def btn_th():
btnpin = machine.Pin(35, mode=machine.Pin.IN)
lastbtnstate = btnpin.value()
while True:
btnstate = btnpin.value()
if btnstate != lastbtnstate:
lastbtnstate = btnstate
_thread.sendmsg(dest_th, "Pressed")
utime.sleep_ms(50)
btnth=_thread.start_new_thread("Button", btn_th, ())
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Software architecture ESP32 GUI with background processes
In the link you posted @Loboris stated
In my view cooperative multi-tasking is greatly preferable unless there are overwhelming reasons to avoid it. Some of the reasons are discussed in this article.
It's just an installable Python library: is there really a problem here?You can add uasyncio as a module (or frozen module).
In my view cooperative multi-tasking is greatly preferable unless there are overwhelming reasons to avoid it. Some of the reasons are discussed in this article.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Software architecture ESP32 GUI with background processes
How long do the background processes take? I mean, if it's only a couple of seconds there's really no need for any threading/asyncio. Unless you intend to overcomplicate things or want to learn how to do it. Just a main loop will do fine, display a message 'Please wait while running backgournd process' and you're good. Less code, simpler code, less bugs.
Re: Software architecture ESP32 GUI with background processes
@Peter Right! I guess, I was still a bit confused since my unsuccessful attempt to install uasyncio on a Pycom flavoured ESP32. That too, may eventually get resolved when Pycom pulls in robert-hh's pull requests. Anyhow, I somewhat lost my interest in Pycom.
On the other hand, what a Great Schism we are witnessing here!
In the East, we have Pope loboris adhering to orthodox threading, while
in the West, Archbishop Peter is preaching a uasyncio reformation…
Just poking some fun, here
Nonetheless, it is not easy for a mere commoner like myself to know who to believe.
This will require an in-depth exegesis from my behalf.
On the other hand, what a Great Schism we are witnessing here!
In the East, we have Pope loboris adhering to orthodox threading, while
in the West, Archbishop Peter is preaching a uasyncio reformation…
Just poking some fun, here
Nonetheless, it is not easy for a mere commoner like myself to know who to believe.
This will require an in-depth exegesis from my behalf.
Serge
Re: Software architecture ESP32 GUI with background processes
@stijn No luck here. The 15 minute background loop could in worst circumstances require up to one minute to complete its task.
Serge
Re: Software architecture ESP32 GUI with background processes
All hail the Schism! I'm learning more about pros/cons of both sides!! Thanks from the agnostic atheist on the sidelines (where else?).on4aa wrote: ↑Mon Jan 22, 2018 6:07 pmOn the other hand, what a Great Schism we are witnessing here!
In the East, we have Pope loboris adhering to orthodox threading, while
in the West, Archbishop Peter is preaching a uasyncio reformation…
Just poking some fun, here
Nonetheless, it is not easy for a mere commoner like myself to know who to believe.
This will require an in-depth exegesis from my behalf.
Re: Software architecture ESP32 GUI with background processes
Peter's article link appears dead over here.
Nonetheless, I found a copy of this 2014 scroll in a cave on the web…
Nonetheless, I found a copy of this 2014 scroll in a cave on the web…
Serge