From 8ad0a39651686c36ad765f98ee536103f616a000 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sat, 23 Apr 2022 18:25:09 -0500 Subject: [PATCH] implemented info plugin --- main.py => __main__.py | 22 +++++++++++++++++----- cinput.py | 9 ++++++--- graphics.py | 3 ++- menu.py | 2 +- plugin/__init__.py | 1 + plugin/info/__init__.py | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 10 deletions(-) rename main.py => __main__.py (69%) create mode 100644 plugin/__init__.py create mode 100644 plugin/info/__init__.py diff --git a/main.py b/__main__.py similarity index 69% rename from main.py rename to __main__.py index 4ba8be8..1365981 100644 --- a/main.py +++ b/__main__.py @@ -1,15 +1,13 @@ import os import signal +import time +from importlib import import_module from icecream import ic -from time import sleep from menu import Menu, MenuItem, MenuType from cinput import ControlInput from graphics import Graphics menu_config = [ - MenuItem("Information", - MenuType.PLUGIN, - {"plugin": "info"}), MenuItem("Apps", MenuType.SUB_MENU, {"sub_menu": [ @@ -17,6 +15,9 @@ menu_config = [ MenuType.PLUGIN, {"plugin": "chess"}), ]}), + MenuItem("Information", + MenuType.PLUGIN, + {"plugin": "info"}), MenuItem("Reboot", MenuType.EXIT_CMD, {"command": "sudo reboot"}), @@ -39,7 +40,18 @@ signal.signal(signal.SIGTERM, program_exit) try: while True: item = menu.get_selection() - if item.menu_type == MenuType.EXIT_CMD: + ic(item) + if item.menu_type == MenuType.PLUGIN: + try: + plugin = import_module("plugin." + item.data["plugin"]) + ic(plugin) + plugin.execute(cinput, graphics) + except: + graphics.clear() + graphics.text("Plugin error!", 0, 0, 1) + graphics.show() + time.sleep(3) + elif item.menu_type == MenuType.EXIT_CMD: os.system(item.data["command"]) program_exit() except Exception as e: diff --git a/cinput.py b/cinput.py index 9b0151c..17c487f 100644 --- a/cinput.py +++ b/cinput.py @@ -1,7 +1,7 @@ import board from digitalio import DigitalInOut, Direction, Pull from enum import Enum, auto -from time import sleep +import time class Button(Enum): BTN_A = auto() @@ -30,12 +30,15 @@ class ControlInput: self._buttons[button].direction = Direction.INPUT self._buttons[button].pull = Pull.UP - def get_one_shot(self): + def get_one_shot(self, timeout = 0): + started = time.time() while True: + time.sleep(0.05) for button in self._buttons: if not self._buttons[button].value and not button in self._pressed: self._pressed.add(button) return button elif self._buttons[button].value: self._pressed.discard(button) - + if timeout > 0 and time.time() - started > timeout: + return None diff --git a/graphics.py b/graphics.py index fa9d7d3..e8a6788 100644 --- a/graphics.py +++ b/graphics.py @@ -7,6 +7,7 @@ class Graphics: OLED_HEIGHT = 64 OLED_ADDR = 0x3C LINE_HEIGHT = 9 + CHAR_WIDTH = 6 MAX_LINES = 7 def __init__(self): @@ -18,7 +19,7 @@ class Graphics: self._display.fill(0) def text(self, text, x, y, c): - self._display.text(text, x, y, c) + self._display.text(text, x * self.CHAR_WIDTH, y * self.LINE_HEIGHT, c) def show(self): self._display.show() diff --git a/menu.py b/menu.py index 5f1e745..16d5200 100644 --- a/menu.py +++ b/menu.py @@ -37,7 +37,7 @@ class Menu: if idx >= len(items): break marker = "> " if idx == self._selected_index else " " - self._graphics.text(marker + items[idx].display, 0, (idx - self._top_index) * Graphics.LINE_HEIGHT, 1) + self._graphics.text(marker + items[idx].display, 0, (idx - self._top_index), 1) self._graphics.show() diff --git a/plugin/__init__.py b/plugin/__init__.py new file mode 100644 index 0000000..f130fd1 --- /dev/null +++ b/plugin/__init__.py @@ -0,0 +1 @@ +# plugins diff --git a/plugin/info/__init__.py b/plugin/info/__init__.py new file mode 100644 index 0000000..7c667c8 --- /dev/null +++ b/plugin/info/__init__.py @@ -0,0 +1,32 @@ +import subprocess +from cinput import ControlInput, Button +from graphics import Graphics + +def execute(cinput: ControlInput, graphics: Graphics): + graphics.clear() + graphics.text("Loading details...", 0, 0, 1) + graphics.show() + + while True: + cmd = "hostname -I | cut -d' ' -f1" + ip = subprocess.check_output(cmd, shell=True).decode("utf-8") + cmd = "top -bn 2 |grep \"Cpu(s)\" | awk '{print $2+$4+$6+$10+$12+$14+$16 \"%\"}' | tail -n1" + cpu = subprocess.check_output(cmd, shell=True).decode("utf-8") + cmd = "free | grep Mem | awk '{print int($3/$2 * 100.0 +0.5) \"%\"}'" + mem = subprocess.check_output(cmd, shell=True).decode("utf-8") + cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'" + disk = subprocess.check_output(cmd, shell=True).decode("utf-8") + cmd = "uptime | awk '{print $3}' | sed 's/,$//'" + up = subprocess.check_output(cmd, shell=True).decode("utf-8") + + graphics.clear() + graphics.text("IP: " + ip, 0, 0, 1) + graphics.text("CPU: " + cpu, 0, 1, 1) + graphics.text("Memory: " + mem, 0, 2, 1) + graphics.text(disk, 0, 3, 1) + graphics.text("Uptime: " + up, 0, 4, 1) + graphics.show() + + key = cinput.get_one_shot(5) + if key == Button.BTN_A: + return