FullExample.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Description: Use GPS Unit to get the coordinate data and time of the current location.
  3. Please install library before compiling:
  4. TinyGPSPlus: file in M5stack lib examples -> modules -> GPS -> TinyGPSPlus-1.0.2.zip (unzip the lib zip file to the Arduino Lib path)
  5. */
  6. #include <M5Stack.h>
  7. #include <TinyGPS++.h>
  8. /*
  9. This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
  10. It requires the use of SoftwareSerial, and assumes that you have a
  11. 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  12. */
  13. static const uint32_t GPSBaud = 9600;
  14. // The TinyGPS++ object
  15. TinyGPSPlus gps;
  16. // The serial connection to the GPS device
  17. HardwareSerial ss(2);
  18. void setup()
  19. {
  20. M5.begin();
  21. M5.Power.begin();
  22. ss.begin(GPSBaud);
  23. // M5.Lcd.println(F("FullExample.ino"));
  24. // M5.Lcd.println(F("An extensive example of many interesting TinyGPS++ features"));
  25. // M5.Lcd.print(F("Testing TinyGPS++ library v. ")); M5.Lcd.println(TinyGPSPlus::libraryVersion());
  26. // M5.Lcd.println(F("by Mikal Hart"));
  27. // M5.Lcd.println();
  28. M5.Lcd.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  29. M5.Lcd.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  30. M5.Lcd.println(F("---------------------------------------------------------------------------------------------------------------------------------------"));
  31. }
  32. void loop()
  33. {
  34. M5.Lcd.setCursor(0, 70);
  35. M5.Lcd.setTextColor(WHITE, BLACK);
  36. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  37. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  38. printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
  39. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  40. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  41. printInt(gps.location.age(), gps.location.isValid(), 5);
  42. printDateTime(gps.date, gps.time);
  43. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  44. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  45. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  46. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  47. unsigned long distanceKmToLondon =
  48. (unsigned long)TinyGPSPlus::distanceBetween(
  49. gps.location.lat(),
  50. gps.location.lng(),
  51. LONDON_LAT,
  52. LONDON_LON) / 1000;
  53. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  54. double courseToLondon =
  55. TinyGPSPlus::courseTo(
  56. gps.location.lat(),
  57. gps.location.lng(),
  58. LONDON_LAT,
  59. LONDON_LON);
  60. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  61. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  62. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  63. printInt(gps.charsProcessed(), true, 6);
  64. printInt(gps.sentencesWithFix(), true, 10);
  65. printInt(gps.failedChecksum(), true, 9);
  66. M5.Lcd.println();
  67. smartDelay(1000);
  68. if (millis() > 5000 && gps.charsProcessed() < 10)
  69. M5.Lcd.println(F("No GPS data received: check wiring"));
  70. }
  71. // This custom version of delay() ensures that the gps object
  72. // is being "fed".
  73. static void smartDelay(unsigned long ms)
  74. {
  75. unsigned long start = millis();
  76. do
  77. {
  78. while (ss.available())
  79. gps.encode(ss.read());
  80. } while (millis() - start < ms);
  81. }
  82. static void printFloat(float val, bool valid, int len, int prec)
  83. {
  84. if (!valid)
  85. {
  86. while (len-- > 1)
  87. M5.Lcd.print('*');
  88. M5.Lcd.print(' ');
  89. }
  90. else
  91. {
  92. M5.Lcd.print(val, prec);
  93. int vi = abs((int)val);
  94. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  95. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  96. for (int i=flen; i<len; ++i)
  97. M5.Lcd.print(' ');
  98. }
  99. smartDelay(0);
  100. }
  101. static void printInt(unsigned long val, bool valid, int len)
  102. {
  103. char sz[32] = "*****************";
  104. if (valid)
  105. sprintf(sz, "%ld", val);
  106. sz[len] = 0;
  107. for (int i=strlen(sz); i<len; ++i)
  108. sz[i] = ' ';
  109. if (len > 0)
  110. sz[len-1] = ' ';
  111. M5.Lcd.print(sz);
  112. smartDelay(0);
  113. }
  114. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  115. {
  116. if (!d.isValid())
  117. {
  118. M5.Lcd.print(F("********** "));
  119. }
  120. else
  121. {
  122. char sz[32];
  123. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  124. M5.Lcd.print(sz);
  125. }
  126. if (!t.isValid())
  127. {
  128. M5.Lcd.print(F("******** "));
  129. }
  130. else
  131. {
  132. char sz[32];
  133. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  134. M5.Lcd.print(sz);
  135. }
  136. printInt(d.age(), d.isValid(), 5);
  137. smartDelay(0);
  138. }
  139. static void printStr(const char *str, int len)
  140. {
  141. int slen = strlen(str);
  142. for (int i=0; i<len; ++i)
  143. M5.Lcd.print(i<slen ? str[i] : ' ');
  144. smartDelay(0);
  145. }