forked from peterhinch/micropython-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastests.py
132 lines (122 loc) · 3.33 KB
/
astests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Test/demo programs for the aswitch module.
# Tested on Pyboard but should run on other microcontroller platforms
# running MicroPython with uasyncio library.
# Author: Peter Hinch.
# Copyright Peter Hinch 2017-2018 Released under the MIT license.
from machine import Pin
from pyb import LED
from aswitch import Switch, Pushbutton
import uasyncio as asyncio
helptext = '''
Test using switch or pushbutton between X1 and gnd.
Ground pin X2 to terminate test.
Soft reset (ctrl-D) after each test.
'''
tests = '''
Available tests:
test_sw Switch test
test_swcb Switch with callback
test_btn Pushutton launching coros
test_btncb Pushbutton launching callbacks
'''
print(tests)
# Pulse an LED (coroutine)
async def pulse(led, ms):
led.on()
await asyncio.sleep_ms(ms)
led.off()
# Toggle an LED (callback)
def toggle(led):
led.toggle()
# Quit test by connecting X2 to ground
async def killer():
pin = Pin('X2', Pin.IN, Pin.PULL_UP)
while pin.value():
await asyncio.sleep_ms(50)
# Test for the Switch class passing coros
def test_sw():
s = '''
close pulses green
open pulses red
'''
print('Test of switch scheduling coroutines.')
print(helptext)
print(s)
pin = Pin('X1', Pin.IN, Pin.PULL_UP)
red = LED(1)
green = LED(2)
sw = Switch(pin)
# Register coros to launch on contact close and open
sw.close_func(pulse, (green, 1000))
sw.open_func(pulse, (red, 1000))
loop = asyncio.get_event_loop()
loop.run_until_complete(killer())
# Test for the switch class with a callback
def test_swcb():
s = '''
close toggles red
open toggles green
'''
print('Test of switch executing callbacks.')
print(helptext)
print(s)
pin = Pin('X1', Pin.IN, Pin.PULL_UP)
red = LED(1)
green = LED(2)
sw = Switch(pin)
# Register a coro to launch on contact close
sw.close_func(toggle, (red,))
sw.open_func(toggle, (green,))
loop = asyncio.get_event_loop()
loop.run_until_complete(killer())
# Test for the Pushbutton class (coroutines)
# Pass True to test suppress
def test_btn(suppress=False, lf=True, df=True):
s = '''
press pulses red
release pulses green
double click pulses yellow
long press pulses blue
'''
print('Test of pushbutton scheduling coroutines.')
print(helptext)
print(s)
pin = Pin('X1', Pin.IN, Pin.PULL_UP)
red = LED(1)
green = LED(2)
yellow = LED(3)
blue = LED(4)
pb = Pushbutton(pin, suppress)
pb.press_func(pulse, (red, 1000))
pb.release_func(pulse, (green, 1000))
if df:
print('Doubleclick enabled')
pb.double_func(pulse, (yellow, 1000))
if lf:
print('Long press enabled')
pb.long_func(pulse, (blue, 1000))
loop = asyncio.get_event_loop()
loop.run_until_complete(killer())
# Test for the Pushbutton class (callbacks)
def test_btncb():
s = '''
press toggles red
release toggles green
double click toggles yellow
long press toggles blue
'''
print('Test of pushbutton executing callbacks.')
print(helptext)
print(s)
pin = Pin('X1', Pin.IN, Pin.PULL_UP)
red = LED(1)
green = LED(2)
yellow = LED(3)
blue = LED(4)
pb = Pushbutton(pin)
pb.press_func(toggle, (red,))
pb.release_func(toggle, (green,))
pb.double_func(toggle, (yellow,))
pb.long_func(toggle, (blue,))
loop = asyncio.get_event_loop()
loop.run_until_complete(killer())