static_test.ino 5.0 KB

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