I have a lot of boards and usually need to re-use code even for different projects.

m5-gps.ino 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <TinyGPS++.h>
  2. #include <wiring_private.h>
  3. #include "TFT_eSPI.h"
  4. TFT_eSPI tft;
  5. static const uint32_t GPSBaud = 115200;
  6. // The TinyGPS++ object
  7. TinyGPSPlus gps;
  8. // The serial connection to the GPS device - Left side Grove connector.
  9. // Left side Grove connector shares pins with I2C1 of 40 pin connector.
  10. static Uart Serial3(&sercom3, PIN_WIRE_SCL, PIN_WIRE_SDA, SERCOM_RX_PAD_1, UART_TX_PAD_0);
  11. void setup()
  12. {
  13. Serial.begin(115200);
  14. tft.begin();
  15. tft.setRotation(3);
  16. tft.fillScreen(TFT_BLACK);
  17. tft.setFreeFont(&FreeSans9pt7b);
  18. tft.setTextColor(TFT_WHITE);
  19. Serial3.begin(GPSBaud);
  20. pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
  21. pinPeripheral(PIN_WIRE_SCL, PIO_SERCOM_ALT);
  22. }
  23. void loop()
  24. {
  25. // This sketch displays information every time a new sentence is correctly encoded.
  26. while (Serial3.available() > 0)
  27. if (gps.encode(Serial3.read()))
  28. displayInfo();
  29. delay(3000);
  30. if (millis() > 5000 && gps.charsProcessed() < 10)
  31. {
  32. Serial.println(F("No GPS detected: check wiring."));
  33. tft.drawString("No GPS detected: check wiring.", 10, 10);
  34. while(true);
  35. }
  36. }
  37. void displayInfo()
  38. {
  39. Serial.print(F("Location: "));
  40. if (gps.location.isValid())
  41. {
  42. Serial.print(gps.location.lat(), 6);
  43. Serial.print(F(","));
  44. Serial.print(gps.location.lng(), 6);
  45. String text = String(gps.location.lat(), 6) + ", " + String(gps.location.lng(), 6);
  46. tft.fillRect(10, 30, 190, 30, TFT_BLACK);
  47. tft.drawString(text, 10, 30);
  48. }
  49. else
  50. {
  51. Serial.print(F("INVALID"));
  52. }
  53. Serial.print(F(" Date/Time: "));
  54. if (gps.date.isValid())
  55. {
  56. Serial.print(gps.date.month());
  57. Serial.print(F("/"));
  58. Serial.print(gps.date.day());
  59. Serial.print(F("/"));
  60. Serial.print(gps.date.year());
  61. }
  62. else
  63. {
  64. Serial.print(F("INVALID"));
  65. }
  66. Serial.print(F(" "));
  67. if (gps.time.isValid())
  68. {
  69. if (gps.time.hour() < 10) Serial.print(F("0"));
  70. Serial.print(gps.time.hour());
  71. Serial.print(F(":"));
  72. if (gps.time.minute() < 10) Serial.print(F("0"));
  73. Serial.print(gps.time.minute());
  74. Serial.print(F(":"));
  75. if (gps.time.second() < 10) Serial.print(F("0"));
  76. Serial.print(gps.time.second());
  77. Serial.print(F("."));
  78. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  79. Serial.print(gps.time.centisecond());
  80. }
  81. else
  82. {
  83. Serial.print(F("INVALID"));
  84. }
  85. Serial.println();
  86. }
  87. void SERCOM3_0_Handler()
  88. {
  89. Serial3.IrqHandler();
  90. }
  91. void SERCOM3_1_Handler()
  92. {
  93. Serial3.IrqHandler();
  94. }
  95. void SERCOM3_2_Handler()
  96. {
  97. Serial3.IrqHandler();
  98. }
  99. void SERCOM3_3_Handler()
  100. {
  101. Serial3.IrqHandler();
  102. }