Skip to content

Commit a685b5a

Browse files
committed
Fix bug where no long press func provided.
1 parent 0d8312a commit a685b5a

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

astests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ def test_btn(suppress=False, lf=True, df=True):
7373
pb.press_func(pulse, (red, 1000))
7474
pb.release_func(pulse, (green, 1000))
7575
if df:
76+
print('Doubleclick enabled')
7677
pb.double_func(pulse, (yellow, 1000))
7778
if lf:
79+
print('Long press enabled')
7880
pb.long_func(pulse, (blue, 1000))
7981
loop = asyncio.get_event_loop()
8082
loop.run_until_complete(killer())

aswitch.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(self, pin, suppress=False):
132132
self._ld = False # Delay_ms instance for long press
133133
self._dd = False # Ditto for doubleclick
134134
self.sense = pin.value() # Convert from electrical to logical value
135-
self.buttonstate = self.rawstate() # Initial state
135+
self.state = self.rawstate() # Initial state
136136
loop = asyncio.get_event_loop()
137137
loop.create_task(self.buttoncheck()) # Thread runs forever
138138

@@ -158,36 +158,31 @@ def rawstate(self):
158158

159159
# Current debounced state of button (True == pressed)
160160
def __call__(self):
161-
return self.buttonstate
161+
return self.state
162162

163163
def _ddto(self): # Doubleclick timeout: no doubleclick occurred
164164
self._dblpend = False
165-
if self._supp:
165+
if self._supp and not self.state:
166166
if not self._ld or (self._ld and not self._ld()):
167167
launch(self._ff, self._fa)
168168

169-
def _ldip(self): # True if a long delay exists and is running
170-
d = self._ld
171-
return d and d()
172-
173169
async def buttoncheck(self):
174-
loop = asyncio.get_event_loop()
175-
if self._lf:
170+
if self._lf: # Instantiate timers if funcs exist
176171
self._ld = Delay_ms(self._lf, self._la)
177172
if self._df:
178173
self._dd = Delay_ms(self._ddto)
179174
while True:
180175
state = self.rawstate()
181176
# State has changed: act on it now.
182-
if state != self.buttonstate:
183-
self.buttonstate = state
184-
if state:
185-
# Button is pressed
186-
if self._lf:
187-
# Start long press delay
177+
if state != self.state:
178+
self.state = state
179+
if state: # Button pressed: launch pressed func
180+
if self._tf:
181+
launch(self._tf, self._ta)
182+
if self._lf: # There's a long func: start long press delay
188183
self._ld.trigger(Pushbutton.long_press_ms)
189184
if self._df:
190-
if self._dd():
185+
if self._dd(): # Second click: timer running
191186
self._dd.stop()
192187
self._dblpend = False
193188
self._dblran = True # Prevent suppressed launch on release
@@ -196,19 +191,18 @@ async def buttoncheck(self):
196191
# First click: start doubleclick timer
197192
self._dd.trigger(Pushbutton.double_click_ms)
198193
self._dblpend = True # Prevent suppressed launch on release
199-
if self._tf:
200-
launch(self._tf, self._ta)
201-
else:
202-
# Button release
194+
else: # Button release. Is there a release func?
203195
if self._ff:
204196
if self._supp:
205-
if self._ldip() and not self._dblpend and not self._dblran:
206-
launch(self._ff, self._fa)
197+
d = self._ld
198+
# If long delay exists, is running and doubleclick status is OK
199+
if not self._dblpend and not self._dblran:
200+
if (d and d()) or not d:
201+
launch(self._ff, self._fa)
207202
else:
208203
launch(self._ff, self._fa)
209-
if self._ldip():
210-
# Avoid interpreting a second click as a long push
211-
self._ld.stop()
204+
if self._ld:
205+
self._ld.stop() # Avoid interpreting a second click as a long push
212206
self._dblran = False
213207
# Ignore state changes until switch has settled
214208
await asyncio.sleep_ms(Pushbutton.debounce_ms)

0 commit comments

Comments
 (0)