Skip to content

Commit 97ada82

Browse files
committed
Bug fixes
1 parent 52e990e commit 97ada82

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

examples/systemd/listen-for-shutdown.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Description=Listen for poweroff button
33

44
[Service]
55
Type=simple
6-
Restart=always
6+
Restart=on-failure
7+
RestartSec=5
78

89
# To define the state to listen for and the pin to listen on
910
# Use the "Environment" or "EnvironmentFile" options

listen-for-shutdown.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,39 @@
1010
direction="FALLING"
1111

1212
if len(sys.argv) > 1:
13-
pin=sys.argv[1]
13+
pin=int(sys.argv[1])
14+
print ("Pin {} from command line".format(str(pin)), flush=True)
1415
elif "ENV_GPIO" in os.environ:
15-
pin=os.getenv("ENV_GPIO")
16+
pin=int(os.getenv("ENV_GPIO"))
17+
print ("Pin {} from environment".format(str(pin)), flush=True)
18+
else:
19+
print ("Using default pin {}".format(str(pin)), flush=True)
1620

1721
if len(sys.argv) > 2:
1822
direction=sys.argv[2]
23+
print ("Detect state {} from command line".format(direction), flush=True)
1924
elif "ENV_STATE" in os.environ:
20-
direction=os.getenv("ENV_DIRECTION")
25+
direction=os.getenv("ENV_STATE")
26+
print ("Detect state {} environment".format(direction), flush=True)
27+
else:
28+
print ("Detect default state {}".format(direction), flush=True)
2129

22-
print ("Waiting for {} on GPIO pin {}".format(direction,str(pin)))
2330

2431
GPIO.setmode(GPIO.BCM)
2532

2633
if direction=="RISING":
34+
print ("Waiting for RISING on GPIO pin {}".format(str(pin)), flush=True)
2735
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
2836
GPIO.wait_for_edge(pin, GPIO.RISING)
37+
print ("Detected state RISING on pin {}".format(str(pin)), flush=True)
2938
else:
39+
print ("Waiting for FALLING on GPIO pin {}".format(str(pin)), flush=True)
3040
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
3141
GPIO.wait_for_edge(pin, GPIO.FALLING)
42+
print ("Detected state FALLING on pin {}".format(str(pin)), flush=True)
3243

33-
print ("GPIO state detected. Shuting down")
44+
print ("GPIO state detected. Shuting down", flush=True)
3445

35-
subprocess.call(['shutdown', '-p', 'now'], shell=False)
46+
subprocess.call(['shutdown', '-h', 'now'], shell=False)
3647

48+
sys.exit(0)

0 commit comments

Comments
 (0)