-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
145 lines (125 loc) · 4.19 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
142
143
144
145
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# You must add a gamepad HID device inside your boot.py file
# in order to use this example.
# See this Learn Guide for details:
# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9
import usb_hid
import displayio
import terminalio
from adafruit_display_shapes.rect import Rect
from adafruit_display_text import label
from adafruit_macropad import MacroPad
from hid_gamepad import Gamepad
def axis_range_map(x):
"""Map encoder value to axis value
Args:
x (int): encoder value
Returns:
int: Mapped axis value
"""
return (x - ENCODER_MIN) * (JOY_MAX - JOY_MIN) // (ENCODER_MAX - ENCODER_MIN) + JOY_MIN
def clamp(value, low, high):
"""Clamp value between low and high
Args:
value (int or float): Value to clamp
low (int or float): Minimum value
high (int or float): Maximum value
Returns:
int or float: Clamped value
"""
return max(low, min(value, high))
macropad = MacroPad()
gamepad = Gamepad(usb_hid.devices)
# Key mapping
keymap = {
'title' : 'PurplePad',
'keys' : [ # List of key functions...
# COLOR LABEL
# 1st row ----------
(0x661199, '1'), (0x661199, '2'), (0x661199, '3'),
# 2nd row ----------
(0x660099, '4'), (0x660099, '5'), (0x660099, '6'),
# 3rd row ----------
(0x660099, '7'), (0x660099, '8'), (0x660099, '9'),
# 4th row ----------
(0x660099, '10'), (0x660099, '11'), (0x660099, '12')
]
}
# Set key LED brightness
macropad.pixels.brightness = 0.5
# Encoder offset
JOY_MAX = +127
JOY_MIN = -127
ENCODER_MAX = +20
ENCODER_MIN = -20
prev_encoder_value = 0.0
encoder_offset = 0
# Initialize display layout
display_group = displayio.Group()
# Key labels (0 - 11)
for key_index in range(12):
x = key_index % 3
y = key_index // 3
display_group.append(
label.Label(
terminalio.FONT,
text='',
color=0xFFFFFF,
anchored_position=((macropad.display.width - 1) * x / 2, macropad.display.height - 1 - (3 - y) * 12),
anchor_point=(x / 2, 1.0)
)
)
# Title bar (12)
display_group.append(Rect(0, 0, macropad.display.width, 13, fill=0xFFFFFF))
# Title text (13)
display_group.append(label.Label(
terminalio.FONT,
text='',
color=0x000000,
anchored_position=(2, 0),
anchor_point=(0.0, 0.0)
))
# Encoder text (14)
display_group.append(label.Label(
terminalio.FONT,
text='',
color=0x000000,
anchored_position=(macropad.display.width, 0),
anchor_point=(1.0, 0.0)
))
# Set display layout group
macropad.display.root_group = display_group
# Set text
display_group[13].text = keymap['title']
display_group[14].text = "{}%".format(prev_encoder_value)
for i in range(12):
macropad.pixels[i] = keymap['keys'][i][0]
display_group[i].text = keymap['keys'][i][1]
# Main loop
while True:
# Check for encoder switch
macropad.encoder_switch_debounced.update()
if macropad.encoder_switch_debounced.pressed:
encoder_offset = -macropad.encoder
macropad.play_tone(1000, 0.1)
while macropad.encoder_switch: continue
# Check if encoder has moved
current_encoder_value = clamp(macropad.encoder + encoder_offset, ENCODER_MIN, ENCODER_MAX)
if not current_encoder_value == prev_encoder_value:
encoder_text = "{}%".format(current_encoder_value / ENCODER_MAX * 100)
if current_encoder_value > 0.0: encoder_text = "+" + encoder_text
display_group[14].text = encoder_text
# Update previous encoder value
prev_encoder_value = current_encoder_value
# Move analog axis
gamepad.move_joysticks(x=axis_range_map(current_encoder_value))
# Check for key press
key_event = macropad.keys.events.get()
if not key_event: continue
if key_event and key_event.pressed:
display_group[key_event.key_number].text = len(keymap['keys'][key_event.key_number][1]) * "#"
gamepad.press_buttons(key_event.key_number + 1)
if key_event and key_event.released:
display_group[key_event.key_number].text = keymap['keys'][key_event.key_number][1]
gamepad.release_buttons(key_event.key_number + 1)