Compare commits

...

10 commits

11 changed files with 516 additions and 484 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
*.pyc
fwledmonitor.service

View file

@ -187,7 +187,7 @@ def init_device(location = "1-4.2"):
# PID = 5678
device_list = list_ports.comports()
for device in device_list:
if device.location == location:
if device.location and device.location.startswith(location):
s = serial.Serial(device.device, 115200)
return s
except Exception as e:
@ -195,10 +195,11 @@ def init_device(location = "1-4.2"):
class DrawingThread(threading.Thread):
def __init__(self, serial_port, input_queue):
def __init__(self, port_location, input_queue):
super().__init__()
self.daemon = True
self.serial_port = init_device(serial_port)
self.port_location = port_location
self.serial_port = init_device(self.port_location)
self.input_queue = input_queue
def run(self):
@ -210,5 +211,5 @@ class DrawingThread(threading.Thread):
print(f"Error in DrawingThread: {e}")
del self.serial_port
time.sleep(1.0)
self.serial_port = init_device(self.serial_port)
self.serial_port = init_device(self.port_location)

21
install_as_service.sh Normal file
View file

@ -0,0 +1,21 @@
chmod +x run.sh
rm -f fwledmonitor.service || true
cat <<EOF >>./fwledmonitor.service
[Unit]
Description=Framework 16 LED System Monitor
After=network.service
[Service]
Type=simple
Restart=always
WorkingDirectory=$PWD
ExecStart=sh -c "'$PWD/run.sh'"
[Install]
WantedBy=default.target
EOF
sudo systemctl stop fwledmonitor
sudo cp fwledmonitor.service /lib/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable fwledmonitor

View file

@ -7,18 +7,7 @@ from drawing import draw_cpu, draw_memory, draw_battery, draw_borders_left, draw
from monitors import CPUMonitor, MemoryMonitor, BatteryMonitor, DiskMonitor, NetworkMonitor, get_monitor_brightness
# External Dependencies
try:
# These are used in later scripts, but imported here to test if missing
import serial # pyserial
from serial.tools import list_ports
import numpy as np
except ImportError:
import pip
for dependency in ["numpy", "pyserial"]:
pip.main(['install', '--user', dependency])
import numpy as np
# print(sbc.get_brightness())
if __name__ == "__main__":
@ -26,9 +15,9 @@ if __name__ == "__main__":
# Right LED Matrix location: "1-3.3"
# Set up monitors and serial for left LED Matrix
min_background_brightness = 8
min_background_brightness = 12
max_background_brightness = 35
min_foreground_brightness = 30
min_foreground_brightness = 24
max_foreground_brightness = 160
cpu_monitor = CPUMonitor()
@ -51,8 +40,8 @@ if __name__ == "__main__":
while True:
try:
screen_brightness = get_monitor_brightness()
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)
background_value = int(screen_brightness * (max_background_brightness - min_background_brightness) + min_background_brightness)
foreground_value = int(screen_brightness * (max_foreground_brightness - min_foreground_brightness) + min_foreground_brightness)
left_start_time = time.time()
# Draw to left LED Matrix
@ -78,9 +67,13 @@ if __name__ == "__main__":
draw_bar(grid, last_network_download, foreground_value, bar_x_offset=5, draw_at_bottom=True) # Download
draw_borders_right(grid, background_value)
right_drawing_queue.put(grid)
except KeyboardInterrupt:
break
except Exception as e:
import traceback
print(f"Error in main loop: {e}")
traceback.print_exc()
time.sleep(1.0)
time.sleep(0.1)
print("Exiting")

Binary file not shown.

View file

@ -5,14 +5,12 @@ import os
if os.name == 'nt':
import wmi
else:
raise Exception("This script is not supported on this OS")
class DiskMonitor:
def __init__(self, hysterisis_time = 20):
self.read_usage_history = []
self.write_usage_history = []
self.history_times = []
self.read_usage_history = [0]
self.write_usage_history = [0]
self.history_times = [0]
self.highest_read_rate = 0.00001
self.highest_write_rate = 0.00001
self.max_history_size = hysterisis_time
@ -46,9 +44,9 @@ class DiskMonitor:
class NetworkMonitor:
def __init__(self, hysterisis_time = 20):
self.sent_usage_history = []
self.recv_usage_history = []
self.history_times = []
self.sent_usage_history = [0]
self.recv_usage_history = [0]
self.history_times = [0]
self.highest_sent_rate = 0.00001
self.highest_recv_rate = 0.00001
self.max_history_size = hysterisis_time
@ -119,12 +117,25 @@ class BatteryMonitor:
battery = psutil.sensors_battery()
if battery is not None:
battery_percentage = battery.percent / 100.0
if os.name == "nt":
battery_plugged = battery.power_plugged
else:
bat_status = open('/sys/class/power_supply/BAT1/status', 'r').read().strip()
battery_plugged = (bat_status != 'Discharging')
return battery_percentage, battery_plugged
def get_monitor_brightness():
try:
return wmi.WMI(namespace='wmi').WmiMonitorBrightness()[0].CurrentBrightness
except:
return 50
if os.name == 'nt':
return wmi.WMI(namespace='wmi').WmiMonitorBrightness()[0].CurrentBrightness / 100.0
else:
try: # First try the dGPU brightness
return int(open('/sys/class/backlight/amdgpu_bl2/brightness', 'r').read()) / 255.0
except: # If that doesn't work, try the iGPU brightness
return int(open('/sys/class/backlight/amdgpu_bl1/brightness', 'r').read()) / 255.0
except Exception as e:
return 1.0
if __name__ == "__main__":
print(get_monitor_brightness())

3
run.bat Normal file
View file

@ -0,0 +1,3 @@
py -3 -m pip install pyserial numpy wmi psutil
py -3 led_system_monitor.py

3
run.sh Normal file
View file

@ -0,0 +1,3 @@
sudo apt install python3-numpy python3-psutil
python3 ./led_system_monitor.py

View file

@ -1 +0,0 @@
py -3 led_system_monitor.py