2022-04-23 13:37:54 -05:00
|
|
|
from board import SCL, SDA
|
|
|
|
from busio import I2C
|
|
|
|
from adafruit_ssd1306 import SSD1306_I2C
|
2022-04-23 10:08:56 -05:00
|
|
|
from enum import Enum, auto
|
|
|
|
from dataclasses import dataclass
|
2022-04-23 13:37:54 -05:00
|
|
|
from cinput import ControlInput, Button
|
|
|
|
from graphics import Graphics
|
2022-04-23 10:08:56 -05:00
|
|
|
|
|
|
|
class MenuType(Enum):
|
|
|
|
SUB_MENU = auto()
|
|
|
|
EXEC_CMD = auto()
|
|
|
|
EXEC_PLUGIN = auto()
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MenuItem:
|
|
|
|
display: str
|
|
|
|
menu_type: MenuType
|
|
|
|
|
2022-04-23 13:37:54 -05:00
|
|
|
class Menu:
|
|
|
|
def __init__(
|
|
|
|
self, config: list[MenuItem], cinput: ControlInput, graphics: Graphics):
|
|
|
|
self._selected_index = 0
|
|
|
|
self._menu = config
|
|
|
|
self._input = cinput
|
|
|
|
self._graphics = graphics
|
|
|
|
|
|
|
|
def _print(self, items: list[MenuItem]):
|
|
|
|
self._graphics.clear()
|
|
|
|
top_index = 0 if self._selected_index < Graphics.MAX_LINES else Graphics.MAX_LINES - self._selected_index + 1
|
|
|
|
|
|
|
|
for idx in range(top_index, top_index + Graphics.MAX_LINES):
|
|
|
|
if idx >= len(items):
|
|
|
|
break
|
|
|
|
marker = "> " if idx == self._selected_index else " "
|
|
|
|
self._graphics.text(marker + items[idx].display, 0, (idx - top_index) * Graphics.LINE_HEIGHT, 1)
|
|
|
|
|
|
|
|
self._graphics.show()
|
|
|
|
|
|
|
|
def get_selection(self):
|
|
|
|
while True:
|
|
|
|
self._print(self._menu)
|
|
|
|
pressed = self._input.get_one_shot()
|
|
|
|
if pressed == Button.DIR_U:
|
|
|
|
self._selected_index -= 1
|
|
|
|
if self._selected_index < 0:
|
|
|
|
self._selected_index = len(self._menu) - 1
|
|
|
|
elif pressed == Button.DIR_D:
|
|
|
|
self._selected_index += 1
|
|
|
|
if self._selected_index >= len(self._menu):
|
|
|
|
self._selected_index = 0
|