Replies: 6 comments 3 replies
-
Your async def prog|_1():
print('program 1')
await asyncio.sleep(1) |
Beta Was this translation helpful? Give feedback.
-
If you have several independent tasks, for example; The simple code below shows the basic principle: import asyncio
from time import sleep, ticks_ms
flag,go = True,False # value and trigger
async def prog_1():
global flag, go
while True:
if flag and go:
print('program 1')
go = False
await asyncio.sleep(1)
async def prog_2():
global flag, go
while True:
if not flag and go:
print('program 2')
go = False
await asyncio.sleep(1)
async def main():
global flag, go
while True:
flag = ticks_ms()%4 == 0 # simulate button press
go = True # set button event
print('flag',flag)
await asyncio.sleep(5)
asyncio.run(asyncio.gather(main(), prog_1(), prog_2())) I hope you find this example code useful. |
Beta Was this translation helpful? Give feedback.
-
The key point is not to To run such a task you need to launch it. One way is with |
Beta Was this translation helpful? Give feedback.
-
You can think of async functions as a special type of function, but they behave in a similar way to normal functions. In normal functions like Now for The Like a normal function, To show how similar normal and async functions are, here is a simple example: import asyncio
def s(n):
return n*2
def t(n):
return n/2
v = t(s(4))
print(v)
async def sa(n):
return n*2
async def ta(n):
return n/2
async def w():
w = await ta( await sa(4))
print(w)
asyncio.run(w()) # cant use await outside async function |
Beta Was this translation helpful? Give feedback.
-
Hi, question for Peter,
Thanks in advance |
Beta Was this translation helpful? Give feedback.
-
You can assign a callback to open, close, both or neither; in the last case you use import uasyncio as asyncio
from primitives import Switch
from machine import Pin
pin = Pin(18, Pin.IN, Pin.PULL_UP)
sw = Switch(pin)
led = Pin(2, Pin.OUT)
flag = False
async def pulse(led,ms):
global flag
flag = not flag
led.value(1)
await asyncio.sleep_ms(ms)
led.value(0)
async def prog_1():
while True:
print('program 1')
await asyncio.sleep(1)
async def prog_2():
while True:
print('program 2')
await asyncio.sleep(1)
async def main():
sw.close_func(pulse, (led, 1000)) # Assign the callback once only
while True:
print('flag',flag)
await asyncio.sleep(5)
asyncio.run(asyncio.gather(main(), prog_1(), prog_2())) Here is an example using the Event-based interface ```python
import uasyncio as asyncio
from primitives import Switch
from machine import Pin
pin = Pin(18, Pin.IN, Pin.PULL_UP)
sw = Switch(pin)
sw.close_func(None) # Instantiate a .close Event
led = Pin(2, Pin.OUT)
flag = False
async def pulse(led, ms):
global flag
while True:
await sw.close.wait() # Wait for switch closure
sw.close.clear() # Clear the Event
flag = not flag
led.value(1)
await asyncio.sleep_ms(ms)
led.value(0)
async def prog_1():
while True:
print('program 1')
await asyncio.sleep(1)
async def prog_2():
while True:
print('program 2')
await asyncio.sleep(1)
async def main():
while True:
print('flag',flag)
await asyncio.sleep(5)
asyncio.run(asyncio.gather(main(), prog_1(), prog_2()), pulse(led, 1_000)) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm trying to use asyncio with an ESP32 but I'm having some difficulty understanding!
Here is what I would like to do:
I start prog_1 because the flag is True
If I press the push button, the flag is False and I switch to prog_2, press again the push button, prog_1, etc.
I use Peter Hinch's pushbutton.py
What I get is prog_1, when I press the push button, the led turns on and off normally, but prog_1 loops and don't give back hand to prog_2.
Where is my mistake?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions