Skip to content

Commit

Permalink
Merge pull request #54 from radiac/feature/change_rotation
Browse files Browse the repository at this point in the history
Add rotate() to change rotation after init
  • Loading branch information
FoamyGuy authored Feb 20, 2024
2 parents f3aa119 + 6fb5471 commit 924ad9d
Showing 1 changed file with 65 additions and 33 deletions.
98 changes: 65 additions & 33 deletions adafruit_macropad.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -328,6 +301,65 @@ 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

@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
Expand Down

0 comments on commit 924ad9d

Please sign in to comment.