Skip to content

Commit a69d6c8

Browse files
authored
Merge pull request #44 from relic-se/aspect-ratio
Support 4:3 aspect ratio with `FRUIT_JAM_OS_4x3` environment variable
2 parents 2de4173 + 4cd27e7 commit a69d6c8

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/code.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import adafruit_imageload
2525
import adafruit_usb_host_descriptors
2626
from adafruit_anchored_group import AnchoredGroup
27-
from adafruit_fruitjam.peripherals import request_display_config
27+
from adafruit_fruitjam.peripherals import request_display_config, VALID_DISPLAY_SIZES
2828
from adafruit_argv_file import read_argv, write_argv
2929

3030
"""
@@ -58,10 +58,18 @@
5858
"bg": os.getenv("FRUIT_JAM_OS_BG", 0x222222),
5959
"fg": os.getenv("FRUIT_JAM_OS_FG", 0xffffff),
6060
"accent": os.getenv("FRUIT_JAM_OS_ACCENT", 0x008800),
61-
"arrow": os.getenv("FRUIT_JAM_OS_ARROW", -1),
61+
"arrow": os.getenv("FRUIT_JAM_OS_ARROW"),
6262
}
6363

64-
request_display_config(720, 400)
64+
if (width_config := os.getenv("CIRCUITPY_DISPLAY_WIDTH")) is not None:
65+
if width_config not in [x[0] for x in VALID_DISPLAY_SIZES]:
66+
raise ValueError(f"Invalid display size. Must be one of: {VALID_DISPLAY_SIZES}")
67+
for display_size in VALID_DISPLAY_SIZES:
68+
if display_size[0] == width_config:
69+
break
70+
else:
71+
display_size = (720, 400)
72+
request_display_config(*display_size)
6573
display = supervisor.runtime.display
6674

6775
scale = 1
@@ -144,8 +152,8 @@
144152

145153
mouse_buf = array.array("b", [0] * 8)
146154

147-
WIDTH = 280
148-
HEIGHT = 182
155+
WIDTH = int(280 / 360 * display.width // scale)
156+
HEIGHT = int(182 / 200 * display.height // scale)
149157

150158
config = {
151159
"menu_title": "Launcher Menu",
@@ -186,10 +194,13 @@
186194
}
187195

188196
cell_width = WIDTH // config["width"]
197+
cell_height = HEIGHT // config["height"]
189198

190199
default_icon_bmp, default_icon_palette = adafruit_imageload.load("launcher_assets/default_icon.bmp")
191200
default_icon_palette.make_transparent(0)
192-
menu_grid = GridLayout(x=40, y=16, width=WIDTH, height=HEIGHT, grid_size=(config["width"], config["height"]),
201+
menu_grid = GridLayout(x=(display.width // scale - WIDTH) // 2,
202+
y=(display.height // scale - HEIGHT) // 2,
203+
width=WIDTH, height=HEIGHT, grid_size=(config["width"], config["height"]),
193204
divider_lines=False)
194205
scaled_group.append(menu_grid)
195206

@@ -276,8 +287,9 @@ def _create_cell_group(app):
276287
cell_group.append(icon_tg)
277288

278289
icon_tg.x = cell_width // 2 - icon_tg.tile_width // 2
279-
title_txt = TextBox(font, text=app["title"], width=WIDTH // config["width"], height=18,
290+
title_txt = TextBox(font, text=app["title"], width=cell_width, height=18,
280291
align=TextBox.ALIGN_CENTER, color=color_palette["fg"])
292+
icon_tg.y = (cell_height - icon_tg.tile_height - title_txt.height) // 2
281293
cell_group.append(title_txt)
282294
title_txt.anchor_point = (0, 0)
283295
title_txt.anchored_position = (0, icon_tg.y + icon_tg.tile_height)
@@ -297,7 +309,7 @@ def _reuse_cell_group(app, cell_group):
297309
icon_tg.pixel_shader = icon_palette
298310

299311
icon_tg.x = cell_width // 2 - icon_tg.tile_width // 2
300-
# title_txt = TextBox(font, text=app["title"], width=WIDTH // config["width"], height=18,
312+
# title_txt = TextBox(font, text=app["title"], width=cell_width, height=18,
301313
# align=TextBox.ALIGN_CENTER, color=color_palette["fg"])
302314
# cell_group.append(title_txt)
303315
title_txt = cell_group[1]
@@ -347,7 +359,7 @@ def display_page(page_index):
347359
print(f"{grid_index} | {grid_index % config["width"], grid_index // config["width"]}")
348360

349361

350-
page_txt = Label(terminalio.FONT, text="", scale=2, color=color_palette["fg"])
362+
page_txt = Label(terminalio.FONT, text="", scale=scale, color=color_palette["fg"])
351363
page_txt.anchor_point = (1.0, 1.0)
352364
page_txt.anchored_position = (display.width - 2, display.height - 2)
353365
main_group.append(page_txt)
@@ -359,7 +371,7 @@ def display_page(page_index):
359371
left_palette.make_transparent(0)
360372
right_bmp, right_palette = adafruit_imageload.load("launcher_assets/arrow_right.bmp")
361373
right_palette.make_transparent(0)
362-
if color_palette["arrow"] >= 0:
374+
if color_palette["arrow"] is not None:
363375
left_palette[2] = right_palette[2] = color_palette["arrow"]
364376

365377
left_tg = AnchoredTileGrid(bitmap=left_bmp, pixel_shader=left_palette)

0 commit comments

Comments
 (0)