Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / gpsfix.py
1 #!/usr/bin/env python
2
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 """
7 GPSTest is a simple example using the SerialPort transport and the NMEA 0183
8 and Rockwell Zodiac GPS protocols to display fix data as it is received from
9 the device.
10 """
11 from twisted.python import log, usage
12 import sys
13
14 if sys.platform == 'win32':
15     from twisted.internet import win32eventreactor
16     win32eventreactor.install()
17
18
19 class GPSFixLogger:
20     def handle_fix(self, *args):
21       """
22       handle_fix gets called whenever either rockwell.Zodiac or nmea.NMEAReceiver
23       receives and decodes fix data.  Generally, GPS receivers will report a
24       fix at 1hz. Implementing only this method is sufficient for most purposes
25       unless tracking of ground speed, course, utc date, or detailed satellite
26       information is necessary.
27
28       For example, plotting a map from MapQuest or a similar service only
29       requires longitude and latitude.
30       """
31       log.msg('fix:\n' + 
32       '\n'.join(map(lambda n: '  %s = %s' % tuple(n), zip(('utc', 'lon', 'lat', 'fix', 'sat', 'hdp', 'alt', 'geo', 'dgp'), map(repr, args)))))
33
34 class GPSOptions(usage.Options):
35     optFlags = [
36         ['zodiac', 'z', 'Use Rockwell Zodiac (DeLorme Earthmate) [default: NMEA 0183]'],
37     ]
38     optParameters = [
39         ['outfile', 'o', None, 'Logfile [default: sys.stdout]'],
40         ['baudrate', 'b', None, 'Serial baudrate [default: 4800 for NMEA, 9600 for Zodiac]'],
41         ['port', 'p', '/dev/ttyS0', 'Serial Port device'],
42     ]
43
44
45 if __name__ == '__main__':
46     from twisted.internet import reactor
47     from twisted.internet.serialport import SerialPort
48
49     o = GPSOptions()
50     try:
51         o.parseOptions()
52     except usage.UsageError, errortext:
53         print '%s: %s' % (sys.argv[0], errortext)
54         print '%s: Try --help for usage details.' % (sys.argv[0])
55         raise SystemExit, 1
56
57     logFile = o.opts['outfile']
58     if logFile is None:
59         logFile = sys.stdout
60     log.startLogging(logFile)
61
62     if o.opts['zodiac']:
63         from twisted.protocols.gps.rockwell import Zodiac as GPSProtocolBase
64         baudrate = 9600
65     else:
66         from twisted.protocols.gps.nmea import NMEAReceiver as GPSProtocolBase
67         baudrate = 4800
68     class GPSTest(GPSProtocolBase, GPSFixLogger):
69         pass
70     
71     if o.opts['baudrate']:
72         baudrate = int(o.opts['baudrate'])
73
74
75     port = o.opts['port']
76     log.msg('Attempting to open %s at %dbps as a %s device' % (port, baudrate, GPSProtocolBase.__name__))
77     s = SerialPort(GPSTest(), o.opts['port'], reactor, baudrate=baudrate)
78     reactor.run()