From 2a810ce1ec78fea4d2fbe7eddbcc3c638f895663 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Wed, 16 Jun 2021 19:26:51 -0400 Subject: [PATCH] Add power button function --- system-status.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/system-status.py b/system-status.py index 50b17fa..6163a87 100644 --- a/system-status.py +++ b/system-status.py @@ -1 +1,46 @@ +from gpiozero import Button +from signal import pause +from subprocess import check_call +from time import sleep from unicornhatmini import UnicornHATMini + +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): + self.hat.set(0, 0, 0, 0, 50) + self.hat.show() + + def terminate(self): + self.hat.set_all(10, 0, 0) + self.hat.show() + sleep(0.25) + self.hat.set_all(0, 0, 0) + self.hat.show() + sleep(0.25) + self.hat.set_all(10, 0, 0) + self.hat.show() + + +def poweroff(systemStatus): + systemStatus.terminate() + sleep(1) + check_call(['sudo', 'poweroff']) + + +def main(): + systemStatus = SystemStatus(UnicornHATMini()) + powerButton = Button(POWER_BUTTON_GPIO_PIN, hold_time=HOLD_TIME_IN_SECONDS) + powerButton.when_held = poweroff(systemStatus) + powerButton.display() + pause() + + +if __name__ == '__main__': + main()