From 2ec295141501bb908c6df7406ae281df2785bb18 Mon Sep 17 00:00:00 2001 From: David Thurstenson Date: Wed, 19 Oct 2022 23:42:31 -0500 Subject: [PATCH] Switch to using adafruit_debouncer instead of failing to roll my own --- colorbar-fastloop.py | 104 +++++++++++++------------------------------ 1 file changed, 30 insertions(+), 74 deletions(-) diff --git a/colorbar-fastloop.py b/colorbar-fastloop.py index 719a0d9..3f683e7 100644 --- a/colorbar-fastloop.py +++ b/colorbar-fastloop.py @@ -3,6 +3,7 @@ import board import neopixel import adafruit_datetime as datetime from adafruit_seesaw import seesaw, rotaryio, digitalio +from adafruit_debouncer import Button import busio @@ -23,8 +24,8 @@ i2c = busio.I2C(SCL, SDA) seesaw = seesaw.Seesaw(i2c, 0x36) seesaw.pin_mode(24, seesaw.INPUT_PULLUP) -button = digitalio.DigitalIO(seesaw, 24) -button_held = False +pin = digitalio.DigitalIO(seesaw, 24) +button = Button(pin) encoder = rotaryio.IncrementalEncoder(seesaw) last_position = -1 @@ -209,58 +210,27 @@ def countdown( # displayio, but just put it on the terminal output for now) print("current time: " + display_time_sign + prettytime(current_time)) - # If the button is currently being pressed - if not button.value: + # Update the debouncer + button.update() + + # Pause on single short button press + if button.short_count == 1: # We are paused pause = True - # There's no long-press option before pausing, so button should - # no longer be down. - button_held = False print("Timer Paused") # Keep looping here as long as we're paused while pause: - # If the button is being pressed - if not button.value and not button_held: - # The button is being held down - button_held = True - # Record when the button started being pressed - button_held_timer = time.monotonic() - # Keep looping while the button is down - while not button.value: - # Continually check if button_hold_delay has elapsed - # while the button is still down - if time.monotonic() - button_held_timer > button_hold_delay: - # Button should no longer be down - button_held = False - # Reset the long-press timer as a debounce method - button_held_timer = time.monotonic() - # No longer in pause mode - pause = False - # Give the user feedback - print("Timer Reset") - # Hang around for half a second for debounce - time.sleep(0.5) - # Return from countdown() back to the main loop - return - # If the button is not being pressed and button_held is True. - # I don't understand why, but it gets hung up here sometimes, - # requiring the user to press the button multiple times to - # resume the timer again... - if button.value and button_held: - # Flip it back to False - button_held = False - # If the button is being short-pressed when it previously wasn't - # being pressed, and we are paused. - if not button.value and not button_held and pause: - # Flip it back to False - button_held = False - # Exiting pause + # Update the debouncer + button.update() + # Resume timer on single short button press + if button.short_count == 1: pause = False - # User Feedback print("Timer Resumed") - # Hang around for half a second for debounce - time.sleep(0.5) - + # Reset timer on long press + if button.long_press: + pause = False + print("Timer Reset") + return # Hard-coded initial value. (will replace with stored value later) set_time_orig = 120 @@ -270,13 +240,13 @@ set_time = set_time_orig # for every encoder click set_time_step = 60 -# How long is a long-press in seconds -button_hold_delay = 2 - # Main loop while True: + # Update the debouncer + button.update() + # Negate the position to make clockwise rotation positive position = -encoder.position @@ -297,31 +267,17 @@ while True: last_position = position # User feedback print("Current set time: " + prettytime(set_time)) - # If the button is being pressed, and button_held is False - if not button.value and not button_held: - # Button is being pressed - button_held = True - # Capture the current monotonic clock time to later detect a long-press - button_held_timer = time.monotonic() - # Keep looping while the button is down - while not button.value: - # Continually check if button_hold_delay has elapsed - # while the button is still down - if time.monotonic() - button_held_timer > button_hold_delay: - # Reset the set_time to the value of set_time_orig - # (eventually, set_time_orig will be read from persistent config) - set_time = set_time_orig - # Give the user feedback - print("Time reset to: " + prettytime(set_time)) - # Button should no longer be down - button_held = False - # Reset the long-press timer as a debounce method - button_held_timer = time.monotonic() - # If the button is not being pressed, and it previously was being pressed - if button.value and button_held: - # Button is no longer being pressed - button_held = False + # Reset timer to config value on long press + if button.long_press: + # Reset the set_time to the value of set_time_orig + # (eventually, set_time_orig will be read from persistent config) + set_time = set_time_orig + # Give the user feedback + print("Time reset to: " + prettytime(set_time)) + + # Start the timer on single short press + if button.short_count == 1: # Start the countdown using the configured set_time countdown(set_time) # Once the timer has been reset, re-init last_position.