Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CircuitPython Trellis M4 color tilt demo
# Trellis M4 Sketch
# tilt the board to change the color
# using https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4
# and https://www.adafruit.com/product/4020
import time
import board
import busio
import adafruit_adxl34x
import adafruit_trellism4
trellis = adafruit_trellism4.TrellisM4Express()
color = (0, 0, 0)
# Our accelerometer
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
accelerometer = adafruit_adxl34x.ADXL345(i2c)
# return full saturation (R, G, B) color
def Wheel(pos):
pos = 255 - pos
if pos <= 85:
return (255 - pos * 3, 0, pos * 3)
elif pos > 85 and pos <= 170:
pos -= 85
return (0, pos * 3, 255 - pos * 3)
elif pos > 170:
pos -= 170
return (pos * 3, 255 - pos * 3, 0)
def linmap(x, inx, iny, ox, oy):
return ((x - inx) * (oy - ox) + ox * (iny - inx)) / (iny - inx)
last_print = 0
print_after = 1
tilt = accelerometer.acceleration[1]
color = Wheel(0)
while True:
tick = time.monotonic()
tilt = accelerometer.acceleration[1]
if tick > last_print + print_after:
print("%0.3f %0.1f %s" % (tick, tilt, color))
last_print = tick
for x in range(trellis.pixels.width):
for y in range(trellis.pixels.height):
# convert acceleration left, right from [-12, 12] to [0, 255]
color = Wheel(int(linmap(tilt, -12, 12, 0, 255)))
trellis.pixels[x, y] = color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.