@@ -33,14 +33,16 @@ This module provides the following classes:
33
33
used to run a callback or to schedule a coro. Its state can be tested by any
34
34
coro.
35
35
36
- The module ` astests.py ` provides examples of usage.
36
+ The module ` astests.py ` provides examples of usage. In the following text the
37
+ term ** function** implies a Python ` callable ` : namely a function, bound method,
38
+ coroutine or bound coroutine interchangeably.
37
39
38
40
## 3.1 Switch class
39
41
40
42
This assumes a normally open switch connected between a pin and ground. The pin
41
- should be initialised as an input with a pullup. Functions may be specified to
42
- run on contact closure or opening. Functions can be callbacks or coroutines;
43
- coroutines will be scheduled for execution and will run asynchronously.
43
+ should be initialised as an input with a pullup. A ** function ** may be
44
+ specified to run on contact closure or opening; where the ** function ** is a
45
+ coroutine it will be scheduled for execution and will run asynchronously.
44
46
Debouncing is implicit: contact bounce will not cause spurious execution of
45
47
these functions.
46
48
@@ -50,10 +52,10 @@ Constructor argument (mandatory):
50
52
51
53
Methods:
52
54
53
- 1 . ` close_func ` Args: ` func ` (mandatory) a function to run on contact
54
- closure. ` args ` a tuple of arguments for the function (default ` () ` )
55
- 2 . ` open_func ` Args: ` func ` (mandatory) a function to run on contact open.
56
- ` args ` a tuple of arguments for the function (default ` () ` )
55
+ 1 . ` close_func ` Args: ` func ` (mandatory) a ** function** to run on contact
56
+ closure. ` args ` a tuple of arguments for the ** function** (default ` () ` )
57
+ 2 . ` open_func ` Args: ` func ` (mandatory) a ** function** to run on contact open.
58
+ ` args ` a tuple of arguments for the ** function** (default ` () ` )
57
59
3 . ` __call__ ` Call syntax e.g. ` myswitch() ` returns the physical debounced
58
60
state of the switch i.e. 0 if grounded, 1 if connected to ` 3V3 ` .
59
61
@@ -62,6 +64,28 @@ Methods 1 and 2 should be called before starting the scheduler.
62
64
Class attribute:
63
65
1 . ` debounce_ms ` Debounce time in ms. Default 50.
64
66
67
+ ``` python
68
+ from pyb import LED
69
+ from machine import Pin
70
+ import uasyncio as asyncio
71
+ from aswitch import Switch
72
+
73
+ async def pulse (led , ms ):
74
+ led.on()
75
+ await asyncio.sleep_ms(ms)
76
+ led.off()
77
+
78
+ async def my_app ():
79
+ await asyncio.sleep(60 ) # Dummy application code
80
+
81
+ pin = Pin(' X1' , Pin.IN , Pin.PULL_UP ) # Hardware: switch to gnd
82
+ red = LED(1 )
83
+ sw = Switch(pin)
84
+ sw.close_func(pulse, (red, 1000 )) # Note how coro and args are passed
85
+ loop = asyncio.get_event_loop()
86
+ loop.run_until_complete(my_app()) # Run main application code
87
+ ```
88
+
65
89
## 3.2 Pushbutton class
66
90
67
91
This can support normally open or normally closed switches, connected to ` gnd `
@@ -70,11 +94,11 @@ initialised appropriately. The assumption is that on initialisation the button
70
94
is not pressed.
71
95
72
96
The Pushbutton class uses logical rather than physical state: a button's state
73
- is considered ` True ` if pressed, otherwise ` False ` regardless of its
74
- physical implementation.
97
+ is considered ` True ` if pressed, otherwise ` False ` regardless of its physical
98
+ implementation.
75
99
76
- Functions may be specified to run on button press, release, double click or
77
- long press events. Functions can be callbacks or coroutines; coroutines will be
100
+ ** function ** instances may be specified to run on button press, release, double
101
+ click or long press events; where the ** function ** is a coroutine it will be
78
102
scheduled for execution and will run asynchronously.
79
103
80
104
Constructor argument (mandatory):
@@ -83,14 +107,14 @@ Constructor argument (mandatory):
83
107
84
108
Methods:
85
109
86
- 1 . ` press_func ` Args: ` func ` (mandatory) a function to run on button push.
87
- ` args ` a tuple of arguments for the function (default ` () ` ).
88
- 2 . ` release_func ` Args: ` func ` (mandatory) a function to run on button
89
- release. ` args ` a tuple of arguments for the function (default ` () ` ).
90
- 3 . ` long_func ` Args: ` func ` (mandatory) a function to run on long button
91
- push. ` args ` a tuple of arguments for the function (default ` () ` ).
92
- 4 . ` double_func ` Args: ` func ` (mandatory) a function to run on double
93
- push. ` args ` a tuple of arguments for the function (default ` () ` ).
110
+ 1 . ` press_func ` Args: ` func ` (mandatory) a ** function** to run on button push.
111
+ ` args ` a tuple of arguments for the ** function** (default ` () ` ).
112
+ 2 . ` release_func ` Args: ` func ` (mandatory) a ** function** to run on button
113
+ release. ` args ` a tuple of arguments for the ** function** (default ` () ` ).
114
+ 3 . ` long_func ` Args: ` func ` (mandatory) a ** function** to run on long button
115
+ push. ` args ` a tuple of arguments for the ** function** (default ` () ` ).
116
+ 4 . ` double_func ` Args: ` func ` (mandatory) a ** function** to run on double
117
+ push. ` args ` a tuple of arguments for the ** function** (default ` () ` ).
94
118
5 . ` __call__ ` Call syntax e.g. ` mybutton() ` Returns the logical debounced
95
119
state of the button (` True ` corresponds to pressed).
96
120
6 . ` rawstate() ` Returns the logical instantaneous state of the button. There
@@ -103,6 +127,26 @@ Class attributes:
103
127
2 . ` long_press_ms ` Threshold time in ms for a long press. Default 1000.
104
128
3 . ` double_click_ms ` Threshold time in ms for a double click. Default 400.
105
129
130
+ ``` python
131
+ from pyb import LED
132
+ from machine import Pin
133
+ import uasyncio as asyncio
134
+ from aswitch import Pushbutton
135
+
136
+ def toggle (led ):
137
+ led.toggle()
138
+
139
+ async def my_app ():
140
+ await asyncio.sleep(60 ) # Dummy
141
+
142
+ pin = Pin(' X1' , Pin.IN , Pin.PULL_UP ) # Pushbutton to gnd
143
+ red = LED(1 )
144
+ pb = Pushbutton(pin)
145
+ pb.press_func(toggle, (red,)) # Note how function and args are passed
146
+ loop = asyncio.get_event_loop()
147
+ loop.run_until_complete(my_app()) # Run main application code
148
+ ```
149
+
106
150
## 3.3 Delay_ms class
107
151
108
152
This implements the software equivalent of a retriggerable monostable or a
@@ -114,15 +158,15 @@ as it is triggered before the time specified in the preceeding trigger it will
114
158
never time out.
115
159
116
160
If it does time out the ` running ` state will revert to ` False ` . This can be
117
- interrogated by the object's ` running() ` method. In addition a function can
118
- be specified to the constructor. This will execute when a timeout occurs. The
119
- function can be a callback or a coroutine; in the latter case it will be
120
- scheduled for execution and will run asynchronously.
161
+ interrogated by the object's ` running() ` method. In addition a ** function** can
162
+ be specified to the constructor. This will execute when a timeout occurs; where
163
+ the ** function** is a coroutine it will be scheduled for execution and will run
164
+ asynchronously.
121
165
122
166
Constructor arguments (defaults in brackets):
123
167
124
- 1 . ` func ` The function to call on timeout (default ` None ` ).
125
- 2 . ` args ` A tuple of arguments for the function (default ` () ` ).
168
+ 1 . ` func ` The ** function** to call on timeout (default ` None ` ).
169
+ 2 . ` args ` A tuple of arguments for the ** function** (default ` () ` ).
126
170
3 . ` can_alloc ` Boolean, default ` True ` . See below.
127
171
128
172
Methods:
@@ -138,6 +182,28 @@ If the `trigger` method is to be called from an interrupt service routine the
138
182
to use a slightly less efficient mode which avoids RAM allocation when
139
183
` trigger ` runs.
140
184
185
+ In this example a 3 second timer starts when the button is pressed. If it is
186
+ pressed repeatedly the timeout will not be triggered. If it is not pressed for
187
+ 3 seconds the timeout triggers and the LED lights.
188
+
189
+ ``` python
190
+ from pyb import LED
191
+ from machine import Pin
192
+ import uasyncio as asyncio
193
+ from aswitch import Pushbutton, Delay_ms
194
+
195
+ async def my_app ():
196
+ await asyncio.sleep(60 ) # Dummy
197
+
198
+ pin = Pin(' X1' , Pin.IN , Pin.PULL_UP ) # Pushbutton to gnd
199
+ red = LED(1 )
200
+ pb = Pushbutton(pin)
201
+ d = Delay_ms(lambda led : led.on(), (red,))
202
+ pb.press_func(d.trigger, (3000 ,)) # Note how function and args are passed
203
+ loop = asyncio.get_event_loop()
204
+ loop.run_until_complete(my_app()) # Run main application code
205
+ ```
206
+
141
207
# 4. Module astests.py
142
208
143
209
This provides demonstration/test functions for the ` Switch ` and ` Pushbutton `
0 commit comments