-
Notifications
You must be signed in to change notification settings - Fork 15
/
pigrrl-shutdown
169 lines (128 loc) · 3.74 KB
/
pigrrl-shutdown
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# Adafruit PiGRRL PCB alternative configuration
# maps buttons to GPIO for connection to PiTFT
# or mounting back to back to a Raspberry Pi Zero
# soft shutdown is supported through L bumper
import time
import signal
import os
import sys
from datetime import datetime
from threading import Timer
try:
from evdev import uinput, UInput, ecodes as e
except ImportError:
exit("This library requires the evdev module\nInstall with: sudo pip install evdev")
try:
import RPi.GPIO as gpio
except ImportError:
exit("This library requires the RPi.GPIO module\nInstall with: sudo pip install RPi.GPIO")
#time.sleep(30)
os.system("sudo modprobe uinput")
DEBUG = False
PIDFILE = "/var/run/picadehatd.pid"
LOGFILE = "/var/log/picadehatd.log"
BOUNCE_TIME = 0.01 # Debounce time in seconds
SHUTDOWN_HOLD_TIME = 3 # Time in seconds that power button must be held
SHUTDOWN = 4
BUTTON_POWER = 12
UP = 16
DOWN = 26
LEFT = 4
RIGHT = 19
BUTTON1 = 14
BUTTON2 = 15
BUTTON3 = 20
BUTTON4 = 18
ENTER = 5
START= 6
BUTTONS = [ENTER,START,BUTTON1,BUTTON2,BUTTON3,BUTTON4,UP,DOWN,LEFT,RIGHT]
KEYS = {
ENTER: e.KEY_ENTER,
START: e.KEY_S,
UP: e.KEY_UP,
DOWN: e.KEY_DOWN,
LEFT: e.KEY_LEFT,
RIGHT: e.KEY_RIGHT,
BUTTON1: e.KEY_LEFTCTRL,
BUTTON2: e.KEY_LEFTALT,
BUTTON3: e.KEY_SPACE,
BUTTON4: e.KEY_LEFTSHIFT,
}
states = [False for b in BUTTONS]
shutdown_timer = None
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
print("Fork #1 failed: {} ({})".format(e.errno, e.strerror))
sys.exit(1)
os.chdir("/")
os.setsid()
os.umask(0)
try:
pid = os.fork()
if pid > 0:
fpid = open(PIDFILE, 'w')
fpid.write(str(pid))
fpid.close()
sys.exit(0)
except OSError, e:
print("Fork #2 failed: {} ({})".format(e.errno, e.strerror))
sys.exit(1)
si = file("/dev/null", 'r')
so = file(LOGFILE, 'a+')
se = file("/dev/null", 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(BUTTONS, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.setup(BUTTON_POWER, gpio.IN, pull_up_down=gpio.PUD_UP)
try:
ui = UInput({e.EV_KEY: KEYS.values()}, name="PiGRRL-PCB", bustype=e.BUS_USB)
except uinput.UInputError as e:
sys.stdout.write(e.message)
sys.stdout.write("Have you tried running as root? sudo {}".format(sys.argv[0]))
sys.exit(0)
def log(msg):
sys.stdout.write(str(datetime.now()))
sys.stdout.write(": ")
sys.stdout.write(msg)
sys.stdout.write("\n")
sys.stdout.flush()
def handle_button(pin):
key = KEYS[pin]
time.sleep(BOUNCE_TIME)
state = 0 if gpio.input(pin) else 1
ui.write(e.EV_KEY, key, state)
ui.syn()
if DEBUG:
log("Pin: {}, KeyCode: {}, Event: {}".format(pin, key, 'press' if state else 'release'))
def perform_shutdown():
log("Shutting down...")
os.system("sudo shutdown -h now")
def handle_shutdown(pin):
global shutdown_timer
time.sleep(BOUNCE_TIME)
state = 0 if gpio.input(pin) else 1
if DEBUG:
log("Power button: {}".format('pressed' if state else 'released'))
if state and shutdown_timer is None:
shutdown_timer = Timer(SHUTDOWN_HOLD_TIME, perform_shutdown)
shutdown_timer.start()
elif not state and shutdown_timer is not None:
shutdown_timer.cancel()
shutdown_timer = None
for button in BUTTONS:
gpio.add_event_detect(button, gpio.BOTH, callback=handle_button, bouncetime=1)
gpio.add_event_detect(BUTTON_POWER, gpio.BOTH, callback=handle_shutdown, bouncetime=1)
log("Picade HAT daemon running...")
try:
signal.pause()
except KeyboardInterrupt:
pass
log("daemon shutting down...")
ui.close()