FW_LED_System_Monitor/led_system_monitor.py

130 lines
4.6 KiB
Python
Raw Normal View History

2024-08-01 06:47:31 +00:00
# Built In Dependencies
import time
import queue
# Internal Dependencies
2024-08-06 00:15:56 +00:00
from drawing import draw_cpu, draw_memory, draw_battery, draw_borders_left, draw_to_LEDs, draw_bar, draw_borders_right
from monitors import CPUMonitorThread, MemoryMonitorThread, BatteryMonitorThread, DiskMonitorThread, NetworkMonitorThread
2024-08-01 06:47:31 +00:00
# External Dependencies
2024-08-01 14:23:52 +00:00
try:
import serial # pyserial
2024-08-03 14:24:03 +00:00
from serial.tools import list_ports
2024-08-01 14:23:52 +00:00
import numpy as np # This is used in a module and we import it here to fetch it if needed
import screen_brightness_control as sbc
except ImportError:
import pip
for dependency in ["numpy", "pyserial", "screen-brightness-control"]:
pip.main(['install', '--user', dependency])
import serial
2024-08-03 14:24:03 +00:00
from serial.tools import list_ports
2024-08-01 14:23:52 +00:00
import screen_brightness_control as sbc
# print(sbc.get_brightness())
2024-08-01 06:47:31 +00:00
2024-08-03 14:24:03 +00:00
def init_device(location = "1-4.2"):
try:
# VID = 1234
# PID = 5678
device_list = list_ports.comports()
for device in device_list:
if device.location == location:
s = serial.Serial(device.device, 115200)
return s
except Exception as e:
print(e)
2024-08-01 06:47:31 +00:00
if __name__ == "__main__":
2024-08-03 14:24:03 +00:00
# Left LED Matrix location: "1-4.2"
# Right LED Matrix location: "1-3.3"
2024-08-01 06:47:31 +00:00
2024-08-06 00:15:56 +00:00
# Set up monitors and serial for left LED Matrix
min_background_brightness = 8
max_background_brightness = 35
min_foreground_brightness = 30
max_foreground_brightness = 160
2024-08-01 14:23:52 +00:00
cpu_queue = queue.Queue(2)
2024-08-01 06:47:31 +00:00
cpu_monitor = CPUMonitorThread(cpu_queue)
cpu_monitor.start()
2024-08-01 20:15:36 +00:00
memory_queue = queue.Queue(2)
memory_monitor = MemoryMonitorThread(memory_queue)
memory_monitor.start()
battery_queue = queue.Queue(2)
battery_monitor = BatteryMonitorThread(battery_queue)
battery_monitor.start()
last_cpu_values = cpu_queue.get()
last_memory_values = memory_queue.get()
last_battery_values = battery_queue.get()
2024-08-01 14:23:52 +00:00
2024-08-06 00:15:56 +00:00
s1 = init_device("1-4.2")
# Set up monitors and serial for right LED Matrix
disk_queue = queue.Queue(2)
disk_monitor = DiskMonitorThread(disk_queue)
disk_monitor.start()
network_queue = queue.Queue(2)
network_monitor = NetworkMonitorThread(network_queue)
network_monitor.start()
last_disk_read, last_disk_write = disk_queue.get()
last_network_upload, last_network_download = network_queue.get()
s2 = init_device("1-3.3")
2024-08-03 14:24:03 +00:00
2024-08-01 06:47:31 +00:00
while True:
2024-08-02 03:04:49 +00:00
try:
2024-08-06 00:15:56 +00:00
screen_brightness = sbc.get_brightness()[0]
background_value = int(screen_brightness / 100 * (max_background_brightness - min_background_brightness) + min_background_brightness)
foreground_value = int(screen_brightness / 100 * (max_foreground_brightness - min_foreground_brightness) + min_foreground_brightness)
# Draw to left LED Matrix
2024-08-02 03:04:49 +00:00
if not cpu_queue.empty():
last_cpu_values = cpu_queue.get()
if not memory_queue.empty():
last_memory_values = memory_queue.get()
if not battery_queue.empty():
last_battery_values = battery_queue.get()
grid = np.zeros((9,34), dtype = int)
draw_cpu(grid, last_cpu_values, foreground_value)
draw_memory(grid, last_memory_values, foreground_value)
draw_battery(grid, last_battery_values[0], last_battery_values[1], foreground_value)
2024-08-06 00:15:56 +00:00
draw_borders_left(grid, background_value)
draw_to_LEDs(s1, grid)
# Draw to right LED Matrix
if not disk_queue.empty():
last_disk_read, last_disk_write = disk_queue.get()
if not network_queue.empty():
last_network_upload, last_network_download = network_queue.get()
grid = np.zeros((9,34), dtype = int)
draw_bar(grid, last_disk_read, foreground_value, bar_x_offset=1, draw_at_bottom=False) # Read
draw_bar(grid, last_disk_write, foreground_value, bar_x_offset=1, draw_at_bottom=True) # Write
draw_bar(grid, last_network_upload, foreground_value, bar_x_offset=5, draw_at_bottom=False) # Upload
draw_bar(grid, last_network_download, foreground_value, bar_x_offset=5, draw_at_bottom=True) # Download
draw_borders_right(grid, background_value)
draw_to_LEDs(s2, grid)
2024-08-02 03:04:49 +00:00
except Exception as e:
print(f"Error in main loop: {e}")
2024-08-06 00:15:56 +00:00
try:
del s1
except:
pass
try:
del s2
except:
pass
2024-08-03 14:24:03 +00:00
time.sleep(1.0)
2024-08-06 00:15:56 +00:00
s1 = init_device("1-4.2")
s2 = init_device("1-3.3")
time.sleep(0.05)