-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
214 lines (175 loc) · 5.36 KB
/
app.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
import RPi.GPIO as GPIO
from serial import Serial
import time
import threading
import os
import sys
# BUTTON
BUTTON_GPIO_PIN = 4
SHORT_PRESS_TICKS = 5
LONG_PRESS_TICKS = 200
TICK_TIME = 0.01
DOWN = 0
# LED
LED_GPIO_PIN = 18
SLOW_FLASH_TIMES = [1,1]
FAST_FLASH_TIMES = [0.2,0.2]
# Global Navigation Satellite System (GNSS): GPS, GLONASS, Galileo, ...
BAUDRATE = 9600
PORT = '/dev/ttyAMA0'
class ButtonControl(threading.Thread):
class ButtonPressStates():
NOTPRESSED = 0
SHORTPRESS = 1
LONGPRESS = 2
def __init__(self, gpio_pin):
threading.Thread.__init__(self)
self.gpio_pin = gpio_pin
self.__current_state = self.ButtonPressStates.NOTPRESSED
self.shortPressTicks = SHORT_PRESS_TICKS
self.longPressTicks = LONG_PRESS_TICKS
self.tickTime = TICK_TIME
GPIO.setup(self.gpio_pin, GPIO.IN)
def get(self):
return GPIO.input(self.gpio_pin)
def is_pressed(self):
buttonPressed = False
if GPIO.input(self.gpio_pin) == DOWN:
buttonPressed = True
return buttonPressed
def run(self):
self.__running = True
self.__current_state = self.ButtonPressStates.NOTPRESSED
while self.is_pressed():
time.sleep(self.tickTime)
while self.__running:
while self.is_pressed() == False and self.__running:
time.sleep(self.tickTime)
ticks = 0
while self.is_pressed() == True and self.__running:
ticks += 1
time.sleep(self.tickTime)
if ticks > self.shortPressTicks and ticks < self.longPressTicks:
self.__current_state = self.ButtonPressStates.SHORTPRESS
if ticks >= self.longPressTicks:
self.__current_state = self.ButtonPressStates.LONGPRESS
time.sleep(0.5)
def get_state(self):
return self.__current_state
def set_not_pressed(self):
self.__current_state = self.ButtonPressStates.NOTPRESSED
def stopController(self):
self.__running = False
class GnssControl(threading.Thread):
class GnssStates():
STOP = 0
PAUSE = 1
RECORD = 2
def __init__(self):
threading.Thread.__init__(self)
self.__running = False
self.__current_state = self.GnssStates.STOP
self.serialGnss = Serial()
self.serialGnss.baudrate = BAUDRATE
self.serialGnss.port = PORT
self.serialGnss.timeout = 4
self.serialGnss.open()
self.fileDescriptor = open('/home/pi/track-%s.nmea' %time.strftime('%Y%m%d%H%M%S'), 'a')
def set_stopped(self):
self.__current_state = self.GnssStates.STOP
self.__running = False
self.fileDescriptor.close()
def set_paused(self):
self.__current_state = self.GnssStates.PAUSE
def run(self):
self.__running = True
while self.__running:
sentence = self.serialGnss.readline()
sentenceStr = str(sentence)
if self.__current_state == self.GnssStates.RECORD:
if(sentence.find('$GP') > 0):
self.fileDescriptor.write('{0:}'.format(sentenceStr))
def set_recording(self):
self.__current_state = self.GnssStates.RECORD
def get_state(self):
return self.__current_state
class LedControl():
class LedStates():
OFF = 0
ON = 1
SLOW_FLASH = 2
FAST_FLASH = 3
def __init__(self, gpio_pin):
self.__current_state = self.LedStates.OFF
self.__current_led_state = False
self.gpio_pin = gpio_pin
GPIO.setup(self.gpio_pin, GPIO.OUT)
self.__set_off()
def __set_off(self):
self.__current_led_state = False
GPIO.output(self.gpio_pin, False)
def __set_on(self):
self.__current_led_state = True
GPIO.output(self.gpio_pin, True)
def __flash(self, time_on, time_off):
while ((self.get_state() == self.LedStates.SLOW_FLASH) or (self.get_state() == self.LedStates.FAST_FLASH)) :
if self.__current_led_state == True:
self.__set_off()
time.sleep(time_off)
else:
self.__set_on()
time.sleep(time_on)
def set_on(self):
self.__current_state = self.LedStates.ON
self.__set_on()
def set_off(self):
self.__current_state = self.LedStates.OFF
self.__set_off()
def set_slow_flash(self):
self.__current_state = self.LedStates.SLOW_FLASH
self.__time_on = SLOW_FLASH_TIMES[0]
self.__time_off = SLOW_FLASH_TIMES[1]
self.__flashthread = threading.Thread(target=self.__flash, args=(self.__time_on, self.__time_off))
self.__flashthread.start()
def set_fast_flash(self):
self.__current_state = self.LedStates.FAST_FLASH
self.__time_on = FAST_FLASH_TIMES[0]
self.__time_off = FAST_FLASH_TIMES[1]
self.__flashthread = threading.Thread(target=self.__flash, args=(self.__time_on, self.__time_off))
self.__flashthread.start()
def get_state(self):
return self.__current_state
if __name__ == "__main__":
try:
GPIO.setmode(GPIO.BCM)
button = ButtonControl(BUTTON_GPIO_PIN)
button.start()
gnss = GnssControl()
gnss.start()
led = LedControl(LED_GPIO_PIN)
led.set_fast_flash()
while(button.get_state() != button.ButtonPressStates.LONGPRESS):
if (button.get_state() == button.ButtonPressStates.SHORTPRESS):
if(gnss.get_state() == gnss.GnssStates.STOP):
led.set_on()
gnss.set_recording()
elif (gnss.get_state() == gnss.GnssStates.RECORD):
led.set_slow_flash()
gnss.set_paused()
elif (gnss.get_state() == gnss.GnssStates.PAUSE):
led.set_on()
gnss.set_recording()
button.set_not_pressed()
except KeyboardInterrupt:
print "User Cancelled (Ctrl C)"
except:
print "Unexpected error - ", sys.exc_info()[0], sys.exc_info()[1]
raise
finally:
button.stopController()
button.join()
led.set_off()
gnss.set_stopped()
gnss.join()
GPIO.cleanup()