Skip to content

Instantly share code, notes, and snippets.

@WolfwithSword
Created February 14, 2022 15:37
Show Gist options
  • Save WolfwithSword/1821d8836eaa31db779299afca634b2c to your computer and use it in GitHub Desktop.
Save WolfwithSword/1821d8836eaa31db779299afca634b2c to your computer and use it in GitHub Desktop.
RPI Pico Oblivion Lamp Code
import array, time
from machine import Pin
import rp2
import random
from rp2 import PIO, StateMachine, asm_pio
# Number of WS2812 LEDs.
NUM_LEDS = 12
# The LEDs on the edges, first two and last two
EDGE_LEDS = [1, 2, NUM_LEDS -1 ,NUM_LEDS]
# The middle two LEDs
RED_CENTRE = [ NUM_LEDS // 2 , NUM_LEDS // 2 +1]
# All LEDs that aren't the edges or centre
Other_LEDS = [ x for x in range(NUM_LEDS)
if x not in EDGE_LEDS
and x not in RED_CENTRE
and x > 0]
@asm_pio(sideset_init=PIO.OUT_LOW, out_shiftdir=PIO.SHIFT_LEFT,
autopull=True, pull_thresh=24)
def ws2812():
T1 = 2
T2 = 5
T3 = 3
label("bitloop")
out(x, 1) .side(0) [T3 - 1]
jmp(not_x, "do_zero") .side(1) [T1 - 1]
jmp("bitloop") .side(1) [T2 - 1]
label("do_zero")
nop() .side(0) [T2 - 1]
# StateMachine for Pin(0).
sm = StateMachine(0, ws2812, freq=8000000, sideset_base=Pin(0))
sm.active(1)
# Display LED Pattern using array of RGB values.
pixel_array = array.array("I", [0 for _ in range(NUM_LEDS)])
########################################
# RGB Coloring
########################################
def updatePixel(brightness=1): # dimming colors, then update
dimmer_array = array.array("I", [0 for _ in range(NUM_LEDS)])
for ii,cc in enumerate(pixel_array):
r = int(((cc >> 8) & 0xFF) * brightness)
g = int(((cc >> 16) & 0xFF) * brightness)
b = int((cc & 0xFF) * brightness)
dimmer_array[ii] = (g<<16) + (r<<8) + b
sm.put(dimmer_array, 8) # update
def set_all_color(color): # set all LEDs to X colour
for ii in range(len(pixel_array)):
set_led_color(color, ii)
def set_led_color(color, led): # set individual LED to X colour
pixel_array[led] = (color[1]<<16) + (color[0]<<8) + color[2]
red = (255,0,0)
#green = (0,255,0)
#blue = (0,0,255)
#white = (255,255,255)
yellow = (255, 215, 0)
orange = (255,69,0)
off = (0,0,0)
########################################
# Startup Procedure
########################################
set_all_color(off)
updatePixel(0)
time.sleep(1.5)
# Set all edges to yellow
for x in EDGE_LEDS:
set_led_color(yellow, x - 1)
init = 0.1
for i in range(10):
updatePixel(init)
time.sleep(0.075)
init = init + 0.075
time.sleep(1.5)
# Set all Other LEDs to orange, in a a wave from both edges going toward centre
for x in range(len(Other_LEDS) // 2):
set_led_color(orange, Other_LEDS[x] - 1)
set_led_color(orange, Other_LEDS[len(Other_LEDS) - x - 1] - 1)
updatePixel(init)
time.sleep(0.125)
time.sleep(1)
# Set all centre LEDs to red
for x in RED_CENTRE:
set_led_color(red, x - 1)
updatePixel(1)
time.sleep(1)
# Randomly shift values of all LEDs to mimic a portaly-fire-like thing.
while True:
for x in range(len(pixel_array)):
color = None
if x + 1 in EDGE_LEDS:
color = (255,
215 - 20 + random.randint(-50, -30),
0)
elif x + 1 in RED_CENTRE:
color = (255 - random.randint(0, 20),
15 - random.randint(0, 15),
0)
else:
color = (random.randint(orange[0] - 30, orange[0] - 10),
random.randint(50, orange[1] + 35),
orange[2])
set_led_color(color, x)
updatePixel(1)
time.sleep(0.06)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment