implemented info plugin

This commit is contained in:
Rudis Muiznieks 2022-04-23 18:25:09 -05:00
parent 47303cdec8
commit 8ad0a39651
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
6 changed files with 59 additions and 10 deletions

View File

@ -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:

View File

@ -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

View File

@ -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()

View File

@ -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()

1
plugin/__init__.py Normal file
View File

@ -0,0 +1 @@
# plugins

32
plugin/info/__init__.py Normal file
View File

@ -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