From 1b414331470475efb1a3a1d11c72f59321f98e00 Mon Sep 17 00:00:00 2001 From: Richard Terry Date: Sat, 10 Feb 2024 07:03:49 +0000 Subject: [PATCH 1/3] Add rotate() to change rotation after init --- adafruit_macropad.py | 94 ++++++++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/adafruit_macropad.py b/adafruit_macropad.py index 6b355a1..c981432 100755 --- a/adafruit_macropad.py +++ b/adafruit_macropad.py @@ -246,43 +246,11 @@ def __init__( layout_class: type[KeyboardLayoutBase] = KeyboardLayoutUS, keycode_class: type[Keycode] = Keycode, ): - if rotation not in (0, 90, 180, 270): - raise ValueError("Only 90 degree rotations are supported.") - # Define LEDs: self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 12) self._led = digitalio.DigitalInOut(board.LED) self._led.switch_to_output() - # Define key and pixel maps based on rotation: - self._rotated_pixels = None - self._key_pins = None - - def _keys_and_pixels( - order: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] - ) -> None: - """ - Generate key and pixel maps based on a specified order. - :param order: Tuple containing the order of the keys and pixels. - """ - self._key_pins = [getattr(board, "KEY%d" % (num + 1)) for num in order] - self._rotated_pixels = _PixelMapLite(self._pixels, order=order) - - if rotation == 0: - _keys_and_pixels(order=ROTATED_KEYMAP_0) - - if rotation == 90: - _keys_and_pixels(order=ROTATED_KEYMAP_90) - - if rotation == 180: - _keys_and_pixels(order=ROTATED_KEYMAP_180) - - if rotation == 270: - _keys_and_pixels(order=ROTATED_KEYMAP_270) - - # Define keys: - self._keys = keypad.Keys(self._key_pins, value_when_pressed=False, pull=True) - # Define rotary encoder and encoder switch: self._encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB) self._encoder_switch = digitalio.DigitalInOut(board.BUTTON) @@ -292,10 +260,15 @@ def _keys_and_pixels( # Define display: if not isinstance(board.DISPLAY, type(None)): self.display = board.DISPLAY - self.display.rotation = rotation self.display.bus.send(_DISPLAY_WAKE_COMMAND, b"") self._display_sleep = False + # Define key and pixel maps based on rotation: + self._rotated_pixels = None + self._key_pins = None + self._keys = None + self.rotate(rotation) + # Define audio: self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) self._speaker_enable.switch_to_output(value=False) @@ -328,6 +301,61 @@ def _keys_and_pixels( # No MIDI ports available. self._midi = None + def rotate(self, rotation): + """ + Set the display rotation + + :param int rotation: The rotational position of the MacroPad. Allows for rotating the MacroPad + in 90 degree increments to four different positions and rotates the keypad + layout and display orientation to match. Keypad layout is always left to + right, top to bottom, beginning with key number 0 in the top left, and + ending with key number 11 in the bottom right. Supports ``0``, ``90``, + ``180``, and ``270`` degree rotations. ``0`` is when the USB port is at + the top, ``90`` is when the USB port is to the left, ``180`` is when the + USB port is at the bottom, and ``270`` is when the USB port is to the + right. Defaults to ``0``. + """ + if rotation not in (0, 90, 180, 270): + raise ValueError("Only 90 degree rotations are supported.") + + self._rotation = rotation + + def _keys_and_pixels( + order: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] + ) -> None: + """ + Generate key and pixel maps based on a specified order. + :param order: Tuple containing the order of the keys and pixels. + """ + self._key_pins = [getattr(board, "KEY%d" % (num + 1)) for num in order] + self._rotated_pixels = _PixelMapLite(self._pixels, order=order) + + if rotation == 0: + _keys_and_pixels(order=ROTATED_KEYMAP_0) + + if rotation == 90: + _keys_and_pixels(order=ROTATED_KEYMAP_90) + + if rotation == 180: + _keys_and_pixels(order=ROTATED_KEYMAP_180) + + if rotation == 270: + _keys_and_pixels(order=ROTATED_KEYMAP_270) + + # Define keys: + if self._keys is not None: + self._keys.deinit() + self._keys = keypad.Keys(self._key_pins, value_when_pressed=False, pull=True) + + self.display.rotation = rotation + + @property + def rotation(self) -> int: + """ + The current rotation + """ + return self._rotation + @property def display_sleep(self) -> bool: """The power saver mode of the display. Set it to put the display to From 43c0575863d11a8fa3776c21c7e10fb511fdc480 Mon Sep 17 00:00:00 2001 From: Richard Terry Date: Sat, 10 Feb 2024 16:23:00 +0000 Subject: [PATCH 2/3] Fix line length --- adafruit_macropad.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/adafruit_macropad.py b/adafruit_macropad.py index c981432..394aed3 100755 --- a/adafruit_macropad.py +++ b/adafruit_macropad.py @@ -305,15 +305,15 @@ def rotate(self, rotation): """ Set the display rotation - :param int rotation: The rotational position of the MacroPad. Allows for rotating the MacroPad - in 90 degree increments to four different positions and rotates the keypad - layout and display orientation to match. Keypad layout is always left to - right, top to bottom, beginning with key number 0 in the top left, and - ending with key number 11 in the bottom right. Supports ``0``, ``90``, - ``180``, and ``270`` degree rotations. ``0`` is when the USB port is at - the top, ``90`` is when the USB port is to the left, ``180`` is when the - USB port is at the bottom, and ``270`` is when the USB port is to the - right. Defaults to ``0``. + :param int rotation: The rotational position of the MacroPad. Allows for rotating the + MacroPad in 90 degree increments to four different positions and + rotates the keypad layout and display orientation to match. Keypad + layout is always left to right, top to bottom, beginning with key + number 0 in the top left, and ending with key number 11 in the bottom + right. Supports ``0``, ``90``, ``180``, and ``270`` degree rotations. + ``0`` is when the USB port is at the top, ``90`` is when the USB port + is to the left, ``180`` is when the USB port is at the bottom, and + ``270`` is when the USB port is to the right. Defaults to ``0``. """ if rotation not in (0, 90, 180, 270): raise ValueError("Only 90 degree rotations are supported.") From 6fb54713abe8f8a4773ecc9fc2cacd46d7618fcb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 19 Feb 2024 19:20:32 -0600 Subject: [PATCH 3/3] add rotation setter --- adafruit_macropad.py | 4 ++++ 1 file changed, 4 insertions(+) mode change 100755 => 100644 adafruit_macropad.py diff --git a/adafruit_macropad.py b/adafruit_macropad.py old mode 100755 new mode 100644 index 394aed3..91954d5 --- a/adafruit_macropad.py +++ b/adafruit_macropad.py @@ -356,6 +356,10 @@ def rotation(self) -> int: """ return self._rotation + @rotation.setter + def rotation(self, new_rotation) -> None: + self.rotate(new_rotation) + @property def display_sleep(self) -> bool: """The power saver mode of the display. Set it to put the display to