00001 // -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*- 00002 00005 00006 #ifndef GPS_h 00007 #define GPS_h 00008 00009 #include <inttypes.h> 00010 #include <Stream.h> 00011 00014 class GPS 00015 { 00016 public: 00017 00025 GPS(Stream *s) : _port(s) {}; 00026 00034 void update(void); 00035 00040 enum GPS_Status { 00041 NO_GPS = 0, 00042 NO_FIX = 1, 00043 GPS_OK = 2 00044 }; 00045 00053 GPS_Status status(void) { return _status; } 00054 00062 virtual void init(void) = 0; 00063 00064 // Properties 00065 long time; 00066 long latitude; 00067 long longitude; 00068 long altitude; 00069 long ground_speed; 00070 long ground_course; 00071 long speed_3d; 00072 int hdop; 00073 uint8_t num_sats; 00074 00078 bool new_data; 00079 00080 // Deprecated properties 00081 bool fix; 00082 bool valid_read; 00083 00084 // Debug support 00085 bool print_errors; 00086 00087 protected: 00088 Stream *_port; 00089 00096 virtual bool read(void) = 0; 00097 00104 long _swapl(const void *bytes); 00105 00111 int _swapi(const void *bytes); 00112 00123 void _error(const char *msg); 00124 00125 private: 00126 00130 static const unsigned long _idleTimeout = 500; 00131 00134 unsigned long _idleTimer; 00135 00137 GPS_Status _status; 00138 }; 00139 00140 inline long 00141 GPS::_swapl(const void *bytes) 00142 { 00143 const uint8_t *b = (const uint8_t *)bytes; 00144 union { 00145 long v; 00146 uint8_t b[4]; 00147 } u; 00148 00149 u.b[0] = b[3]; 00150 u.b[1] = b[2]; 00151 u.b[2] = b[1]; 00152 u.b[3] = b[0]; 00153 00154 return(u.v); 00155 } 00156 00157 inline int16_t 00158 GPS::_swapi(const void *bytes) 00159 { 00160 const uint8_t *b = (const uint8_t *)bytes; 00161 union { 00162 int16_t v; 00163 uint8_t b[2]; 00164 } u; 00165 00166 u.b[0] = b[1]; 00167 u.b[1] = b[0]; 00168 00169 return(u.v); 00170 } 00171 00172 #endif