Skip to content

Commit 4f9be49

Browse files
committed
use built-in display if it exists and is initialized.
1 parent 77710f4 commit 4f9be49

File tree

3 files changed

+120
-35
lines changed

3 files changed

+120
-35
lines changed

Metro/Metro_RP2350_Memory/memory_game/code.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,32 @@ def update_score_text():
5858
# initial state is title screen
5959
CUR_STATE = STATE_TITLE
6060

61-
# use the built-in display
62-
display = supervisor.runtime.display
61+
if hasattr(supervisor.runtime, "display") and supervisor.runtime.display is not None:
62+
# use the built-in HSTX display for Metro RP2350
63+
display = supervisor.runtime.display
64+
else:
65+
from displayio import release_displays
66+
import picodvi
67+
import board
68+
import framebufferio
69+
70+
# initialize display
71+
release_displays()
72+
73+
fb = picodvi.Framebuffer(
74+
320,
75+
240,
76+
clk_dp=board.CKP,
77+
clk_dn=board.CKN,
78+
red_dp=board.D0P,
79+
red_dn=board.D0N,
80+
green_dp=board.D1P,
81+
green_dn=board.D1N,
82+
blue_dp=board.D2P,
83+
blue_dn=board.D2N,
84+
color_depth=16,
85+
)
86+
display = framebufferio.FramebufferDisplay(fb)
6387

6488
# main group will hold all of our visual elements
6589
main_group = Group()

Metro/Metro_RP2350_Memory/mouse_demo/code.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,33 @@
1414
import terminalio
1515
import usb.core
1616

17-
# use the built-in HSTX display for Metro RP2350
18-
display = supervisor.runtime.display
17+
18+
if hasattr(supervisor.runtime, "display") and supervisor.runtime.display is not None:
19+
# use the built-in HSTX display for Metro RP2350
20+
display = supervisor.runtime.display
21+
else:
22+
from displayio import release_displays
23+
import picodvi
24+
import board
25+
import framebufferio
26+
27+
# initialize display
28+
release_displays()
29+
30+
fb = picodvi.Framebuffer(
31+
320,
32+
240,
33+
clk_dp=board.CKP,
34+
clk_dn=board.CKN,
35+
red_dp=board.D0P,
36+
red_dn=board.D0N,
37+
green_dp=board.D1P,
38+
green_dn=board.D1N,
39+
blue_dp=board.D2P,
40+
blue_dn=board.D2N,
41+
color_depth=16,
42+
)
43+
display = framebufferio.FramebufferDisplay(fb)
1944

2045
# group to hold visual elements
2146
main_group = Group()

Metro/Metro_RP2350_Memory/tictactoe_demo/code.py

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,33 @@
1818
import supervisor
1919
import terminalio
2020
import usb.core
21-
# use the built-in HSTX display for Metro RP2350
22-
display = supervisor.runtime.display
21+
22+
if hasattr(supervisor.runtime, "display") and supervisor.runtime.display is not None:
23+
# use the built-in HSTX display for Metro RP2350
24+
display = supervisor.runtime.display
25+
else:
26+
from displayio import release_displays
27+
import picodvi
28+
import board
29+
import framebufferio
30+
31+
# initialize display
32+
release_displays()
33+
34+
fb = picodvi.Framebuffer(
35+
320,
36+
240,
37+
clk_dp=board.CKP,
38+
clk_dn=board.CKN,
39+
red_dp=board.D0P,
40+
red_dn=board.D0N,
41+
green_dp=board.D1P,
42+
green_dn=board.D1N,
43+
blue_dp=board.D2P,
44+
blue_dn=board.D2N,
45+
color_depth=16,
46+
)
47+
display = framebufferio.FramebufferDisplay(fb)
2348

2449
# group to hold visual elements
2550
main_group = Group()
@@ -71,13 +96,7 @@
7196
buf = array.array("b", [0] * 4)
7297

7398
# set up a 3x3 grid for the tic-tac-toe board
74-
board_grid = GridLayout(
75-
x=40,
76-
y=40,
77-
width=128,
78-
height=128,
79-
grid_size=(3, 3)
80-
)
99+
board_grid = GridLayout(x=40, y=40, width=128, height=128, grid_size=(3, 3))
81100

