今天给大家带来的产品是一款GPS速度计,当然,它可不是计算步行速度的,它可以用在车里。听到这儿肯定许多人会感到奇怪,车里不是有仪表盘吗?难道你开的是自行车啊?是啊,但很多人的单任务运行的大脑总觉得简单易懂的数字速度计更符合我的要求,而且可以直接把它固定在面前。
#include //math library to use for round function
#include "TinyGPS.h" //GPS module library
#include // LCD library
TinyGPS gps; //create a gps object
float fLat, fLong; //floats for longitude and latitude; will be used to determine whether the GPS data is up to date or not
unsigned long fix_age; // returns +- latitude/longitude in degrees
int digit1, digit2, digit3; //integers to hold the three digits making up the speed
int iSpeed; //integer to hold the speed value from the GPS module
void setup()
{
Serial.begin(9600); //start the serial communication
// initialise the library, non inverted writes pixels onto a clear screen
GLCD.Init(NON_INVERTED);
}
void loop()
{
while (Serial.available())
{
//incoming serial data from GPS
int c = Serial.read();
if (gps.encode(c))
{
// process new gps info here, in case you want to display more details
}
}
gps.f_get_position(&fLat, &fLong, &fix_age); //get longitude and latitude, used to find if data is up to date
if (fix_age == TinyGPS::GPS_INVALID_AGE || fix_age > 2000)
{
//data was not updated for some time, assume that GPS connection is lost
iSpeed = 0;
}
else
{
//GPS connection is up to date, get the speed information, and round it to closest integer value
iSpeed = round(gps.f_speed_kmph()); // speed in km/h
}
//when not moving, the GPS module will still read some "speed" value, not necessarily zero
//in that case assume the car is not moving; only display values greater than 2
if (iSpeed == 1) iSpeed = 0;
displaySpeed(iSpeed); //call function to display the speed, digit by digit
}
void displaySpeed(int iSpeed)
{
//"simple" maths to extract each digit value
digit1 = iSpeed / 100;
iSpeed = iSpeed - (digit1 * 100);
digit2 = iSpeed / 10;
digit3 = iSpeed - (digit2 *10);
//display "blank" bitmap when necessary, instead of number zero
if (digit2 == 0 && digit1 == 0) digit2 = -1; //digit 2 is blank
if (digit1 == 0) digit1 = -1; //digit 1 is blank
//call function to display each digit, at their required position
drawDigit(digit1, 0);
drawDigit(digit2, 45);
drawDigit(digit3, 90);
}
void drawDigit(int digit, int pos)
{
switch (digit)
{
case 0:
GLCD.DrawBitmap(Number0, pos, 0);
break;
case 1:
GLCD.DrawBitmap(Number1, pos, 0);
break;
case 2:
GLCD.DrawBitmap(Number2, pos, 0);
break;
case 3:
GLCD.DrawBitmap(Number3, pos, 0);
break;
case 4:
GLCD.DrawBitmap(Number4, pos, 0);
break;
case 5:
GLCD.DrawBitmap(Number5, pos, 0);
break;
case 6:
GLCD.DrawBitmap(Number6, pos, 0);
break;
case 7:
GLCD.DrawBitmap(Number7, pos, 0);
break;
case 8:
GLCD.DrawBitmap(Number8, pos, 0);
break;
case 9:
GLCD.DrawBitmap(Number9, pos, 0);
break;
default:
GLCD.DrawBitmap(NumberBlank, pos, 0);
break;
}
}