Skip to content

Commit b0dcc78

Browse files
committed
Implement Pusbutton lpmode.
1 parent 26e49b5 commit b0dcc78

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

DRIVERS.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ implementation.
101101
click or long press events; where the **function** is a coroutine it will be
102102
scheduled for execution and will run asynchronously.
103103

104-
Constructor argument (mandatory):
104+
Constructor arguments:
105105

106-
1. `pin` The initialised Pin instance.
106+
1. `pin` Mandatory. The initialised Pin instance.
107+
2. `lpmode` Default `False`. See below.
107108

108109
Methods:
109110

@@ -147,6 +148,18 @@ loop = asyncio.get_event_loop()
147148
loop.run_until_complete(my_app()) # Run main application code
148149
```
149150

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+
150163
## 3.3 Delay_ms class
151164

152165
This implements the software equivalent of a retriggerable monostable or a

astests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ def test_swcb():
6060
loop.run_until_complete(killer())
6161

6262
# Test for the Pushbutton class (coroutines)
63-
def test_btn():
63+
# Pass True to test lpmode
64+
def test_btn(lpmode=False):
6465
print('Test of pushbutton scheduling coroutines.')
6566
print(helptext)
6667
pin = Pin('X1', Pin.IN, Pin.PULL_UP)
6768
red = LED(1)
6869
green = LED(2)
6970
yellow = LED(3)
7071
blue = LED(4)
71-
pb = Pushbutton(pin)
72+
pb = Pushbutton(pin, lpmode)
7273
pb.press_func(pulse, (red, 1000))
7374
pb.release_func(pulse, (green, 1000))
7475
pb.double_func(pulse, (yellow, 1000))

aswitch.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ class Pushbutton(object):
118118
debounce_ms = 50
119119
long_press_ms = 1000
120120
double_click_ms = 400
121-
def __init__(self, pin):
121+
def __init__(self, pin, lpmode=False):
122122
self.pin = pin # Initialise for input
123+
self._lpmode = lpmode
124+
self._press_pend = False
123125
self._true_func = False
124126
self._false_func = False
125127
self._double_func = False
@@ -176,13 +178,19 @@ async def buttoncheck(self):
176178
# First click: start doubleclick timer
177179
doubledelay.trigger(Pushbutton.double_click_ms)
178180
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)
180185
else:
181186
# Button release
182-
if self._long_func and longdelay.running():
187+
if longdelay.running():
183188
# Avoid interpreting a second click as a long push
184189
longdelay.stop()
190+
if self._press_pend:
191+
launch(self._true_func, self._true_args)
185192
if self._false_func:
186193
launch(self._false_func, self._false_args)
194+
self._press_pend = False
187195
# Ignore state changes until switch has settled
188196
await asyncio.sleep_ms(Pushbutton.debounce_ms)

0 commit comments

Comments
 (0)