buttons: Only call into main thread on an adc button change
Determine the selected button (and if it has changed) in the background thread first. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
196a69c613
commit
124ba12485
|
@ -115,42 +115,38 @@ class MCU_ADC_buttons:
|
||||||
|
|
||||||
def adc_callback(self, read_time, read_value):
|
def adc_callback(self, read_time, read_value):
|
||||||
adc = max(.00001, min(.99999, read_value))
|
adc = max(.00001, min(.99999, read_value))
|
||||||
r = self.pullup * adc / (1.0 - adc)
|
value = self.pullup * adc / (1.0 - adc)
|
||||||
self.reactor.register_async_callback(
|
|
||||||
(lambda e, s=self, v=r: s.handle_button(e, v)))
|
|
||||||
|
|
||||||
def get_button(self, value):
|
# Determine button pressed
|
||||||
|
btn = None
|
||||||
if self.min_value <= value <= self.max_value:
|
if self.min_value <= value <= self.max_value:
|
||||||
for i, (min_value, max_value, cb) in enumerate(self.buttons):
|
for i, (min_value, max_value, cb) in enumerate(self.buttons):
|
||||||
if min_value < value < max_value:
|
if min_value < value < max_value:
|
||||||
return i
|
btn = i
|
||||||
return None
|
break
|
||||||
|
|
||||||
def handle_button(self, eventtime, value):
|
|
||||||
btn = self.get_button(int(value))
|
|
||||||
|
|
||||||
# If the button changed, due to noise or pressing:
|
# If the button changed, due to noise or pressing:
|
||||||
if btn != self.last_button:
|
if btn != self.last_button:
|
||||||
# reset the debouncing timer
|
# reset the debouncing timer
|
||||||
self.last_debouncetime = eventtime
|
self.last_debouncetime = read_time
|
||||||
|
|
||||||
# button debounce check & new button pressed
|
# button debounce check & new button pressed
|
||||||
if ((eventtime - self.last_debouncetime) >= ADC_DEBOUNCE_TIME
|
if ((read_time - self.last_debouncetime) >= ADC_DEBOUNCE_TIME
|
||||||
and self.last_button == btn and self.last_pressed != btn):
|
and self.last_button == btn and self.last_pressed != btn):
|
||||||
# release last_pressed
|
# release last_pressed
|
||||||
if self.last_pressed is not None:
|
if self.last_pressed is not None:
|
||||||
self.call_button(eventtime, self.last_pressed, False)
|
self.call_button(self.last_pressed, False)
|
||||||
self.last_pressed = None
|
self.last_pressed = None
|
||||||
if btn is not None:
|
if btn is not None:
|
||||||
self.call_button(eventtime, btn, True)
|
self.call_button(btn, True)
|
||||||
self.last_pressed = btn
|
self.last_pressed = btn
|
||||||
|
|
||||||
self.last_button = btn
|
self.last_button = btn
|
||||||
|
|
||||||
def call_button(self, eventtime, button, state):
|
def call_button(self, button, state):
|
||||||
if button < len(self.buttons):
|
|
||||||
minval, maxval, callback = self.buttons[button]
|
minval, maxval, callback = self.buttons[button]
|
||||||
callback(eventtime, state)
|
self.reactor.register_async_callback(
|
||||||
|
(lambda e, cb=callback, s=state: cb(e, s)))
|
||||||
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
Loading…
Reference in New Issue