Skip to content

Commit 9175c44

Browse files
authored
Merge pull request #9 from relic-se/request_display_config-default
Automatically configure display size based on `CIRCUITPY_DISPLAY_WIDTH`
2 parents 71dbc2a + 6224c65 commit 9175c44

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

adafruit_fruitjam/peripherals.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,40 @@
5555
}
5656

5757

58-
def request_display_config(width, height, color_depth=None):
58+
def request_display_config(width=None, height=None, color_depth=None):
5959
"""
6060
Request a display size configuration. If the display is un-initialized,
6161
or is currently using a different configuration it will be initialized
6262
to the requested width and height.
6363
6464
This function will set the initialized display to ``supervisor.runtime.display``
6565
66-
:param width: The width of the display in pixels.
67-
:param height: The height of the display in pixels.
66+
:param width: The width of the display in pixels. Leave unspecified to default
67+
to the ``CIRCUITPY_DISPLAY_WIDTH`` environmental variable if provided. Otherwise,
68+
a ``ValueError`` exception will be thrown.
69+
:param height: The height of the display in pixels. Leave unspecified to default
70+
to the appropriate height for the provided width.
6871
:param color_depth: The color depth of the display in bits.
6972
Valid values are 1, 2, 4, 8, 16, 32. Larger resolutions must use
7073
smaller color_depths due to RAM limitations. Default color_depth for
7174
720 and 640 width is 8, and default color_depth for 320 and 360 width
7275
is 16.
7376
:return: None
7477
"""
75-
if (width, height) not in VALID_DISPLAY_SIZES:
78+
# if user does not specify width, use default configuration
79+
if width is None and (width := os.getenv("CIRCUITPY_DISPLAY_WIDTH")) is None:
80+
raise ValueError("No CIRCUITPY_DISPLAY_WIDTH specified in settings.toml.")
81+
82+
# check that we have a valid display size
83+
if (height is not None and (width, height) not in VALID_DISPLAY_SIZES) or (
84+
height is None and width not in [size[0] for size in VALID_DISPLAY_SIZES]
85+
):
7686
raise ValueError(f"Invalid display size. Must be one of: {VALID_DISPLAY_SIZES}")
7787

88+
# if user does not specify height, use matching height
89+
if height is None:
90+
height = next((h for w, h in VALID_DISPLAY_SIZES if width == w))
91+
7892
# if user does not specify a requested color_depth
7993
if color_depth is None:
8094
# use the maximum color depth for given width

0 commit comments

Comments
 (0)