@@ -132,7 +132,7 @@ def __init__(self, pin, suppress=False):
132
132
self ._ld = False # Delay_ms instance for long press
133
133
self ._dd = False # Ditto for doubleclick
134
134
self .sense = pin .value () # Convert from electrical to logical value
135
- self .buttonstate = self .rawstate () # Initial state
135
+ self .state = self .rawstate () # Initial state
136
136
loop = asyncio .get_event_loop ()
137
137
loop .create_task (self .buttoncheck ()) # Thread runs forever
138
138
@@ -158,36 +158,31 @@ def rawstate(self):
158
158
159
159
# Current debounced state of button (True == pressed)
160
160
def __call__ (self ):
161
- return self .buttonstate
161
+ return self .state
162
162
163
163
def _ddto (self ): # Doubleclick timeout: no doubleclick occurred
164
164
self ._dblpend = False
165
- if self ._supp :
165
+ if self ._supp and not self . state :
166
166
if not self ._ld or (self ._ld and not self ._ld ()):
167
167
launch (self ._ff , self ._fa )
168
168
169
- def _ldip (self ): # True if a long delay exists and is running
170
- d = self ._ld
171
- return d and d ()
172
-
173
169
async def buttoncheck (self ):
174
- loop = asyncio .get_event_loop ()
175
- if self ._lf :
170
+ if self ._lf : # Instantiate timers if funcs exist
176
171
self ._ld = Delay_ms (self ._lf , self ._la )
177
172
if self ._df :
178
173
self ._dd = Delay_ms (self ._ddto )
179
174
while True :
180
175
state = self .rawstate ()
181
176
# 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
188
183
self ._ld .trigger (Pushbutton .long_press_ms )
189
184
if self ._df :
190
- if self ._dd ():
185
+ if self ._dd (): # Second click: timer running
191
186
self ._dd .stop ()
192
187
self ._dblpend = False
193
188
self ._dblran = True # Prevent suppressed launch on release
@@ -196,19 +191,18 @@ async def buttoncheck(self):
196
191
# First click: start doubleclick timer
197
192
self ._dd .trigger (Pushbutton .double_click_ms )
198
193
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?
203
195
if self ._ff :
204
196
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 )
207
202
else :
208
203
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
212
206
self ._dblran = False
213
207
# Ignore state changes until switch has settled
214
208
await asyncio .sleep_ms (Pushbutton .debounce_ms )
0 commit comments