timer-bar/colorbar-sleep.py

136 lines
4.6 KiB
Python
Raw Normal View History

2022-10-03 01:03:44 +00:00
import time
import board
import neopixel
####
# 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
####
# 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):
# Turn all pixels off
pixels.fill(BLANK)
pixels.show()
# Init the loop tracking variable
elapsed_seconds = 0
# Pre-calculate rtime and ytime
rtime = seconds - redtime
ytime = seconds - yellowtime
# Loop until timer has expired
while elapsed_seconds <= seconds:
# Convert the elapsed time into a percentage
percent_complete = elapsed_seconds / seconds
# Convert the percentage complete to percentage of num_pixels,
# rounded to the nearest integer.
pixels_to_light = round(percent_complete * num_pixels)
# Loop once for every pixel that should be lit in our progress bar.
# The logic contained within will determine what color the pixel
# in question is set to.
for i in range(pixels_to_light):
# Set the current pixel in the loop to the appropriate color
# based on the selected colormode
if colormode == "fill":
# Fill the entire elapsed bar with GREEN, YELLOW, or RED
# based on the specified yellowtime and redtime.
# Decide what to do based on whether
# redtime or yellowtime has been reached
if elapsed_seconds >= rtime:
# When redtime has been reached
# Set all pixels that should be lit to RED
pixels[i] = RED
elif elapsed_seconds >= ytime:
# When yellowtime has been reached
# Set all pixels that should be lit to YELLOW
pixels[i] = YELLOW
else:
# When neither yellowtime or redtime has been reached yet
# Set all pixels that should be lit to GREEN
pixels[i] = GREEN
elif colormode == "candybar":
# Turn on only the next pixel with the appropriate color. Leave
# all pixels that have already been written alone
if pixels[i] == BLANK:
# If the pixel hasn't had any value written to it already
if elapsed_seconds >= rtime:
# When redtime has been reached
# Set just the next pixel or group of pixels to RED
pixels[i] = RED
elif elapsed_seconds >= ytime:
# When yellowtime has been reached
# Set just the next pixel or group of pixels to YELLOW
pixels[i] = YELLOW
else:
# When neither yellowtime or redtime has been reached yet
# Set just the next pixel or group of pixels to GREEN
pixels[i] = GREEN
else:
# If the pixel has already been written to before this point
# Do nothing
pass
else:
# Invalid colormodes end up here
raise Exception("Invalid colormode: " + colormode)
# Display the result IRL
pixels.show()
# Sleep for 1 second
time.sleep(1)
# Increment the tracking variable
elapsed_seconds += 1