Skip to content

Instantly share code, notes, and snippets.

@PatrickLang
Last active July 8, 2021 05:41
Show Gist options
  • Save PatrickLang/41ed784ad409139395416f6223d93d13 to your computer and use it in GitHub Desktop.
Save PatrickLang/41ed784ad409139395416f6223d93d13 to your computer and use it in GitHub Desktop.
CircuitPython on Adafruit RP2040 feather

Just a collection of snippets from when I was testing out the RP2040 feather

# rename to code.py before upload
import board
import digitalio
import neopixel_write
import time
import random
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led.value = False
strip_io = digitalio.DigitalInOut(board.D4)
strip_io.direction = digitalio.Direction.OUTPUT
neopixel_write.neopixel_write(strip_io, bytearray([255,0,0]))
neo = digitalio.DigitalInOut(board.NEOPIXEL)
neo.direction = digitalio.Direction.OUTPUT
pixel_off = bytearray([0, 0, 0])
neopixel_write.neopixel_write(neo, pixel_off)
max_brightness = 16
r = int(random.random() * max_brightness)
g = int(random.random() * max_brightness)
b = int(random.random() * max_brightness)
def increment(n):
if n == max_brightness:
n = 0
else:
n = n + 1
return n
def argb_extend(r,g,b,length):
out = []
for i in range(length):
out.extend([r,g,b])
return bytearray(out)
while True:
direction = int(random.random() * 3)
if direction == 1:
r = increment(r)
elif direction == 2:
g = increment(g)
else:
b = increment(b)
led.value = not led.value
neopixel_write.neopixel_write(strip_io, argb_extend(r,g,b,8))
time.sleep(0.1)
# Sweep color changes across all 8 pixels
import board
import digitalio
import neopixel_write
import time
import random
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led.value = False
strip_io = digitalio.DigitalInOut(board.D4)
strip_io.direction = digitalio.Direction.OUTPUT
neopixel_write.neopixel_write(strip_io, bytearray([255,0,0]))
neo = digitalio.DigitalInOut(board.NEOPIXEL)
neo.direction = digitalio.Direction.OUTPUT
pixel_off = bytearray([0, 0, 0])
neopixel_write.neopixel_write(neo, pixel_off)
max_brightness = 16
r = int(random.random() * max_brightness)
g = int(random.random() * max_brightness)
b = int(random.random() * max_brightness)
def increment(n):
if n == max_brightness:
n = 0
else:
n = n + 1
return n
def argb_extend(r,g,b,length):
out = []
for i in range(length):
out.extend([r,g,b])
return out
def argb_append(r,g,b, out):
out = out[3:]
out.extend([r,g,b])
return out
initial = argb_extend(r,g,b,8)
while True:
direction = int(random.random() * 3)
if direction == 1:
r = increment(r)
elif direction == 2:
g = increment(g)
else:
b = increment(b)
led.value = not led.value
#neopixel_write.neopixel_write(strip_io, bytearray(argb_extend(r,g,b,8)))
initial = argb_append(r,g,b,initial)
neopixel_write.neopixel_write(strip_io, bytearray(initial))
time.sleep(0.1)
# rename to code.py before copying to device
import board
import digitalio
import neopixel_write
import time
import random
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led.value = False
neo = digitalio.DigitalInOut(board.NEOPIXEL)
neo.direction = digitalio.Direction.OUTPUT
pixel_off = bytearray([0, 0, 0])
neopixel_write.neopixel_write(neo, pixel_off)
max_brightness = 4
r = int(random.random() * max_brightness)
g = int(random.random() * max_brightness)
b = int(random.random() * max_brightness)
def increment(n):
if n == max_brightness:
n = 0
else:
n = n + 1
return n
while True:
direction = int(random.random() * 3)
if direction == 1:
r = increment(r)
elif direction == 2:
g = increment(g)
else:
b = increment(b)
led.value = not led.value
neopixel_write.neopixel_write(neo, bytearray([r, g, b]))
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment