Created
February 2, 2024 20:19
-
-
Save Kaurin/47012e530c85f61f7c53f2b444e8760b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Sense Hat CPU LED Matrix monitor. | |
Author: M. Manojlovic (synchromatik@gmail.com) | |
This script uses the Sense HAT and displays CPU load information on the LED matrix. | |
""" | |
from sense_hat import SenseHat | |
from scipy.interpolate import interp1d | |
import time | |
import psutil | |
import numpy as np | |
from itertools import chain | |
sense = SenseHat() | |
def constructArray(cpuLoad): | |
""" | |
Constructs an array representing the LED matrix configuration based on CPU load. | |
Parameters: | |
- cpuLoad (float): The CPU load value (percentage). | |
Returns: | |
- np.ndarray: An array representing the LED matrix configuration. | |
""" | |
my_array_initial = np.arange(start=1, stop=cpuLoad+1) | |
if my_array_initial.size == 0: | |
my_array_final = np.zeros(8) | |
else: | |
diff = 8 - my_array_initial.size | |
my_array_final = np.append(my_array_initial, [[0]*diff]) | |
return my_array_final | |
def arrayToCollor(arr): | |
""" | |
Converts an array of numbers to a list of color names. | |
Parameters: | |
- arr (list): List of numbers. | |
Returns: | |
- list: List of color names corresponding to the input numbers. | |
""" | |
colors = [chr(number + 96) for number in arr] | |
colors2 = list(map(lambda x: x.replace('`', 'none'), colors)) | |
return colors2 | |
colors = { | |
"a" : (60, 180, 75), # Green | |
"b" : (60, 180, 75), # Green | |
"c" : (255, 255, 0), # Yellow | |
"d" : (255, 255, 0), # Yellow | |
"e" : (255, 140, 0), # Orange | |
"f" : (255, 140, 0), # Orange | |
"g" : (255, 0, 0), # Red | |
"h" : (255, 0, 0), # Red | |
"none" : (0, 0, 0) # Black (None) | |
} | |
load = [] | |
loadfinal = [] | |
print('Script running, check LEDs') | |
while True: | |
percorelinear = interp1d([0, 100], [0, 8])(psutil.cpu_percent(percpu=True)).round(0).tolist() | |
output = [constructArray(i) for i in percorelinear] | |
for e in output: | |
load.insert(0, arrayToCollor(list(map(int, e))) * 2) | |
load = list(chain.from_iterable(load)) | |
for e in load: | |
loadfinal.insert(0, colors[e]) | |
sense.set_pixels(loadfinal) | |
time.sleep(1) | |
load.clear() | |
loadfinal.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment