I'm playing around with a Neo-8M and an ILI9225 2" TFT display.
I have managed to get the values displaying on the screen. example 21.3 but I would like it to show 21.34567. I have tried everything I could find. I found one bit of code but the result was 213456.7.
Below the my sketch
#include <TinyGPS++.h>
#include <TinyGPSPlus.h>
#include <AltSoftSerial.h>
#include "SPI.h"
#include "TFT_22_ILI9225.h"
#define TFT_RST 6
#define TFT_RS 7
#define TFT_CS 10 // SS
#define TFT_SDI 11 // MOSI
#define TFT_CLK 13 // SCK
#define TFT_LED 3 // 0 if wired to +5V directly
#define TFT_BRIGHTNESS 200 // Initial brightness of TFT backlight (optional)
static const int RXPin = 8, TXPin = 9;
static const uint32_t GPSBaud = 9600;
int gpswaitT = 1000;
// The TinyGPSPlus object
TinyGPSPlus gps;
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS);
// The serial connection to the GPS device
AltSoftSerial ss(RXPin, TXPin);
int PRETORIA_LAT = -25.731340;
int PRETORIA_LON = 28.218370;
int16_t x = 0, y = 0, w, h;
int orientation ();
void setup()
{
Serial.begin(115200);
ss.begin(GPSBaud);
Serial.println(F("FullExample.ino"));
Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to Pretoria ---- RX RX Fail"));
Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
#if defined(ESP32)
hspi.begin();
tft.begin(hspi);
#else
tft.begin();
#endif
tft.clear();
tft.setOrientation (3);
}
void loop()
{
printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
printInt(gps.location.age(), gps.location.isValid(), 5);
printDateTime(gps.date, gps.time);
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
unsigned long distanceKmToPretoria =
(unsigned long)TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
PRETORIA_LAT,
PRETORIA_LON) / 1000;
printInt(distanceKmToPretoria, gps.location.isValid(), 9);
double courseToPretoria =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
PRETORIA_LAT,
PRETORIA_LON);
printFloat(courseToPretoria, gps.location.isValid(), 7, 2);
const char *cardinalToPretoria = TinyGPSPlus::cardinal(courseToPretoria);
printStr(gps.location.isValid() ? cardinalToPretoria : "*** ", 6);
printInt(gps.charsProcessed(), true, 6);
printInt(gps.sentencesWithFix(), true, 10);
printInt(gps.failedChecksum(), true, 9);
Serial.println();
smartDelay(gpswaitT);
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
float lt = gps.location.lat();
float ln = gps.location.lng();
tft.setFont(Terminal11x16);
tft.drawText(10, 5, "Lat:", COLOR_BLUE);
drawTempInColour(120, 5, lt, GetColourForTemp(lt));
tft.setFont(Terminal11x16);
tft.drawText(10, 25, "Lon:", COLOR_BLUE);
drawTempInColour(120, 25, ln, GetColourForTemp(ln));
}
void drawTempInColour(byte x, byte y, float temp, unsigned int colour)
{
char buf[16];
dtostrf(temp, 6, 1, buf);
tft.drawText(x, y, buf, colour);
}
unsigned int GetColourForTemp(float temp)
{
unsigned int Colour = COLOR_CYAN;
return Colour;
}
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i = flen; i < len; ++i)
Serial.print(' ');
}
smartDelay(0);
}
static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i = strlen(sz); i < len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len - 1] = ' ';
Serial.print(sz);
smartDelay(0);
}
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}
if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}
static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i = 0; i < len; ++i)
Serial.print(i < slen ? str[i] : ' ');
smartDelay(0);
}
Any help will be very much appreciated.