TinyGPS.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. TinyGPS - a small GPS library for Arduino providing basic NMEA parsing
  3. Based on work by and "distance_to" and "course_to" courtesy of Maarten Lamers.
  4. Suggestion to add satellites(), course_to(), and cardinal(), by Matt Monson.
  5. Precision improvements suggested by Wayne Holder.
  6. Copyright (C) 2008-2013 Mikal Hart
  7. All rights reserved.
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef TinyGPS_h
  21. #define TinyGPS_h
  22. #if defined(ARDUINO) && ARDUINO >= 100
  23. #include "Arduino.h"
  24. #else
  25. #include "WProgram.h"
  26. #endif
  27. #include <stdlib.h>
  28. #define _GPS_VERSION 13 // software version of this library
  29. #define _GPS_MPH_PER_KNOT 1.15077945
  30. #define _GPS_MPS_PER_KNOT 0.51444444
  31. #define _GPS_KMPH_PER_KNOT 1.852
  32. #define _GPS_MILES_PER_METER 0.00062137112
  33. #define _GPS_KM_PER_METER 0.001
  34. // #define _GPS_NO_STATS
  35. class TinyGPS
  36. {
  37. public:
  38. enum {
  39. GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999,
  40. GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0,
  41. GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999,
  42. GPS_INVALID_FIX_TIME = 0xFFFFFFFF, GPS_INVALID_SATELLITES = 0xFF,
  43. GPS_INVALID_HDOP = 0xFFFFFFFF
  44. };
  45. static const float GPS_INVALID_F_ANGLE, GPS_INVALID_F_ALTITUDE, GPS_INVALID_F_SPEED;
  46. TinyGPS();
  47. bool encode(char c); // process one character received from GPS
  48. TinyGPS &operator << (char c) {encode(c); return *this;}
  49. // lat/long in MILLIONTHs of a degree and age of fix in milliseconds
  50. // (note: versions 12 and earlier gave lat/long in 100,000ths of a degree.
  51. void get_position(long *latitude, long *longitude, unsigned long *fix_age = 0);
  52. // date as ddmmyy, time as hhmmsscc, and age in milliseconds
  53. void get_datetime(unsigned long *date, unsigned long *time, unsigned long *age = 0);
  54. // signed altitude in centimeters (from GPGGA sentence)
  55. inline long altitude() { return _altitude; }
  56. // course in last full GPRMC sentence in 100th of a degree
  57. inline unsigned long course() { return _course; }
  58. // speed in last full GPRMC sentence in 100ths of a knot
  59. inline unsigned long speed() { return _speed; }
  60. // satellites used in last full GPGGA sentence
  61. inline unsigned short satellites() { return _numsats; }
  62. // horizontal dilution of precision in 100ths
  63. inline unsigned long hdop() { return _hdop; }
  64. void f_get_position(float *latitude, float *longitude, unsigned long *fix_age = 0);
  65. void crack_datetime(int *year, byte *month, byte *day,
  66. byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0);
  67. float f_altitude();
  68. float f_course();
  69. float f_speed_knots();
  70. float f_speed_mph();
  71. float f_speed_mps();
  72. float f_speed_kmph();
  73. static int library_version() { return _GPS_VERSION; }
  74. static float distance_between (float lat1, float long1, float lat2, float long2);
  75. static float course_to (float lat1, float long1, float lat2, float long2);
  76. static const char *cardinal(float course);
  77. #ifndef _GPS_NO_STATS
  78. void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs);
  79. #endif
  80. private:
  81. enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_OTHER};
  82. // properties
  83. unsigned long _time, _new_time;
  84. unsigned long _date, _new_date;
  85. long _latitude, _new_latitude;
  86. long _longitude, _new_longitude;
  87. long _altitude, _new_altitude;
  88. unsigned long _speed, _new_speed;
  89. unsigned long _course, _new_course;
  90. unsigned long _hdop, _new_hdop;
  91. unsigned short _numsats, _new_numsats;
  92. unsigned long _last_time_fix, _new_time_fix;
  93. unsigned long _last_position_fix, _new_position_fix;
  94. // parsing state variables
  95. byte _parity;
  96. bool _is_checksum_term;
  97. char _term[15];
  98. byte _sentence_type;
  99. byte _term_number;
  100. byte _term_offset;
  101. bool _gps_data_good;
  102. #ifndef _GPS_NO_STATS
  103. // statistics
  104. unsigned long _encoded_characters;
  105. unsigned short _good_sentences;
  106. unsigned short _failed_checksum;
  107. unsigned short _passed_checksum;
  108. #endif
  109. // internal utilities
  110. int from_hex(char a);
  111. unsigned long parse_decimal();
  112. unsigned long parse_degrees();
  113. bool term_complete();
  114. bool gpsisdigit(char c) { return c >= '0' && c <= '9'; }
  115. long gpsatol(const char *str);
  116. int gpsstrcmp(const char *str1, const char *str2);
  117. };
  118. #if !defined(ARDUINO)
  119. // Arduino 0012 workaround
  120. #undef int
  121. #undef char
  122. #undef long
  123. #undef byte
  124. #undef float
  125. #undef abs
  126. #undef round
  127. #endif
  128. #endif