mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 05:00:26 +01:00
extmod/uasyncio: Add clear method to ThreadSafeFlag.
This is useful in situations where the ThreadSafeFlag is reused and needs to be cleared of any previous, unwanted event. For example, clear the flag at the start of an operation, trigger the operation (eg an I2C write), then (a)wait for an external event to set the flag (eg a pin IRQ). Further events may trigger the flag again but these are unwanted and should be cleared before the next cycle starts.
This commit is contained in:
@@ -75,5 +75,25 @@ async def main():
|
||||
print("wait task")
|
||||
await t
|
||||
|
||||
# Flag set, cleared, and set again.
|
||||
print("----")
|
||||
print("set event")
|
||||
flag.set()
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("clear event")
|
||||
flag.clear()
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
t = asyncio.create_task(task(4, flag))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("set event")
|
||||
flag.set()
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("wait task")
|
||||
await t
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -19,3 +19,14 @@ yield
|
||||
task 3
|
||||
task 3 done
|
||||
wait task
|
||||
----
|
||||
set event
|
||||
yield
|
||||
clear event
|
||||
yield
|
||||
yield
|
||||
task 4
|
||||
set event
|
||||
yield
|
||||
wait task
|
||||
task 4 done
|
||||
|
||||
Reference in New Issue
Block a user