Move time sign (-/+) into prettytime(), and move the countdown pause functionality to its own function

This commit is contained in:
David Thurstenson 2022-10-20 00:21:46 -05:00
parent 32d339c0fa
commit e47c2babe1
1 changed files with 37 additions and 26 deletions

View File

@ -69,7 +69,16 @@ pixels.show()
# Use datetime.timedelta to convert an int of seconds to a
# string with the format MM:SS
def prettytime(seconds):
return str(datetime.timedelta(seconds=abs(seconds)))[2:]
# Add a negative sign to the output when current_time is negative.
# prettytime() puts the given value through abs() because the way
# datetime.timedelta() represents negative values is kind of a PITA
# to deal with.
if seconds < 0:
display_time_sign = "-"
else:
display_time_sign = ""
return display_time_sign + str(datetime.timedelta(seconds=abs(seconds)))[2:]
# Set the color on a single neopixel based on colormode and
@ -108,6 +117,30 @@ def colorizer(pxnum, colormode="fill", yellow=False, red=False):
raise Exception("Invalid colormode: " + colormode)
# Pause the timer until the user resumes it again
def pause():
# We are paused
pause = True
print("Timer Paused")
# Keep looping here as long as we're paused
while pause:
# Update the debouncer
button.update()
# Resume timer on single short button press
if button.short_count == 1:
pause = False
print("Timer Resumed")
# Reset timer on long press
if button.long_press:
pause = False
print("Timer Reset")
# When resetting the timer, return with a specific
# value so that the calling logic can handle this case.
return "reset"
# If we've exited the loop here, then resume
# the timer by simply returning
return
# Count down from the given total seconds, using the chosen
# colormode (how the colors are filled into each pixel),
# and the given yellowtime (seconds before timer has elapsed
@ -197,40 +230,18 @@ def countdown(
# Increment the elapsed time variable
current_time -= update_interval
# Add a negative sign to the output when current_time is negative.
# prettytime() puts the given value through abs() because the way
# datetime.timedelta() represents negative values is kind of a PITA
# to deal with.
if current_time < 0:
display_time_sign = "-"
else:
display_time_sign = " "
# Give the user feedback
# (this string will eventually go to a ssd1306 OLED display via
# displayio, but just put it on the terminal output for now)
print("current time: " + display_time_sign + prettytime(current_time))
print("current time: " + prettytime(current_time))
# Update the debouncer
button.update()
# Pause on single short button press
if button.short_count == 1:
# We are paused
pause = True
print("Timer Paused")
# Keep looping here as long as we're paused
while pause:
# Update the debouncer
button.update()
# Resume timer on single short button press
if button.short_count == 1:
pause = False
print("Timer Resumed")
# Reset timer on long press
if button.long_press:
pause = False
print("Timer Reset")
return
if pause() == "reset":
return
# Hard-coded initial value. (will replace with stored value later)
set_time_orig = 120