From 54d00556e0e1ed27a507d1d0cb85ba66731d6281 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 28 Jul 2021 11:18:37 -0400 Subject: [PATCH 1/3] Adding demos. --- adafruit_macropad.py | 12 +++++------ examples/blinka.bmp | Bin 0 -> 1088 bytes examples/blinka.bmp.license | 3 +++ examples/macropad_display_image.py | 12 +++++++++++ examples/macropad_rainbow_keys.py | 2 +- examples/macropad_simpletest_display.py | 21 ++++++++++++++++++ examples/macropad_tone_keypad.py | 27 ++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 examples/blinka.bmp create mode 100644 examples/blinka.bmp.license create mode 100644 examples/macropad_display_image.py create mode 100644 examples/macropad_simpletest_display.py create mode 100644 examples/macropad_tone_keypad.py diff --git a/adafruit_macropad.py b/adafruit_macropad.py index 65f5e30..961f939 100755 --- a/adafruit_macropad.py +++ b/adafruit_macropad.py @@ -292,9 +292,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 @@ -748,9 +748,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() diff --git a/examples/blinka.bmp b/examples/blinka.bmp new file mode 100644 index 0000000000000000000000000000000000000000..996182062f4ff1d1df2cded4a5f04d5e3c9a6d36 GIT binary patch literal 1088 zcmZ9L!A=4(5QY~tSrTI~o;Zo#Jn{g>5-*;N592{ES`-t!@d!Q&=s`?0`T!;f!MGO_ z*I=Y%OMi!jZae#LrtR06>Fl=MgQNOtkZPaWU9tvQovc95Tt)J4t}QH`pawuXq@HBL zMQ__Ukan2%-D-o)G-S8vrNjb_t3sC1>zN{t_ z8PaWT8Ytc76Hg%M0Gx(7p5|CXxWlA0tt4a9^)3tf>38r>gW?0pcnayA9x(2Bmy7ZT yf^*cFUvQ_$1Z=(dVx6(tdSma0hg0^w7`6HS{NUEDP9tRmW5fPlay6|x2>t=zgQVL4 literal 0 HcmV?d00001 diff --git a/examples/blinka.bmp.license b/examples/blinka.bmp.license new file mode 100644 index 0000000..224289f --- /dev/null +++ b/examples/blinka.bmp.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense diff --git a/examples/macropad_display_image.py b/examples/macropad_display_image.py new file mode 100644 index 0000000..8d70cf2 --- /dev/null +++ b/examples/macropad_display_image.py @@ -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") diff --git a/examples/macropad_rainbow_keys.py b/examples/macropad_rainbow_keys.py index 1015ede..e3e2a1b 100644 --- a/examples/macropad_rainbow_keys.py +++ b/examples/macropad_rainbow_keys.py @@ -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() diff --git a/examples/macropad_simpletest_display.py b/examples/macropad_simpletest_display.py new file mode 100644 index 0000000..65cd83a --- /dev/null +++ b/examples/macropad_simpletest_display.py @@ -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() diff --git a/examples/macropad_tone_keypad.py b/examples/macropad_tone_keypad.py new file mode 100644 index 0000000..2c25205 --- /dev/null +++ b/examples/macropad_tone_keypad.py @@ -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() From c48a33159fc1abdb78f485b75f5b77831672f8e0 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 28 Jul 2021 11:41:11 -0400 Subject: [PATCH 2/3] Create objects when needed. --- adafruit_macropad.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/adafruit_macropad.py b/adafruit_macropad.py index 961f939..677b65d 100755 --- a/adafruit_macropad.py +++ b/adafruit_macropad.py @@ -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( @@ -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 @@ -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 @@ -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 @@ -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 From 4de30a0a2f3174076bd00c980ba270b5e0ddf4d7 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 28 Jul 2021 13:00:19 -0400 Subject: [PATCH 3/3] Add keyboard mouse demo. --- examples/macropad_keyboard_mouse.py | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 examples/macropad_keyboard_mouse.py diff --git a/examples/macropad_keyboard_mouse.py b/examples/macropad_keyboard_mouse.py new file mode 100755 index 0000000..d9f16ec --- /dev/null +++ b/examples/macropad_keyboard_mouse.py @@ -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