]>
vault307.fbx.one Git - ir_remote.git/blob - primitives/tests/event_test.py
1 # event_test.py Test WaitAll, WaitAny, ESwwitch, EButton
3 # Copyright (c) 2022 Peter Hinch
4 # Released under the MIT License (MIT) - see LICENSE file
6 # from primitives.tests.event_test import *
8 import uasyncio
as asyncio
9 from primitives
import Delay_ms
, WaitAny
, ESwitch
, WaitAll
, EButton
12 events
= [asyncio
.Event() for _
in range(4)]
14 async def set_events(*ev
):
16 await asyncio
.sleep(1)
25 async def can(obj
, tim
):
26 await asyncio
.sleep(tim
)
27 print("About to cancel")
34 async def wait_test():
59 wa
= WaitAny((events
[0], events
[1], WaitAll((events
[2], events
[3]))))
60 asyncio
.create_task(set_events(0))
62 clear("Tested WaitAny 0")
63 asyncio
.create_task(set_events(1))
65 clear("Tested WaitAny 1")
66 asyncio
.create_task(set_events(2, 3))
68 clear("Tested WaitAll 2, 3")
69 wa
= WaitAll((WaitAny((events
[0], events
[1])), WaitAny((events
[2], events
[3]))))
70 asyncio
.create_task(set_events(0, 3))
72 clear("Tested WaitAny 0, 3")
73 task
= asyncio
.create_task(wa
.wait())
74 asyncio
.create_task(set_events(0, 1)) # Does nothing
75 asyncio
.create_task(can(task
, 3))
79 except asyncio
.CancelledError
: # TODO why must we trap this?
81 print("Waiting for 4s")
83 await asyncio
.wait_for(wa
.wait(), 4)
84 except asyncio
.TimeoutError
:
93 async def monitor(evt
, v
, verbose
):
99 verbose
and print("Got", hex(v
), hex(val
))
101 async def pulse(ms
=100):
103 await asyncio
.sleep_ms(ms
)
111 print(f
"Fail: expected {e} got {v}")
114 async def btest(btn
, verbose
, supp
):
117 events
= btn
.press
, btn
.release
, btn
.double
, btn
.long
119 for n
, evt
in enumerate(events
):
120 tasks
.append(asyncio
.create_task(monitor(evt
, 1 << 3 * n
, verbose
)))
121 await asyncio
.sleep(1)
122 print("Start short press test")
124 await asyncio
.sleep(1)
125 verbose
and print("Test of short press", hex(val
))
128 await asyncio
.sleep(1)
129 print("Start long press test")
131 await asyncio
.sleep(4)
132 verbose
and print("Long press", hex(val
))
133 exp
= 0x208 if supp
else 0x209
136 await asyncio
.sleep(1)
137 print("Start double press test")
139 await asyncio
.sleep_ms(100)
141 await asyncio
.sleep(4)
142 verbose
and print("Double press", hex(val
))
143 exp
= 0x48 if supp
else 0x52
148 async def stest(sw
, verbose
):
151 events
= sw
.open, sw
.close
153 for n
, evt
in enumerate(events
):
154 tasks
.append(asyncio
.create_task(monitor(evt
, 1 << 3 * n
, verbose
)))
155 asyncio
.create_task(pulse(2000))
156 await asyncio
.sleep(1)
158 await asyncio
.sleep(4) # Wait for any spurious events
159 verbose
and print("Switch close and open", hex(val
))
164 async def switch_test(pol
, verbose
):
165 global val
, pout
, polarity
167 pin
= Pin('Y1', Pin
.IN
)
168 pout
= Pin('Y2', Pin
.OUT
, value
=pol
)
169 print("Testing EButton.")
170 print("suppress == False")
172 await btest(btn
, verbose
, False)
173 print("suppress == True")
174 btn
= EButton(pin
, suppress
=True)
175 await btest(btn
, verbose
, True)
176 print("Testing ESwitch")
177 sw
= ESwitch(pin
, pol
)
178 await stest(sw
, verbose
)
179 print("Failures occurred.") if fail
else print("All tests passed.")
185 1. test_switches(polarity=1, verbose=False) Test the ESwitch and Ebutton classe.
186 2. test_wait() Test the WaitAny and WaitAll primitives.
188 Switch tests assume a Pyboard with a link between Y1 and Y2.
194 def test_switches(polarity
=1, verbose
=False):
196 asyncio
.run(switch_test(polarity
, verbose
)) # polarity 1/0 is normal (off) electrical state.
198 asyncio
.new_event_loop()
203 asyncio
.run(wait_test())
205 asyncio
.new_event_loop()