11import time
2- import trio
3- import trio_gpio as gpio
2+ import anyio
3+ import asyncgpio as gpio
44
55"""
66This 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 .
88Use at your own risk.
99
1010If 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
1313who 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
5050class 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.
7780yellow = Led (21 )
7881yellowbutton = Button (23 , yellow ._on , yellow ._off )
7982
8083
8184async 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
8790if __name__ == "__main__" :
88- trio .run (main , 1 )
91+ anyio .run (main , 1 , backend = "trio" )
0 commit comments