Added auto brightness adjustment
This commit is contained in:
parent
8537b30b49
commit
383cb15866
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.pyc
|
@ -10,7 +10,18 @@ from drawing import make_cpu_grid, draw_to_LEDs
|
|||||||
from monitors import CPUMonitorThread
|
from monitors import CPUMonitorThread
|
||||||
|
|
||||||
# External Dependencies
|
# External Dependencies
|
||||||
import serial # pyserial
|
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())
|
||||||
|
|
||||||
def get_ports():
|
def get_ports():
|
||||||
"""Returns a list of all available serial ports on the system.
|
"""Returns a list of all available serial ports on the system.
|
||||||
@ -47,18 +58,26 @@ if __name__ == "__main__":
|
|||||||
# print(get_ports())
|
# print(get_ports())
|
||||||
port = "COM3"
|
port = "COM3"
|
||||||
|
|
||||||
cpu_queue = queue.Queue()
|
cpu_queue = queue.Queue(2)
|
||||||
cpu_monitor = CPUMonitorThread(cpu_queue)
|
cpu_monitor = CPUMonitorThread(cpu_queue)
|
||||||
cpu_monitor.start()
|
cpu_monitor.start()
|
||||||
|
|
||||||
s = serial.Serial(port, 115200)
|
s = serial.Serial(port, 115200)
|
||||||
|
|
||||||
|
min_background_brightness = 10
|
||||||
|
max_background_brightness = 30
|
||||||
|
min_foreground_brightness = 30
|
||||||
|
max_foreground_brightness = 120
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not cpu_queue.empty():
|
if not cpu_queue.empty():
|
||||||
cpu_values = cpu_queue.get()
|
cpu_values = cpu_queue.get()
|
||||||
grid = make_cpu_grid(cpu_values, 10, 30)
|
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 = make_cpu_grid(cpu_values, background_value, foreground_value)
|
||||||
draw_to_LEDs(s, grid)
|
draw_to_LEDs(s, grid)
|
||||||
time.sleep(0.1)
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
132
monitors.py
132
monitors.py
@ -20,28 +20,29 @@ class DiskMonitorThread(threading.Thread):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
disk_io = psutil.disk_io_counters()
|
if not self.output_queue.full():
|
||||||
read_usage = disk_io.read_bytes
|
disk_io = psutil.disk_io_counters()
|
||||||
write_usage = disk_io.write_bytes
|
read_usage = disk_io.read_bytes
|
||||||
self.read_usage_history.append(read_usage)
|
write_usage = disk_io.write_bytes
|
||||||
self.write_usage_history.append(write_usage)
|
self.read_usage_history.append(read_usage)
|
||||||
self.history_times.append(time.time())
|
self.write_usage_history.append(write_usage)
|
||||||
if len(self.read_usage_history) > self.max_history_size:
|
self.history_times.append(time.time())
|
||||||
self.read_usage_history = self.read_usage_history[-self.max_history_size:]
|
if len(self.read_usage_history) > self.max_history_size:
|
||||||
self.write_usage_history = self.write_usage_history[-self.max_history_size:]
|
self.read_usage_history = self.read_usage_history[-self.max_history_size:]
|
||||||
self.history_times = self.history_times[-self.max_history_size:]
|
self.write_usage_history = self.write_usage_history[-self.max_history_size:]
|
||||||
|
self.history_times = self.history_times[-self.max_history_size:]
|
||||||
|
|
||||||
if len(self.read_usage_history) == self.max_history_size:
|
if len(self.read_usage_history) == self.max_history_size:
|
||||||
read_diff = self.read_usage_history[-1] - self.read_usage_history[0]
|
read_diff = self.read_usage_history[-1] - self.read_usage_history[0]
|
||||||
write_diff = self.write_usage_history[-1] - self.write_usage_history[0]
|
write_diff = self.write_usage_history[-1] - self.write_usage_history[0]
|
||||||
time_diff = self.history_times[-1] - self.history_times[0]
|
time_diff = self.history_times[-1] - self.history_times[0]
|
||||||
read_rate = read_diff / time_diff
|
read_rate = read_diff / time_diff
|
||||||
write_rate = write_diff / time_diff
|
write_rate = write_diff / time_diff
|
||||||
self.highest_read_rate = max(self.highest_read_rate, read_rate)
|
self.highest_read_rate = max(self.highest_read_rate, read_rate)
|
||||||
self.highest_write_rate = max(self.highest_write_rate, write_rate)
|
self.highest_write_rate = max(self.highest_write_rate, write_rate)
|
||||||
read_percent = min(1.0, read_rate / self.highest_read_rate)
|
read_percent = min(1.0, read_rate / self.highest_read_rate)
|
||||||
write_percent = min(1.0, write_rate / self.highest_write_rate)
|
write_percent = min(1.0, write_rate / self.highest_write_rate)
|
||||||
self.output_queue.put((read_percent, write_percent))
|
self.output_queue.put((read_percent, write_percent))
|
||||||
|
|
||||||
time.sleep(self.update_interval)
|
time.sleep(self.update_interval)
|
||||||
|
|
||||||
@ -60,28 +61,29 @@ class NetworkMonitorThread(threading.Thread):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
net_io = psutil.net_io_counters()
|
if not self.output_queue.full():
|
||||||
sent_usage = net_io.bytes_sent
|
net_io = psutil.net_io_counters()
|
||||||
recv_usage = net_io.bytes_recv
|
sent_usage = net_io.bytes_sent
|
||||||
self.sent_usage_history.append(sent_usage)
|
recv_usage = net_io.bytes_recv
|
||||||
self.recv_usage_history.append(recv_usage)
|
self.sent_usage_history.append(sent_usage)
|
||||||
self.history_times.append(time.time())
|
self.recv_usage_history.append(recv_usage)
|
||||||
if len(self.sent_usage_history) > self.max_history_size:
|
self.history_times.append(time.time())
|
||||||
self.sent_usage_history = self.sent_usage_history[-self.max_history_size:]
|
if len(self.sent_usage_history) > self.max_history_size:
|
||||||
self.recv_usage_history = self.recv_usage_history[-self.max_history_size:]
|
self.sent_usage_history = self.sent_usage_history[-self.max_history_size:]
|
||||||
self.history_times = self.history_times[-self.max_history_size:]
|
self.recv_usage_history = self.recv_usage_history[-self.max_history_size:]
|
||||||
|
self.history_times = self.history_times[-self.max_history_size:]
|
||||||
|
|
||||||
if len(self.sent_usage_history) == self.max_history_size:
|
if len(self.sent_usage_history) == self.max_history_size:
|
||||||
sent_diff = self.sent_usage_history[-1] - self.sent_usage_history[0]
|
sent_diff = self.sent_usage_history[-1] - self.sent_usage_history[0]
|
||||||
recv_diff = self.recv_usage_history[-1] - self.recv_usage_history[0]
|
recv_diff = self.recv_usage_history[-1] - self.recv_usage_history[0]
|
||||||
time_diff = self.history_times[-1] - self.history_times[0]
|
time_diff = self.history_times[-1] - self.history_times[0]
|
||||||
sent_rate = sent_diff / time_diff
|
sent_rate = sent_diff / time_diff
|
||||||
recv_rate = recv_diff / time_diff
|
recv_rate = recv_diff / time_diff
|
||||||
self.highest_sent_rate = max(self.highest_sent_rate, sent_rate)
|
self.highest_sent_rate = max(self.highest_sent_rate, sent_rate)
|
||||||
self.highest_recv_rate = max(self.highest_recv_rate, recv_rate)
|
self.highest_recv_rate = max(self.highest_recv_rate, recv_rate)
|
||||||
sent_percent = min(1.0, sent_rate / self.highest_sent_rate)
|
sent_percent = min(1.0, sent_rate / self.highest_sent_rate)
|
||||||
recv_percent = min(1.0, recv_rate / self.highest_recv_rate)
|
recv_percent = min(1.0, recv_rate / self.highest_recv_rate)
|
||||||
self.output_queue.put((sent_percent, recv_percent))
|
self.output_queue.put((sent_percent, recv_percent))
|
||||||
|
|
||||||
time.sleep(self.update_interval)
|
time.sleep(self.update_interval)
|
||||||
|
|
||||||
@ -98,20 +100,21 @@ class CPUMonitorThread(threading.Thread):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
cpu_usage = psutil.cpu_percent(percpu=True)
|
if not self.output_queue.full():
|
||||||
for i in range(self.cpu_count):
|
cpu_usage = psutil.cpu_percent(percpu=True)
|
||||||
useage = 2 * max(cpu_usage[2*i], cpu_usage[2*i+1]) # Combine logical cores
|
|
||||||
if useage > 100:
|
|
||||||
useage = 100
|
|
||||||
self.cpu_usage_history[i].append(useage / 100.0)
|
|
||||||
self.history_times.append(time.time())
|
|
||||||
if len(self.cpu_usage_history[0]) > self.max_history_size:
|
|
||||||
for i in range(self.cpu_count):
|
for i in range(self.cpu_count):
|
||||||
self.cpu_usage_history[i] = self.cpu_usage_history[i][-self.max_history_size:]
|
useage = 2 * max(cpu_usage[2*i], cpu_usage[2*i+1]) # Combine logical cores
|
||||||
self.history_times = self.history_times[-self.max_history_size:]
|
if useage > 100:
|
||||||
if len(self.cpu_usage_history[0]) == self.max_history_size:
|
useage = 100
|
||||||
cpu_percentages = [sum(core_history) / self.max_history_size for core_history in self.cpu_usage_history]
|
self.cpu_usage_history[i].append(useage / 100.0)
|
||||||
self.output_queue.put(cpu_percentages)
|
self.history_times.append(time.time())
|
||||||
|
if len(self.cpu_usage_history[0]) > self.max_history_size:
|
||||||
|
for i in range(self.cpu_count):
|
||||||
|
self.cpu_usage_history[i] = self.cpu_usage_history[i][-self.max_history_size:]
|
||||||
|
self.history_times = self.history_times[-self.max_history_size:]
|
||||||
|
if len(self.cpu_usage_history[0]) == self.max_history_size:
|
||||||
|
cpu_percentages = [sum(core_history) / self.max_history_size for core_history in self.cpu_usage_history]
|
||||||
|
self.output_queue.put(cpu_percentages)
|
||||||
time.sleep(self.update_interval)
|
time.sleep(self.update_interval)
|
||||||
|
|
||||||
class MemoryMonitorThread(threading.Thread):
|
class MemoryMonitorThread(threading.Thread):
|
||||||
@ -126,15 +129,16 @@ class MemoryMonitorThread(threading.Thread):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
memory_usage = psutil.virtual_memory().percent / 100.0
|
if not self.output_queue.full():
|
||||||
self.memory_usage_history.append(memory_usage)
|
memory_usage = psutil.virtual_memory().percent / 100.0
|
||||||
self.history_times.append(time.time())
|
self.memory_usage_history.append(memory_usage)
|
||||||
if len(self.memory_usage_history) > self.max_history_size:
|
self.history_times.append(time.time())
|
||||||
self.memory_usage_history = self.memory_usage_history[-self.max_history_size:]
|
if len(self.memory_usage_history) > self.max_history_size:
|
||||||
self.history_times = self.history_times[-self.max_history_size:]
|
self.memory_usage_history = self.memory_usage_history[-self.max_history_size:]
|
||||||
if len(self.memory_usage_history) == self.max_history_size:
|
self.history_times = self.history_times[-self.max_history_size:]
|
||||||
avg_memory_usage = sum(self.memory_usage_history) / self.max_history_size
|
if len(self.memory_usage_history) == self.max_history_size:
|
||||||
self.output_queue.put(avg_memory_usage)
|
avg_memory_usage = sum(self.memory_usage_history) / self.max_history_size
|
||||||
|
self.output_queue.put(avg_memory_usage)
|
||||||
time.sleep(self.update_interval)
|
time.sleep(self.update_interval)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user