timer-bar/colorbar-fastloop.py

80 lines
1.8 KiB
Python

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, update_interval=1):
# Turn all pixels off
pixels.fill(BLANK)
pixels.show()
# Init the update interval tracking variable
last_update_time = -1
# Init the elapsed time variable
elapsed_time = 0
# Pre-calculate rtime and ytime
rtime = seconds - redtime
ytime = seconds - yellowtime
while True:
# Get the current time
now = time.monotonic()
# Is it time for an update yet?
if now >= last_update_time + update_interval:
# d the last update time
last_update_time = now
# Do update stuff
for pixel in range(round(num_pixels * (elapsed_time / seconds))):
# Set pixel color stuff
pass
# Display the result IRL
pixels.show()
# Increment the elapsed time variable
elapsed_time += update_interval