PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.0KB

  1. #include <TinyGPS.h>
  2. /* This sample code demonstrates the basic use of a TinyGPS object.
  3. Typically, you would feed it characters from a serial GPS device, but
  4. this example uses static strings for simplicity.
  5. */
  6. const char str1[] PROGMEM = "$GPRMC,201547.000,A,3014.5527,N,09749.5808,W,0.24,163.05,040109,,*1A";
  7. const char str2[] PROGMEM = "$GPGGA,201548.000,3014.5529,N,09749.5808,W,1,07,1.5,225.6,M,-22.5,M,18.8,0000*78";
  8. const char str3[] PROGMEM = "$GPRMC,201548.000,A,3014.5529,N,09749.5808,W,0.17,53.25,040109,,*2B";
  9. const char str4[] PROGMEM = "$GPGGA,201549.000,3014.5533,N,09749.5812,W,1,07,1.5,223.5,M,-22.5,M,18.8,0000*7C";
  10. const char *teststrs[4] = {str1, str2, str3, str4};
  11. static void sendstring(TinyGPS &gps, const char *str);
  12. static void gpsdump(TinyGPS &gps);
  13. static void print_float(float val, float invalid, int len, int prec);
  14. static void print_int(unsigned long val, unsigned long invalid, int len);
  15. static void print_date(TinyGPS &gps);
  16. static void print_str(const char *str, int len);
  17. void setup()
  18. {
  19. TinyGPS test_gps;
  20. Serial.begin(115200);
  21. Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  22. Serial.println("by Mikal Hart");
  23. Serial.println();
  24. Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  25. Serial.println();
  26. Serial.println("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum");
  27. Serial.println(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail");
  28. Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  29. gpsdump(test_gps);
  30. for (int i=0; i<4; ++i)
  31. {
  32. sendstring(test_gps, teststrs[i]);
  33. gpsdump(test_gps);
  34. }
  35. }
  36. void loop()
  37. {
  38. }
  39. static void sendstring(TinyGPS &gps, const char *str)
  40. {
  41. while (true)
  42. {
  43. char c = pgm_read_byte_near(str++);
  44. if (!c) break;
  45. gps.encode(c);
  46. }
  47. gps.encode('\r');
  48. gps.encode('\n');
  49. }
  50. static void gpsdump(TinyGPS &gps)
  51. {
  52. float flat, flon;
  53. unsigned long age, chars = 0;
  54. unsigned short sentences = 0, failed = 0;
  55. static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  56. print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  57. print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  58. gps.f_get_position(&flat, &flon, &age);
  59. print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
  60. print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
  61. print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  62. print_date(gps);
  63. print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2);
  64. print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  65. print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  66. print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  67. print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  68. print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  69. print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);
  70. gps.stats(&chars, &sentences, &failed);
  71. print_int(chars, 0xFFFFFFFF, 6);
  72. print_int(sentences, 0xFFFFFFFF, 10);
  73. print_int(failed, 0xFFFFFFFF, 9);
  74. Serial.println();
  75. }
  76. static void print_int(unsigned long val, unsigned long invalid, int len)
  77. {
  78. char sz[32];
  79. if (val == invalid)
  80. strcpy(sz, "*******");
  81. else
  82. sprintf(sz, "%ld", val);
  83. sz[len] = 0;
  84. for (int i=strlen(sz); i<len; ++i)
  85. sz[i] = ' ';
  86. if (len > 0)
  87. sz[len-1] = ' ';
  88. Serial.print(sz);
  89. }
  90. static void print_float(float val, float invalid, int len, int prec)
  91. {
  92. char sz[32];
  93. if (val == invalid)
  94. {
  95. strcpy(sz, "*******");
  96. sz[len] = 0;
  97. if (len > 0)
  98. sz[len-1] = ' ';
  99. for (int i=7; i<len; ++i)
  100. sz[i] = ' ';
  101. Serial.print(sz);
  102. }
  103. else
  104. {
  105. Serial.print(val, prec);
  106. int vi = abs((int)val);
  107. int flen = prec + (val < 0.0 ? 2 : 1);
  108. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  109. for (int i=flen; i<len; ++i)
  110. Serial.print(" ");
  111. }
  112. }
  113. static void print_date(TinyGPS &gps)
  114. {
  115. int year;
  116. byte month, day, hour, minute, second, hundredths;
  117. unsigned long age;
  118. gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  119. if (age == TinyGPS::GPS_INVALID_AGE)
  120. Serial.print("******* ******* ");
  121. else
  122. {
  123. char sz[32];
  124. sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
  125. month, day, year, hour, minute, second);
  126. Serial.print(sz);
  127. }
  128. print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  129. }
  130. static void print_str(const char *str, int len)
  131. {
  132. int slen = strlen(str);
  133. for (int i=0; i<len; ++i)
  134. Serial.print(i<slen ? str[i] : ' ');
  135. }