import time
|
|
import board
|
|
import digitalio
|
|
import gc
|
|
import busio
|
|
|
|
from adafruit_hid.keyboard import Keyboard
|
|
from adafruit_hid.keycode import Keycode
|
|
from adafruit_hid.mouse import Mouse
|
|
from adafruit_neotrellis.neotrellis import NeoTrellis
|
|
|
|
#create the i2c object for the trellis
|
|
i2c_bus = busio.I2C(board.SCL, board.SDA)
|
|
|
|
#create the trellis
|
|
trellis = NeoTrellis(i2c_bus)
|
|
|
|
SWITCH_PINS = (board.D5, board.D6, board.D9, board.D10, board.D11, board.D12, board.A1)
|
|
BOUNCE_SECS = 0.100
|
|
|
|
gc.collect()
|
|
print(gc.mem_free())
|
|
|
|
switch_ins = tuple(digitalio.DigitalInOut(pin) for pin in SWITCH_PINS)
|
|
for switch_in in switch_ins:
|
|
switch_in.switch_to_input(pull=digitalio.Pull.UP)
|
|
|
|
kbd = Keyboard()
|
|
|
|
OFF = (1, 0, 0)
|
|
LIT = (40, 20, 0)
|
|
|
|
states = {}
|
|
|
|
def toggle(event):
|
|
#turn the LED on when a rising edge is detected
|
|
if event.edge == NeoTrellis.EDGE_RISING:
|
|
states[event.number] = not states[event.number]
|
|
|
|
for state, value in states.items():
|
|
if value:
|
|
trellis.pixels[state] = LIT
|
|
else:
|
|
trellis.pixels[state] = OFF
|
|
|
|
for i in range(16):
|
|
states[i] = False
|
|
#activate rising edge events on all keys
|
|
trellis.activate_key(i, NeoTrellis.EDGE_RISING)
|
|
#activate falling edge events on all keys
|
|
trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
|
|
#set all keys to trigger the blink callback
|
|
trellis.callbacks[i] = toggle
|
|
|
|
"""Start shortcut processing. Run forever."""
|
|
while True:
|
|
for switch, switch_in in enumerate(switch_ins):
|
|
trellis.sync()
|
|
if not switch_in.value:
|
|
# If switch is pressed, it's pulled low. Debounce by waiting for bounce time.
|
|
time.sleep(BOUNCE_SECS)
|
|
print('switch: ', switch)
|
|
print(gc.mem_free())
|
|
kbd.send(Keycode.LEFT_GUI, Keycode.TAB)
|
|
|
|
# Wait for switch to be released.
|
|
while not switch_in.value:
|
|
pass
|