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 os
import signal
from time import sleep
from menu import Menu, MenuItem, MenuType from menu import Menu, MenuItem, MenuType
from cinput import ControlInput from cinput import ControlInput
from graphics import Graphics from graphics import Graphics
menu_config = [ menu_config = [
MenuItem("Information", MenuItem("Information",
MenuType.EXEC_PLUGIN), MenuType.EXEC_PLUGIN,
{"plugin": "info"}),
MenuItem("Apps", MenuItem("Apps",
MenuType.SUB_MENU), MenuType.SUB_MENU,
{"sub_menu": [
MenuItem("Chess",
MenuType.EXEC_PLUGIN,
{"plugin": "chess"}),
]}),
MenuItem("Reboot", MenuItem("Reboot",
MenuType.EXEC_CMD), MenuType.EXEC_CMD,
{"command": "sudo reboot"}),
MenuItem("Shutdown", MenuItem("Shutdown",
MenuType.EXEC_CMD)] MenuType.EXEC_CMD,
{"command": "sudo shutdown now"})]
cinput = ControlInput() cinput = ControlInput()
graphics = Graphics() graphics = Graphics()
menu = Menu(menu_config, cinput, graphics) menu = Menu(menu_config, cinput, graphics)
try:
menu.get_selection() def program_exit(*_):
except KeyboardInterrupt:
graphics.clear() graphics.clear()
graphics.show() graphics.show()
os._exit(0) 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 enum import Enum, auto
from dataclasses import dataclass from dataclasses import dataclass
from cinput import ControlInput, Button from cinput import ControlInput, Button
@ -15,31 +12,40 @@ class MenuType(Enum):
class MenuItem: class MenuItem:
display: str display: str
menu_type: MenuType menu_type: MenuType
data: dict
class Menu: class Menu:
def __init__( def __init__(
self, config: list[MenuItem], cinput: ControlInput, graphics: Graphics): self, config: list[MenuItem], cinput: ControlInput, graphics: Graphics):
self._top_index = 0
self._selected_index = 0 self._selected_index = 0
self._menu_path = list()
self._menu = config self._menu = config
self._cur_menu = self._menu
self._input = cinput self._input = cinput
self._graphics = graphics self._graphics = graphics
def _print(self, items: list[MenuItem]): def _print(self, items: list[MenuItem]):
self._graphics.clear() 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): if idx >= len(items):
break break
marker = "> " if idx == self._selected_index else " " 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() self._graphics.show()
def get_selection(self): def get_selection(self):
while True: while True:
self._print(self._menu) self._print(self._cur_menu)
pressed = self._input.get_one_shot() pressed = self._input.get_one_shot()
if pressed == Button.DIR_U: if pressed == Button.DIR_U:
self._selected_index -= 1 self._selected_index -= 1
if self._selected_index < 0: if self._selected_index < 0:
@ -48,3 +54,17 @@ class Menu:
self._selected_index += 1 self._selected_index += 1
if self._selected_index >= len(self._menu): if self._selected_index >= len(self._menu):
self._selected_index = 0 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"]