Skip to content

Instantly share code, notes, and snippets.

@rndmcnlly
Created November 3, 2024 01:10
Show Gist options
  • Save rndmcnlly/e7dbb973cf54ba620392b93ec9f318eb to your computer and use it in GitHub Desktop.
Save rndmcnlly/e7dbb973cf54ba620392b93ec9f318eb to your computer and use it in GitHub Desktop.
CircuitPython code for a glowing SeaWing costume based on the Trinket M0 board
# mommy and daddy 10/18/24
import board
import time
import math
from digitalio import DigitalInOut, Direction, Pull
from adafruit_dotstar import DotStar
from neopixel_write import neopixel_write
# colored dotstar led on board (not neopixel)
color_led = DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
color_led.brightness = 0.05
# basic little red LED near usb charger
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
# switch connecting D2 to GND
switch = DigitalInOut(board.D2)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
# neopixel strip on D3
strip = DigitalInOut(board.D3)
strip.direction = Direction.OUTPUT
num_lights = 22
pixel_buffer = bytearray(3 * num_lights)
alpha = 0 # color blend
beta = 0 # activation blend
gamma = 15 # activation transition rate
last_t = 0
ticks = 0
def lerp(a, b, t):
return int(a + (b - a) * t)
def shade_channel(light, channel):
base_a = 1 - ((light - t * num_lights * 2) % num_lights) / num_lights
base_value = lerp(base_grb[channel], base_grb[channel] // 2, base_a)
signal_a = 1 - ((light + t * num_lights * 4) % num_lights) / num_lights
signal_value = lerp(signal_grb[channel], signal_grb[channel] // 4, signal_a)
q = light / num_lights
return lerp(min(255, base_value + signal_value * beta), 0, q)
while True:
ticks += 1
ticks %= num_lights
t = time.monotonic()
dt = t - last_t
last_t = t
alpha = math.sin(2 * math.pi * (t % 1)) * 0.5 + 0.5
target = int(not switch.value)
beta = beta + (target - beta) * gamma * dt
beta = min(1, max(0, beta))
base_grb = [lerp(12, 6, alpha), lerp(0, 0, alpha), lerp(6, 12, alpha)]
signal_grb = [lerp(64, 32, alpha), lerp(64, 64, alpha), lerp(128, 128, alpha)]
subsample = 3
for i in range(ticks % subsample, num_lights * 3, subsample):
pixel_buffer[i] = shade_channel(i // 3, i % 3)
neopixel_write(strip, pixel_buffer)
led.value = not switch.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment