Κώδικας:
/* This sketch is based on TinyGPS++ and MCUFRIEND_kbv libraries examples.
* Many thanks to creators.
* By Akritas.
*/
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans9pt7b.h>
#include <MCUFRIEND_kbv.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 10, TXPin = 11;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0x7BEF
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
ss.begin(GPSBaud);
tft.reset();
tft.begin(0x9325); // define TFT controller
tft.setRotation(0); // portrait
tft.fillScreen(BLUE);
tft.setTextSize(1);
tft.setTextColor(WHITE);
tft.setFont(&FreeSans9pt7b);
tft.fillRect(5, 5 + 5, 230, 22, BLUE);
tft.setCursor(5 ,5 + 18);
tft.print("Date/Time: ");
tft.fillRect(5, 65 + 5, 230, 22, BLUE);
tft.setCursor(5, 65 + 18);
tft.print("Location: ");
tft.fillRect(5, 125 + 5, 230, 22, BLUE);
tft.setCursor(5, 125 + 18);
tft.print("Course/Speed: ");
tft.fillRect(5, 185 + 5, 230, 22, BLUE);
tft.setCursor(5, 185 + 18);
tft.print("Distance/Course To Fix:");
tft.fillRect(5, 245 + 5, 230, 22, BLUE);
tft.setCursor(5, 245 + 18);
tft.print("Altitude:");
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
tft.fillRect(5, 5 + 5, 230, 22, BLUE); // change only x, y, length
tft.setCursor(5 ,5 + 18); // change only x, y
tft.print("No GPS detected!");
while(true);
}
}
void displayInfo()
{
if (gps.date.isValid())
{
tft.fillRect(5, 30 + 5, 230, 22, BLUE);
tft.setCursor(5, 30 + 18);
tft.print(gps.date.day());
tft.print("/");
tft.print(gps.date.month());
tft.print("/");
tft.print(gps.date.year());
tft.print(" ");
}
else
{
tft.fillRect(5, 30 + 5, 230, 22, BLUE);
tft.setCursor(5, 30 + 18);
tft.print(" INVALID");
}
tft.print(" / ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) tft.print("0");
tft.print(gps.time.hour());
tft.print(":");
if (gps.time.minute() < 10) tft.print("0");
tft.print(gps.time.minute());
//tft.print(":");
//if (gps.time.second() < 10) tft.print("0");
//tft.print(gps.time.second());
//tft.print(".");
//if (gps.time.centisecond() < 10) tft.print("0");
//tft.print(gps.time.centisecond());
}
else
{
tft.print("INVALID");
}
if (gps.location.isValid())
{
tft.fillRect(5, 90 + 5, 230, 22, BLUE);
tft.setCursor(5 ,90 + 18);
tft.print(gps.location.lat(), 6);
tft.print(" / ");
tft.print(gps.location.lng(), 6);
}
else
{
tft.fillRect(5, 90 + 5, 230, 22, BLUE);
tft.setCursor(5, 90 + 18);
tft.print("INVALID");
}
if ((gps.course.isValid()) and (gps.speed.isValid()))
{
tft.fillRect(5, 150 + 5, 230, 22, BLUE);
tft.setCursor(5, 150 + 18);
tft.print(gps.course.deg());
tft.print(" deg / ");
tft.print(gps.speed.kmph());
tft.print(" kmph");
}
else
{
tft.fillRect(5, 150 + 5, 230, 22, BLUE);
tft.setCursor(5, 150 + 18);
tft.print("INVALID");
}
if (gps.location.isValid())
{
static const double FIX_LAT = 40.χχχχχχ, FIX_LON = 25.χχχχχχ;
double distanceToFix =
TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
FIX_LAT,
FIX_LON);
double courseToFix =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
FIX_LAT,
FIX_LON);
tft.fillRect(5, 210 + 5, 230, 22, BLUE);
tft.setCursor(5, 210 + 18);
tft.print(distanceToFix/1000, 3);
tft.print(" km");
tft.print(" / ");
tft.print(courseToFix, 2);
tft.print(" deg");
}
else
{
tft.fillRect(5, 210 + 5, 230, 22, BLUE);
tft.setCursor(5, 210 + 18);
tft.print("INVALID");
}
if (gps.altitude.isValid())
{
tft.fillRect(5, 270 + 5, 230, 22, BLUE);
tft.setCursor(5, 270 + 18);
tft.print(gps.altitude.meters());
tft.print(" mtrs");
}
else
{
tft.fillRect(5, 270 + 5, 230, 22, BLUE);
tft.setCursor(5, 270 + 18);
tft.print("INVALID");
}
delay(5000);
}
Το αποτέλεσμα