Initial Commit

This commit is contained in:
David Thurstenson 2022-10-02 20:03:44 -05:00
commit 856af97eb3
4 changed files with 270 additions and 0 deletions

79
colorbar-fastloop.py Normal file
View File

@ -0,0 +1,79 @@
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

136
colorbar-sleep.py Normal file
View File

@ -0,0 +1,136 @@
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

View File

@ -0,0 +1,34 @@
import board
import busio
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
import time
import digitalio
# Display
WIDTH = 128
HEIGHT = 32
CENTER_X = int(WIDTH/2)
CENTER_Y = int(HEIGHT/2)
displayio.release_displays()
SDA = board.GP0
SCL = board.GP1
i2c = busio.I2C(SCL, SDA)
display_bus = displayio.I2CDisplay(i2c, device_address=60)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)
text = "HELLO WORLD"
font = terminalio.FONT
color = 0xFFFFFF
text_area = label.Label(font, text=text, color=color)
text_area.x = 1
text_area.y = 12
display.show(text_area)

View File

@ -0,0 +1,21 @@
import time
import board
import digitalio
####
# Onboard LED
# Initialize
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# Flash onboard LED every half second
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
# END Onboard LED
####