Traffic lights with a button interrupt - how would you have done it?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Traffic lights with a button interrupt - how would you have done it?

Post by karfas » Sat Jul 23, 2022 5:20 pm

@pythoncoder: You misunderstood me, I think. Even when this came out as an anti-asyncio rant, I also wrote
karfas wrote:
Fri Jul 22, 2022 3:55 pm
asyncio is great for doing many different things in parallel, and it's definitely worth learning.
Handling 300 traffic lights in a small city ? Asyncio, of course. Nothing better exists in the moment.
asyncio in an example for a newbie ? No way.

Despite my experience with different async/cooperative/multitasking environments during the last 45 years, I still struggle to wrap my head around some of the (u)asyncio concepts.

Do you have one or two pointers to one of these large asyncio programs so I could study how things work here ? I mean real programs, not some library ?
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

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

Re: Traffic lights with a button interrupt - how would you have done it?

Post by pythoncoder » Sun Jul 24, 2022 10:58 am

To give a simple example of where uasyncio trumps the alternative, consider a requirement to flash an LED at a rate which can be varied. Then extend it to say four LED's, all flashing at different rates and with each rate subject to variability. Try to code that using a state machine or otherwise and it soon becomes an evil tangle. In uasyncio:

Code: Select all

class FlashingLed:
    def __init__(pin, period):
        self.period = period
        self.pin = pin
        asyncio.create_task(self.run())

    async def run(self):
        while True:
            await asyncio.sleep_ms(self.period)
            self.pin(not self.pin())
Instantiate as many of those as you wish, change their periods at will. Simple logic, no globals, good OOP practice, and minimal hardware dependence.

As for examples, you could look at micro-gui or asynchronous MQTT. Admittedly these are libraries (what have you got against them?) but there is a music player here which uses micro-gui.
Peter Hinch
Index to my micropython libraries.

User avatar
curt
Posts: 25
Joined: Thu Jul 29, 2021 3:52 am
Location: Big Lake, Alaska

Re: Traffic lights with a button interrupt - how would you have done it?

Post by curt » Wed Jul 27, 2022 4:08 am

The traffic light application is interesting. I developed an example application for a module I am developing. It can be found here:
https://github.com/ctimmer/poll-looper

You only need:
poll_looper.py
and
example/trafficlights.py (app)

It was written for micropython but it runs on my raspberry pi (python3). The application only prints so no IO pins are used.

Curt

Post Reply