diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 33c2a61..88bca9f 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -8,6 +8,9 @@ # Required version: 2 +sphinx: + configuration: docs/conf.py + build: os: ubuntu-20.04 tools: diff --git a/adafruit_macropad.py b/adafruit_macropad.py old mode 100755 new mode 100644 index e56153c..91954d5 --- 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,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 @@ -899,9 +931,9 @@ def display_text( Defaults to 80. :param int text_scale: Scale the size of the data lines. Scales the title as well. Defaults to 1. - :param font: The font or the path to the custom font file to use to display the text. - Defaults to the built-in ``terminalio.FONT``. Custom font files must be - provided as a string, e.g. ``"/Arial12.bdf"``. + :param ~FontProtocol|None font: The custom font to use to display the text. Defaults to the + built-in ``terminalio.FONT``. For more details, see: + https://docs.circuitpython.org/en/latest/shared-bindings/fontio/index.html The following example displays a title and lines of text indicating which key is pressed, the relative position of the rotary encoder, and whether the encoder switch is pressed. @@ -909,11 +941,14 @@ def display_text( .. code-block:: python + from adafruit_bitmap_font import bitmap_font from adafruit_macropad import MacroPad + from displayio import Bitmap macropad = MacroPad() - text_lines = macropad.display_text(title="MacroPad Info") + custom_font = bitmap_font.load_font("/Arial12.bdf", Bitmap) + text_lines = macropad.display_text(title="MacroPad Info", font=custom_font) while True: key_event = macropad.keys.events.get() diff --git a/docs/conf.py b/docs/conf.py index 4e66766..20488a9 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -129,7 +129,6 @@ import sphinx_rtd_theme html_theme = "sphinx_rtd_theme" -html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, diff --git a/docs/requirements.txt b/docs/requirements.txt index 4f3e4c2..e6ff49f 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -3,5 +3,6 @@ # # SPDX-License-Identifier: MIT -sphinx>=4.0.0 +sphinx sphinxcontrib-jquery +sphinx-rtd-theme