-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathcode.py
141 lines (134 loc) · 4.95 KB
/
code.py
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
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import board
import keypad
import rotaryio
import neopixel
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
mouse = Mouse(usb_hid.devices)
# neopixel colors
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
AQUA = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (127, 0, 255)
PINK = (255, 0, 255)
OFF = (50, 50, 50)
# axis states selected with keys 9-11
axis_states = [0, "x", "y", "z"]
state = axis_states[0]
# keymap for key matrix
keymap = {
(0): (axis_states[0], (Keycode.COMMAND, Keycode.COMMA), BLUE), # SETTINGS
(1): (axis_states[0], (Keycode.COMMAND, Keycode.P), ORANGE), # SLICE MODEL
(2): (axis_states[0], (Keycode.COMMAND, Keycode.D), RED), # CLEAR BED
(3): (axis_states[0], [Keycode.T], GREEN), # MOVE
(4): (axis_states[0], [Keycode.S], AQUA), # SCALE
(5): (axis_states[0], [Keycode.R], BLUE), # ROTATE
(6): (axis_states[0], [Keycode.M], AQUA), # MIRROR
(7): (axis_states[0], [Keycode.E], PURPLE), # SUPPORT BLOCKERS
(8): (axis_states[0], [Keycode.I], PINK), # TABS
(9): (axis_states[1], None, RED), # SET X-AXIS STATE
(10): (axis_states[2], None, GREEN), # SET Y-AXIS STATE
(11): (axis_states[3], None, BLUE), # SET Z-AXIS STATE
}
# keymap for encoder based on state; pos = [0], neg = [1]
encoder_map = {
("x"): ([Keycode.RIGHT_ARROW], [Keycode.LEFT_ARROW]),
("y"): ([Keycode.UP_ARROW], [Keycode.DOWN_ARROW]),
# ("z"): ([Keycode.W], [Keycode.S]),
}
# make a keyboard
kbd = Keyboard(usb_hid.devices)
# key matrix
COLUMNS = 3
ROWS = 4
keys = keypad.KeyMatrix(
row_pins=(board.D12, board.D11, board.D10, board.D9),
column_pins=(board.A0, board.A1, board.A2),
columns_to_anodes=False,
)
# neopixels and key num to pixel function
pixels = neopixel.NeoPixel(board.D5, 12, brightness=0.3)
def key_to_pixel_map(key_number):
row = key_number // COLUMNS
column = key_number % COLUMNS
if row % 2 == 1:
column = COLUMNS - column - 1
return row * COLUMNS + column
pixels.fill(OFF) # Begin with pixels off.
# make an encoder
encoder = rotaryio.IncrementalEncoder(board.D24, board.D25)
last_position = 0
while True:
# poll for key event
key_event = keys.events.get()
# get position of encoder
position = encoder.position
# if position changes..
if position != last_position:
# ..and it increases..
if position > last_position:
# ..and state is x:
if state is axis_states[1]:
kbd.press(*encoder_map[state][0])
# ..and state is y:
if state is axis_states[2]:
kbd.press(*encoder_map[state][0])
# ..and state is z:
if state is axis_states[3]:
mouse.move(wheel=-1)
# ..and it decreases..
if position < last_position:
# ..and state is x:
if state is axis_states[1]:
kbd.press(*encoder_map[state][1])
# ..and state is y:
if state is axis_states[2]:
kbd.press(*encoder_map[state][1])
# ..and state is z:
if state is axis_states[3]:
mouse.move(wheel=1)
# print(position)
# release all keys
kbd.release_all()
# update last_position
last_position = position
# if a key event..
if key_event:
# print(key_event)
# ..and it's pressed..
if key_event.pressed:
# ..and it's keys 0-8, send key presses from keymap:
if keymap[key_event.key_number][0] is axis_states[0]:
state = axis_states[0]
kbd.press(*keymap[key_event.key_number][1])
# ..and it's key 9, set state to x
if keymap[key_event.key_number][0] is axis_states[1]:
state = axis_states[1]
pixels[key_to_pixel_map(10)] = OFF
pixels[key_to_pixel_map(11)] = OFF
# ..and it's key 10, set state to y
if keymap[key_event.key_number][0] is axis_states[2]:
state = axis_states[2]
pixels[key_to_pixel_map(9)] = OFF
pixels[key_to_pixel_map(11)] = OFF
# ..and it's key 11, set state to z
if keymap[key_event.key_number][0] is axis_states[3]:
state = axis_states[3]
pixels[key_to_pixel_map(9)] = OFF
pixels[key_to_pixel_map(10)] = OFF
# turn on neopixel for key with color from keymap
pixels[key_to_pixel_map(key_event.key_number)] = keymap[key_event.key_number][2]
# ..and it's released..
if key_event.released:
# if it's key 0-8, release the key press and turn off neopixel
if keymap[key_event.key_number][0] is axis_states[0]:
kbd.release(*keymap[key_event.key_number][1])
pixels.fill(OFF)