From 086a0afe0b87ac37b7a5424353f77b19e82c22e7 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Wed, 16 Jun 2021 19:36:25 -0400 Subject: [PATCH] Don't immediately poweroff --- system-status.py | 52 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/system-status.py b/system-status.py index 6163a87..5f0d267 100644 --- a/system-status.py +++ b/system-status.py @@ -1,3 +1,6 @@ +import sys +import signal + from gpiozero import Button from signal import pause from subprocess import check_call @@ -17,28 +20,59 @@ class SystemStatus: self.hat.set(0, 0, 0, 0, 50) self.hat.show() - def terminate(self): - self.hat.set_all(10, 0, 0) + def exit(self): + self.hat.set_all(0, 0, 50) 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.set_all(0, 0, 50) + self.hat.show() + sleep(0.25) + self.hat.set_all(0, 0, 0) + self.hat.show() + + def terminate(self): + 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) + self.hat.set_all(50, 0, 0) + self.hat.show() + sleep(0.25) + self.hat.set_all(0, 0, 0) self.hat.show() -def poweroff(systemStatus): - systemStatus.terminate() - sleep(1) - check_call(['sudo', 'poweroff']) +def cleanExit(systemStatus, button): + def exit(signum, frame): + button.close() + systemStatus.exit() + sys.exit(0) + + return exit + + +def poweroff(systemStatus, button): + def terminate(): + button.close() + systemStatus.terminate() + check_call(['sudo', 'poweroff']) + + return terminate def main(): systemStatus = SystemStatus(UnicornHATMini()) powerButton = Button(POWER_BUTTON_GPIO_PIN, hold_time=HOLD_TIME_IN_SECONDS) - powerButton.when_held = poweroff(systemStatus) - powerButton.display() + signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton)) + + powerButton.when_held = poweroff(systemStatus, powerButton) + systemStatus.display() + pause()