-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_control.py
169 lines (133 loc) · 4.34 KB
/
test_control.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
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
import threading
import RPi.GPIO as GPIO
#from GPIOEmulator.EmulatorGUI import GPIO
import time
import requests
import json
import logging
API_URL = 'http://localhost:8088'
#API_URL = 'http://192.168.1.1'
class Led(object):
def __init__(self, gpio):
self.gpio = gpio
self._stop = None
self.blinking = False
def blink(self):
if self.blinking:
return
self.blinking = True
self._stop = threading.Event()
threading.Thread(target=self._blink).start()
def _blink(self):
out = True
while not self._stop.is_set():
GPIO.output(self.gpio, int(out))
out = not out
self._stop.wait(0.3)
def _blink_stop(self):
self.blinking = False
if self._stop:
self._stop.set()
def set(self, on):
if on: self.on()
else: self.off()
def on(self):
self._blink_stop()
GPIO.output(self.gpio, 1)
def off(self):
self._blink_stop()
GPIO.output(self.gpio, 0)
BTN_KEY = 23
LED_ON_GPIO = 7
LED_ON = Led(LED_ON_GPIO)
BTN_DEMO = 10
LED_DEMO_GPIO = 25
LED_DEMO = Led(LED_DEMO_GPIO)
BTN_CALL = 17
BTN_START = 27
LED_START_GPIO = 8
LED_START = Led(LED_START_GPIO)
BTN_STOP = 22
# setup GPIO for remote
GPIO.setmode(GPIO.BCM)
GPIO.setup(BTN_KEY, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_DEMO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_CALL, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_START, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_STOP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED_ON_GPIO, GPIO.OUT)
GPIO.setup(LED_DEMO_GPIO, GPIO.OUT)
GPIO.setup(LED_START_GPIO, GPIO.OUT)
GPIO.output(LED_ON_GPIO, 0)
GPIO.output(LED_DEMO_GPIO, 0)
GPIO.output(LED_START_GPIO, 0)
logger = logging.getLogger("Gestion'air Remote")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("remote.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Starting the Gestion'air Remote daemon...")
def effect_start():
for i in range(1,6):
GPIO.output(LED_START_GPIO, 0)
GPIO.output(LED_ON_GPIO, 1)
time.sleep(0.2)
GPIO.output(LED_ON_GPIO, 0)
GPIO.output(LED_DEMO_GPIO, 1)
time.sleep(0.2)
GPIO.output(LED_DEMO_GPIO, 0)
GPIO.output(LED_START_GPIO, 1)
time.sleep(0.2)
GPIO.output(LED_START_GPIO, 0)
GPIO.output(LED_ON_GPIO, 1)
def key_event(channel):
# ON: Should trigger led test and then get into normal operation mode
if GPIO.input(BTN_KEY) == 0:
effect_start()
LED_ON.on()
else:
# OFF: QUESTION: ?? lock dashboard or stop sim?
LED_ON.off()
LED_DEMO.off()
GPIO.add_event_detect(BTN_KEY, GPIO.BOTH, callback=key_event)
def start_event(channel):
requests.get(API_URL + '/game/start')
LED_START.blink()
GPIO.add_event_detect(BTN_START, GPIO.FALLING, callback=start_event, bouncetime=500)
def stop_event(channel):
requests.get(API_URL + '/game/stop')
LED_START.blink()
GPIO.add_event_detect(BTN_STOP, GPIO.FALLING, callback=stop_event, bouncetime=500)
def call_event(channel):
requests.get(API_URL + '/game/api/play_sound/call')
GPIO.add_event_detect(BTN_CALL, GPIO.FALLING, callback=call_event, bouncetime=500)
def demo_event(channel):
requests.get(API_URL + '/game/api/call/1201')
LED_DEMO.blink()
GPIO.add_event_detect(BTN_DEMO, GPIO.FALLING, callback=demo_event, bouncetime=500)
try:
while True:
if GPIO.input(BTN_KEY) == 0:
# check sim status for leds
try:
res = requests.get(API_URL + '/game/api/status').json()
LED_ON.on()
except:
res = {
'isRunning': False,
'demoState': 'ERROR',
}
LED_ON.blink()
## led_on ??
LED_START.set(res['isRunning'])
# on indicate ready for demo, blinking during ringing, off during answer
if res['demoState'] == 'FREE':
LED_DEMO.on()
elif res['demoState'] != 'RINGING':
LED_DEMO.off()
# some sleep
time.sleep(1)
finally:
logger.info("Terminating the Gestion'air Remote daemon...")
GPIO.cleanup()