forked from pi-desktop/deb-make
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpd-powerkey.py
executable file
·34 lines (27 loc) · 970 Bytes
/
pd-powerkey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
#
# pd-powerkey.py - monitor GPIO to detect power key press from Power MCU (PCU)
#
import RPi.GPIO as GPIO
import time,os,sys
print("pidesktop: power button service initializing")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31,GPIO.OUT) # Pi to PCU - start/stop shutdown timer
GPIO.setup(33,GPIO.IN) # PCU to Pi - detect power key pressed
GPIO.output(31,GPIO.LOW) # tell PCU we are alive
GPIO.output(31,GPIO.HIGH) # cause blink by starting shutdown timer
time.sleep(0.5)
GPIO.output(31,GPIO.LOW) # clear timer we really are alive
# callback function
def powerkey_pressed(channels):
print("pidesktop: power button press detected, initiating shutdown")
os.system("sync")
os.system("shutdown -h now")
sys.exit()
# wait for power key press
print("pidesktop: power button monitor enabled")
GPIO.add_event_detect(33,GPIO.RISING,callback=powerkey_pressed)
# idle - TODO: use wait
while True:
time.sleep(10)