Skip to content

Commit 2cfd1cd

Browse files
committed
refactor(mcp23009e): Migrate menu_navigation example to steami_screen.
1 parent 92f0982 commit 2cfd1cd

1 file changed

Lines changed: 28 additions & 48 deletions

File tree

lib/mcp23009e/examples/menu_navigation.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
UP/DOWN -> move in the menu
55
RIGHT -> select
66
LEFT -> go back
7-
8-
The UI stays in the center of the round display to avoid cropped corners.
97
"""
108

119
from time import sleep_ms
1210

11+
import micropython
1312
import ssd1327
1413
from machine import I2C, SPI, Pin
1514
from mcp23009e import MCP23009E
@@ -23,6 +22,7 @@
2322
MCP23009_LOGIC_LOW,
2423
MCP23009_PULLUP,
2524
)
25+
from steami_screen import Screen, SSD1327Display
2626

2727
# Setup MCP23009E on I2C bus
2828
i2c = I2C(1)
@@ -34,7 +34,8 @@
3434
dc = Pin("DATA_COMMAND_DISPLAY")
3535
res = Pin("RST_DISPLAY")
3636
cs = Pin("CS_DISPLAY")
37-
display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs)
37+
display = SSD1327Display(ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs))
38+
screen = Screen(display)
3839

3940
# D-PAD button mapping
4041
BUTTONS = {
@@ -44,42 +45,32 @@
4445
MCP23009_BTN_RIGHT: "RIGHT",
4546
}
4647

47-
# Keep short labels for a compact centered UI
4848
MENU_ITEMS = [
49-
("Battery", "4.01 V"),
50-
("Press", "1013 hPa"),
51-
("Hum", "48.6 %"),
49+
("Battery", 4.01, "V"),
50+
("Press", 1013, "hPa"),
51+
("Hum", 48.6, "%"),
5252
]
5353

54-
# Central safe area for the round display
55-
X0 = 28
56-
TITLE_Y = 28
57-
ITEM_Y = 46
58-
ITEM_SPACING = 14
59-
FOOTER_Y = 92
60-
6154

6255
def setup_buttons():
63-
"""Configure all D-PAD buttons as inputs with pull-ups."""
6456
for pin_number in BUTTONS:
6557
mcp.setup(pin_number, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP)
6658

6759

60+
@micropython.native
61+
def is_any_pressed():
62+
for pin_number in BUTTONS: # noqa: SIM110
63+
if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW:
64+
return True
65+
return False
66+
67+
6868
def wait_all_released():
69-
"""Wait until all buttons are released."""
70-
while True:
71-
all_released = True
72-
for pin_number in BUTTONS:
73-
if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW:
74-
all_released = False
75-
break
76-
if all_released:
77-
return
69+
while is_any_pressed():
7870
sleep_ms(20)
7971

8072

8173
def wait_for_button():
82-
"""Wait for a button press and return its name."""
8374
wait_all_released()
8475

8576
while True:
@@ -92,35 +83,24 @@ def wait_for_button():
9283

9384

9485
def draw_menu(selected_index):
95-
"""Draw the centered menu."""
96-
display.fill(0)
97-
98-
display.text("MENU", 48, TITLE_Y, 15)
99-
100-
for index, (label, _) in enumerate(MENU_ITEMS):
101-
y = ITEM_Y + index * ITEM_SPACING
102-
prefix = ">" if index == selected_index else " "
103-
display.text(prefix + label, X0, y, 15)
104-
105-
display.text("R:OK L:BACK", 24, FOOTER_Y, 8)
106-
display.show()
86+
labels = [label for label, _, _ in MENU_ITEMS]
87+
screen.clear()
88+
screen.title("MENU")
89+
screen.menu(labels, selected=selected_index)
90+
screen.subtitle("R:OK", "L:Back")
91+
screen.show()
10792

10893

10994
def draw_detail(selected_index):
110-
"""Draw the centered detail screen."""
111-
name, value = MENU_ITEMS[selected_index]
112-
113-
display.fill(0)
114-
115-
display.text(name, 44, 34, 15)
116-
display.text(value, 34, 56, 15)
117-
display.text("LEFT BACK", 34, 88, 8)
118-
119-
display.show()
95+
name, val, unit = MENU_ITEMS[selected_index]
96+
screen.clear()
97+
screen.title(name)
98+
screen.value(val, unit=unit)
99+
screen.subtitle("L:Back")
100+
screen.show()
120101

121102

122103
def main():
123-
"""Main UI loop."""
124104
setup_buttons()
125105

126106
selected_index = 0

0 commit comments

Comments
 (0)