From 856af97eb3d854f6e789e0c1b366e16956a94591 Mon Sep 17 00:00:00 2001 From: David Thurstenson Date: Sun, 2 Oct 2022 20:03:44 -0500 Subject: [PATCH] Initial Commit --- colorbar-fastloop.py | 79 +++++++++++++++++ colorbar-sleep.py | 136 ++++++++++++++++++++++++++++++ examples/displayio-hello-world.py | 34 ++++++++ examples/flash-onboard-led.py | 21 +++++ 4 files changed, 270 insertions(+) create mode 100644 colorbar-fastloop.py create mode 100644 colorbar-sleep.py create mode 100644 examples/displayio-hello-world.py create mode 100644 examples/flash-onboard-led.py diff --git a/colorbar-fastloop.py b/colorbar-fastloop.py new file mode 100644 index 0000000..0f3e3b8 --- /dev/null +++ b/colorbar-fastloop.py @@ -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 diff --git a/colorbar-sleep.py b/colorbar-sleep.py new file mode 100644 index 0000000..2f63370 --- /dev/null +++ b/colorbar-sleep.py @@ -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 \ No newline at end of file diff --git a/examples/displayio-hello-world.py b/examples/displayio-hello-world.py new file mode 100644 index 0000000..54fc3f7 --- /dev/null +++ b/examples/displayio-hello-world.py @@ -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) \ No newline at end of file diff --git a/examples/flash-onboard-led.py b/examples/flash-onboard-led.py new file mode 100644 index 0000000..b96bd7a --- /dev/null +++ b/examples/flash-onboard-led.py @@ -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 +####