system-status/system-status.py

70 lines
1.5 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-16 16:59:16 -04:00
from unicornhatmini import UnicornHATMini
2021-06-16 19:26:51 -04:00
POWER_BUTTON_GPIO_PIN = 5
HOLD_TIME_IN_SECONDS = 1
class SystemStatus:
def __init__(self, hat):
self.hat = hat
self.hat.set_brightness(0.1)
def display(self):
2021-06-16 19:49:22 -04:00
self.hat.set_pixel(0, 0, 0, 0, 50)
2021-06-16 19:26:51 -04:00
self.hat.show()
2021-06-16 19:36:25 -04:00
def exit(self):
2021-06-16 20:06:13 -04:00
self.hat.set_all(50, 0, 0)
2021-06-16 19:36:25 -04:00
self.hat.show()
2021-06-16 20:06:13 -04:00
sleep(0.50)
2021-06-16 19:36:25 -04:00
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):
self.hat.set_all(50, 0, 0)
self.hat.show()
sleep(0.25)
self.hat.set_all(0, 0, 0)
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
def poweroff(systemStatus, button):
def terminate():
systemStatus.terminate()
check_call(['sudo', 'poweroff'])
return terminate
2021-06-16 19:26:51 -04:00
def main():
systemStatus = SystemStatus(UnicornHATMini())
powerButton = Button(POWER_BUTTON_GPIO_PIN, hold_time=HOLD_TIME_IN_SECONDS)
2021-06-16 19:36:25 -04:00
signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton))
powerButton.when_held = poweroff(systemStatus, powerButton)
systemStatus.display()
2021-06-16 19:26:51 -04:00
pause()
if __name__ == '__main__':
main()