@@ -922,18 +922,33 @@ tim = Timer(1, freq=1, callback=cb)
922
922
923
923
asyncio.run(foo())
924
924
```
925
- Another example ([ posted by Damien] ( https://github.com/micropython/micropython/pull/6886#issuecomment-779863757 ) ):
925
+ An example [ based on one posted by Damien] ( https://github.com/micropython/micropython/pull/6886#issuecomment-779863757 )
926
+ Link pins X1 and X2 to test.
926
927
``` python
928
+ from machine import Pin, Timer
929
+ import uasyncio as asyncio
930
+
927
931
class AsyncPin :
928
932
def __init__ (self , pin , trigger ):
929
933
self .pin = pin
930
- self .flag = ThreadSafeFlag()
934
+ self .flag = asyncio. ThreadSafeFlag()
931
935
self .pin.irq(lambda pin : self .flag.set(), trigger, hard = True )
932
936
933
- def wait_edge (self ):
934
- return self .flag.wait()
937
+ async def wait_edge (self ):
938
+ await self .flag.wait()
939
+
940
+ async def foo ():
941
+ pin_in = Pin(' X1' , Pin.IN )
942
+ async_pin = AsyncPin(pin_in, Pin.IRQ_RISING )
943
+ pin_out = Pin(' X2' , Pin.OUT ) # Toggle pin to test
944
+ t = Timer(- 1 , period = 500 , callback = lambda _ : pin_out(not pin_out()))
945
+ await asyncio.sleep(0 )
946
+ while True :
947
+ await async_pin.wait_edge()
948
+ print (' Got edge.' )
949
+
950
+ asyncio.run(foo())
935
951
```
936
- You then call ` await async_pin.wait_edge() ` .
937
952
938
953
The current implementation provides no performance benefits against polling the
939
954
hardware: other pending tasks may be granted execution first in round-robin
0 commit comments