Skip to content

Commit a641b33

Browse files
committed
anyio-ize the examples
1 parent cfb5970 commit a641b33

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

examples/line_echo_polled.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import trio
1+
import anyio
22
import asyncgpio as gpio
33
"""
44
This script oggles a pin and watches another. The two are presumed to be connected (hardware wire).
@@ -8,9 +8,9 @@
88
async def pling(line):
99
while True:
1010
line.value = 1
11-
await trio.sleep(1)
11+
await anyio.sleep(1)
1212
line.value = 0
13-
await trio.sleep(1)
13+
await anyio.sleep(1)
1414

1515

1616
async def main():
@@ -21,8 +21,8 @@ async def main():
2121
await n.spawn(pling, out_)
2222
while True:
2323
print(in_.value)
24-
await trio.sleep(0.3)
24+
await anyio.sleep(0.3)
2525

2626

2727
if __name__ == "__main__":
28-
trio.run(main, backend="trio")
28+
anyio.run(main, backend="trio")

examples/push_button_event.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import time
2-
import trio
3-
import trio_gpio as gpio
2+
import anyio
3+
import asyncgpio as gpio
44

55
"""
66
This example is taken out of my furnace controller.
7-
It has been tested with Raspberry Pi Zero W and I assume it will work with any board supported by trio_gpio.
7+
It has been tested with Raspberry Pi Zero W and I assume it will work with any board supported by asyncgpio.
88
Use at your own risk.
99
1010
If you aren't sure about how to hook up a button and led to your board, there are a lot of examples online.
1111
12-
Thank you @smurfix, who wrote trio_gpio and @njsmith and other in glitter:python-trio/general room
12+
Thank you @smurfix, who wrote asyncgpio and @njsmith and other in glitter:python-trio/general room
1313
who helped me out.
1414
"""
1515

@@ -20,31 +20,31 @@ class Led:
2020
# called at the same time or trio might await at the wrong spot.
2121
def __init__(self, line):
2222
self.x = line
23-
self._on = trio.Event()
24-
self._off = trio.Event()
23+
self._on = anyio.create_event()
24+
self._off = anyio.create_event()
2525

2626
async def liteon(self):
2727
with gpio.open_chip() as chip:
2828
with chip.line(self.x).open(direction=gpio.DIRECTION_OUTPUT) as line:
2929
self._on.clear()
30-
self._off.set()
30+
await self._off.set()
3131
while True:
3232
if self._on.is_set():
3333
line.value = 1
3434
# print('lite on')
3535
await self._off.wait()
36-
self._on.clear()
36+
self._on = anyio.create_event()
3737
elif self._off.is_set():
3838
line.value = 0
3939
# print('lite off')d
4040
await self._on.wait()
41-
self._off.clear()
41+
self._off = anyio.create_event()
4242
else:
4343
# should never be reached.
4444
# if the code does reach here,
4545
# turn off the power to whatever is being powered
4646
print('error: both are off.')
47-
self._off.set()
47+
await self._off.set()
4848

4949

5050
class Button:
@@ -69,20 +69,23 @@ async def push(self):
6969
now = float(str(secs)+'.'+str(ns_secs))
7070
if now >= last + .25:
7171
print('button', e.value, secs, ns_secs, now)
72-
self._off.set() if self._on.is_set() else self._on.set()
72+
if self._on.is_set():
73+
await self._off.set()
74+
else:
75+
await self._on.set()
7376
last = now
7477

75-
# Trio-gpio uses the BCM pin numbering. So, the led is on the pin 21
78+
# Asyncgpio uses the BCM pin numbering. So, the led is on the pin 21
7679
# and the button that controls the yellow is hooked to pin 23.
7780
yellow = Led(21)
7881
yellowbutton = Button(23, yellow._on, yellow._off)
7982

8083

8184
async def main(y):
82-
async with trio.open_nursery() as nursery:
83-
nursery.start_soon(yellowbutton.push)
84-
nursery.start_soon(yellow.liteon)
85+
async with anyio.create_task_group() as nursery:
86+
await nursery.spawn(yellowbutton.push)
87+
await nursery.spawn(yellow.liteon)
8588

8689

8790
if __name__ == "__main__":
88-
trio.run(main, 1)
91+
anyio.run(main, 1, backend="trio")

0 commit comments

Comments
 (0)