Skip to content

Commit ae8f775

Browse files
authored
Merge pull request #13 from kattni/macropad-demos
Add demos, update code to create HID objects when needed
2 parents 39097f3 + 4de30a0 commit ae8f775

8 files changed

+130
-12
lines changed

adafruit_macropad.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,10 @@ def _keys_and_pixels(order=None):
161161
self._sine_wave_sample = None
162162

163163
# Define HID:
164-
self._keyboard = Keyboard(usb_hid.devices)
165-
# This will need to be updated if we add more keyboard layouts. Currently there is only US.
166-
self._keyboard_layout = KeyboardLayoutUS(self._keyboard)
167-
self._consumer_control = ConsumerControl(usb_hid.devices)
168-
self._mouse = Mouse(usb_hid.devices)
164+
self._keyboard = None
165+
self._keyboard_layout = None
166+
self._consumer_control = None
167+
self._mouse = None
169168

170169
# Define MIDI:
171170
self._midi = adafruit_midi.MIDI(
@@ -292,9 +291,9 @@ def keys(self):
292291
macropad = MacroPad()
293292
294293
while True:
295-
event = macropad.keys.events.get()
296-
if event:
297-
print(event)
294+
key_event = macropad.keys.events.get()
295+
if key_event:
296+
print(key_event)
298297
"""
299298
return self._keys
300299

@@ -380,6 +379,8 @@ def keyboard(self):
380379
if macropad.encoder_switch:
381380
macropad.keyboard.send(macropad.Keycode.A)
382381
"""
382+
if self._keyboard is None:
383+
self._keyboard = Keyboard(usb_hid.devices)
383384
return self._keyboard
384385

385386
@property
@@ -402,6 +403,10 @@ def keyboard_layout(self):
402403
if macropad.encoder_switch:
403404
macropad.keyboard_layout.write("Hello World")
404405
"""
406+
if self._keyboard is None:
407+
self._keyboard = Keyboard(usb_hid.devices)
408+
# This will need to be updated if we add more keyboard layouts. Currently there is only US.
409+
self._keyboard_layout = KeyboardLayoutUS(self._keyboard)
405410
return self._keyboard_layout
406411

407412
@property
@@ -421,6 +426,8 @@ def consumer_control(self):
421426
if macropad.encoder_switch:
422427
macropad.consumer_control.send(macropad.ConsumerControlCode.VOLUME_DECREMENT)
423428
"""
429+
if self._consumer_control is None:
430+
self._consumer_control = ConsumerControl(usb_hid.devices)
424431
return self._consumer_control
425432

426433
@property
@@ -441,6 +448,8 @@ def mouse(self):
441448
if macropad.encoder_switch:
442449
macropad.mouse.click(macropad.Mouse.LEFT_BUTTON)
443450
"""
451+
if self._mouse is None:
452+
self._mouse = Mouse(usb_hid.devices)
444453
return self._mouse
445454

446455
@property
@@ -748,9 +757,9 @@ def display_text(
748757
text_lines = macropad.display_text(title="MacroPad Info")
749758
750759
while True:
751-
event = macropad.keys.events.get()
752-
if event:
753-
text_lines[0].text = "Key {} pressed!".format(event.key_number)
760+
key_event = macropad.keys.events.get()
761+
if key_event:
762+
text_lines[0].text = "Key {} pressed!".format(key_event.key_number)
754763
text_lines[1].text = "Rotary encoder {}".format(macropad.encoder)
755764
text_lines[2].text = "Encoder switch: {}".format(macropad.encoder_switch)
756765
text_lines.show()

examples/blinka.bmp

1.06 KB
Binary file not shown.

examples/blinka.bmp.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense

examples/macropad_display_image.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
MacroPad display image demo. Displays a bitmap image on the built-in display.
6+
"""
7+
from adafruit_macropad import MacroPad
8+
9+
macropad = MacroPad()
10+
11+
while True:
12+
macropad.display_image("blinka.bmp")

examples/macropad_keyboard_mouse.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
MacroPad HID keyboard and mouse demo. The demo sends "a" when the first key is pressed, a "B" when
6+
the second key is pressed, "Hello, World!" when the third key is pressed, and decreases the volume
7+
when the fourth key is pressed. It sends a right mouse click when the rotary encoder switch is
8+
pressed. Finally, it moves the mouse left and right when the rotary encoder is rotated
9+
counterclockwise and clockwise respectively.
10+
"""
11+
from adafruit_macropad import MacroPad
12+
13+
macropad = MacroPad()
14+
15+
last_position = 0
16+
while True:
17+
key_event = macropad.keys.events.get()
18+
19+
if key_event:
20+
if key_event.pressed:
21+
if key_event.key_number is 0:
22+
macropad.keyboard.send(macropad.Keycode.A)
23+
if key_event.key_number is 1:
24+
macropad.keyboard.press(macropad.Keycode.SHIFT, macropad.Keycode.B)
25+
macropad.keyboard.release_all()
26+
if key_event.key_number is 2:
27+
macropad.keyboard_layout.write("Hello, World!")
28+
if key_event.key_number is 3:
29+
macropad.consumer_control.send(
30+
macropad.ConsumerControlCode.VOLUME_DECREMENT
31+
)
32+
33+
macropad.encoder_switch_debounced.update()
34+
35+
if macropad.encoder_switch_debounced.pressed:
36+
macropad.mouse.click(macropad.Mouse.RIGHT_BUTTON)
37+
38+
current_position = macropad.encoder
39+
40+
if macropad.encoder > last_position:
41+
macropad.mouse.move(x=+5)
42+
last_position = current_position
43+
44+
if macropad.encoder < last_position:
45+
macropad.mouse.move(x=-5)
46+
last_position = current_position

examples/macropad_rainbow_keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"""
99
import displayio
1010
import terminalio
11+
from rainbowio import colorwheel
1112
from adafruit_display_text import bitmap_label as label
1213
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
1314
from adafruit_macropad import MacroPad
14-
from rainbowio import colorwheel
1515

1616
macropad = MacroPad()
1717

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
Simpletest demo for MacroPad. Displays the key pressed, the relative position of the rotary
6+
encoder, and the state of the rotary encoder switch to the built-in display. Note that the key
7+
pressed line does not appear until a key is pressed.
8+
"""
9+
from adafruit_macropad import MacroPad
10+
11+
macropad = MacroPad()
12+
13+
text_lines = macropad.display_text(title="MacroPad Info")
14+
15+
while True:
16+
key_event = macropad.keys.events.get()
17+
if key_event and key_event.pressed:
18+
text_lines[0].text = "Key {} pressed!".format(key_event.key_number)
19+
text_lines[1].text = "Rotary encoder {}".format(macropad.encoder)
20+
text_lines[2].text = "Encoder switch: {}".format(macropad.encoder_switch)
21+
text_lines.show()

examples/macropad_tone_keypad.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
MacroPad tone demo. Plays a different tone for each key pressed and lights up each key a different
6+
color while the key is pressed.
7+
"""
8+
from rainbowio import colorwheel
9+
from adafruit_macropad import MacroPad
10+
11+
macropad = MacroPad()
12+
13+
tones = [196, 220, 246, 262, 294, 330, 349, 392, 440, 494, 523, 587]
14+
15+
while True:
16+
key_event = macropad.keys.events.get()
17+
18+
if key_event:
19+
if key_event.pressed:
20+
macropad.pixels[key_event.key_number] = colorwheel(
21+
int(255 / 12) * key_event.key_number
22+
)
23+
macropad.start_tone(tones[key_event.key_number])
24+
25+
else:
26+
macropad.pixels.fill((0, 0, 0))
27+
macropad.stop_tone()

0 commit comments

Comments
 (0)