176 lines
5.8 KiB
Python
176 lines
5.8 KiB
Python
import time
|
|
import board
|
|
import neopixel
|
|
import adafruit_datetime as datetime
|
|
|
|
|
|
####
|
|
# Neopixel setup
|
|
|
|
# Set Constants
|
|
pixel_pin = board.GP5
|
|
num_pixels = 144
|
|
brightness = 0.1
|
|
|
|
# Create neopixel object named pixels
|
|
pixels = neopixel.NeoPixel(
|
|
pixel_pin,
|
|
num_pixels,
|
|
brightness=brightness,
|
|
auto_write=False,
|
|
pixel_order="GRBW"
|
|
)
|
|
|
|
# Colors used in this script
|
|
# FORMAT: (R, G, B, W)
|
|
RED = (255, 0, 0, 0)
|
|
YELLOW = (255, 150, 0, 0)
|
|
GREEN = (0, 255, 0, 0)
|
|
BLANK = (0, 0, 0, 0)
|
|
|
|
# Turn all pixels off
|
|
pixels.fill(BLANK)
|
|
pixels.show()
|
|
|
|
# END Neopixel setup
|
|
####
|
|
|
|
# Set the color on a single neopixel based on colormode and
|
|
# whether yellowtime or redtime has been reached. Calling
|
|
# logic should iterate over every neopixel ID that should
|
|
# be updated during the current update interval, and then
|
|
# call pixels.show() after all pixels have been set. It's
|
|
# up to the calling logic to calculate whether the yellow
|
|
# or red parameters should be set to True. Behavior when
|
|
# red and yellow are both set to True depends on how the
|
|
# colormode is configured.
|
|
def colorizer(pxnum, colormode="fill", yellow=False, red=False):
|
|
# Fill every pixel from lowest to currently highest with
|
|
# the current color.
|
|
if colormode == "fill":
|
|
if red:
|
|
pixels[pxnum] = RED
|
|
elif yellow:
|
|
pixels[pxnum] = YELLOW
|
|
else:
|
|
pixels[pxnum] = GREEN
|
|
# Only fill the next pixel with the current color if it's
|
|
# currently BLANK
|
|
elif colormode == "candybar":
|
|
if pixels[pxnum] == BLANK:
|
|
if red:
|
|
pixels[pxnum] = RED
|
|
elif yellow:
|
|
pixels[pxnum] = YELLOW
|
|
else:
|
|
pixels[pxnum] = GREEN
|
|
else:
|
|
pass
|
|
else:
|
|
# Invalid colormodes end up here
|
|
raise Exception("Invalid colormode: " + colormode)
|
|
|
|
|
|
# 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
|
|
# that the bar should show yellow), and redtime (same as
|
|
# yellowtime). The colormode determines what happens at
|
|
# yellowtime and redtime.
|
|
def countdown(
|
|
seconds,
|
|
colormode="fill",
|
|
yellowtime=120,
|
|
redtime=60,
|
|
update_interval=1):
|
|
|
|
# Turn all pixels off
|
|
pixels.fill(BLANK)
|
|
pixels.show()
|
|
|
|
# Init the update interval tracking variable
|
|
last_update_time = -1
|
|
|
|
# Init the current time variable
|
|
current_time = seconds
|
|
|
|
# This begins what I like to call the "Are We There Yet?"
|
|
# loop. Instead of making the script wait for an interval
|
|
# before continuing as a form of forced timed pacing, we
|
|
# simply write an infinite loop that will iterate very
|
|
# quickly between update intervals, essentially repeatedly
|
|
# asking the CPU to calculate whether it's time to do an
|
|
# update yet. The high frequency of the update interval
|
|
# checks will make sure our update is fired on-time.
|
|
#
|
|
# ...unless we decide to configure a way to kill the loop entirely, I guess...? ;)
|
|
while True:
|
|
# Get the current time
|
|
now = time.monotonic()
|
|
|
|
# Is it time for an update yet?
|
|
if now >= last_update_time + update_interval:
|
|
|
|
# Update the last update time
|
|
last_update_time = now
|
|
|
|
# Do update stuff
|
|
|
|
# Calculate the current position.
|
|
# Takes the percentage of time elapsed, multiplied with
|
|
# the total numbers of pixels, and rounded to the nearest
|
|
# decimal. This results in a number of pixels proportional
|
|
# to the elapsed time
|
|
current_position = round(num_pixels * ((seconds - current_time) / seconds))
|
|
|
|
# Catch a couple of special cases
|
|
if current_position == 0:
|
|
# Light the first LED when the timer starts
|
|
# regardless of other factors
|
|
colorizer(0, colormode)
|
|
elif current_position == num_pixels and current_time > 0:
|
|
# If current_position calls for *all*
|
|
# pixels to be lit, and the timer
|
|
# hasn't expired yet, don't do anything.
|
|
# This will delay the last pixel from
|
|
# lighting until the timer has fully elapsed
|
|
pass
|
|
else:
|
|
# Loop over every pixel ID that should be lit
|
|
# based on the elapsed time
|
|
for pixel in range(current_position):
|
|
# Set pixel color stuff
|
|
|
|
# If current_time has gone negative, don't
|
|
# change any pixels, just keep counting for
|
|
# user feedback
|
|
if current_time < 0:
|
|
pass
|
|
elif current_time <= redtime:
|
|
colorizer(pixel, colormode, red=True)
|
|
elif current_time <= yellowtime:
|
|
colorizer(pixel, colormode, yellow=True)
|
|
else:
|
|
colorizer(pixel, colormode)
|
|
|
|
# All the pixels have now been set based on the
|
|
# specified colormode, now display the result IRL.
|
|
pixels.show()
|
|
|
|
# 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 = " "
|
|
print("current time: " + display_time_sign + display_time)
|
|
|
|
|
|
|
|
|