forked from codepope/KeyBow-Pico-CircuitPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
182 lines (157 loc) · 6.27 KB
/
code.py
File metadata and controls
182 lines (157 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import board
import digitalio
import adafruit_dotstar as dotstar
import random
import time
from adafruit_debouncer import Button
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
# Set up the keyboard and layout
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
# Set up consumer control (used to send media key presses)
consumer_control = ConsumerControl(usb_hid.devices)
#IO locations
_PINS = [board.GP17,
board.GP22,
board.GP6]
NUM_KEYS = len(_PINS)
#LED locations
_LEDS = [2,
1,
0]
def random_color():
return random.randrange(0, 7) * 32
#intialize the array of LEDs
pixels=dotstar.DotStar(board.GP10, board.GP11, NUM_KEYS, brightness=0.2)
#initialize Debounce Buttons
switches = []
_short_press = 200 #ms
_long_press = 500 #ms
#define the button array per PIN in the array above
for i in range(len(_PINS)):
_pin = digitalio.DigitalInOut(_PINS[i])
_pin.direction = digitalio.Direction.INPUT
_pin.pull = digitalio.Pull.UP
#adafruit_debouncer.Button(pin: Union[ROValueIO, Callable[[], bool]],
# short_duration_ms: int = 200,
# long_duration_ms: int = 500,
# value_when_pressed: bool = False,
# **kwargs)
_switches = Button(_pin, _short_press, _long_press)
switches.append(_switches)
# Our layers. The key of item in the layer dictionary is the key number on
# Keybow to map to, and the value is the key press to send.
# Note that keys 0-3 are reserved as the modifier and layer selector keys
# respectively.
layer_1 = {0: Keycode.ZERO,
1: Keycode.ONE,
2: Keycode.TWO
}
layer_2 = {0: "pack ",
1: "my ",
2: "box "
}
layer_3 = {0: ConsumerControlCode.MUTE,
1: ConsumerControlCode.VOLUME_DECREMENT,
2: ConsumerControlCode.VOLUME_INCREMENT
}
layers = {0: layer_1,
1: layer_2,
2: layer_3
}
# Define the modifier key and layer selector keys
modifier = [0]
selectors = [1,2]
# Start on layer 1
current_layer = 0
# The colours for each layer
colours = {0: (255, 0, 255),
1: (0, 255, 255),
2: (255, 255, 0)}
layer_keys = range(4, 16)
# Set the LEDs for each key in the current layer
for x in range(NUM_KEYS):
pixels[_LEDS[x]] = colours[current_layer]
# To prevent the strings (as opposed to single key presses) that are sent from
# refiring on a single key press, the debounce time for the strings has to be
# longer.
short_debounce = 0.03
long_debounce = 0.15
debounce = 0.03
fired = False
while True:
for k in range(NUM_KEYS):
# Always remember to call button.update()!
switches[k].update()
#Modifications mode - change layers or brightness
if current_layer == -1:
if switches[k].short_count == 1:
current_layer = k
for layer in layers.keys():
pixels[_LEDS[layer]] = colours[current_layer]
if switches[k].short_count > 1:
newBrightness = switches[k].short_count/10
if k==1:
pixels.brightness=pixels.brightness+newBrightness
elif k==2:
pixels.brightness=pixels.brightness-newBrightness
pixels.brightness=pixels.brightness+0.01
#doubleclick - Long Press event
elif switches[k].long_press and switches[k].short_count == 1:
if k is 0:
print("MODIFIER TRIGGER")
#pixels[_LEDS[k]] = (0,0,0)
# If the modifier key is held, light up the layer selector keys
for layer in layers.keys():
pixels[_LEDS[layer]] = colours[layer]
current_layer = -1
else:
#doesn't currently have a use????
print("That's a long double press !" , k)
#pixels[_LEDS[k]] = (random_color(), random_color(), random_color())
#Long Press event
elif switches[k].long_press:
#maybe new layer for longpress keys?
key_press = layers[current_layer][k]
#doesn't currently have a use????
print("Long Press", k)
while switches[k].released == False:
if current_layer == 0:
debounce = short_debounce
#print(key_press, "current_layer ", current_layer)
keyboard.send(key_press)
elif current_layer == 1:
debounce = long_debounce
#print(key_press, "current_layer ", current_layer)
layout.write(key_press)
elif current_layer == 2:
debounce = short_debounce
#print(key_press, "current_layer ", current_layer)
consumer_control.send(key_press)
switches[k].update()
time.sleep(debounce)
#Short Press event
elif switches[k].short_count != 0:
print("Short Press Count =", switches[k].short_count, "k = ", k)
for n in range(switches[k].short_count):
#capture expected output based on current layer and k(key number)
key_press = layers[current_layer][k]
if current_layer == 0:
debounce = short_debounce
#print(key_press, "current_layer ", current_layer)
keyboard.send(key_press)
elif current_layer == 1:
debounce = long_debounce
#print(key_press, "current_layer ", current_layer)
layout.write(key_press)
elif current_layer == 2:
debounce = short_debounce
#print(key_press, "current_layer ", current_layer)
consumer_control.send(key_press)
switches[k].update()
time.sleep(debounce)