forked from monash-human-power/raspicam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
72 lines (49 loc) · 1.37 KB
/
switch.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import argparse
import asyncio
import subprocess
import sys
import config
from hardware.hal import get_hal, cleanup
switch_on_state = True
configs = config.read_configs()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--host",
type=str,
default=configs["broker_ip"],
help="ip address of the broker",
)
args = parser.parse_args()
brokerIP = args.host
hal = get_hal(configs["bike"])
overlay_process = None
def enable():
print("ON")
hal.display_power_led.turn_off()
global overlay_process
overlay_process = subprocess.Popen(
[sys.executable, config.get_active_overlay(), "--host", brokerIP]
)
def disable():
print("OFF")
hal.display_power_led.turn_on()
global overlay_process
if overlay_process:
subprocess.Popen.kill(overlay_process)
async def main():
try:
if hal.display_power_switch.read() == switch_on_state:
enable()
while True:
await hal.display_power_switch.wait_for_state(not switch_on_state)
disable()
await hal.display_power_switch.wait_for_state(switch_on_state)
enable()
except KeyboardInterrupt:
if overlay_process:
subprocess.Popen.kill(overlay_process)
cleanup()
if __name__ == "__main__":
asyncio.run(main())