Move time sign (-/+) into prettytime(), and move the countdown pause functionality to its own function
This commit is contained in:
parent
32d339c0fa
commit
e47c2babe1
|
@ -69,7 +69,16 @@ pixels.show()
|
||||||
# Use datetime.timedelta to convert an int of seconds to a
|
# Use datetime.timedelta to convert an int of seconds to a
|
||||||
# string with the format MM:SS
|
# string with the format MM:SS
|
||||||
def prettytime(seconds):
|
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
|
# 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)
|
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
|
# Count down from the given total seconds, using the chosen
|
||||||
# colormode (how the colors are filled into each pixel),
|
# colormode (how the colors are filled into each pixel),
|
||||||
# and the given yellowtime (seconds before timer has elapsed
|
# and the given yellowtime (seconds before timer has elapsed
|
||||||
|
@ -197,39 +230,17 @@ def countdown(
|
||||||
# Increment the elapsed time variable
|
# Increment the elapsed time variable
|
||||||
current_time -= update_interval
|
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
|
# Give the user feedback
|
||||||
# (this string will eventually go to a ssd1306 OLED display via
|
# (this string will eventually go to a ssd1306 OLED display via
|
||||||
# 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: " + prettytime(current_time))
|
||||||
|
|
||||||
# Update the debouncer
|
# Update the debouncer
|
||||||
button.update()
|
button.update()
|
||||||
|
|
||||||
# Pause on single short button press
|
# Pause on single short button press
|
||||||
if button.short_count == 1:
|
if button.short_count == 1:
|
||||||
# We are paused
|
if pause() == "reset":
|
||||||
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
|
return
|
||||||
|
|
||||||
# Hard-coded initial value. (will replace with stored value later)
|
# Hard-coded initial value. (will replace with stored value later)
|
||||||
|
|
Loading…
Reference in New Issue