File tree 3 files changed +29
-7
lines changed 3 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -101,9 +101,10 @@ implementation.
101
101
click or long press events; where the ** function** is a coroutine it will be
102
102
scheduled for execution and will run asynchronously.
103
103
104
- Constructor argument (mandatory) :
104
+ Constructor arguments :
105
105
106
- 1 . ` pin ` The initialised Pin instance.
106
+ 1 . ` pin ` Mandatory. The initialised Pin instance.
107
+ 2 . ` lpmode ` Default ` False ` . See below.
107
108
108
109
Methods:
109
110
@@ -147,6 +148,18 @@ loop = asyncio.get_event_loop()
147
148
loop.run_until_complete(my_app()) # Run main application code
148
149
```
149
150
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.
162
+
150
163
## 3.3 Delay_ms class
151
164
152
165
This implements the software equivalent of a retriggerable monostable or a
Original file line number Diff line number Diff line change @@ -60,15 +60,16 @@ def test_swcb():
60
60
loop .run_until_complete (killer ())
61
61
62
62
# Test for the Pushbutton class (coroutines)
63
- def test_btn ():
63
+ # Pass True to test lpmode
64
+ def test_btn (lpmode = False ):
64
65
print ('Test of pushbutton scheduling coroutines.' )
65
66
print (helptext )
66
67
pin = Pin ('X1' , Pin .IN , Pin .PULL_UP )
67
68
red = LED (1 )
68
69
green = LED (2 )
69
70
yellow = LED (3 )
70
71
blue = LED (4 )
71
- pb = Pushbutton (pin )
72
+ pb = Pushbutton (pin , lpmode )
72
73
pb .press_func (pulse , (red , 1000 ))
73
74
pb .release_func (pulse , (green , 1000 ))
74
75
pb .double_func (pulse , (yellow , 1000 ))
Original file line number Diff line number Diff line change @@ -118,8 +118,10 @@ class Pushbutton(object):
118
118
debounce_ms = 50
119
119
long_press_ms = 1000
120
120
double_click_ms = 400
121
- def __init__ (self , pin ):
121
+ def __init__ (self , pin , lpmode = False ):
122
122
self .pin = pin # Initialise for input
123
+ self ._lpmode = lpmode
124
+ self ._press_pend = False
123
125
self ._true_func = False
124
126
self ._false_func = False
125
127
self ._double_func = False
@@ -176,13 +178,19 @@ async def buttoncheck(self):
176
178
# First click: start doubleclick timer
177
179
doubledelay .trigger (Pushbutton .double_click_ms )
178
180
if self ._true_func :
179
- launch (self ._true_func , self ._true_args )
181
+ if self ._long_func and self ._lpmode :
182
+ self ._press_pend = True # Delay launch until release
183
+ else :
184
+ launch (self ._true_func , self ._true_args )
180
185
else :
181
186
# Button release
182
- if self . _long_func and longdelay .running ():
187
+ if longdelay .running ():
183
188
# Avoid interpreting a second click as a long push
184
189
longdelay .stop ()
190
+ if self ._press_pend :
191
+ launch (self ._true_func , self ._true_args )
185
192
if self ._false_func :
186
193
launch (self ._false_func , self ._false_args )
194
+ self ._press_pend = False
187
195
# Ignore state changes until switch has settled
188
196
await asyncio .sleep_ms (Pushbutton .debounce_ms )
You can’t perform that action at this time.
0 commit comments