Beitian BN-880 GPs and Compass Module Experiments - Part III - Tying It Up
So here it is, the schematic & code for the final assembly (well, final for now ;) )
A warning: the code is a quickly hashed together dog's breakfast of source. It works, but it's very ugly.
The GPS wiring is exactly the same as the first article, and the wiring for the GLCD
is exactly the same as the last article.
Here's the link to the video:
Here's the Source:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
// Variables for the GPS module
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
char zs[32] = "";
int x = 0;
int y = 0;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 8);
void setup(void) {
ss.begin(GPSBaud);
u8g2.begin();
u8g2.clearBuffer();
}
void loop(void) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_squeezed_r6_tr);
printInt(5, 15, gps.satellites.value(), gps.satellites.isValid(), 5);
printFloat(5, 25, gps.location.lat(), gps.location.isValid(), 10, 7);
printFloat(5, 35, gps.location.lng(), gps.location.isValid(), 10, 7);
printDateTime(5,55, gps.date, gps.time);
} while ( u8g2.nextPage() );
smartDelay(500);
}
/*----------------------------------------------------------------------*/
/* All functions/subroutines are below this comment section */
/*----------------------------------------------------------------------*/
// 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(int x, int y, float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
u8g2.drawStr(x, y, '*');
u8g2.drawStr(x, y, ' ');
}
else
{
dtostrf(val, len, prec, zs);
u8g2.drawStr(x, y, zs);
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)
u8g2.drawStr(x, y, ' ');
}
smartDelay(0);
}
static void printInt(int x, int y, 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] = ' ';
u8g2.drawStr(x, y, "Sats: ");
u8g2.drawStr(x+17, y, sz);
smartDelay(0);
}
static void printDateTime(int x, int y, TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
u8g2.drawStr(x, y, "Time: ********** ");
}
else
{
char sz[32];
sprintf(sz, "GMT Time: %02d/%02d/%02d : %02d:%02d:%02d", d.month(), d.day(), d.year(), t.hour(), t.minute(), t.second());
u8g2.drawStr(x, y, sz);
}
smartDelay(0);
}
Like I said: It's ugly.
じゃまた
0 comments:
Post a Comment