Daniel de kock / TinyGPSplus

Dependents:   WNC_Pubnub_obd2b_ign_em506SoftSerial_RESETFE2 mbed_xbeetest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TinyGPSplus.h Source File

TinyGPSplus.h

00001 /*
00002 TinyGPS++ - a small GPS library for Arduino providing universal NMEA parsing
00003 Based on work by and "distanceBetween" and "courseTo" courtesy of Maarten Lamers.
00004 Suggestion to add satellites, courseTo(), and cardinal() by Matt Monson.
00005 Location precision improvements suggested by Wayne Holder.
00006 Copyright (C) 2008-2013 Mikal Hart
00007 All rights reserved.
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   Ported to mbed by Daniel de Kock
00024 */
00025 
00026 #include "mbed.h"
00027 #include "types.h"
00028 #include "constants.h"
00029 #include "limits.h"
00030 
00031 #ifndef __TinyGPSPlus_h
00032 #define __TinyGPSPlus_h
00033 
00034 #define _GPS_VERSION "0.92" // software version of this library
00035 #define _GPS_MPH_PER_KNOT 1.15077945
00036 #define _GPS_MPS_PER_KNOT 0.51444444
00037 #define _GPS_KMPH_PER_KNOT 1.852
00038 #define _GPS_MILES_PER_METER 0.00062137112
00039 #define _GPS_KM_PER_METER 0.001
00040 #define _GPS_FEET_PER_METER 3.2808399
00041 #define _GPS_MAX_FIELD_SIZE 15
00042 
00043 struct RawDegrees
00044 {
00045    uint16_t deg;
00046    uint32_t billionths;
00047    bool negative;
00048 public:
00049    RawDegrees() : deg(0), billionths(0), negative(false)
00050    {}
00051 };
00052 
00053 struct TinyGPSLocation
00054 {
00055    friend class TinyGPSPlus;
00056 public:
00057    bool isValid() const    { return valid; }
00058    bool isUpdated() const  { return updated; }
00059    uint32_t age() const    { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00060    const RawDegrees &rawLat()     { updated = false; return rawLatData; }
00061    const RawDegrees &rawLng()     { updated = false; return rawLngData; }
00062    double lat();
00063    double lng();
00064    int32_t lngBinary();
00065    int32_t latBinary();
00066 
00067    TinyGPSLocation() : valid(false), updated(false)
00068    {}
00069 
00070 private:
00071    bool valid, updated;
00072    RawDegrees rawLatData, rawLngData, rawNewLatData, rawNewLngData;
00073    uint32_t lastCommitTime;
00074    void commit();
00075    void setLatitude(const char *term);
00076    void setLongitude(const char *term);
00077 };
00078 
00079 struct TinyGPSDate
00080 {
00081    friend class TinyGPSPlus;
00082 public:
00083    bool isValid() const       { return valid; }
00084    bool isUpdated() const     { return updated; }
00085    uint32_t age() const       { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00086 
00087    uint32_t value()           { updated = false; return date; }
00088    uint16_t year();
00089    uint8_t month();
00090    uint8_t day();
00091 
00092    TinyGPSDate() : valid(false), updated(false), date(0)
00093    {}
00094 
00095 private:
00096    bool valid, updated;
00097    uint32_t date, newDate;
00098    uint32_t lastCommitTime;
00099    void commit();
00100    void setDate(const char *term);
00101 };
00102 
00103 struct TinyGPSTime
00104 {
00105    friend class TinyGPSPlus;
00106 public:
00107    bool isValid() const       { return valid; }
00108    bool isUpdated() const     { return updated; }
00109    uint32_t age() const       { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00110 
00111    uint32_t value()           { updated = false; return time; }
00112    uint8_t hour();
00113    uint8_t minute();
00114    uint8_t second();
00115    uint8_t centisecond();
00116 
00117    TinyGPSTime() : valid(false), updated(false), time(0)
00118    {}
00119 
00120 private:
00121    bool valid, updated;
00122    uint32_t time, newTime;
00123    uint32_t lastCommitTime;
00124    void commit();
00125    void setTime(const char *term);
00126 };
00127 
00128 struct TinyGPSDecimal
00129 {
00130    friend class TinyGPSPlus;
00131 public:
00132    bool isValid() const    { return valid; }
00133    bool isUpdated() const  { return updated; }
00134    uint32_t age() const    { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00135    int32_t value()         { updated = false; return val; }
00136 
00137    TinyGPSDecimal() : valid(false), updated(false), val(0)
00138    {}
00139 
00140 private:
00141    bool valid, updated;
00142    uint32_t lastCommitTime;
00143    int32_t val, newval;
00144    void commit();
00145    void set(const char *term);
00146 };
00147 
00148 struct TinyGPSInteger
00149 {
00150    friend class TinyGPSPlus;
00151 public:
00152    bool isValid() const    { return valid; }
00153    bool isUpdated() const  { return updated; }
00154    uint32_t age() const    { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00155    uint32_t value()        { updated = false; return val; }
00156 
00157    TinyGPSInteger() : valid(false), updated(false), val(0)
00158    {}
00159 
00160 private:
00161    bool valid, updated;
00162    uint32_t lastCommitTime;
00163    uint32_t val, newval;
00164    void commit();
00165    void set(const char *term);
00166 };
00167 
00168 struct TinyGPSSpeed : TinyGPSDecimal
00169 {
00170    double knots()    { return value() / 100.0; }
00171    double mph()      { return _GPS_MPH_PER_KNOT * value() / 100.0; }
00172    double mps()      { return _GPS_MPS_PER_KNOT * value() / 100.0; }
00173    double kmph()     { return _GPS_KMPH_PER_KNOT * value() / 100.0; }
00174 };
00175 
00176 struct TinyGPSCourse : public TinyGPSDecimal
00177 {
00178    double deg()      { return value() / 100.0; }
00179 };
00180 
00181 struct TinyGPSAltitude : TinyGPSDecimal
00182 {
00183    double meters()       { return value() / 100.0; }
00184    double miles()        { return _GPS_MILES_PER_METER * value() / 100.0; }
00185    double kilometers()   { return _GPS_KM_PER_METER * value() / 100.0; }
00186    double feet()         { return _GPS_FEET_PER_METER * value() / 100.0; }
00187 };
00188 
00189 class TinyGPSPlus;
00190 class TinyGPSCustom
00191 {
00192 public:
00193    TinyGPSCustom() {};
00194    TinyGPSCustom(TinyGPSPlus &gps, const char *sentenceName, int termNumber);
00195    void begin(TinyGPSPlus &gps, const char *_sentenceName, int _termNumber);
00196 
00197    bool isUpdated() const  { return updated; }
00198    bool isValid() const    { return valid; }
00199    uint32_t age() const    { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
00200    const char *value()     { updated = false; return buffer; }
00201 
00202 private:
00203    void commit();
00204    void set(const char *term);
00205 
00206    char stagingBuffer[_GPS_MAX_FIELD_SIZE + 1];
00207    char buffer[_GPS_MAX_FIELD_SIZE + 1];
00208    unsigned long lastCommitTime;
00209    bool valid, updated;
00210    const char *sentenceName;
00211    int termNumber;
00212    friend class TinyGPSPlus;
00213    TinyGPSCustom *next;
00214 };
00215 
00216 class TinyGPSPlus
00217 {
00218 public:
00219   TinyGPSPlus();
00220   bool encode(char c); // process one character received from GPS
00221   TinyGPSPlus &operator << (char c) {encode(c); return *this;}
00222 
00223   TinyGPSLocation location;
00224   TinyGPSDate date;
00225   TinyGPSTime time;
00226   TinyGPSSpeed speed;
00227   TinyGPSCourse course;
00228   TinyGPSAltitude altitude;
00229   TinyGPSInteger satellites;
00230   TinyGPSDecimal hdop;
00231 
00232   static const char *libraryVersion() { return _GPS_VERSION; }
00233 
00234   static double distanceBetween(double lat1, double long1, double lat2, double long2);
00235   static double courseTo(double lat1, double long1, double lat2, double long2);
00236   static const char *cardinal(double course);
00237 
00238   static int32_t parseDecimal(const char *term);
00239   static void parseDegrees(const char *term, RawDegrees &deg);
00240 
00241   uint32_t charsProcessed()   const { return encodedCharCount; }
00242   uint32_t sentencesWithFix() const { return sentencesWithFixCount; }
00243   uint32_t failedChecksum()   const { return failedChecksumCount; }
00244   uint32_t passedChecksum()   const { return passedChecksumCount; }
00245 
00246 private:
00247   enum {GPS_SENTENCE_GPGGA, GPS_SENTENCE_GPRMC, GPS_SENTENCE_OTHER};
00248 
00249   // parsing state variables
00250   uint8_t parity;
00251   bool isChecksumTerm;
00252   char term[_GPS_MAX_FIELD_SIZE];
00253   uint8_t curSentenceType;
00254   uint8_t curTermNumber;
00255   uint8_t curTermOffset;
00256   bool sentenceHasFix;
00257 
00258   // custom element support
00259   friend class TinyGPSCustom;
00260   TinyGPSCustom *customElts;
00261   TinyGPSCustom *customCandidates;
00262   void insertCustom(TinyGPSCustom *pElt, const char *sentenceName, int index);
00263 
00264   // statistics
00265   uint32_t encodedCharCount;
00266   uint32_t sentencesWithFixCount;
00267   uint32_t failedChecksumCount;
00268   uint32_t passedChecksumCount;
00269 
00270   // internal utilities
00271   int fromHex(char a);
00272   bool endOfTermHandler();
00273 };
00274 
00275 #endif // def(__TinyGPSPlus_h)