-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·143 lines (125 loc) · 5.27 KB
/
code.py
File metadata and controls
executable file
·143 lines (125 loc) · 5.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
from adafruit_macropad import MacroPad
import time
# Mode of the rotary switch, changed by pressing the switch
class CurrentMode():
# Change the number of dice being rolled
COUNT = 1
# Change the +/- modifier added to the roll
MODIFIER = 2
# The dice available to roll, in the order they're presented
# There's some flexibility to change this, but changing the length will require additional changes
dice = [
"2",
"4",
"6",
"8",
"10",
"12",
"20",
"100"
]
# string format for a die to display
die_format = "{{0:2d}}d{:3}"
# All string formats for the display: dice to roll, plus last line
text_formats = [
"{0} {0} {0}".format(die_format).format(*dice[0:3]),
"{0} {0} {0}".format(die_format).format(*dice[3:6]),
"{0} {0}".format(die_format).format(*dice[6:]),
"{0:6s} {1:<+3d} {{mute}}"
]
# Refreshes the macropad's display given the mode, dice_count, dice_modifier, and whether newlines should be sent
# This calls refresh_pixels
def refresh_display(macropad, current_mode, dice_count, dice_modifier, send_newline):
text_lines = macropad.display_text()
text_lines[0].text = text_formats[0].format(dice_count)
text_lines[1].text = text_formats[1].format(dice_count)
text_lines[2].text = text_formats[2].format(dice_count)
text_lines[3].text = text_formats[3].format(" \\n" if send_newline else "", dice_modifier)
text_lines.show()
refresh_pixels(macropad.pixels, current_mode, dice_count, dice_modifier, send_newline)
# Refreshes the macropad's pixels for a given mode, dice_count and dice_modifier.
# Helper for refresh_display, don't call directly
#
# Uses the pixels to display (in white) the number of dice that'll be rolled
# Uses green/red for magnitude of the modifier
# Blended color if the pixel is lit up for both dice count & modifier
# Auto-send toggle in bottom left in blue
# Reset button in bottom middle, red if it'll do anything
# Mute button on bottom right
def refresh_pixels(pixels, current_mode, dice_count, dice_modifier, send_newline):
new_pixels = []
for i in range(9):
is_on_for_count = i < dice_count
if i >= abs(dice_modifier) and not is_on_for_count:
# completely off
color = 0x0
elif i >= abs(dice_modifier):
# Only on for dice count
color = 0x808080
elif dice_modifier >= 0 and not is_on_for_count:
# positive modifier, not on for count
color = 0x00C000
elif dice_modifier >= 0:
# positive modifier and dice count
color = 0X40FF40
elif not is_on_for_count:
# negative modifier, not on for count
color = 0xC00000
else:
# negative modifier and dice count
color = 0XFF2020
new_pixels.append(color)
new_pixels.extend([
0x0000C0 if send_newline else 0x000010,
0x800000 if not (current_mode == CurrentMode.COUNT and dice_count is 1 and dice_modifier is 0) else 0x0,
0x808000
])
pixels[:] = new_pixels
macropad = MacroPad()
previous_encoder = macropad.encoder
current_mode = CurrentMode.COUNT
dice_count = 1
dice_modifier = 0
send_newline = True
debug_output = False
refresh_display(macropad, current_mode, dice_count, dice_modifier, send_newline)
while True:
macropad.encoder_switch_debounced.update()
if macropad.encoder_switch_debounced.pressed:
current_mode = CurrentMode.MODIFIER if current_mode == CurrentMode.COUNT else CurrentMode.COUNT
continue
encoder_delta = macropad.encoder - previous_encoder
previous_encoder = macropad.encoder
if encoder_delta != 0:
if current_mode == CurrentMode.COUNT:
dice_count = max(1, dice_count + encoder_delta)
else:
dice_modifier += encoder_delta
refresh_display(macropad, current_mode, dice_count, dice_modifier, send_newline)
key_event = macropad.keys.events.get()
if key_event and key_event.pressed:
if key_event.key_number in range(len(dice)):
# Send key presses matching the "roll" string for this die
output = "/r {}d{}{}{}".format(
dice_count,
dice[key_event.key_number],
" {:+d}".format(dice_modifier) if dice_modifier != 0 else "",
"\n" if send_newline else ""
)
macropad.keyboard_layout.write(output)
if debug_output:
print(output.replace("\n", "\\n"))
if key_event.key_number is 9:
# toggle whether or not newline is sent at the end of the string
send_newline = not send_newline
refresh_display(macropad, current_mode, dice_count, dice_modifier, send_newline)
if key_event.key_number is 10:
# Reset dice count, modifier, and current mode
dice_count = 1
dice_modifier = 0
current_mode = CurrentMode.COUNT
refresh_display(macropad, current_mode, dice_count, dice_modifier, send_newline)
if key_event.key_number is 11:
# Send key combination for Zoom's global mute/un-mute shortcut
macropad.keyboard.press(macropad.Keycode.SHIFT, macropad.Keycode.COMMAND, macropad.Keycode.A)
macropad.keyboard.release_all()