#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <TFT_eWidget.h>
#include <SoftwareSerial.h>
#include <TinyGPSPlus.h>
#include <Free_Fonts.h>
TFT_eSPI tft = TFT_eSPI();
MeterWidget speed = MeterWidget(&tft);
SoftwareSerial gpsSerial(21,22,true);
TinyGPSPlus gps;
#define GPSECHO true
#define LOOP_PERIOD 35 // Display updates every 35 ms
uint32_t timer = millis();
float maxSpeed = 0.00;
float speedValue = 0.0;
String lastNMEASentence = "";
float mapValue(float ip, float ipmin, float ipmax, float tomin, float tomax)
{
return tomin + (((tomax - tomin) * (ip - ipmin))/ (ipmax - ipmin));
}
void setup(void)
{
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
gpsSerial.begin(4800);
speed.setZones(0, 0, 0, 0, 0, 0, 0, 60); // Example here red starts at 75% and ends at 100% of full scale
String initText = "Initializing GPS...";
Serial.println(initText);
tft.setCursor((tft.width() / 2) - (initText.length() * 4), 1, 4);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(initText);
speed.analogMeter((tft.width() / 2) - (239 / 2),0, 0, 60, "MPH", "0", "15", "30", "45", "60");
}
void loop()
{
static uint32_t updateTime = 0;
while (gpsSerial.available() > 0)
gps.encode(gpsSerial.read());
float current;
if (millis() - updateTime >= LOOP_PERIOD){
updateTime = millis();
speedValue = gps.speed.mph();
if(speedValue > maxSpeed) maxSpeed = speedValue;
current = mapValue(speedValue, (float)0.0, (float)60.0, (float)0.0, (float)60.0);
speed.updateNeedle(current, 0);
tft.setCursor(0,tft.height() / 2, 4);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE,TFT_BLACK,true);
tft.setTextPadding(32);
tft.println((String)gps.speed.mph() + "mph ");
tft.setTextSize(1);
String maxSpeedText = "Max speed: " + (String)maxSpeed + " ";
tft.println(maxSpeedText);
String compassText = "Compass: " + (String)gps.course.deg() + " ";
tft.println(compassText);
String latlonText = "Location: " + (String)gps.location.lat() + ", " + (String)gps.location.lng() + " ";
tft.println(latlonText);
String satellitesText = "Satellites: " + (String)gps.satellites.value();
tft.println(satellitesText + " ");
}
}