Skip to content

Instantly share code, notes, and snippets.

@curtmerrill

curtmerrill/main.py

Created Nov 7, 2020
Embed
What would you like to do?
Busy box Mk.I
import board
import neopixel
import time
from adafruit_debouncer import Debouncer
from digitalio import DigitalInOut, Direction, Pull
from random import randint
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
b2 = DigitalInOut(board.D4)
b2.direction = Direction.INPUT
b2.pull = Pull.UP
blue_tri = Debouncer(b2)
blue_led = DigitalInOut(board.D2)
blue_led.direction = Direction.OUTPUT
b3 = DigitalInOut(board.D3)
b3.direction = Direction.INPUT
b3.pull = Pull.UP
org_but = Debouncer(b3)
org_led = DigitalInOut(board.D1)
org_led.direction = Direction.OUTPUT
neo_pin = board.D0
num_pixels = 24
neo = neopixel.NeoPixel(neo_pin, num_pixels, brightness=.2, auto_write=False)
# ######################## HELPERS ##############################
colors = [
('red', (255, 0, 0)),
('blue', (0, 0, 255)),
('green', (0, 255, 0)),
('orange', (255, 128, 0)),
('yellow', (255, 255, 0)),
('purple', (128, 0, 255)),
]
modes = [
# name, delay, number of frames
('chase', .05, 24),
('dbl_chase', .05, 12),
('bounce', .05, 24),
('pulse', .001, 256),
('fill', .1, 24),
('alexa', .05, 12),
('vu', .05, 28),
('rainbow', 0, 256),
]
frame = 0
mode_index = 0
color = colors[0][1]
bg_color = (0, 0, 0)
speed = modes[mode_index][1]
num_frames = modes[mode_index][2]
initial_time = time.monotonic()
def random_color():
new_color = randint(1, len(colors)) - 1
return colors[new_color][1]
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
def next_frame():
if modes[mode_index][0] == 'chase':
neo.fill(bg_color)
neo[frame % num_pixels] = color
neo.show()
if modes[mode_index][0] == 'dbl_chase':
neo.fill(bg_color)
neo[frame % num_pixels] = color
neo[frame % num_pixels + 12] = color
neo.show()
if modes[mode_index][0] == 'pulse':
intensity = (frame / 128) - 1
intensity = abs(intensity)
neo.fill(tuple(int(c*intensity) for c in color))
neo.show()
if modes[mode_index][0] == 'rainbow':
j = frame % 255
for i in range(num_pixels):
rc_index = (i * 255 // num_pixels) + j
neo[i] = wheel(rc_index & 255)
neo.show()
if modes[mode_index][0] == 'fill':
neo[frame % num_pixels] = color
neo.show()
if modes[mode_index][0] == 'vu':
t = abs(frame - num_frames / 2)
neo.fill((0, 0, 0))
for i in range(t):
c = (255, 0, 0)
if i < 10:
c = (192, 96, 0)
if i < 6:
c = (0, 255, 0)
neo[i] = c
neo[-i] = c
neo.show()
if modes[mode_index][0] == 'bounce':
t = 12 - frame
neo.fill((0, 0, 0))
neo[t] = color
neo[-t] = color
neo.show()
if modes[mode_index][0] == 'alexa':
t = frame * 2
neo.fill((0, 0, 64))
neo[t] = (0, 128, 128)
neo[t+1] = (0, 128, 128)
neo.show()
def set_mode(idx):
idx = idx + 1
if idx >= len(modes):
idx = 0
neo.fill((0, 0, 0))
neo[idx*2] = (128, 128, 128)
neo[idx*2+1] = (128, 128, 128)
neo.show()
time.sleep(.5)
return(idx)
######################### MAIN LOOP ##############################
while True:
now = time.monotonic()
blue_tri.update()
if blue_tri.fell:
led.value = not led.value
blue_led.value = False
mode_index = set_mode(mode_index)
speed = modes[mode_index][1]
num_frames = modes[mode_index][2]
blue_led.value = True
org_but.update()
if org_but.fell:
led.value = not led.value
org_led.value = not org_led.value
color = random_color()
if now - initial_time > speed:
if frame >= num_frames - 1:
frame = 0
if modes[mode_index][0] == 'fill':
color = random_color()
else:
frame += 1
next_frame()
initial_time = time.monotonic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.