Skip to content

Commit e48157f

Browse files
committed
add arguments to setup layout on init
1 parent f8a889b commit e48157f

File tree

2 files changed

+104
-36
lines changed

2 files changed

+104
-36
lines changed

adafruit_macropad.py

+59-36
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
import audiomp3
6161
import usb_hid
6262
from adafruit_hid.keyboard import Keyboard
63-
from adafruit_hid.keycode import Keycode
63+
from adafruit_hid.keyboard_layout_base import KeyboardLayoutBase
6464
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
65+
from adafruit_hid.keycode import Keycode
6566
from adafruit_hid.consumer_control import ConsumerControl
6667
from adafruit_hid.consumer_control_code import ConsumerControlCode
6768
from adafruit_hid.mouse import Mouse
@@ -80,7 +81,7 @@
8081
from typing import Tuple, Optional, Union, Iterator
8182
from neopixel import NeoPixel
8283
from keypad import Keys
83-
import adafruit_hid
84+
import adafruit_hid # pylint:disable=ungrouped-imports
8485
except ImportError:
8586
pass
8687

@@ -93,6 +94,10 @@
9394
ROTATED_KEYMAP_270 = (9, 6, 3, 0, 10, 7, 4, 1, 11, 8, 5, 2)
9495

9596

97+
keycodes = Keycode
98+
"""Module level Keycode class, to be changed when initing Macropad with a different language"""
99+
100+
96101
class _PixelMapLite:
97102
"""Generate a pixel map based on a specified order. Designed to work with a set of 12 pixels,
98103
e.g. the MacroPad keypad LEDs.
@@ -163,7 +168,7 @@ def brightness(self, value: float) -> None:
163168
self._pixels.brightness = value
164169

165170

166-
# pylint: disable=too-many-lines
171+
# pylint: disable=too-many-lines, disable=invalid-name, too-many-instance-attributes, too-many-public-methods, too-many-arguments
167172
class MacroPad:
168173
"""
169174
Class representing a single MacroPad.
@@ -182,6 +187,14 @@ class MacroPad:
182187
channels. Defaults to 1.
183188
:param int midi_out_channel: The MIDI output channel. Defaults to 1.
184189
190+
:param type[KeyboardLayoutBase] layout_class: Class for the keyboard layout, to setup an
191+
international or alternative keyboard. Defaults
192+
to KeyboardLayoutUS from adafruit_hid.
193+
:param type[Keycode] keycode_class: Class used for the keycode names provided by
194+
adafruit_macropad.Keycode. Defaults to the standard Keycode
195+
from adafruit_hid.
196+
197+
185198
The following shows how to initialise the MacroPad library with the board rotated 90 degrees,
186199
and the MIDI channels both set to 1.
187200
@@ -192,9 +205,43 @@ class MacroPad:
192205
macropad = MacroPad(rotation=90, midi_in_channel=1, midi_out_channel=1)
193206
"""
194207

195-
# pylint: disable=invalid-name, too-many-instance-attributes, too-many-public-methods
208+
Keycode = Keycode
209+
"""
210+
The contents of the Keycode module are available as a property of MacroPad. This includes all
211+
keycode constants available within the Keycode module, which includes all the keys on a
212+
regular PC or Mac keyboard.
213+
214+
Remember that keycodes are the names for key _positions_ on a US keyboard, and may not
215+
correspond to the character that you mean to send if you want to emulate non-US keyboard.
216+
217+
For usage example, see the ``keyboard`` documentation in this library.
218+
"""
219+
220+
ConsumerControlCode = ConsumerControlCode
221+
"""
222+
The contents of the ConsumerControlCode module are available as a property of MacroPad.
223+
This includes the available USB HID Consumer Control Device constants. This list is not
224+
exhaustive.
225+
226+
For usage example, see the ``consumer_control`` documentation in this library.
227+
"""
228+
229+
Mouse = Mouse
230+
"""
231+
The contents of the Mouse module are available as a property of MacroPad. This includes the
232+
``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON`` constants. The rest of the
233+
functionality of the ``Mouse`` module should be used through ``macropad.mouse``.
234+
235+
For usage example, see the ``mouse`` documentation in this library.
236+
"""
237+
196238
def __init__(
197-
self, rotation: int = 0, midi_in_channel: int = 1, midi_out_channel: int = 1
239+
self,
240+
rotation: int = 0,
241+
midi_in_channel: int = 1,
242+
midi_out_channel: int = 1,
243+
layout_class: type[KeyboardLayoutBase] = KeyboardLayoutUS,
244+
keycode_class: type[Keycode] = Keycode,
198245
):
199246

200247
if rotation not in (0, 90, 180, 270):
@@ -257,6 +304,12 @@ def _keys_and_pixels(
257304
self._keyboard_layout = None
258305
self._consumer_control = None
259306
self._mouse = None
307+
self._layout_class = layout_class
308+
self.Keycode = keycode_class
309+
# pylint:disable=global-statement
310+
global keycodes
311+
keycodes = keycode_class
312+
# pylint:enable=global-statement
260313

261314
# Define MIDI:
262315
try:
@@ -271,36 +324,6 @@ def _keys_and_pixels(
271324
# No MIDI ports available.
272325
self._midi = None
273326

274-
Keycode = Keycode
275-
"""
276-
The contents of the Keycode module are available as a property of MacroPad. This includes all
277-
keycode constants available within the Keycode module, which includes all the keys on a
278-
regular PC or Mac keyboard.
279-
280-
Remember that keycodes are the names for key _positions_ on a US keyboard, and may not
281-
correspond to the character that you mean to send if you want to emulate non-US keyboard.
282-
283-
For usage example, see the ``keyboard`` documentation in this library.
284-
"""
285-
286-
ConsumerControlCode = ConsumerControlCode
287-
"""
288-
The contents of the ConsumerControlCode module are available as a property of MacroPad.
289-
This includes the available USB HID Consumer Control Device constants. This list is not
290-
exhaustive.
291-
292-
For usage example, see the ``consumer_control`` documentation in this library.
293-
"""
294-
295-
Mouse = Mouse
296-
"""
297-
The contents of the Mouse module are available as a property of MacroPad. This includes the
298-
``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON`` constants. The rest of the
299-
functionality of the ``Mouse`` module should be used through ``macropad.mouse``.
300-
301-
For usage example, see the ``mouse`` documentation in this library.
302-
"""
303-
304327
@property
305328
def pixels(self) -> Optional[_PixelMapLite]:
306329
"""Sequence-like object representing the twelve NeoPixel LEDs in a 3 x 4 grid on the
@@ -501,7 +524,7 @@ def keyboard_layout(self) -> adafruit_hid.keyboard_layout_base.KeyboardLayoutBas
501524
"""
502525
if self._keyboard_layout is None:
503526
# This will need to be updated if we add more layouts. Currently there is only US.
504-
self._keyboard_layout = KeyboardLayoutUS(self.keyboard)
527+
self._keyboard_layout = self._layout_class(self.keyboard)
505528
return self._keyboard_layout
506529

507530
@property

examples/macropad_keyboard_layout.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
International layout demo for MacroPad.
6+
"""
7+
import time
8+
from keyboard_layout_win_fr import KeyboardLayout
9+
from keycode_win_fr import Keycode
10+
from adafruit_macropad import MacroPad
11+
12+
macropad = MacroPad(
13+
layout_class=KeyboardLayout,
14+
keycode_class=Keycode,
15+
)
16+
17+
keycodes = [
18+
"https://adafruit.com/",
19+
"https://adafru.it/discord",
20+
"https://circuitpython.org",
21+
Keycode.A,
22+
Keycode.D,
23+
Keycode.A,
24+
Keycode.F,
25+
Keycode.R,
26+
Keycode.U,
27+
Keycode.I,
28+
Keycode.T,
29+
Keycode.PERIOD,
30+
# Keycode.C, Keycode.O, Keycode.M,
31+
]
32+
33+
while True:
34+
key_event = macropad.keys.events.get()
35+
if key_event:
36+
keycode = keycodes[key_event.key_number]
37+
if key_event.pressed:
38+
if isinstance(keycode, int):
39+
macropad.keyboard.press(keycode)
40+
else:
41+
macropad.keyboard_layout.write(keycode)
42+
else:
43+
if isinstance(keycode, int):
44+
macropad.keyboard.release(keycode)
45+
time.sleep(0.05)

0 commit comments

Comments
 (0)