Refatorado

Committer:
renanbmx123
Date:
Sun Apr 04 14:38:47 2021 +0000
Revision:
0:9678e357caf9
Refatorado

Who changed what in which revision?

UserRevisionLine numberNew contents of line
renanbmx123 0:9678e357caf9 1 /*
renanbmx123 0:9678e357caf9 2 TinyGPS++ - a small GPS library for Arduino providing universal NMEA parsing
renanbmx123 0:9678e357caf9 3 Based on work by and "distanceBetween" and "courseTo" courtesy of Maarten Lamers.
renanbmx123 0:9678e357caf9 4 Suggestion to add satellites, courseTo(), and cardinal() by Matt Monson.
renanbmx123 0:9678e357caf9 5 Location precision improvements suggested by Wayne Holder.
renanbmx123 0:9678e357caf9 6 Copyright (C) 2008-2013 Mikal Hart
renanbmx123 0:9678e357caf9 7 All rights reserved.
renanbmx123 0:9678e357caf9 8
renanbmx123 0:9678e357caf9 9 This library is free software; you can redistribute it and/or
renanbmx123 0:9678e357caf9 10 modify it under the terms of the GNU Lesser General Public
renanbmx123 0:9678e357caf9 11 License as published by the Free Software Foundation; either
renanbmx123 0:9678e357caf9 12 version 2.1 of the License, or (at your option) any later version.
renanbmx123 0:9678e357caf9 13
renanbmx123 0:9678e357caf9 14 This library is distributed in the hope that it will be useful,
renanbmx123 0:9678e357caf9 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
renanbmx123 0:9678e357caf9 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
renanbmx123 0:9678e357caf9 17 Lesser General Public License for more details.
renanbmx123 0:9678e357caf9 18
renanbmx123 0:9678e357caf9 19 You should have received a copy of the GNU Lesser General Public
renanbmx123 0:9678e357caf9 20 License along with this library; if not, write to the Free Software
renanbmx123 0:9678e357caf9 21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
renanbmx123 0:9678e357caf9 22 */
renanbmx123 0:9678e357caf9 23
renanbmx123 0:9678e357caf9 24 #ifndef __TinyGPSPlus_h
renanbmx123 0:9678e357caf9 25 #define __TinyGPSPlus_h
renanbmx123 0:9678e357caf9 26
renanbmx123 0:9678e357caf9 27 /*#if defined(ARDUINO) && ARDUINO >= 100
renanbmx123 0:9678e357caf9 28 #include "Arduino.h"
renanbmx123 0:9678e357caf9 29 #else
renanbmx123 0:9678e357caf9 30 #include "WProgram.h"
renanbmx123 0:9678e357caf9 31 #endif
renanbmx123 0:9678e357caf9 32 */
renanbmx123 0:9678e357caf9 33
renanbmx123 0:9678e357caf9 34 #include "mbed.h"
renanbmx123 0:9678e357caf9 35 #include "types.h"
renanbmx123 0:9678e357caf9 36
renanbmx123 0:9678e357caf9 37 #include <stdlib.h>
renanbmx123 0:9678e357caf9 38 #include <limits.h>
renanbmx123 0:9678e357caf9 39
renanbmx123 0:9678e357caf9 40 #define _GPS_VERSION "0.92" // software version of this library
renanbmx123 0:9678e357caf9 41 #define _GPS_MPH_PER_KNOT 1.15077945
renanbmx123 0:9678e357caf9 42 #define _GPS_MPS_PER_KNOT 0.51444444
renanbmx123 0:9678e357caf9 43 #define _GPS_KMPH_PER_KNOT 1.852
renanbmx123 0:9678e357caf9 44 #define _GPS_MILES_PER_METER 0.00062137112
renanbmx123 0:9678e357caf9 45 #define _GPS_KM_PER_METER 0.001
renanbmx123 0:9678e357caf9 46 #define _GPS_FEET_PER_METER 3.2808399
renanbmx123 0:9678e357caf9 47 #define _GPS_MAX_FIELD_SIZE 15
renanbmx123 0:9678e357caf9 48
renanbmx123 0:9678e357caf9 49 #define PI 3.1415926535897932384626433832795
renanbmx123 0:9678e357caf9 50 #define HALF_PI 1.5707963267948966192313216916398
renanbmx123 0:9678e357caf9 51 #define TWO_PI 6.283185307179586476925286766559
renanbmx123 0:9678e357caf9 52 #define DEG_TO_RAD 0.017453292519943295769236907684886
renanbmx123 0:9678e357caf9 53 #define RAD_TO_DEG 57.295779513082320876798154814105
renanbmx123 0:9678e357caf9 54 #define EULER 2.718281828459045235360287471352
renanbmx123 0:9678e357caf9 55 #define radians(deg) ((deg)*DEG_TO_RAD)
renanbmx123 0:9678e357caf9 56 #define degrees(rad) ((rad)*RAD_TO_DEG)
renanbmx123 0:9678e357caf9 57 #define sq(x) ((x)*(x))
renanbmx123 0:9678e357caf9 58
renanbmx123 0:9678e357caf9 59
renanbmx123 0:9678e357caf9 60 struct RawDegrees
renanbmx123 0:9678e357caf9 61 {
renanbmx123 0:9678e357caf9 62 uint16_t deg;
renanbmx123 0:9678e357caf9 63 uint32_t billionths;
renanbmx123 0:9678e357caf9 64 bool negative;
renanbmx123 0:9678e357caf9 65 public:
renanbmx123 0:9678e357caf9 66 RawDegrees() : deg(0), billionths(0), negative(false)
renanbmx123 0:9678e357caf9 67 {}
renanbmx123 0:9678e357caf9 68 };
renanbmx123 0:9678e357caf9 69
renanbmx123 0:9678e357caf9 70 struct TinyGPSLocation
renanbmx123 0:9678e357caf9 71 {
renanbmx123 0:9678e357caf9 72 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 73 public:
renanbmx123 0:9678e357caf9 74 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 75 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 76 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 77 const RawDegrees &rawLat() { updated = false; return rawLatData; }
renanbmx123 0:9678e357caf9 78 const RawDegrees &rawLng() { updated = false; return rawLngData; }
renanbmx123 0:9678e357caf9 79 double lat();
renanbmx123 0:9678e357caf9 80 double lng();
renanbmx123 0:9678e357caf9 81
renanbmx123 0:9678e357caf9 82 TinyGPSLocation() : valid(false), updated(false)
renanbmx123 0:9678e357caf9 83 {}
renanbmx123 0:9678e357caf9 84
renanbmx123 0:9678e357caf9 85 private:
renanbmx123 0:9678e357caf9 86 bool valid, updated;
renanbmx123 0:9678e357caf9 87 RawDegrees rawLatData, rawLngData, rawNewLatData, rawNewLngData;
renanbmx123 0:9678e357caf9 88 uint32_t lastCommitTime;
renanbmx123 0:9678e357caf9 89 void commit();
renanbmx123 0:9678e357caf9 90 void setLatitude(const char *term);
renanbmx123 0:9678e357caf9 91 void setLongitude(const char *term);
renanbmx123 0:9678e357caf9 92 };
renanbmx123 0:9678e357caf9 93
renanbmx123 0:9678e357caf9 94 struct TinyGPSDate
renanbmx123 0:9678e357caf9 95 {
renanbmx123 0:9678e357caf9 96 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 97 public:
renanbmx123 0:9678e357caf9 98 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 99 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 100 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 101
renanbmx123 0:9678e357caf9 102 uint32_t value() { updated = false; return date; }
renanbmx123 0:9678e357caf9 103 uint16_t year();
renanbmx123 0:9678e357caf9 104 uint8_t month();
renanbmx123 0:9678e357caf9 105 uint8_t day();
renanbmx123 0:9678e357caf9 106
renanbmx123 0:9678e357caf9 107 TinyGPSDate() : valid(false), updated(false), date(0)
renanbmx123 0:9678e357caf9 108 {}
renanbmx123 0:9678e357caf9 109
renanbmx123 0:9678e357caf9 110 private:
renanbmx123 0:9678e357caf9 111 bool valid, updated;
renanbmx123 0:9678e357caf9 112 uint32_t date, newDate;
renanbmx123 0:9678e357caf9 113 uint32_t lastCommitTime;
renanbmx123 0:9678e357caf9 114 void commit();
renanbmx123 0:9678e357caf9 115 void setDate(const char *term);
renanbmx123 0:9678e357caf9 116 };
renanbmx123 0:9678e357caf9 117
renanbmx123 0:9678e357caf9 118 struct TinyGPSTime
renanbmx123 0:9678e357caf9 119 {
renanbmx123 0:9678e357caf9 120 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 121 public:
renanbmx123 0:9678e357caf9 122 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 123 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 124 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 125
renanbmx123 0:9678e357caf9 126 uint32_t value() { updated = false; return time; }
renanbmx123 0:9678e357caf9 127 uint8_t hour();
renanbmx123 0:9678e357caf9 128 uint8_t minute();
renanbmx123 0:9678e357caf9 129 uint8_t second();
renanbmx123 0:9678e357caf9 130 uint8_t centisecond();
renanbmx123 0:9678e357caf9 131
renanbmx123 0:9678e357caf9 132 TinyGPSTime() : valid(false), updated(false), time(0)
renanbmx123 0:9678e357caf9 133 {}
renanbmx123 0:9678e357caf9 134
renanbmx123 0:9678e357caf9 135 private:
renanbmx123 0:9678e357caf9 136 bool valid, updated;
renanbmx123 0:9678e357caf9 137 uint32_t time, newTime;
renanbmx123 0:9678e357caf9 138 uint32_t lastCommitTime;
renanbmx123 0:9678e357caf9 139 void commit();
renanbmx123 0:9678e357caf9 140 void setTime(const char *term);
renanbmx123 0:9678e357caf9 141 };
renanbmx123 0:9678e357caf9 142
renanbmx123 0:9678e357caf9 143 struct TinyGPSDecimal
renanbmx123 0:9678e357caf9 144 {
renanbmx123 0:9678e357caf9 145 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 146 public:
renanbmx123 0:9678e357caf9 147 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 148 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 149 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 150 int32_t value() { updated = false; return val; }
renanbmx123 0:9678e357caf9 151
renanbmx123 0:9678e357caf9 152 TinyGPSDecimal() : valid(false), updated(false), val(0)
renanbmx123 0:9678e357caf9 153 {}
renanbmx123 0:9678e357caf9 154
renanbmx123 0:9678e357caf9 155 private:
renanbmx123 0:9678e357caf9 156 bool valid, updated;
renanbmx123 0:9678e357caf9 157 uint32_t lastCommitTime;
renanbmx123 0:9678e357caf9 158 int32_t val, newval;
renanbmx123 0:9678e357caf9 159 void commit();
renanbmx123 0:9678e357caf9 160 void set(const char *term);
renanbmx123 0:9678e357caf9 161 };
renanbmx123 0:9678e357caf9 162
renanbmx123 0:9678e357caf9 163 struct TinyGPSInteger
renanbmx123 0:9678e357caf9 164 {
renanbmx123 0:9678e357caf9 165 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 166 public:
renanbmx123 0:9678e357caf9 167 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 168 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 169 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 170 uint32_t value() { updated = false; return val; }
renanbmx123 0:9678e357caf9 171
renanbmx123 0:9678e357caf9 172 TinyGPSInteger() : valid(false), updated(false), val(0)
renanbmx123 0:9678e357caf9 173 {}
renanbmx123 0:9678e357caf9 174
renanbmx123 0:9678e357caf9 175 private:
renanbmx123 0:9678e357caf9 176 bool valid, updated;
renanbmx123 0:9678e357caf9 177 uint32_t lastCommitTime;
renanbmx123 0:9678e357caf9 178 uint32_t val, newval;
renanbmx123 0:9678e357caf9 179 void commit();
renanbmx123 0:9678e357caf9 180 void set(const char *term);
renanbmx123 0:9678e357caf9 181 };
renanbmx123 0:9678e357caf9 182
renanbmx123 0:9678e357caf9 183 struct TinyGPSSpeed : TinyGPSDecimal
renanbmx123 0:9678e357caf9 184 {
renanbmx123 0:9678e357caf9 185 float meters() {return value();}
renanbmx123 0:9678e357caf9 186 double knots() { return value() / 100.0; }
renanbmx123 0:9678e357caf9 187 double mph() { return _GPS_MPH_PER_KNOT * value() / 100.0; }
renanbmx123 0:9678e357caf9 188 double mps() { return _GPS_MPS_PER_KNOT * value() / 100.0; }
renanbmx123 0:9678e357caf9 189 double kmph() { return _GPS_KMPH_PER_KNOT * value() / 100.0; }
renanbmx123 0:9678e357caf9 190 };
renanbmx123 0:9678e357caf9 191
renanbmx123 0:9678e357caf9 192 struct TinyGPSCourse : public TinyGPSDecimal
renanbmx123 0:9678e357caf9 193 {
renanbmx123 0:9678e357caf9 194 double deg() { return value() / 100.0; }
renanbmx123 0:9678e357caf9 195 };
renanbmx123 0:9678e357caf9 196
renanbmx123 0:9678e357caf9 197 struct TinyGPSAltitude : TinyGPSDecimal
renanbmx123 0:9678e357caf9 198 {
renanbmx123 0:9678e357caf9 199 double meters() { return value() / 100.0; }
renanbmx123 0:9678e357caf9 200 double miles() { return _GPS_MILES_PER_METER * value() / 100.0; }
renanbmx123 0:9678e357caf9 201 double kilometers() { return _GPS_KM_PER_METER * value() / 100.0; }
renanbmx123 0:9678e357caf9 202 double feet() { return _GPS_FEET_PER_METER * value() / 100.0; }
renanbmx123 0:9678e357caf9 203 };
renanbmx123 0:9678e357caf9 204
renanbmx123 0:9678e357caf9 205 class TinyGPSPlus;
renanbmx123 0:9678e357caf9 206 class TinyGPSCustom
renanbmx123 0:9678e357caf9 207 {
renanbmx123 0:9678e357caf9 208 public:
renanbmx123 0:9678e357caf9 209 TinyGPSCustom() {};
renanbmx123 0:9678e357caf9 210 TinyGPSCustom(TinyGPSPlus &gps, const char *sentenceName, int termNumber);
renanbmx123 0:9678e357caf9 211 void begin(TinyGPSPlus &gps, const char *_sentenceName, int _termNumber);
renanbmx123 0:9678e357caf9 212
renanbmx123 0:9678e357caf9 213 bool isUpdated() const { return updated; }
renanbmx123 0:9678e357caf9 214 bool isValid() const { return valid; }
renanbmx123 0:9678e357caf9 215 uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
renanbmx123 0:9678e357caf9 216 const char *value() { updated = false; return buffer; }
renanbmx123 0:9678e357caf9 217
renanbmx123 0:9678e357caf9 218 private:
renanbmx123 0:9678e357caf9 219 void commit();
renanbmx123 0:9678e357caf9 220 void set(const char *term);
renanbmx123 0:9678e357caf9 221
renanbmx123 0:9678e357caf9 222 char stagingBuffer[_GPS_MAX_FIELD_SIZE + 1];
renanbmx123 0:9678e357caf9 223 char buffer[_GPS_MAX_FIELD_SIZE + 1];
renanbmx123 0:9678e357caf9 224 unsigned long lastCommitTime;
renanbmx123 0:9678e357caf9 225 bool valid, updated;
renanbmx123 0:9678e357caf9 226 const char *sentenceName;
renanbmx123 0:9678e357caf9 227 int termNumber;
renanbmx123 0:9678e357caf9 228 friend class TinyGPSPlus;
renanbmx123 0:9678e357caf9 229 TinyGPSCustom *next;
renanbmx123 0:9678e357caf9 230 };
renanbmx123 0:9678e357caf9 231
renanbmx123 0:9678e357caf9 232 class TinyGPSPlus
renanbmx123 0:9678e357caf9 233 {
renanbmx123 0:9678e357caf9 234 public:
renanbmx123 0:9678e357caf9 235 TinyGPSPlus();
renanbmx123 0:9678e357caf9 236 bool encode(char c); // process one character received from GPS
renanbmx123 0:9678e357caf9 237 TinyGPSPlus &operator << (char c) {encode(c); return *this;}
renanbmx123 0:9678e357caf9 238
renanbmx123 0:9678e357caf9 239 TinyGPSLocation location;
renanbmx123 0:9678e357caf9 240 TinyGPSDate date;
renanbmx123 0:9678e357caf9 241 TinyGPSTime time;
renanbmx123 0:9678e357caf9 242 TinyGPSSpeed speed;
renanbmx123 0:9678e357caf9 243 TinyGPSCourse course;
renanbmx123 0:9678e357caf9 244 TinyGPSAltitude altitude;
renanbmx123 0:9678e357caf9 245 TinyGPSInteger satellites;
renanbmx123 0:9678e357caf9 246 TinyGPSDecimal hdop;
renanbmx123 0:9678e357caf9 247 char NorthSouth;
renanbmx123 0:9678e357caf9 248 char EastWest;
renanbmx123 0:9678e357caf9 249 static const char *libraryVersion() { return _GPS_VERSION; }
renanbmx123 0:9678e357caf9 250
renanbmx123 0:9678e357caf9 251 static double distanceBetween(double lat1, double long1, double lat2, double long2);
renanbmx123 0:9678e357caf9 252 static double courseTo(double lat1, double long1, double lat2, double long2);
renanbmx123 0:9678e357caf9 253 static const char *cardinal(double course);
renanbmx123 0:9678e357caf9 254
renanbmx123 0:9678e357caf9 255 static int32_t parseDecimal(const char *term);
renanbmx123 0:9678e357caf9 256 static void parseDegrees(const char *term, RawDegrees &deg);
renanbmx123 0:9678e357caf9 257
renanbmx123 0:9678e357caf9 258 uint32_t charsProcessed() const { return encodedCharCount; }
renanbmx123 0:9678e357caf9 259 uint32_t sentencesWithFix() const { return sentencesWithFixCount; }
renanbmx123 0:9678e357caf9 260 uint32_t failedChecksum() const { return failedChecksumCount; }
renanbmx123 0:9678e357caf9 261 uint32_t passedChecksum() const { return passedChecksumCount; }
renanbmx123 0:9678e357caf9 262
renanbmx123 0:9678e357caf9 263 private:
renanbmx123 0:9678e357caf9 264 enum {GPS_SENTENCE_GPGGA, GPS_SENTENCE_GPRMC, GPS_SENTENCE_OTHER};
renanbmx123 0:9678e357caf9 265
renanbmx123 0:9678e357caf9 266 // parsing state variables
renanbmx123 0:9678e357caf9 267 uint8_t parity;
renanbmx123 0:9678e357caf9 268 bool isChecksumTerm;
renanbmx123 0:9678e357caf9 269 char term[_GPS_MAX_FIELD_SIZE];
renanbmx123 0:9678e357caf9 270 uint8_t curSentenceType;
renanbmx123 0:9678e357caf9 271 uint8_t curTermNumber;
renanbmx123 0:9678e357caf9 272 uint8_t curTermOffset;
renanbmx123 0:9678e357caf9 273 bool sentenceHasFix;
renanbmx123 0:9678e357caf9 274
renanbmx123 0:9678e357caf9 275 // custom element support
renanbmx123 0:9678e357caf9 276 friend class TinyGPSCustom;
renanbmx123 0:9678e357caf9 277 TinyGPSCustom *customElts;
renanbmx123 0:9678e357caf9 278 TinyGPSCustom *customCandidates;
renanbmx123 0:9678e357caf9 279 void insertCustom(TinyGPSCustom *pElt, const char *sentenceName, int index);
renanbmx123 0:9678e357caf9 280
renanbmx123 0:9678e357caf9 281
renanbmx123 0:9678e357caf9 282 // statistics
renanbmx123 0:9678e357caf9 283 uint32_t encodedCharCount;
renanbmx123 0:9678e357caf9 284 uint32_t sentencesWithFixCount;
renanbmx123 0:9678e357caf9 285 uint32_t failedChecksumCount;
renanbmx123 0:9678e357caf9 286 uint32_t passedChecksumCount;
renanbmx123 0:9678e357caf9 287
renanbmx123 0:9678e357caf9 288 // internal utilities
renanbmx123 0:9678e357caf9 289 int fromHex(char a);
renanbmx123 0:9678e357caf9 290 bool endOfTermHandler();
renanbmx123 0:9678e357caf9 291 };
renanbmx123 0:9678e357caf9 292
renanbmx123 0:9678e357caf9 293 #endif // def(__TinyGPSPlus_h)