Last active
January 22, 2025 03:19
-
-
Save stonehippo/46b97ca03e2fbf6758dc39568faeacf8 to your computer and use it in GitHub Desktop.
Messing with NeoPixels in Micropython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin | |
from neopixel import NeoPixel | |
import time | |
pixels = NeoPixel(Pin(0), 12, bpp=4) | |
WHITE = (0,0,0,255) | |
RED = (255,0,0,0) | |
GREEN = (0,255,0,0) | |
BLUE = (0,0,255,0) | |
BLACK = (0,0,0,0) | |
def colorwheel(pos): | |
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 set_brightness(color, brightness=1): | |
return tuple(int(c * brightness) for c in color) | |
def set_pixel(pixel, color, brightness=1): | |
pixels[pixel] = set_brightness(color, brightness) | |
pixels.write() | |
def set_all_pixels(color, brightness=1): | |
pixels.fill(set_brightness(color, brightness)) | |
pixels.write() | |
# for color in (WHITE, RED, GREEN, BLUE, BLACK): | |
# set_all_pixels(color, .25) | |
# time.sleep(1) | |
# | |
CLOCKWISE = -1 | |
COUNTER_CLOCKWISE = 1 | |
def animate(pattern, speed=0.1, rotation=COUNTER_CLOCKWISE): | |
if rotation == None: | |
rotation = COUNTER_CLOCKWISE | |
print(pattern) | |
while True: | |
for p in range(0,12): | |
set_pixel(p, pattern[p], 0.2) | |
pattern = pattern[rotation:] + pattern[:rotation] | |
time.sleep(speed) | |
def show_rainbow(speed=0.025, rotation=None): | |
rainbow = [(colorwheel(c * 21) + (0,)) for c in range(12)] | |
animate(rainbow, speed, rotation) | |
def show_comet(speed=0.025, rotation=None, length=6): | |
if length < 3: | |
length = 3 | |
if length > 12: | |
length = 12 | |
comet = [(0, 0, 0, b * 4) for b in reversed(range(length))] + [(0, 0, 0, 0) for _ in range(12 - length)] | |
animate(comet, speed, rotation) | |
def marching_ants(speed=0.25, rotation=None, color=BLUE): | |
faded = set_brightness(color, 0.25) | |
ants = [BLACK, BLACK, faded] * 6 | |
animate(ants, speed, rotation) | |
def chase(speed=0.25, rotation=None, color=BLUE): | |
chasers = ([color] * 3 + [BLACK] * 3) * 2 | |
animate(chasers, speed, rotation) | |
# show_comet(0.05, rotation=CLOCKWISE, length=5) | |
# marching_ants(rotation=COUNTER_CLOCKWISE) | |
chase(speed=0.1, color=set_brightness(colorwheel(33) + (0,), 0.25)) | |
# set_all_pixels(BLACK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment