system-status/system-status.py

81 lines
1.9 KiB
Python

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
class SystemStatus:
def __init__(self, hat):
self.hat = hat
self.hat.set_brightness(0.1)
self.hat.set_rotation(90)
self.width, self.height = self.hat.get_shape()
self.cpuStartColumn = 0
self.cpuEndColumn = int(self.width / 2) - 1
self.tempStartColumn = int(self.width / 2) + 1
self.tempEndColumn = self.width - 1
def display(self):
for x in range(self.cpuStartColumn, self.cpuEndColumn):
for y in range(self.height):
self.hat.set_pixel(x, y, 255, 0, 127)
for x in range(self.tempStartColumn, self.tempEndColumn):
for y in range(self.height):
self.hat.set_pixel(x, y, 0, 127, 255)
self.hat.show()
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()