@@ -16,8 +16,7 @@ events.
16
16
and also a software retriggerable delay object. Pushbuttons are a
17
17
generalisation of switches providing logical rather than physical status along
18
18
with double-clicked and long pressed events.
19
- 3 . ` asyn.py ` Provides synchronisation primitives. Required by ` aswitch.py ` .
20
- 4 . ` astests.py ` Test/demonstration programs for ` aswitch.py ` .
19
+ 3 . ` astests.py ` Test/demonstration programs for ` aswitch.py ` .
21
20
22
21
# 3. Module aswitch.py
23
22
@@ -104,7 +103,7 @@ scheduled for execution and will run asynchronously.
104
103
Constructor arguments:
105
104
106
105
1 . ` pin ` Mandatory. The initialised Pin instance.
107
- 2 . ` lpmode ` Default ` False ` . See below.
106
+ 2 . ` suppress ` Default ` False ` . See 3.2.1 below.
108
107
109
108
Methods:
110
109
@@ -148,17 +147,28 @@ loop = asyncio.get_event_loop()
148
147
loop.run_until_complete(my_app()) # Run main application code
149
148
```
150
149
151
- The ` lpmode ` constructor argument modifies the behaviour of ` press_func ` in the
152
- event that a ` long_func ` is specified.
153
-
154
- If ` lpmode ` is ` False ` , if a button press occurs ` press_func ` runs immediately;
155
- ` long_func ` runs if the button is still pressed when the timeout has elapsed.
156
- If ` lpmode ` is ` True ` , ` press_func ` is delayed until button release. If, at the
157
- time of release, ` long_func ` has run, ` press_func ` will be suppressed.
158
-
159
- The default provides for low latency but a long button press will trigger
160
- ` press_func ` and ` long_func ` . ` lpmode ` = ` True ` prevents ` press_func ` from
161
- running.
150
+ ### 3.2.1 The suppress constructor argument
151
+
152
+ When the button is pressed ` press_func ` runs immediately. This minimal latency
153
+ is ideal for applications such as games, but does imply that in the event of a
154
+ long press, both ` press_func ` and ` long_func ` run: ` press_func ` immediately and
155
+ ` long_func ` if the button is still pressed when the timer has elapsed. Similar
156
+ reasoning applies to the double click function.
157
+
158
+ There can be a need for a ** function** which runs if a button is pressed but
159
+ only if a doubleclick or long press function does not run. The soonest that the
160
+ absence of a long press can be detected is on button release. The absence of a
161
+ double click can only be detected when the double click timer times out without
162
+ a second press occurring.
163
+
164
+ This ** function** is the ` release_func ` . If the ` suppress ` constructor arg is
165
+ set, ` release_func ` will be launched as follows:
166
+ 1 . If ` double_func ` does not exist on rapid button release.
167
+ 2 . If ` double_func ` exists, after the expiration of the doubleclick timer.
168
+ 3 . If ` long_func ` exists and the press duration causes ` long_func ` to be
169
+ launched, ` release_func ` will not be launched.
170
+ 4 . If ` double_func ` exists and a double click occurs, ` release_func ` will not
171
+ be launched.
162
172
163
173
## 3.3 Delay_ms class
164
174
@@ -192,6 +202,7 @@ Methods:
192
202
2 . ` stop ` No argument. Cancels the timeout, setting the ` running ` status
193
203
` False ` . The timer can be restarted by issuing ` trigger ` again.
194
204
3 . ` running ` No argument. Returns the running status of the object.
205
+ 4 . ` __call__ ` Alias for running.
195
206
196
207
If the ` trigger ` method is to be called from an interrupt service routine the
197
208
` can_alloc ` constructor arg should be ` False ` . This causes the delay object
@@ -236,11 +247,18 @@ of its behaviour if the switch is toggled rapidly.
236
247
237
248
Demonstrates the use of callbacks to toggle the red and green LED's.
238
249
239
- ## 4.3 Function test_btn()
250
+ ## 4.3 Function test_btn(lpmode=False )
240
251
241
252
This will flash the red LED on button push, and the green LED on release. A
242
253
long press will flash the blue LED and a double-press the yellow one.
243
254
255
+ Test the launching of coroutines and also the ` suppress ` constructor arg.
256
+
257
+ It takes three optional positional boolean args:
258
+ 1 . ` Suppresss=False ` If ` True ` sets the ` suppress ` constructor arg.
259
+ 2 . ` lf=True ` Declare a long press coro.
260
+ 3 . ` df=true ` Declare a double click coro.
261
+
244
262
The note below on race conditions applies.
245
263
246
264
## 4.4 Function test_btncb()
@@ -268,3 +286,6 @@ In the case of this test program it might be to ignore events while a similar
268
286
one is running, or to extend the timer to prolong the LED illumination.
269
287
Alternatively a subsequent button press might be required to terminate the
270
288
illumination. The "right" behaviour is application dependent.
289
+
290
+ A further consequence of scheduling new coroutine instances when one or more
291
+ are already running is that the ` uasyncio ` queue can fill causing an exception.
0 commit comments