-
Notifications
You must be signed in to change notification settings - Fork 0
/
poweroffbutton.py
49 lines (35 loc) · 1.14 KB
/
poweroffbutton.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""poweroffbutton.py will start poweroff routine on pull-down event on a GPIO Pin
Configure BUTTON_PIN and LED_PIN variables to valid GPIO labeled number pins.
LED pin configuration is Optional
Requires: gpiozero
Regarding sudo on poweoff() function
sudo command on poweroff() is optional
It is not required if run through a systemd service script controlled by root
or if this script is already run with sudo.
`sudo poweroff` is used so this script could be run without superuser permissions
by an user account which has /sbin/poweroff allowed to use without password on a
sudoers.d configuration file.
"""
from signal import pause
from os import system
import atexit
from gpiozero import Button, LED
BUTTON_PIN = 19 # GPIO19, BOARD PIN 35
LED_PIN = 26 # GPIO26, BOARD PIN 37
led = LED(LED_PIN)
button = Button(
BUTTON_PIN,
pull_up=True,
bounce_time=0.5)
def poweroff():
print("Poweroff pressed")
led.on()
system('logger "Poweroff button pressed"')
system('sudo poweroff')
pause()
def powerdown_led():
led.off()
powerdown_led()
atexit.register(powerdown_led)
button.when_pressed = poweroff
pause()