-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
151 lines (116 loc) · 3.99 KB
/
code.py
File metadata and controls
151 lines (116 loc) · 3.99 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
import board
import collections
import keypad
import math
import time
import usb_hid
import colorsys
import neopixel
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
NEOPIXEL_PIN = board.GP0
KEY_PINS = ( # from left to right
board.GP3,
board.GP5,
board.GP6,
board.GP8,
)
KEY_VALUES = (
Keycode.D,
Keycode.F,
Keycode.J,
Keycode.K,
)
N_KEYS = len(KEY_PINS)
# list of integer gamma-correction values for accurate neopixel colours
GAMMA_TABLE = [int(math.pow(i / 255, 2.8) * 255 + 0.5) for i in range(256)]
def gamma_corrected(colour):
r, g, b = colour
return GAMMA_TABLE[r], GAMMA_TABLE[g], GAMMA_TABLE[b]
COLOUR_OFF = gamma_corrected((63, 63, 63))
# as the keys are pressed with varying intensity, their
# colour/hue should vary to indicate it
HUE_SLOW = 0.40
HUE_FAST = 0.02
HUE_STEPS = 16
INTENSITY_COLOURS = [
colorsys.hsv_to_rgb(
HUE_SLOW + step * (HUE_FAST - HUE_SLOW) / (HUE_STEPS - 1),
1.0,
1.0,
) for step in range(HUE_STEPS)
]
# how many recent key presses to indicate intensity
RECENT_PRESS_COUNT = 3
INTENSE_KPS = 10
INTENSITY_RESET_NS = 1 * 10 ** 9
# as the keys are held, their colour should fade.
COLOUR_HOLD = gamma_corrected(colorsys.hsv_to_rgb(0.65, 0.25, 1.0))
FADE_STEPS = 16
FADE_STEP_INTERVAL_NS = int(0.5 * 10 ** 9) // FADE_STEPS
COLOUR_MATRIX = [
# generate a gradient between the key press colour and
# the final hold colour, using linear interpolation
# across the space of RGB colours.
[
gamma_corrected(tuple(int(press_colour[i] + step * (
COLOUR_HOLD[i] - press_colour[i]
) / (FADE_STEPS - 1)) for i in range(3))) # r, g, b
for step in range(FADE_STEPS)
] for press_colour in INTENSITY_COLOURS
]
# end of constants. set up lights and keys!
neopixels = neopixel.NeoPixel(NEOPIXEL_PIN, n=N_KEYS, auto_write=False)
neopixels.fill(COLOUR_OFF)
keys = keypad.Keys(KEY_PINS, value_when_pressed=True, pull=True)
keyboard = Keyboard(usb_hid.devices)
# use deques to keep track of recent press times
keys_intensity = [0] * N_KEYS
recent_presses = tuple(
collections.deque((), RECENT_PRESS_COUNT) for _ in range(N_KEYS)
)
keys_held_since = [None] * N_KEYS
key_colours = [COLOUR_OFF] * N_KEYS
def calculate_intensity_step(key_presses, time_now):
press_reset_threshold = time_now - INTENSITY_RESET_NS
while key_presses and key_presses[0] < press_reset_threshold:
key_presses.popleft()
if not key_presses:
key_presses.append(time_now)
return 0 # first press, not enough info yet
avg_ns_per_key = (time_now - key_presses[0]) // len(key_presses)
intensity_step = (HUE_STEPS * 10 ** 9) // (avg_ns_per_key * INTENSE_KPS)
key_presses.append(time_now)
return intensity_step
# now begins the event loop
while True:
time_now = time.monotonic_ns()
write_pixels = False
event = keys.events.get()
if event:
key = event.key_number
if event.pressed:
keyboard.press(KEY_VALUES[key])
keys_held_since[key] = time_now
keys_intensity[key] = calculate_intensity_step(
recent_presses[key], time_now
)
if event.released:
keyboard.release(KEY_VALUES[key])
keys_held_since[key] = None
# there isn't any special colour behaviour for
# released keys, so that can just be set now.
neopixels[key] = COLOUR_OFF
write_pixels = True
for key, held_since in enumerate(keys_held_since):
if not held_since:
continue
current_fade_step = (time_now - held_since) // FADE_STEP_INTERVAL_NS
fade_index = min(FADE_STEPS - 1, current_fade_step)
intensity_index = min(HUE_STEPS - 1, keys_intensity[key])
current_colour = COLOUR_MATRIX[intensity_index][fade_index]
if neopixels[key] != current_colour:
neopixels[key] = current_colour
write_pixels = True
if write_pixels:
neopixels.show()