implemented exec command actions

This commit is contained in:
Rudis Muiznieks 2022-04-23 15:33:19 -05:00
parent a74caf0f0a
commit db2583ad84
Signed by: rudism
GPG key ID: CABF2F86EF7884F9
2 changed files with 58 additions and 14 deletions

38
main.py
View file

@ -1,24 +1,48 @@
import os
import signal
from time import sleep
from menu import Menu, MenuItem, MenuType
from cinput import ControlInput
from graphics import Graphics
menu_config = [
MenuItem("Information",
MenuType.EXEC_PLUGIN),
MenuType.EXEC_PLUGIN,
{"plugin": "info"}),
MenuItem("Apps",
MenuType.SUB_MENU),
MenuType.SUB_MENU,
{"sub_menu": [
MenuItem("Chess",
MenuType.EXEC_PLUGIN,
{"plugin": "chess"}),
]}),
MenuItem("Reboot",
MenuType.EXEC_CMD),
MenuType.EXEC_CMD,
{"command": "sudo reboot"}),
MenuItem("Shutdown",
MenuType.EXEC_CMD)]
MenuType.EXEC_CMD,
{"command": "sudo shutdown now"})]
cinput = ControlInput()
graphics = Graphics()
menu = Menu(menu_config, cinput, graphics)
try:
menu.get_selection()
except KeyboardInterrupt:
def program_exit(*_):
graphics.clear()
graphics.show()
os._exit(0)
signal.signal(signal.SIGINT, program_exit)
signal.signal(signal.SIGTERM, program_exit)
try:
while True:
item = menu.get_selection()
if item.menu_type == MenuType.EXEC_CMD:
graphics.clear()
graphics.show()
os.system(item.data["command"])
except Exception as e:
ic(e)
program_exit()

34
menu.py
View file

@ -1,6 +1,3 @@
from board import SCL, SDA
from busio import I2C
from adafruit_ssd1306 import SSD1306_I2C
from enum import Enum, auto
from dataclasses import dataclass
from cinput import ControlInput, Button
@ -15,31 +12,40 @@ class MenuType(Enum):
class MenuItem:
display: str
menu_type: MenuType
data: dict
class Menu:
def __init__(
self, config: list[MenuItem], cinput: ControlInput, graphics: Graphics):
self._top_index = 0
self._selected_index = 0
self._menu_path = list()
self._menu = config
self._cur_menu = self._menu
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
selected_diff = self._selected_index - self._top_index
if selected_diff < 0:
self._top_index = self._selected_index
if selected_diff >= Graphics.MAX_LINES:
self._top_index = self._selected_index - Graphics.MAX_LINES + 1
for idx in range(top_index, top_index + Graphics.MAX_LINES):
for idx in range(self._top_index, self._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.text(marker + items[idx].display, 0, (idx - self._top_index) * Graphics.LINE_HEIGHT, 1)
self._graphics.show()
def get_selection(self):
while True:
self._print(self._menu)
self._print(self._cur_menu)
pressed = self._input.get_one_shot()
if pressed == Button.DIR_U:
self._selected_index -= 1
if self._selected_index < 0:
@ -48,3 +54,17 @@ class Menu:
self._selected_index += 1
if self._selected_index >= len(self._menu):
self._selected_index = 0
elif pressed == Button.BTN_B or pressed == Button.DIR_R:
item = self._cur_menu[self._selected_index]
if item.menu_type == MenuType.SUB_MENU:
self._menu_path.append(self._selected_index)
self._selected_index = 0
else:
return item
elif pressed == Button.BTN_A or pressed == Button.DIR_L:
if len(self._menu_path) > 0:
self._selected_index = self._menu_path.pop()
self._cur_menu = self._menu
for i in range(0, len(self._menu_path)):
self._cur_menu = self._cur_menu[self._menu_path[i]].data["sub_menu"]