Skip to content

Add demos, update code to create HID objects when needed #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions adafruit_macropad.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ def _keys_and_pixels(order=None):
self._sine_wave_sample = None

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

# Define MIDI:
self._midi = adafruit_midi.MIDI(
Expand Down Expand Up @@ -292,9 +291,9 @@ def keys(self):
macropad = MacroPad()

while True:
event = macropad.keys.events.get()
if event:
print(event)
key_event = macropad.keys.events.get()
if key_event:
print(key_event)
"""
return self._keys

Expand Down Expand Up @@ -380,6 +379,8 @@ def keyboard(self):
if macropad.encoder_switch:
macropad.keyboard.send(macropad.Keycode.A)
"""
if self._keyboard is None:
self._keyboard = Keyboard(usb_hid.devices)
return self._keyboard

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

@property
Expand All @@ -421,6 +426,8 @@ def consumer_control(self):
if macropad.encoder_switch:
macropad.consumer_control.send(macropad.ConsumerControlCode.VOLUME_DECREMENT)
"""
if self._consumer_control is None:
self._consumer_control = ConsumerControl(usb_hid.devices)
return self._consumer_control

@property
Expand All @@ -441,6 +448,8 @@ def mouse(self):
if macropad.encoder_switch:
macropad.mouse.click(macropad.Mouse.LEFT_BUTTON)
"""
if self._mouse is None:
self._mouse = Mouse(usb_hid.devices)
return self._mouse

@property
Expand Down Expand Up @@ -748,9 +757,9 @@ def display_text(
text_lines = macropad.display_text(title="MacroPad Info")

while True:
event = macropad.keys.events.get()
if event:
text_lines[0].text = "Key {} pressed!".format(event.key_number)
key_event = macropad.keys.events.get()
if key_event:
text_lines[0].text = "Key {} pressed!".format(key_event.key_number)
text_lines[1].text = "Rotary encoder {}".format(macropad.encoder)
text_lines[2].text = "Encoder switch: {}".format(macropad.encoder_switch)
text_lines.show()
Expand Down
Binary file added examples/blinka.bmp
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/blinka.bmp.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
12 changes: 12 additions & 0 deletions examples/macropad_display_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
MacroPad display image demo. Displays a bitmap image on the built-in display.
"""
from adafruit_macropad import MacroPad

macropad = MacroPad()

while True:
macropad.display_image("blinka.bmp")
46 changes: 46 additions & 0 deletions examples/macropad_keyboard_mouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
MacroPad HID keyboard and mouse demo. The demo sends "a" when the first key is pressed, a "B" when
the second key is pressed, "Hello, World!" when the third key is pressed, and decreases the volume
when the fourth key is pressed. It sends a right mouse click when the rotary encoder switch is
pressed. Finally, it moves the mouse left and right when the rotary encoder is rotated
counterclockwise and clockwise respectively.
"""
from adafruit_macropad import MacroPad

macropad = MacroPad()

last_position = 0
while True:
key_event = macropad.keys.events.get()

if key_event:
if key_event.pressed:
if key_event.key_number is 0:
macropad.keyboard.send(macropad.Keycode.A)
if key_event.key_number is 1:
macropad.keyboard.press(macropad.Keycode.SHIFT, macropad.Keycode.B)
macropad.keyboard.release_all()
if key_event.key_number is 2:
macropad.keyboard_layout.write("Hello, World!")
if key_event.key_number is 3:
macropad.consumer_control.send(
macropad.ConsumerControlCode.VOLUME_DECREMENT
)

macropad.encoder_switch_debounced.update()

if macropad.encoder_switch_debounced.pressed:
macropad.mouse.click(macropad.Mouse.RIGHT_BUTTON)

current_position = macropad.encoder

if macropad.encoder > last_position:
macropad.mouse.move(x=+5)
last_position = current_position

if macropad.encoder < last_position:
macropad.mouse.move(x=-5)
last_position = current_position
2 changes: 1 addition & 1 deletion examples/macropad_rainbow_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"""
import displayio
import terminalio
from rainbowio import colorwheel
from adafruit_display_text import bitmap_label as label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
from adafruit_macropad import MacroPad
from rainbowio import colorwheel

macropad = MacroPad()

Expand Down
21 changes: 21 additions & 0 deletions examples/macropad_simpletest_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
Simpletest demo for MacroPad. Displays the key pressed, the relative position of the rotary
encoder, and the state of the rotary encoder switch to the built-in display. Note that the key
pressed line does not appear until a key is pressed.
"""
from adafruit_macropad import MacroPad

macropad = MacroPad()

text_lines = macropad.display_text(title="MacroPad Info")

while True:
key_event = macropad.keys.events.get()
if key_event and key_event.pressed:
text_lines[0].text = "Key {} pressed!".format(key_event.key_number)
text_lines[1].text = "Rotary encoder {}".format(macropad.encoder)
text_lines[2].text = "Encoder switch: {}".format(macropad.encoder_switch)
text_lines.show()
27 changes: 27 additions & 0 deletions examples/macropad_tone_keypad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
MacroPad tone demo. Plays a different tone for each key pressed and lights up each key a different
color while the key is pressed.
"""
from rainbowio import colorwheel
from adafruit_macropad import MacroPad

macropad = MacroPad()

tones = [196, 220, 246, 262, 294, 330, 349, 392, 440, 494, 523, 587]

while True:
key_event = macropad.keys.events.get()

if key_event:
if key_event.pressed:
macropad.pixels[key_event.key_number] = colorwheel(
int(255 / 12) * key_event.key_number
)
macropad.start_tone(tones[key_event.key_number])

else:
macropad.pixels.fill((0, 0, 0))
macropad.stop_tone()