Skip to content

Instantly share code, notes, and snippets.

@jvantuyl
Created May 29, 2019 20:21
Show Gist options
  • Select an option

  • Save jvantuyl/9f6095f19d74a397fb2253ab40813f6f to your computer and use it in GitHub Desktop.

Select an option

Save jvantuyl/9f6095f19d74a397fb2253ab40813f6f to your computer and use it in GitHub Desktop.
Adafruit CircuitExpress Fiddling
from adafruit_circuitplayground.express import cpx
import time
## Constants
# timing settings
SLEEP = 0.001
# light range settings
MIN = 0
MAX = 16
# color ratios
CR = (1.0, 0.0, 0.0)
CO = (1.0, 0.5, 0.0)
CY = (1.0, 1.0, 0.0)
CG = (0.0, 1.0, 0.0)
CB = (0.0, 0.0, 1.0)
CI = (0.3, 0.5, 0.0)
CV = (0.6, 0.0, 0.8)
W1 = (0.1, 0.1, 0.1)
W3 = (0.3, 0.3, 0.3)
W5 = (0.5, 0.5, 0.5)
WX = (1.0, 1.0, 1.0)
## Helper Functions
# scale a color ratio into a final color
def scale(color, val):
return (
int(color[0] * val),
int(color[1] * val),
int(color[2] * val),
)
# helper to set colors to happy rainbow, scaled by some value and offset by some number of leds
def set_color(val, offset):
cpx.pixels[(0 + offset) % 10] = scale(CR, val)
cpx.pixels[(1 + offset) % 10] = scale(CO, val)
cpx.pixels[(2 + offset) % 10] = scale(CY, val)
cpx.pixels[(3 + offset) % 10] = scale(CG, val)
cpx.pixels[(4 + offset) % 10] = scale(CI, val)
cpx.pixels[(5 + offset) % 10] = scale(CV, val)
cpx.pixels[(6 + offset) % 10] = scale(W1, val)
cpx.pixels[(7 + offset) % 10] = scale(W3, val)
cpx.pixels[(8 + offset) % 10] = scale(W5, val)
cpx.pixels[(9 + offset) % 10] = scale(WX, val)
cpx.pixels.show()
# class to track button presses over time
class Buttons(object):
def __init__(self):
self.a = False
self.b = False
self.presses = []
def get_presses(self):
for press in self.presses:
yield press
self.presses = []
def check_io(self):
a = cpx.button_a
b = cpx.button_b
if not self.a and a:
self.presses.append('a')
if not self.b and b:
self.presses.append('b')
if cpx.tapped:
self.presses.append('tap')
self.a, self.b = a, b
## Initialization of Useful Stuff
butt = Buttons()
cpx.pixels.auto_write = False
cpx.detect_taps = 1
# generator of an infinite see-saw pattern of scale values to "pulse" leds
def see_saw():
while True:
for i in range(MIN, MAX):
yield i
for i in range(MAX, MIN, -1):
yield i
## Main logic
def main():
print("Starting...")
ofs = 0
# for an infinite see-saw of patterns
for i in see_saw():
# set colors
set_color(i, ofs)
# wait
time.sleep(SLEEP)
# stop any beep we started
cpx.stop_tone()
# check for button presses
butt.check_io()
# process all button presses to get new offset
for press in butt.get_presses():
if press == 'a':
# A offsets counter-clockwise
ofs -= 1
if press == 'b':
# B offsets clockwise
ofs += 1
if press == 'tap':
# taps reset offset
ofs = 0
# cap offset in [0, 9]
ofs %= 10
# print changed offset
print("Offset:", ofs)
# start a beep whose tone depends on the offset (gets stopped after the sleep above)
cpx.start_tone(500 + 20 * ofs)
# run code
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment