peterhinch / micropython-async

Application of uasyncio to hardware interfaces. Tutorial and code.
MIT License
739 stars 168 forks source link

question? #117

Closed gladyscatface closed 5 months ago

gladyscatface commented 7 months ago

Thank you Sir, for your help in getting some asyncio code working, is there such a thing as :(sw.toggle_func)?

async def my_app(): pin = Pin(16, Pin.IN, Pin.PULL_UP) # Hardware: switch to gnd red = led sw = Switch(pin) sw.close_func(pulse, (red, 1000)) # Note how coro and args are passed sw.open_func(pulse, (red, 1000)) await asyncio.sleep(60) # Dummy application code

This was cut and pasted from your tutorial, I don't like callbacks, and wish to build mqtt switches without loops that hide away in dark places. Thanks Brian

peterhinch commented 7 months ago

For a callback-free solution see ESwitch. If you want to toggle a Pin each time a switch closes, adapt the sample code as follows:

async def closure(pin):
    while True:
        es.close.clear()
        await es.close.wait()
        pin.value(not pin.value())
peterhinch commented 5 months ago

Closed due to inactivity.