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 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_pixel(0, 0, 0, 0, 50) self.hat.show() 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(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 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) signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton)) powerButton.when_held = poweroff(systemStatus, powerButton) systemStatus.display() pause() if __name__ == '__main__': main()