Skip to content

Instantly share code, notes, and snippets.

@strickinato
Created December 12, 2019 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strickinato/de1c1a3eff3d61078752d210f5cc62fa to your computer and use it in GitHub Desktop.
Save strickinato/de1c1a3eff3d61078752d210f5cc62fa to your computer and use it in GitHub Desktop.
code.py for the quilt prototype board
import time
import board
import neopixel
import terminalio
import random
from adafruit_display_text import label
from adafruit_pybadger import PyBadger
pybadger = PyBadger()
board_pixels = pybadger.pixels
num_board_pixels = 5
ring_pin = board.D12
num_ring_pixels = 16
ring_pixels = neopixel.NeoPixel(ring_pin, num_ring_pixels, brightness=0.3, auto_write=False)
dot_pin = board.D10
num_dot_pixels = 7
dot_pixels = neopixel.NeoPixel(dot_pin, num_dot_pixels, brightness=0.3, auto_write=False)
count_limit = 255
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 color_chase(color, wait):
for i in range(num_pixels):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(rc_index & 255)
pixels.show()
time.sleep(wait)
NONE = (0,0,0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
options = [
'rainbow',
'blinky',
'red',
'ring',
'light ring',
'water',
]
colors = [
RED,
YELLOW,
GREEN,
CYAN,
BLUE,
PURPLE
]
selected_animation = 0
selected_color = 0
count = 0
first_run = True
board_pixels_on = True
def fill_pixels(color, pixels):
for i in range(len(pixels)):
pixels[i] = color
def animate_blinky(color, count, pixels):
if (count // 10) % 2 == 1:
fill_pixels(color, pixels)
else:
fill_pixels(NONE, pixels)
def animate_rainbow(count, pixels):
speed = 3
num_pixels = len(pixels)
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + count
pixels[i] = wheel(rc_index & 255)
def animate_ring(color, count, pixels):
speed = 1
num_pixels = len(pixels)
selected_light = count // speed % num_pixels
fill_pixels(NONE, pixels)
pixels[selected_light] = color
def animate_light_ring(color, count, pixels):
speed = 1
num_pixels = len(pixels)
selected_light = count // speed % num_pixels
fill_pixels(color, pixels)
pixels[selected_light] = NONE
water_palette = [
(15,94,156),
(40,13,102),
(35,120,110),
(35,137,218),
(28,163,236),
]
def animate_water(count, pixels):
num_pixels = len(pixels)
for i in range(num_pixels):
pixels[i] = random.choice(water_palette)
def animate(selected_animation, selected_color, count, pixels):
animation = options[selected_animation]
color = colors[selected_color]
if animation == 'red':
fill_pixels(color, pixels)
elif animation == 'blinky':
animate_blinky(color, count, pixels)
elif animation == 'rainbow':
animate_rainbow(count, pixels)
elif animation == 'ring':
animate_ring(color, count, pixels)
elif animation == 'light ring':
animate_light_ring(color, count, pixels)
elif animation == 'water':
animate_water(count, pixels)
else:
fill_pixels(NONE, pixels)
pixels.show()
while True:
if first_run:
fill_pixels(RED, board_pixels)
board_pixels.show()
dot_pixels.show()
ring_pixels.show()
time.sleep(0.5)
fill_pixels(NONE, board_pixels)
first_run = False
if pybadger.button.down:
time.sleep(0.1)
if pybadger.button.down:
selected_animation += 1
if pybadger.button.up:
time.sleep(0.1)
if pybadger.button.up:
selected_animation -= 1
if selected_animation < 0:
selected_animation = 0
if selected_animation >= len(options):
selected_animation = len(options) - 1
if pybadger.button.right:
time.sleep(0.1)
if pybadger.button.right:
selected_color += 1
if pybadger.button.left:
time.sleep(0.1)
if pybadger.button.left:
selected_color -= 1
if selected_color < 0:
selected_color = 0
if selected_color >= len(options):
selected_color = len(options) - 1
text = ""
for i, option in enumerate(options):
if i == selected_animation:
text += "- "
text += option
text += "\n"
text_area = label.Label(terminalio.FONT, text=text)
text_area.x = 10
text_area.y = 60
board.DISPLAY.show(text_area)
if pybadger.button.select:
time.sleep(0.1)
if pybadger.button.select:
board_pixels_on = not board_pixels_on
if board_pixels_on:
animate(selected_animation, selected_color, count, board_pixels)
else:
fill_pixels(NONE, board_pixels)
animate(selected_animation, selected_color, count, dot_pixels)
animate(selected_animation, selected_color, count, ring_pixels)
fill_pixels(GREEN, dot_pixels)
count = (count % count_limit) + 1
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment