FW_LED_System_Monitor/led_system_monitor.py

121 lines
4.2 KiB
Python
Raw Normal View History

2024-08-01 06:47:31 +00:00
# Built In Dependencies
import sys
import glob
import time
import queue
# Internal Dependencies
from commands import Commands, send_command
2024-08-01 20:15:36 +00:00
from drawing import draw_cpu, draw_memory, draw_battery, draw_borders, draw_to_LEDs
from monitors import CPUMonitorThread, MemoryMonitorThread, BatteryMonitorThread
2024-08-01 06:47:31 +00:00
# External Dependencies
2024-08-01 14:23:52 +00:00
try:
import serial # pyserial
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
import screen_brightness_control as sbc
# print(sbc.get_brightness())
2024-08-01 06:47:31 +00:00
def get_ports():
"""Returns a list of all available serial ports on the system.
Raises:
EnvironmentError: Will be returned if the platform is not Windows, Linux, Cygwin, or Darwin.
Returns:
[list(str)]: A list of valid serial ports on the system.
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i+1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = reversed(glob.glob('/dev/ttyUSB*'))
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
if __name__ == "__main__":
# print(get_ports())
port = "COM3"
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()
2024-08-01 06:47:31 +00:00
s = serial.Serial(port, 115200)
2024-08-01 20:15:36 +00:00
min_background_brightness = 8
max_background_brightness = 20
2024-08-01 14:23:52 +00:00
min_foreground_brightness = 30
2024-08-01 20:15:36 +00:00
max_foreground_brightness = 110
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-01 06:47:31 +00:00
while True:
if not cpu_queue.empty():
2024-08-01 20:15:36 +00:00
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()
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)
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)
draw_borders(grid, background_value)
draw_to_LEDs(s, grid)
2024-08-01 14:23:52 +00:00
time.sleep(0.05)
2024-08-01 06:47:31 +00:00
# # print(send_command(port, Commands.Version, with_response=True))
# with serial.Serial(port, 115200) as s:
# for cval in range(16):
# for column_number in range(9):
# column_values = [cval] * 34
# params = bytearray([column_number]) + bytearray(column_values)
# send_command(s, Commands.StageCol, parameters=params)
# print(f"Flushing cval: {cval}")
# send_command(s, Commands.FlushCols)
# Columns are filled left to right top to bottom
# with serial.Serial(port, 115200) as s:
# column_number = 0
# column_values = [50] * 17 + [0] * 17
# params = bytearray([column_number]) + bytearray(column_values)
# send_command(s, Commands.StageCol, parameters=params)
# send_command(s, Commands.FlushCols)