82101
# load the tic-tac-toe spritesheet
83102
tictactoe_spritesheet = OnDiskBitmap("tictactoe_spritesheet.bmp")
@@ -95,10 +114,15 @@
95114
# loop over columns
96115
for x in range(3):
97116
# create a TileGrid for this cell
98-
new_tg = TileGrid(bitmap=tictactoe_spritesheet, default_tile=0,
99-
tile_height=32, tile_width=32,
100-
height=1, width=1,
101-
pixel_shader=tictactoe_spritesheet.pixel_shader)
117+
new_tg = TileGrid(
118+
bitmap=tictactoe_spritesheet,
119+
default_tile=0,
120+
tile_height=32,
121+
tile_width=32,
122+
height=1,
123+
width=1,
124+
pixel_shader=tictactoe_spritesheet.pixel_shader,
125+
)
102126

103127
# add the new TileGrid to the board grid at the current position
104128
board_grid.add_content(new_tg, grid_position=(x, y), cell_size=(1, 1))
@@ -122,40 +146,52 @@ def check_for_winner():
122146
# check rows
123147
for row_idx in range(3):
124148
# if the 3 cells in this row match
125-
if (board_grid[0 + (row_idx * 3)][0] != 0 and
126-
board_grid[0 + (row_idx * 3)][0] ==
127-
board_grid[1 + (row_idx * 3)][0] ==
128-
board_grid[2 + (row_idx * 3)][0]):
149+
if (
150+
board_grid[0 + (row_idx * 3)][0] != 0
151+
and board_grid[0 + (row_idx * 3)][0]
152+
== board_grid[1 + (row_idx * 3)][0]
153+
== board_grid[2 + (row_idx * 3)][0]
154+
):
129155
return board_grid[0 + (row_idx * 3)][0]
130156

131157
# if any of the cells in this row are empty
132-
if 0 in (board_grid[0 + (row_idx * 3)][0],
133-
board_grid[1 + (row_idx * 3)][0],
134-
board_grid[2 + (row_idx * 3)][0]):
158+
if 0 in (
159+
board_grid[0 + (row_idx * 3)][0],
160+
board_grid[1 + (row_idx * 3)][0],
161+
board_grid[2 + (row_idx * 3)][0],
162+
):
135163
found_empty = True
136164

137165
# check columns
138166
for col_idx in range(3):
139167
# if the 3 cells in this column match
140-
if (board_grid[0 + col_idx][0] != 0 and
141-
board_grid[0 + col_idx][0] ==
142-
board_grid[3 + col_idx][0] ==
143-
board_grid[6 + col_idx][0]):
168+
if (
169+
board_grid[0 + col_idx][0] != 0
170+
and board_grid[0 + col_idx][0]
171+
== board_grid[3 + col_idx][0]
172+
== board_grid[6 + col_idx][0]
173+
):
144174
return board_grid[0 + col_idx][0]
145175

146176
# if any of the cells in this column are empty
147-
if 0 in (board_grid[0 + col_idx][0],
148-
board_grid[3 + col_idx][0],
149-
board_grid[6 + col_idx][0]):
177+
if 0 in (
178+
board_grid[0 + col_idx][0],
179+
board_grid[3 + col_idx][0],
180+
board_grid[6 + col_idx][0],
181+
):
150182
found_empty = True
151183

152184
# check diagonals
153-
if board_grid[0][0] != 0 and \
154-
board_grid[0][0] == board_grid[4][0] == board_grid[8][0]:
185+
if (
186+
board_grid[0][0] != 0
187+
and board_grid[0][0] == board_grid[4][0] == board_grid[8][0]
188+
):
155189
return board_grid[0][0]
156190

157-
if board_grid[2][0] != 0 and \
158-
board_grid[2][0] == board_grid[4][0] == board_grid[6][0]:
191+
if (
192+
board_grid[2][0] != 0
193+
and board_grid[2][0] == board_grid[4][0] == board_grid[6][0]
194+
):
159195
return board_grid[2][0]
160196

161197
if found_empty:

0 commit comments

Comments
 (0)