Skip to content

Commit 4f2aac0

Browse files
committed
TUTORIAL.md Clarify ThreadSafeFlag example.
1 parent ef931a3 commit 4f2aac0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

v3/docs/TUTORIAL.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,18 +922,33 @@ tim = Timer(1, freq=1, callback=cb)
922922

923923
asyncio.run(foo())
924924
```
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.
926927
```python
928+
from machine import Pin, Timer
929+
import uasyncio as asyncio
930+
927931
class AsyncPin:
928932
def __init__(self, pin, trigger):
929933
self.pin = pin
930-
self.flag = ThreadSafeFlag()
934+
self.flag = asyncio.ThreadSafeFlag()
931935
self.pin.irq(lambda pin: self.flag.set(), trigger, hard=True)
932936

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())
935951
```
936-
You then call `await async_pin.wait_edge()`.
937952

938953
The current implementation provides no performance benefits against polling the
939954
hardware: other pending tasks may be granted execution first in round-robin

0 commit comments

Comments
 (0)