Switch to using adafruit_debouncer instead of failing to roll my own

This commit is contained in:
David Thurstenson 2022-10-19 23:42:31 -05:00
parent d90a17d79a
commit 2ec2951415
1 changed files with 30 additions and 74 deletions

View File

@ -3,6 +3,7 @@ import board
import neopixel import neopixel
import adafruit_datetime as datetime import adafruit_datetime as datetime
from adafruit_seesaw import seesaw, rotaryio, digitalio from adafruit_seesaw import seesaw, rotaryio, digitalio
from adafruit_debouncer import Button
import busio import busio
@ -23,8 +24,8 @@ i2c = busio.I2C(SCL, SDA)
seesaw = seesaw.Seesaw(i2c, 0x36) seesaw = seesaw.Seesaw(i2c, 0x36)
seesaw.pin_mode(24, seesaw.INPUT_PULLUP) seesaw.pin_mode(24, seesaw.INPUT_PULLUP)
button = digitalio.DigitalIO(seesaw, 24) pin = digitalio.DigitalIO(seesaw, 24)
button_held = False button = Button(pin)
encoder = rotaryio.IncrementalEncoder(seesaw) encoder = rotaryio.IncrementalEncoder(seesaw)
last_position = -1 last_position = -1
@ -209,58 +210,27 @@ def countdown(
# displayio, but just put it on the terminal output for now) # displayio, but just put it on the terminal output for now)
print("current time: " + display_time_sign + prettytime(current_time)) print("current time: " + display_time_sign + prettytime(current_time))
# If the button is currently being pressed # Update the debouncer
if not button.value: button.update()
# Pause on single short button press
if button.short_count == 1:
# We are paused # We are paused
pause = True pause = True
# There's no long-press option before pausing, so button should
# no longer be down.
button_held = False
print("Timer Paused") print("Timer Paused")
# Keep looping here as long as we're paused # Keep looping here as long as we're paused
while pause: while pause:
# If the button is being pressed # Update the debouncer
if not button.value and not button_held: button.update()
# The button is being held down # Resume timer on single short button press
button_held = True if button.short_count == 1:
# 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
pause = False pause = False
# User Feedback
print("Timer Resumed") print("Timer Resumed")
# Hang around for half a second for debounce # Reset timer on long press
time.sleep(0.5) if button.long_press:
pause = False
print("Timer Reset")
return
# Hard-coded initial value. (will replace with stored value later) # Hard-coded initial value. (will replace with stored value later)
set_time_orig = 120 set_time_orig = 120
@ -270,13 +240,13 @@ set_time = set_time_orig
# for every encoder click # for every encoder click
set_time_step = 60 set_time_step = 60
# How long is a long-press in seconds
button_hold_delay = 2
# Main loop # Main loop
while True: while True:
# Update the debouncer
button.update()
# Negate the position to make clockwise rotation positive # Negate the position to make clockwise rotation positive
position = -encoder.position position = -encoder.position
@ -297,31 +267,17 @@ while True:
last_position = position last_position = position
# User feedback # User feedback
print("Current set time: " + prettytime(set_time)) 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 # Reset timer to config value on long press
if button.value and button_held: if button.long_press:
# Button is no longer being pressed # Reset the set_time to the value of set_time_orig
button_held = False # (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 # Start the countdown using the configured set_time
countdown(set_time) countdown(set_time)
# Once the timer has been reset, re-init last_position. # Once the timer has been reset, re-init last_position.