Skip to content

Commit 924ad9d

Browse files
authored
Merge pull request #54 from radiac/feature/change_rotation
Add rotate() to change rotation after init
2 parents f3aa119 + 6fb5471 commit 924ad9d

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

adafruit_macropad.py

100755100644
Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -246,43 +246,11 @@ def __init__(
246246
layout_class: type[KeyboardLayoutBase] = KeyboardLayoutUS,
247247
keycode_class: type[Keycode] = Keycode,
248248
):
249-
if rotation not in (0, 90, 180, 270):
250-
raise ValueError("Only 90 degree rotations are supported.")
251-
252249
# Define LEDs:
253250
self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 12)
254251
self._led = digitalio.DigitalInOut(board.LED)
255252
self._led.switch_to_output()
256253

257-
# Define key and pixel maps based on rotation:
258-
self._rotated_pixels = None
259-
self._key_pins = None
260-
261-
def _keys_and_pixels(
262-
order: Tuple[int, int, int, int, int, int, int, int, int, int, int, int]
263-
) -> None:
264-
"""
265-
Generate key and pixel maps based on a specified order.
266-
:param order: Tuple containing the order of the keys and pixels.
267-
"""
268-
self._key_pins = [getattr(board, "KEY%d" % (num + 1)) for num in order]
269-
self._rotated_pixels = _PixelMapLite(self._pixels, order=order)
270-
271-
if rotation == 0:
272-
_keys_and_pixels(order=ROTATED_KEYMAP_0)
273-
274-
if rotation == 90:
275-
_keys_and_pixels(order=ROTATED_KEYMAP_90)
276-
277-
if rotation == 180:
278-
_keys_and_pixels(order=ROTATED_KEYMAP_180)
279-
280-
if rotation == 270:
281-
_keys_and_pixels(order=ROTATED_KEYMAP_270)
282-
283-
# Define keys:
284-
self._keys = keypad.Keys(self._key_pins, value_when_pressed=False, pull=True)
285-
286254
# Define rotary encoder and encoder switch:
287255
self._encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
288256
self._encoder_switch = digitalio.DigitalInOut(board.BUTTON)
@@ -292,10 +260,15 @@ def _keys_and_pixels(
292260
# Define display:
293261
if not isinstance(board.DISPLAY, type(None)):
294262
self.display = board.DISPLAY
295-
self.display.rotation = rotation
296263
self.display.bus.send(_DISPLAY_WAKE_COMMAND, b"")
297264
self._display_sleep = False
298265

266+
# Define key and pixel maps based on rotation:
267+
self._rotated_pixels = None
268+
self._key_pins = None
269+
self._keys = None
270+
self.rotate(rotation)
271+
299272
# Define audio:
300273
self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
301274
self._speaker_enable.switch_to_output(value=False)
@@ -328,6 +301,65 @@ def _keys_and_pixels(
328301
# No MIDI ports available.
329302
self._midi = None
330303

304+
def rotate(self, rotation):
305+
"""
306+
Set the display rotation
307+
308+
:param int rotation: The rotational position of the MacroPad. Allows for rotating the
309+
MacroPad in 90 degree increments to four different positions and
310+
rotates the keypad layout and display orientation to match. Keypad
311+
layout is always left to right, top to bottom, beginning with key
312+
number 0 in the top left, and ending with key number 11 in the bottom
313+
right. Supports ``0``, ``90``, ``180``, and ``270`` degree rotations.
314+
``0`` is when the USB port is at the top, ``90`` is when the USB port
315+
is to the left, ``180`` is when the USB port is at the bottom, and
316+
``270`` is when the USB port is to the right. Defaults to ``0``.
317+
"""
318+
if rotation not in (0, 90, 180, 270):
319+
raise ValueError("Only 90 degree rotations are supported.")
320+
321+
self._rotation = rotation
322+
323+
def _keys_and_pixels(
324+
order: Tuple[int, int, int, int, int, int, int, int, int, int, int, int]
325+
) -> None:
326+
"""
327+
Generate key and pixel maps based on a specified order.
328+
:param order: Tuple containing the order of the keys and pixels.
329+
"""
330+
self._key_pins = [getattr(board, "KEY%d" % (num + 1)) for num in order]
331+
self._rotated_pixels = _PixelMapLite(self._pixels, order=order)
332+
333+
if rotation == 0:
334+
_keys_and_pixels(order=ROTATED_KEYMAP_0)
335+
336+
if rotation == 90:
337+
_keys_and_pixels(order=ROTATED_KEYMAP_90)
338+
339+
if rotation == 180:
340+
_keys_and_pixels(order=ROTATED_KEYMAP_180)
341+
342+
if rotation == 270:
343+
_keys_and_pixels(order=ROTATED_KEYMAP_270)
344+
345+
# Define keys:
346+
if self._keys is not None:
347+
self._keys.deinit()
348+
self._keys = keypad.Keys(self._key_pins, value_when_pressed=False, pull=True)
349+
350+
self.display.rotation = rotation
351+
352+
@property
353+
def rotation(self) -> int:
354+
"""
355+
The current rotation
356+
"""
357+
return self._rotation
358+
359+
@rotation.setter
360+
def rotation(self, new_rotation) -> None:
361+
self.rotate(new_rotation)
362+
331363
@property
332364
def display_sleep(self) -> bool:
333365
"""The power saver mode of the display. Set it to put the display to

0 commit comments

Comments
 (0)