Created
November 7, 2018 01:47
-
-
Save abachman/a882af93a1502058cc77015f6e5b5010 to your computer and use it in GitHub Desktop.
CircuitPython Trellis M4 color tilt demo
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
# 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