Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 28 additions & 48 deletions lib/mcp23009e/examples/menu_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
UP/DOWN -> move in the menu
RIGHT -> select
LEFT -> go back

The UI stays in the center of the round display to avoid cropped corners.
"""

from time import sleep_ms

import micropython
import ssd1327
from machine import I2C, SPI, Pin
from mcp23009e import MCP23009E
Expand All @@ -23,6 +22,7 @@
MCP23009_LOGIC_LOW,
MCP23009_PULLUP,
)
from steami_screen import Screen, SSD1327Display

# Setup MCP23009E on I2C bus
i2c = I2C(1)
Expand All @@ -34,7 +34,8 @@
dc = Pin("DATA_COMMAND_DISPLAY")
res = Pin("RST_DISPLAY")
cs = Pin("CS_DISPLAY")
display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs)
display = SSD1327Display(ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs))
screen = Screen(display)

# D-PAD button mapping
BUTTONS = {
Expand All @@ -44,42 +45,32 @@
MCP23009_BTN_RIGHT: "RIGHT",
}

# Keep short labels for a compact centered UI
MENU_ITEMS = [
("Battery", "4.01 V"),
("Press", "1013 hPa"),
("Hum", "48.6 %"),
("Battery", 4.01, "V"),
("Press", 1013, "hPa"),
("Hum", 48.6, "%"),
]

# Central safe area for the round display
X0 = 28
TITLE_Y = 28
ITEM_Y = 46
ITEM_SPACING = 14
FOOTER_Y = 92


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


@micropython.native
def is_any_pressed():
for pin_number in BUTTONS: # noqa: SIM110
if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW:
return True
return False


def wait_all_released():
"""Wait until all buttons are released."""
while True:
all_released = True
for pin_number in BUTTONS:
if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW:
all_released = False
break
if all_released:
return
while is_any_pressed():
sleep_ms(20)


def wait_for_button():
"""Wait for a button press and return its name."""
wait_all_released()

while True:
Expand All @@ -92,35 +83,24 @@ def wait_for_button():


def draw_menu(selected_index):
"""Draw the centered menu."""
display.fill(0)

display.text("MENU", 48, TITLE_Y, 15)

for index, (label, _) in enumerate(MENU_ITEMS):
y = ITEM_Y + index * ITEM_SPACING
prefix = ">" if index == selected_index else " "
display.text(prefix + label, X0, y, 15)

display.text("R:OK L:BACK", 24, FOOTER_Y, 8)
display.show()
labels = [label for label, _, _ in MENU_ITEMS]
screen.clear()
screen.title("MENU")
screen.menu(labels, selected=selected_index)
screen.subtitle("R:OK", "L:Back")
screen.show()


def draw_detail(selected_index):
"""Draw the centered detail screen."""
name, value = MENU_ITEMS[selected_index]

display.fill(0)

display.text(name, 44, 34, 15)
display.text(value, 34, 56, 15)
display.text("LEFT BACK", 34, 88, 8)

display.show()
name, val, unit = MENU_ITEMS[selected_index]
screen.clear()
screen.title(name)
screen.value(val, unit=unit)
screen.subtitle("L:Back")
screen.show()


def main():
"""Main UI loop."""
setup_buttons()

selected_index = 0
Expand Down
Loading