Last active
February 6, 2021 22:13
-
-
Save shapr/1b2c4951293d8a7521b977cbb63a4a64 to your computer and use it in GitHub Desktop.
This file contains 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
import time | |
import board | |
import busio | |
import adafruit_trellism4 | |
import adafruit_adxl34x | |
import supervisor | |
# Set up Trellis and accelerometer | |
trellis = adafruit_trellism4.TrellisM4Express(rotation=0) | |
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) | |
sensor = adafruit_adxl34x.ADXL345(i2c) | |
#define colors | |
black = (0,0,0) | |
red = (255,0,0) | |
green = (0,255,0) | |
yellow = (255,255,0) | |
off = (16,16,64) | |
# enable tap to clear | |
sensor.enable_tap_detection(threshold=50) | |
brightness = 1.0 | |
trellis.pixels.fill((16,16,64)) | |
# trellis.pixels.brightness(1) # full brightness on the LEDs, values go from 0 - 1, 0.3 is 30% brightness | |
pixlsh = [[x,y] for x in range(trellis.pixels.width) for y in range(trellis.pixels.height) ] | |
pixlsw = [[x,y] for y in range(trellis.pixels.height) for x in range(trellis.pixels.width) ] | |
# def timer(color,time): | |
# for | |
def countup(color,delay=0.01): | |
#[setloc(x,y,color) for x in range(trellis.pixels.width) for y in range(trellis.pixels.height)] | |
[setloc(xy[0],xy[1],color,delay) for xy in pixlsh] | |
[setloc(xy[0],xy[1],color,delay) for xy in pixlsw] | |
def setloc(x,y,color,delay): | |
#trellis.pixels[x,y] = color | |
trellis.pixels[x,y] = color | |
time.sleep(delay) | |
def color(c): | |
trellis.pixels.fill(c) | |
def color_from_input(): | |
data = input().strip() | |
if data == "": | |
return | |
if data == 'r': | |
#color(red) | |
countup(red) | |
time.sleep(0.5) | |
if data == 'g': | |
#color(green) | |
countup(green) | |
time.sleep(0.5) | |
if data == 'y': | |
#color(yellow) | |
countup(yellow) | |
time.sleep(0.5) | |
if data == 'o': | |
color(off) | |
while True: | |
pressed = trellis.pressed_keys | |
if pressed: | |
button_x_value = pressed[0][0] # first element of the list, first element of the x,y pair, should be 0-7 | |
brightness = (button_x_value + 3) / 10 | |
trellis.pixels.brightness = brightness | |
print("set trellis brightness to",brightness) | |
time.sleep(0.1) | |
if supervisor.runtime.serial_bytes_available: | |
color_from_input() | |
# check tap, no colors when detected | |
if sensor.events['tap']: | |
countup((16,16,64)) | |
countup((5,5,5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment