Software architecture ESP32 GUI with background processes

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Software architecture ESP32 GUI with background processes

Post by on4aa » Sun Jan 21, 2018 12:31 pm

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.
Serge

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: Software architecture ESP32 GUI with background processes

Post by loboris » Sun Jan 21, 2018 7:53 pm

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.

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, ())
Similary, dest_th should be defined which can check for the message from Button thread and take some acction.

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

Re: Software architecture ESP32 GUI with background processes

Post by pythoncoder » Mon Jan 22, 2018 5:25 am

In the link you posted @Loboris stated
You can add uasyncio as a module (or frozen module).
It's just an installable Python library: is there really a problem here?

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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Software architecture ESP32 GUI with background processes

Post by stijn » Mon Jan 22, 2018 9:38 am

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.

User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Re: Software architecture ESP32 GUI with background processes

Post by on4aa » Mon Jan 22, 2018 6:07 pm

@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 :lol:
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

User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Re: Software architecture ESP32 GUI with background processes

Post by on4aa » Mon Jan 22, 2018 6:11 pm

@stijn No luck here. The 15 minute background loop could in worst circumstances require up to one minute to complete its task.
Serge

kaybee
Posts: 4
Joined: Thu Nov 24, 2016 1:31 pm

Re: Software architecture ESP32 GUI with background processes

Post by kaybee » Mon Jan 22, 2018 6:39 pm

on4aa wrote:
Mon Jan 22, 2018 6:07 pm
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 :lol:
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.
All hail the Schism! I'm learning more about pros/cons of both sides!! Thanks from the agnostic atheist on the sidelines (where else?).

User avatar
on4aa
Posts: 70
Joined: Sat Nov 11, 2017 8:41 pm
Location: Europe
Contact:

Re: Software architecture ESP32 GUI with background processes

Post by on4aa » Mon Jan 22, 2018 8:30 pm

Peter's article link appears dead over here.
Nonetheless, I found a copy of this 2014 scroll in a cave on the web…
Serge

Post Reply