113 lines
2.8 KiB
Python
113 lines
2.8 KiB
Python
import psutil
|
|
import sys
|
|
import signal
|
|
|
|
from gpiozero import Button
|
|
from signal import pause
|
|
from subprocess import check_call
|
|
from time import sleep
|
|
from unicornhatmini import UnicornHATMini, BUTTON_A
|
|
|
|
POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1
|
|
TEMP_COLOR = [64, 0, 0]
|
|
CPU_COLOR = [0, 16, 32]
|
|
MAX_TEMP = 80
|
|
MIN_TEMP = 40
|
|
BLACK = [0, 0, 0]
|
|
|
|
|
|
class SystemStatus:
|
|
def __init__(self, hat):
|
|
self.hat = hat
|
|
self.hat.set_brightness(0.1)
|
|
self.hat.set_rotation(270)
|
|
self.width, self.height = self.hat.get_shape()
|
|
self.tempStartColumn = 0
|
|
self.tempEndColumn = int(self.width / 2)
|
|
self.cpuStartColumn = int(self.width / 2)
|
|
self.cpuEndColumn = self.width - 1
|
|
|
|
def display(self):
|
|
self.displayTemp()
|
|
self.displayCpu()
|
|
self.hat.show()
|
|
|
|
def displayTemp(self):
|
|
self.displayField(
|
|
start=self.tempStartColumn,
|
|
end=self.tempEndColumn,
|
|
value=self.getTempValue(),
|
|
color=TEMP_COLOR
|
|
)
|
|
|
|
def displayCpu(self):
|
|
self.displayField(
|
|
start=self.cpuStartColumn,
|
|
end=self.cpuEndColumn,
|
|
value=self.getCpuValue(),
|
|
color=CPU_COLOR
|
|
)
|
|
|
|
def displayField(self, start, end, value, color):
|
|
for x in range(start, end):
|
|
for y in range(value):
|
|
self.hat.set_pixel(x, y, *color)
|
|
for y in range(value, self.height):
|
|
self.hat.set_pixel(x, y, BLACK)
|
|
|
|
def getTempValue(self):
|
|
temp = psutil.sensors_temperatures()['cpu_thermal'][0].current
|
|
adjusted = max(0, temp - MIN_TEMP)
|
|
percent = min(1, adjusted / (MAX_TEMP - MIN_TEMP))
|
|
|
|
return int(self.height * percent)
|
|
|
|
def getCpuValue(self):
|
|
return int(self.height * (psutil.cpu_percent() / 100))
|
|
|
|
def exit(self):
|
|
self.hat.set_all(0, 0, 0)
|
|
self.hat.show()
|
|
|
|
def terminate(self):
|
|
for _ in range(3):
|
|
self.hat.set_all(0, 0, 0)
|
|
self.hat.show()
|
|
sleep(0.25)
|
|
self.hat.set_all(255, 127, 0)
|
|
self.hat.show()
|
|
sleep(0.25)
|
|
|
|
|
|
def cleanExit(systemStatus, button):
|
|
def exit(signum, frame):
|
|
button.close()
|
|
systemStatus.exit()
|
|
sys.exit(0)
|
|
|
|
return exit
|
|
|
|
|
|
def poweroff(systemStatus):
|
|
def terminate():
|
|
systemStatus.terminate()
|
|
check_call(['sudo', 'poweroff'])
|
|
|
|
return terminate
|
|
|
|
|
|
def main():
|
|
systemStatus = SystemStatus(UnicornHATMini())
|
|
powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS)
|
|
signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton))
|
|
|
|
powerButton.when_held = poweroff(systemStatus)
|
|
|
|
while True:
|
|
systemStatus.display()
|
|
sleep(2)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|