Tweak margins
[uberlogger] / nmea.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2010 Dmitry Marakasov
4 #
5 # This file is part of UberLogger.
6 #
7 # UberLogger is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # UberLogger is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with UberLogger.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import re
22
23 class GPSData:
24         def __init__(self):
25                 self.reset()
26
27         def reset(self):
28                 self.time = None
29                 self.lat = None
30                 self.lon = None
31                 self.quality = None
32                 self.nsat = None
33                 self.hdop = None
34                 self.ele = None
35
36         def parse_nmea_string(self, string):
37                 try:
38                         string = string.lstrip('$')
39                         (data, dummy, csum) = string.rpartition('*')
40
41                         mycsum = 0
42                         for byte in data:
43                                 mycsum ^= ord(byte)
44
45                         if mycsum != int(csum, 16): # can throw with invalid csum
46                                 return
47
48                         data = data.split(',')
49
50                         if data[0] == 'GPGGA':
51                                 self.reset()
52
53                                 # time
54                                 (temp, n) = re.subn('^(\d{2})(\d{2})(\d{2}\.\d+)$', '\\1:\\2:\\3', data[1])
55                                 if n == 1:
56                                         self.time = temp
57
58                                 # latitude
59                                 (temp, n) = re.subn('^(\d{2})(\d{2})\.(.*)$', '\\1.\\2\\3', data[2])
60                                 if n == 1:
61                                         self.lat = temp + data[3]
62
63                                 # longitude
64                                 (temp, n) = re.subn('^0?(\d{2,3}?)(\d{2})\.(.*)$', '\\1.\\2\\3', data[4])
65                                 if n == 1:
66                                         self.lon = temp + data[5]
67
68                                 # quality
69                                 if int(data[6]) == 0:
70                                         self.quality = "none"
71                                 elif int(data[6]) == 1:
72                                         self.quality = "normal"
73                                 elif int(data[6]) == 2:
74                                         self.quality = "diff"
75                                 elif int(data[6]) == 3:
76                                         self.quality = "precision"
77                                 else:
78                                         self.quality = None
79
80                                 # other params
81                                 self.nsat = int(data[7])
82                                 self.hdop = data[8]
83                                 self.ele = data[9]
84                 except:
85                         return
86
87         def dump(self):
88                 return "Time: %s\tQuality: %s\nLat: %s\tSat: %s\nLon: %s\tHDOP: %s\nEle: %s" % (self.time, self.quality, self.lat, self.nsat, self.lon, self.hdop, self.ele)