1
1
import time
2
- import trio
3
- import trio_gpio as gpio
2
+ import anyio
3
+ import asyncgpio as gpio
4
4
5
5
"""
6
6
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 .
8
8
Use at your own risk.
9
9
10
10
If you aren't sure about how to hook up a button and led to your board, there are a lot of examples online.
11
11
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
13
13
who helped me out.
14
14
"""
15
15
@@ -20,31 +20,31 @@ class Led:
20
20
# called at the same time or trio might await at the wrong spot.
21
21
def __init__ (self , line ):
22
22
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 ()
25
25
26
26
async def liteon (self ):
27
27
with gpio .open_chip () as chip :
28
28
with chip .line (self .x ).open (direction = gpio .DIRECTION_OUTPUT ) as line :
29
29
self ._on .clear ()
30
- self ._off .set ()
30
+ await self ._off .set ()
31
31
while True :
32
32
if self ._on .is_set ():
33
33
line .value = 1
34
34
# print('lite on')
35
35
await self ._off .wait ()
36
- self ._on . clear ()
36
+ self ._on = anyio . create_event ()
37
37
elif self ._off .is_set ():
38
38
line .value = 0
39
39
# print('lite off')d
40
40
await self ._on .wait ()
41
- self ._off . clear ()
41
+ self ._off = anyio . create_event ()
42
42
else :
43
43
# should never be reached.
44
44
# if the code does reach here,
45
45
# turn off the power to whatever is being powered
46
46
print ('error: both are off.' )
47
- self ._off .set ()
47
+ await self ._off .set ()
48
48
49
49
50
50
class Button :
@@ -69,20 +69,23 @@ async def push(self):
69
69
now = float (str (secs )+ '.' + str (ns_secs ))
70
70
if now >= last + .25 :
71
71
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 ()
73
76
last = now
74
77
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
76
79
# and the button that controls the yellow is hooked to pin 23.
77
80
yellow = Led (21 )
78
81
yellowbutton = Button (23 , yellow ._on , yellow ._off )
79
82
80
83
81
84
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 )
85
88
86
89
87
90
if __name__ == "__main__" :
88
- trio .run (main , 1 )
91
+ anyio .run (main , 1 , backend = "trio" )
0 commit comments