-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbutton.py
More file actions
92 lines (68 loc) · 2.73 KB
/
Copy pathbutton.py
File metadata and controls
92 lines (68 loc) · 2.73 KB
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
#!/usr/bin/env python3
from gpiozero import Button, LED
from signal import pause
import os, sys
# Define the location of the script to restore default configuration
defaultsscriptlocation = "/etc/bikecamera/hostapd/reset.sh"
scriptlocation = sys.argv[1] if len(sys.argv) >= 2 else defaultsscriptlocation
buttonGPIO = 21 # Pushbutton is connected to GPIO 21 (pin 40)
ledGPIO = 20 # LED is connected to GPIO 20 (pin 38)
restarttime = 1 # restart if held for greater than restarttime seconds
offtime = 6 # shut down if held for greater than offtime seconds
defaultstime = 15 # reset BikeCamera to default config if held for greater than defaultstime seconds
restartready = False # Flag for reset time exceeded
shutdownready = False # Flag for shutdown time exceeded
defaultsready = False # Flag for defaults reset flag exceeded
def when_held():
global restartready
global shutdownready
global defaultsready
# find how long the button has been held
held_time = button.held_time + restarttime
# blink rate will increase the longer we hold
# Set flags for the action needed when the button is released
if held_time > restarttime and held_time < offtime and not restartready:
led.blink(on_time=0.5, off_time=0.5)
restartready = True
shutdownready = False
defaultsready = False
print ("Restart time reached")
if held_time > offtime and held_time < defaultstime and not shutdownready:
led.blink(on_time=0.25, off_time=0.25)
restartready = False
shutdownready = True
defaultsready = False
print ("Shutdown time reached")
if held_time > defaultstime and not defaultsready:
led.blink(on_time=0.1, off_time=0.1)
restartready = False
shutdownready = False
defaultsready = True
print ("Restore defaults time reached")
def when_released():
global restartready
global shutdownready
global defaultsready
led.on()
if restartready:
print ("System restarting")
os.system("sudo reboot")
if shutdownready:
print ("System powering down")
os.system("sudo poweroff")
if defaultsready:
print ("System restoring BikeCamera defaults")
os.system("sudo bash " + scriptlocation)
os.system("sudo reboot")
# Clear flags if the button was released early
print ("Button released before any action was needed")
restartready = False
shutdownready = False
defaultsready = False
led = LED(ledGPIO)
led.on()
button = Button(buttonGPIO, hold_time=restarttime, hold_repeat=True)
button.when_held = when_held
button.when_released = when_released
print ("Waiting for a button press")
pause()