From f5b3367c2bc8e9e6094107933c8b1eaa497b1e0c Mon Sep 17 00:00:00 2001 From: Cooper Dalrymple Date: Tue, 12 Aug 2025 13:50:01 -0500 Subject: [PATCH 1/4] Automatically configure display size based on `CIRCUITPY_DISPLAY_WIDTH` --- adafruit_fruitjam/peripherals.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index 1c2c5ad..df55f69 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -55,7 +55,7 @@ } -def request_display_config(width, height, color_depth=None): +def request_display_config(width=None, height=None, color_depth=None): """ Request a display size configuration. If the display is un-initialized, or is currently using a different configuration it will be initialized @@ -72,8 +72,22 @@ def request_display_config(width, height, color_depth=None): is 16. :return: None """ - if (width, height) not in VALID_DISPLAY_SIZES: + # if user does not specify width, use default configuration + if width is None and (width := os.getenv("CIRCUITPY_DISPLAY_WIDTH")) is None: + raise ValueError("No CIRCUITPY_DISPLAY_WIDTH specified in settings.toml.") + + # check that we have a valid display size + if ( + (height is not None and (width, height) not in VALID_DISPLAY_SIZES) or + (height is None and width not in [size[0] for size in VALID_DISPLAY_SIZES]) + ): raise ValueError(f"Invalid display size. Must be one of: {VALID_DISPLAY_SIZES}") + + # if user does not specify height, use matching height + if height is None: + for size in VALID_DISPLAY_SIZES: + if width == size[0]: + height = size[1] # if user does not specify a requested color_depth if color_depth is None: @@ -202,7 +216,7 @@ def any_button_pressed(self) -> bool: """ Return whether any button is pressed """ - return True in [button.value for (i, button) in enumerate(self._buttons)] + return True in [not button.value for (i, button) in enumerate(self._buttons)] @property def dac(self): From bfc6140e6d0c302874618b049a56828d696f5cc1 Mon Sep 17 00:00:00 2001 From: Cooper Dalrymple Date: Tue, 12 Aug 2025 13:59:59 -0500 Subject: [PATCH 2/4] Optimize display height assignment --- adafruit_fruitjam/peripherals.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index df55f69..f49e65b 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -85,9 +85,7 @@ def request_display_config(width=None, height=None, color_depth=None): # if user does not specify height, use matching height if height is None: - for size in VALID_DISPLAY_SIZES: - if width == size[0]: - height = size[1] + height = next((h for w, h in VALID_DISPLAY_SIZES if width == w)) # if user does not specify a requested color_depth if color_depth is None: From 3cd3c6e596b41b48a3daacb3f625156525ddebf1 Mon Sep 17 00:00:00 2001 From: Cooper Dalrymple Date: Tue, 12 Aug 2025 14:08:31 -0500 Subject: [PATCH 3/4] Fix formatting --- adafruit_fruitjam/peripherals.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index f49e65b..8c77735 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -75,14 +75,13 @@ def request_display_config(width=None, height=None, color_depth=None): # if user does not specify width, use default configuration if width is None and (width := os.getenv("CIRCUITPY_DISPLAY_WIDTH")) is None: raise ValueError("No CIRCUITPY_DISPLAY_WIDTH specified in settings.toml.") - + # check that we have a valid display size - if ( - (height is not None and (width, height) not in VALID_DISPLAY_SIZES) or - (height is None and width not in [size[0] for size in VALID_DISPLAY_SIZES]) + if (height is not None and (width, height) not in VALID_DISPLAY_SIZES) or ( + height is None and width not in [size[0] for size in VALID_DISPLAY_SIZES] ): raise ValueError(f"Invalid display size. Must be one of: {VALID_DISPLAY_SIZES}") - + # if user does not specify height, use matching height if height is None: height = next((h for w, h in VALID_DISPLAY_SIZES if width == w)) From 6224c6510d7d9ec631ce822ccdc5e741adaf4ff1 Mon Sep 17 00:00:00 2001 From: Cooper Dalrymple Date: Tue, 12 Aug 2025 14:44:18 -0500 Subject: [PATCH 4/4] Update `request_display_config` docstring --- adafruit_fruitjam/peripherals.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/adafruit_fruitjam/peripherals.py b/adafruit_fruitjam/peripherals.py index 8c77735..461bbcf 100644 --- a/adafruit_fruitjam/peripherals.py +++ b/adafruit_fruitjam/peripherals.py @@ -63,8 +63,11 @@ def request_display_config(width=None, height=None, color_depth=None): This function will set the initialized display to ``supervisor.runtime.display`` - :param width: The width of the display in pixels. - :param height: The height of the display in pixels. + :param width: The width of the display in pixels. Leave unspecified to default + to the ``CIRCUITPY_DISPLAY_WIDTH`` environmental variable if provided. Otherwise, + a ``ValueError`` exception will be thrown. + :param height: The height of the display in pixels. Leave unspecified to default + to the appropriate height for the provided width. :param color_depth: The color depth of the display in bits. Valid values are 1, 2, 4, 8, 16, 32. Larger resolutions must use smaller color_depths due to RAM limitations. Default color_depth for