]>
vault307.fbx.one Git - ir_remote.git/blob - 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.
5 # Copyright (c) 2018-2022 Peter Hinch
6 # Released under the MIT License (MIT) - see LICENSE file
7 # Now executes .deinit()
10 # from primitives.tests.switches import *
11 # test_sw() # For example
13 from machine
import Pin
15 from primitives
import Switch
, Pushbutton
16 import uasyncio
as asyncio
19 Test using switch or pushbutton between X1 and gnd.
20 Ground pin X2 to terminate 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.
37 # Pulse an LED (coroutine)
38 async def pulse(led
, ms
):
40 await asyncio
.sleep_ms(ms
)
43 # Pulse an LED when an event triggered
44 async def evt_pulse(event
, led
):
49 await asyncio
.sleep_ms(500)
52 # Toggle an LED (callback)
56 # Quit test by connecting X2 to ground
57 async def killer(obj
):
58 pin
= Pin('X2', Pin
.IN
, Pin
.PULL_UP
)
60 await asyncio
.sleep_ms(50)
62 await asyncio
.sleep_ms(0)
66 asyncio
.run(killer(obj
))
67 except KeyboardInterrupt:
70 asyncio
.new_event_loop()
74 # Test for the Switch class passing coros
80 print('Test of switch scheduling coroutines.')
83 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
87 # Register coros to launch on contact close and open
88 sw
.close_func(pulse
, (green
, 1000))
89 sw
.open_func(pulse
, (red
, 1000))
92 # Test for the switch class with a callback
98 print('Test of switch executing callbacks.')
101 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
105 # Register a coro to launch on contact close
106 sw
.close_func(toggle
, (red
,))
107 sw
.open_func(toggle
, (green
,))
110 # Test for the Switch class (events)
111 async def do_sw_event():
112 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
117 for event
, led
in ((sw
.close
, 1), (sw
.open, 2)):
118 tasks
.append(asyncio
.create_task(evt_pulse(event
, LED(led
))))
128 print('Test of switch triggering events.')
132 asyncio
.run(do_sw_event())
133 except KeyboardInterrupt:
136 asyncio
.new_event_loop()
139 # Test for the Pushbutton class (coroutines)
140 # Pass True to test suppress
141 def test_btn(suppress
=False, lf
=True, df
=True):
145 double click pulses yellow
146 long press pulses blue
148 print('Test of pushbutton scheduling coroutines.')
151 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
156 pb
= Pushbutton(pin
, suppress
)
157 pb
.press_func(pulse
, (red
, 1000))
158 pb
.release_func(pulse
, (green
, 1000))
160 print('Doubleclick enabled')
161 pb
.double_func(pulse
, (yellow
, 1000))
163 print('Long press enabled')
164 pb
.long_func(pulse
, (blue
, 1000))
167 # Test for the Pushbutton class (callbacks)
171 release toggles green
172 double click toggles yellow
173 long press toggles blue
175 print('Test of pushbutton executing callbacks.')
178 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
184 pb
.press_func(toggle
, (red
,))
185 pb
.release_func(toggle
, (green
,))
186 pb
.double_func(toggle
, (yellow
,))
187 pb
.long_func(toggle
, (blue
,))
190 # Test for the Pushbutton class where callback coros change dynamically
191 def setup(pb
, press
, release
, dbl
, lng
, t
=1000):
193 Functions are changed:
194 LED's pulse for 2 seconds
197 double click pulses green
200 pb
.press_func(pulse
, (press
, t
))
201 pb
.release_func(pulse
, (release
, t
))
202 pb
.double_func(pulse
, (dbl
, t
))
204 pb
.long_func(pulse
, (lng
, t
))
211 double click pulses yellow
212 long press changes button functions.
214 print('Test of pushbutton scheduling coroutines.')
217 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
223 setup(pb
, red
, green
, yellow
, None)
224 pb
.long_func(setup
, (pb
, blue
, red
, green
, yellow
, 2000))
227 # Test for the Pushbutton class (events)
228 async def do_btn_event():
229 pin
= Pin('X1', Pin
.IN
, Pin
.PULL_UP
)
232 pb
.release_func(None)
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
))))
246 double click pulses yellow
247 long press pulses blue
249 print('Test of pushbutton triggering events.')
253 asyncio
.run(do_btn_event())
254 except KeyboardInterrupt:
257 asyncio
.new_event_loop()