]> vault307.fbx.one Git - ir_remote.git/blob - primitives/tests/switches.py
infrared remote
[ir_remote.git] / primitives / tests / switches.py
1 # Test/demo programs for Switch and Pushbutton classes
2 # Tested on Pyboard but should run on other microcontroller platforms
3 # running MicroPython with uasyncio library.
4
5 # Copyright (c) 2018-2022 Peter Hinch
6 # Released under the MIT License (MIT) - see LICENSE file
7 # Now executes .deinit()
8
9 # To run:
10 # from primitives.tests.switches import *
11 # test_sw() # For example
12
13 from machine import Pin
14 from pyb import LED
15 from primitives import Switch, Pushbutton
16 import uasyncio as asyncio
17
18 helptext = '''
19 Test using switch or pushbutton between X1 and gnd.
20 Ground pin X2 to terminate test.
21
22 '''
23 tests = '''
24 \x1b[32m
25 Available tests:
26 test_sw Switch test.
27 test_swcb Switch with callback.
28 test_sw_event Switch with event.
29 test_btn Pushutton launching coros.
30 test_btncb Pushbutton launching callbacks.
31 btn_dynamic Change coros launched at runtime.
32 btn_event Pushbutton event interface.
33 \x1b[39m
34 '''
35 print(tests)
36
37 # Pulse an LED (coroutine)
38 async def pulse(led, ms):
39 led.on()
40 await asyncio.sleep_ms(ms)
41 led.off()
42
43 # Pulse an LED when an event triggered
44 async def evt_pulse(event, led):
45 while True:
46 event.clear()
47 await event.wait()
48 led.on()
49 await asyncio.sleep_ms(500)
50 led.off()
51
52 # Toggle an LED (callback)
53 def toggle(led):
54 led.toggle()
55
56 # Quit test by connecting X2 to ground
57 async def killer(obj):
58 pin = Pin('X2', Pin.IN, Pin.PULL_UP)
59 while pin.value():
60 await asyncio.sleep_ms(50)
61 obj.deinit()
62 await asyncio.sleep_ms(0)
63
64 def run(obj):
65 try:
66 asyncio.run(killer(obj))
67 except KeyboardInterrupt:
68 print('Interrupted')
69 finally:
70 asyncio.new_event_loop()
71 print(tests)
72
73
74 # Test for the Switch class passing coros
75 def test_sw():
76 s = '''
77 close pulses green
78 open pulses red
79 '''
80 print('Test of switch scheduling coroutines.')
81 print(helptext)
82 print(s)
83 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
84 red = LED(1)
85 green = LED(2)
86 sw = Switch(pin)
87 # Register coros to launch on contact close and open
88 sw.close_func(pulse, (green, 1000))
89 sw.open_func(pulse, (red, 1000))
90 run(sw)
91
92 # Test for the switch class with a callback
93 def test_swcb():
94 s = '''
95 close toggles red
96 open toggles green
97 '''
98 print('Test of switch executing callbacks.')
99 print(helptext)
100 print(s)
101 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
102 red = LED(1)
103 green = LED(2)
104 sw = Switch(pin)
105 # Register a coro to launch on contact close
106 sw.close_func(toggle, (red,))
107 sw.open_func(toggle, (green,))
108 run(sw)
109
110 # Test for the Switch class (events)
111 async def do_sw_event():
112 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
113 sw = Switch(pin)
114 sw.open_func(None)
115 sw.close_func(None)
116 tasks = []
117 for event, led in ((sw.close, 1), (sw.open, 2)):
118 tasks.append(asyncio.create_task(evt_pulse(event, LED(led))))
119 await killer(sw)
120 for task in tasks:
121 task.cancel()
122
123 def test_sw_event():
124 s = '''
125 close pulse red
126 open pulses green
127 '''
128 print('Test of switch triggering events.')
129 print(helptext)
130 print(s)
131 try:
132 asyncio.run(do_sw_event())
133 except KeyboardInterrupt:
134 print('Interrupted')
135 finally:
136 asyncio.new_event_loop()
137 print(tests)
138
139 # Test for the Pushbutton class (coroutines)
140 # Pass True to test suppress
141 def test_btn(suppress=False, lf=True, df=True):
142 s = '''
143 press pulses red
144 release pulses green
145 double click pulses yellow
146 long press pulses blue
147 '''
148 print('Test of pushbutton scheduling coroutines.')
149 print(helptext)
150 print(s)
151 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
152 red = LED(1)
153 green = LED(2)
154 yellow = LED(3)
155 blue = LED(4)
156 pb = Pushbutton(pin, suppress)
157 pb.press_func(pulse, (red, 1000))
158 pb.release_func(pulse, (green, 1000))
159 if df:
160 print('Doubleclick enabled')
161 pb.double_func(pulse, (yellow, 1000))
162 if lf:
163 print('Long press enabled')
164 pb.long_func(pulse, (blue, 1000))
165 run(pb)
166
167 # Test for the Pushbutton class (callbacks)
168 def test_btncb():
169 s = '''
170 press toggles red
171 release toggles green
172 double click toggles yellow
173 long press toggles blue
174 '''
175 print('Test of pushbutton executing callbacks.')
176 print(helptext)
177 print(s)
178 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
179 red = LED(1)
180 green = LED(2)
181 yellow = LED(3)
182 blue = LED(4)
183 pb = Pushbutton(pin)
184 pb.press_func(toggle, (red,))
185 pb.release_func(toggle, (green,))
186 pb.double_func(toggle, (yellow,))
187 pb.long_func(toggle, (blue,))
188 run(pb)
189
190 # Test for the Pushbutton class where callback coros change dynamically
191 def setup(pb, press, release, dbl, lng, t=1000):
192 s = '''
193 Functions are changed:
194 LED's pulse for 2 seconds
195 press pulses blue
196 release pulses red
197 double click pulses green
198 long pulses yellow
199 '''
200 pb.press_func(pulse, (press, t))
201 pb.release_func(pulse, (release, t))
202 pb.double_func(pulse, (dbl, t))
203 if lng is not None:
204 pb.long_func(pulse, (lng, t))
205 print(s)
206
207 def btn_dynamic():
208 s = '''
209 press pulses red
210 release pulses green
211 double click pulses yellow
212 long press changes button functions.
213 '''
214 print('Test of pushbutton scheduling coroutines.')
215 print(helptext)
216 print(s)
217 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
218 red = LED(1)
219 green = LED(2)
220 yellow = LED(3)
221 blue = LED(4)
222 pb = Pushbutton(pin)
223 setup(pb, red, green, yellow, None)
224 pb.long_func(setup, (pb, blue, red, green, yellow, 2000))
225 run(pb)
226
227 # Test for the Pushbutton class (events)
228 async def do_btn_event():
229 pin = Pin('X1', Pin.IN, Pin.PULL_UP)
230 pb = Pushbutton(pin)
231 pb.press_func(None)
232 pb.release_func(None)
233 pb.double_func(None)
234 pb.long_func(None)
235 tasks = []
236 for event, led in ((pb.press, 1), (pb.release, 2), (pb.double, 3), (pb.long, 4)):
237 tasks.append(asyncio.create_task(evt_pulse(event, LED(led))))
238 await killer(pb)
239 for task in tasks:
240 task.cancel()
241
242 def btn_event():
243 s = '''
244 press pulse red
245 release pulses green
246 double click pulses yellow
247 long press pulses blue
248 '''
249 print('Test of pushbutton triggering events.')
250 print(helptext)
251 print(s)
252 try:
253 asyncio.run(do_btn_event())
254 except KeyboardInterrupt:
255 print('Interrupted')
256 finally:
257 asyncio.new_event_loop()
258 print(tests)
259