Add power button function

This commit is contained in:
Mike Cifelli 2021-06-16 19:26:51 -04:00
parent 4e7a1178d9
commit 2a810ce1ec
1 changed files with 45 additions and 0 deletions

View File

@ -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()