system-status/system-status.py

81 lines
1.9 KiB
Python
Raw Normal View History

2021-06-16 19:36:25 -04:00
import sys
import signal
2021-06-16 19:26:51 -04:00
from gpiozero import Button
from signal import pause
from subprocess import check_call
from time import sleep
2021-06-17 09:09:56 -04:00
from unicornhatmini import UnicornHATMini, BUTTON_A
POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1
2021-06-16 19:26:51 -04:00
class SystemStatus:
def __init__(self, hat):
self.hat = hat
self.hat.set_brightness(0.1)
2021-06-17 09:09:56 -04:00
self.hat.set_rotation(90)
self.width, self.height = self.hat.get_shape()
self.cpuStartColumn = 0
2021-06-17 09:11:22 -04:00
self.cpuEndColumn = int(self.width / 2) - 1
self.tempStartColumn = int(self.width / 2) + 1
2021-06-17 09:09:56 -04:00
self.tempEndColumn = self.width - 1
2021-06-16 19:26:51 -04:00
def display(self):
2021-06-17 09:09:56 -04:00
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)
2021-06-16 19:26:51 -04:00
self.hat.show()
2021-06-16 19:36:25 -04:00
def exit(self):
self.hat.set_all(0, 0, 0)
self.hat.show()
2021-06-16 19:26:51 -04:00
def terminate(self):
2021-06-16 20:06:13 -04:00
for _ in range(3):
2021-06-17 09:09:56 -04:00
self.hat.set_all(0, 0, 0)
2021-06-16 20:06:13 -04:00
self.hat.show()
sleep(0.25)
2021-06-17 09:09:56 -04:00
self.hat.set_all(255, 127, 0)
2021-06-16 20:06:13 -04:00
self.hat.show()
sleep(0.25)
2021-06-16 19:36:25 -04:00
2021-06-16 19:26:51 -04:00
2021-06-16 19:36:25 -04:00
def cleanExit(systemStatus, button):
def exit(signum, frame):
button.close()
systemStatus.exit()
sys.exit(0)
2021-06-16 19:26:51 -04:00
2021-06-16 19:36:25 -04:00
return exit
2021-06-17 09:09:56 -04:00
def poweroff(systemStatus):
2021-06-16 19:36:25 -04:00
def terminate():
systemStatus.terminate()
check_call(['sudo', 'poweroff'])
return terminate
2021-06-16 19:26:51 -04:00
def main():
systemStatus = SystemStatus(UnicornHATMini())
2021-06-17 09:09:56 -04:00
powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS)
2021-06-16 19:36:25 -04:00
signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton))
2021-06-17 09:09:56 -04:00
powerButton.when_held = poweroff(systemStatus)
2021-06-16 19:36:25 -04:00
2021-06-17 09:09:56 -04:00
while True:
systemStatus.display()
sleep(2)
2021-06-16 19:26:51 -04:00
if __name__ == '__main__':
main()