Skip to content

Instantly share code, notes, and snippets.

@kaderud
Created June 2, 2022 06:26
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 kaderud/7f14ff68dd0e62acee89d6f77a0aaeea to your computer and use it in GitHub Desktop.
Save kaderud/7f14ff68dd0e62acee89d6f77a0aaeea to your computer and use it in GitHub Desktop.
2x 8x8 NeoMatrix-compatible LED signage with buttons
import board
import neopixel
import gc
from digitalio import DigitalInOut, Direction, Pull
from adafruit_debouncer import Debouncer
from time import sleep, monotonic
from adafruit_led_animation.color import (AQUA, BLACK, BLUE, PURPLE, WHITE,
CYAN, RED, AMBER, JADE, TEAL, PINK,
ORANGE, GREEN, YELLOW)
num_pixels = 64*2
BRIGHTNESS = 0.05
ID4 = (0, 255, 255)
pixels = neopixel.NeoPixel(pin=board.GP15, n=num_pixels, brightness=BRIGHTNESS, auto_write=False, pixel_order=neopixel.GRB)
def clear_neopixel():
pixels.fill(0)
pixels.show()
def box_anim():
box3 = [7,6,5,4,3,2,1,0,15,23,31,39,47,55,63,71,79,87,95,103,111,119,127,
8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,126,125,124,123,122,121]
box2 = [14,13,12,11,10,9,22,30,38,46,54,62,70,78,86,94,102,110,118,17,25,33,41,
49,57,65,73,81,89,97,105,113,117,116,115,114]
box1 = [21,29,37,45,53,61,69,77,85,93,101,109,
18,26,34,42,50,58,66,74,82,90,98,106,19,20,108,107]
box0 = [28,36,44,52,60,68,76,84,92,100,27,35,43,51,59,67,75,83,91,99]
for c in box3:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box2:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box1:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box0:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box1:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box2:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.5)
clear_neopixel()
for c in box3:
pixels[c] = (32,0,0)
pixels.show()
sleep(0.1)
clear_neopixel()
def flag_sweden():
cross = [47,55,46,54,45,53,4,12,20,28,36,44,52,60,68,76,84,92,100,108,116,124,
3,11,19,27,35,43,51,59,67,75,83,91,99,107,115,123,42,50,41,49,40,48]
for i in range(0, num_pixels):
pixels[i] = (BLUE)
for c in cross:
pixels[c] = (YELLOW)
pixels.show()
def word_isa(r,g,b:tuple):
letter_i = [6,14,22,30,13,21,12,20,11,19,10,18,9,17,0,8,16,24]
letter_s = [46,54,62,70,37,45,69,77,36,44,43,51,59,67,
66,74,65,73,33,41,40,48,56,64]
letter_a = [94,102,110,118,85,93,84,92,83,91,82,90,81,89,
80,88,99,107,117,125,116,124,115,123,114,122,113,121,112,120]
for i in letter_i:
pixels[i] = r
for s in letter_s:
pixels[s] = g
for a in letter_a:
pixels[a] = b
pixels.show()
def arrow():
up_arrow = [3,4,10,13,17,22,24,25,26,27,28,29,30,31,35,36,43,44,51,52,59,60]
up_arrow_f = [11,12,18,19,20,21]
for u in up_arrow:
pixels[u] = (0,32,0)
for f in up_arrow_f:
pixels[f] = (0,32,0)
pixels.show()
def pixel_runner(color:tuple, delay:int) -> None:
for i in range(0,num_pixels):
pixels[i] = color
sleep(delay)
pixels.show()
sleep(delay)
for i in reversed(range(0,num_pixels)):
pixels[i] = (0,0,0)
sleep(delay)
pixels.show()
def get_pin_value(pin) -> bool:
gpio = DigitalInOut(pin)
gpio.direction = Direction.INPUT
gpio.pull = Pull.UP
return lambda: gpio.value
button1 = Debouncer(get_pin_value(board.GP17)) # Green button
button2 = Debouncer(get_pin_value(board.GP16)) # Red button
button3 = Debouncer(get_pin_value(board.GP18)) # Yellow button
def main():
gc.collect()
print(f'Available memory after gc: {gc.mem_free()} bytes')
mode = 0
counter1 = 0
counter2 = 0
counter3 = 0
while True:
now = monotonic()
button1.update()
button2.update()
button3.update()
if button1.fell:
print(f'{now} Button 1: Pushed')
clear_neopixel()
mode = 0
counter1 += 1
if counter1 > 2:
counter1 = 0
gc.collect()
print(f'{now} Button 1: Counter: {counter1}')
if button1.rose:
print(f'{now} Button 1: Released')
print(f'Available memory: {gc.mem_free()} bytes')
if button2.fell:
print(f'{now} Button 2: Pushed')
clear_neopixel()
mode = 1
counter2 += 1
if counter2 > 12:
counter2 = 1
gc.collect()
print(f'{now} Button 2: Counter: {counter2}')
if button2.rose:
print(f'{now} Button 2: Released')
print(f'Available memory: {gc.mem_free()} bytes')
if button3.fell:
print(f'{now} Button 3: Pushed')
clear_neopixel()
mode = 2
counter3 += 1
if counter3 > 3:
counter3 = 0
gc.collect()
print(f'{now} Button 3: Counter: {counter3}')
if button3.rose:
print(f'{now} Button 3: Released')
print(f'Available memory: {gc.mem_free()} bytes')
if mode == 0:
if counter1 == 0:
word_isa(RED,GREEN,BLUE)
elif counter1 == 1:
word_isa(BLUE,RED,GREEN)
elif counter1 == 2:
word_isa(GREEN,BLUE,RED)
if mode == 1:
if counter2 == 1:
word_isa(RED,RED,RED)
elif counter2 == 2:
word_isa(GREEN,GREEN,GREEN)
elif counter2 == 3:
word_isa(BLUE,BLUE,BLUE)
elif counter2 == 4:
word_isa(AQUA,AQUA,AQUA)
elif counter2 == 5:
word_isa(PURPLE,PURPLE,PURPLE)
elif counter2 == 6:
word_isa(CYAN,CYAN,CYAN)
elif counter2 == 7:
word_isa(AMBER,AMBER,AMBER)
elif counter2 == 8:
word_isa(JADE,JADE,JADE)
elif counter2 == 9:
word_isa(TEAL,TEAL,TEAL)
elif counter2 == 10:
word_isa(YELLOW,YELLOW,YELLOW)
elif counter2 == 11:
word_isa(PINK,PINK,PINK)
elif counter2 == 12:
word_isa(ID4,ID4,ID4)
if mode == 2:
if counter3 == 0:
flag_sweden()
elif counter3 == 1:
arrow()
elif counter3 == 2:
pixel_runner(color=(0,0,64), delay=0.02)
elif counter3 == 3:
box_anim()
#sleep(0.05)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment