GPS Library  0.1
GPS driver and NMEA parser
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
GPSHandler.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *
3  * Project: Ducktor Navi - my navigation software
4  * Purpose: Generic GPS Driver
5  * Author: György Kövesdi (kgy@teledigit.eu)
6  * License: GPL (see file 'COPYING' in the project root for more details)
7  * Comments:
8  *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10 
11 #include "GPSHandler.h"
12 
13 #include <unistd.h>
14 
15 AUTON_INTERFACE(I_GPSHandler);
16 AUTON_IMPLEMENT(GPS::GPSHandler, I_GPSHandler);
17 
18 SYS_DEFINE_MODULE(DM_GPS);
19 
20 using namespace GPS;
21 
22 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
23  * *
24  * class GPSHandler: *
25  * *
26 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
29  Threads::Thread("GPSHandler"),
30  interpreter(NULL),
31  gpsData(*this)
32 {
33  SYS_DEBUG_MEMBER(DM_GPS);
34 
35  Start(2*65536);
36 }
37 
39 {
40  SYS_DEBUG_MEMBER(DM_GPS);
41 }
42 
44 {
45  return gpsData;
46 }
47 
48 void GPSHandler::GotGPSData(const void * data, int length, I_GPSDriver * driver)
49 {
50  SYS_DEBUG_MEMBER(DM_GPS);
51 
52  const char * ch = reinterpret_cast<const char *>(data);
53  while (length) {
54  if (interpreter) {
55  if (!interpreter->GotData(*ch, driver)) {
56  interpreter = NULL;
57  }
58  } else {
60  if (i) {
61  interpreter = i;
62  }
63  }
64  ++ch;
65  --length;
66  }
67 }
68 
70 {
71  return (SYS::TimeDelay().SetNow() - last_localtime).ToMillisecond() / 1000.0f;
72 }
73 
75 
85 {
86  SYS_DEBUG_MEMBER(DM_GPS);
87 
88  SYS::TimeDelay now;
89  now.SetNow();
90  last_localtime = now;
91 
92  int32_t time2Wait = (next_localtime - now).ToMillisecond();
93 
94  // Note: the 'day' is initialized to -1
95  if (prev_time.GetDay() == 0) {
96  // The 'prev_time' is valid. Calculate the time we have really waited:
97  next_localtime.AddMillisecond((time - prev_time).ToMillisecond());
98  }
99  prev_time = time;
100 
101  SYS_DEBUG(DL_INFO1, "Time to sleep: " << time2Wait);
102 
103  if (time2Wait < 0) {
104  // Not necessary to wait
105  time2Wait = 0;
106  } else if (time2Wait > 10000) {
107  time2Wait = 5000;
108  SYS_DEBUG(DL_INFO1, " - Time is too long, reduced to " << time2Wait);
109  }
110 
111  return time2Wait;
112 }
113 
115 {
116  SYS_DEBUG_MEMBER(DM_GPS);
117 
118  for (;;) {
119  GPSDriverPtr gps = GetGPSDriver();
120 
121  if (!gps) {
122  usleep(20000);
123  continue;
124  }
125 
126  int result = gps->Run(*this);
127  if (result < 0) {
128  RemoveGPSDriver();
129  }
130  }
131 }
132 
133 /* * * * * * * * * * * * * End - of - File * * * * * * * * * * * * * * */