Browse Source

move basic sketch into a class, similar to glitterpos

master
Brennen Bearnes 2 years ago
parent
commit
eaf8dc7e6b
3 changed files with 71 additions and 68 deletions
  1. +12
    -0
      code.py
  2. +0
    -68
      main.py
  3. +59
    -0
      snakeswitch.py

+ 12
- 0
code.py View File

@ -0,0 +1,12 @@
"""
Main program loop for SnakeSwitch
https://github.com/adafruit/snakeswitch
* Author(s): Brennen Bearnes
"""
from snakeswitch import SnakeSwitch
ss = SnakeSwitch()
while True:
ss.advance_frame()

+ 0
- 68
main.py View File

@ -1,68 +0,0 @@
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

+ 59
- 0
snakeswitch.py View File

@ -0,0 +1,59 @@
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
OFF = (1, 0, 0)
LIT = (40, 20, 0)
SWITCH_PINS = (board.D5, board.D6, board.D9, board.D10, board.D11, board.D12, board.A1)
BOUNCE_SECS = 0.100
class SnakeSwitch:
def __init__(self):
i2c_bus = busio.I2C(board.SCL, board.SDA)
self.trellis = NeoTrellis(i2c_bus)
self.switch_ins = tuple(digitalio.DigitalInOut(pin) for pin in SWITCH_PINS)
for switch_in in self.switch_ins:
switch_in.switch_to_input(pull=digitalio.Pull.UP)
self.kbd = Keyboard()
self.states = {}
for i in range(16):
self.states[i] = False
# activate rising / falling edge events on all keys, set up callback
self.trellis.activate_key(i, NeoTrellis.EDGE_RISING)
self.trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
self.trellis.callbacks[i] = self.toggle
def toggle(self, event):
# turn the LED on when a rising edge is detected
if event.edge == NeoTrellis.EDGE_RISING:
self.states[event.number] = not self.states[event.number]
for state, value in self.states.items():
if value:
self.trellis.pixels[state] = LIT
else:
self.trellis.pixels[state] = OFF
def advance_frame(self):
for switch, switch_in in enumerate(self.switch_ins):
self.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())
self.kbd.send(Keycode.LEFT_GUI, Keycode.TAB)
# Wait for switch to be released.
while not switch_in.value:
pass

Loading…
Cancel
Save