An example for TinyGPSPlus

Dependencies:   TinyGPSPlus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 DigitalOut led1(LED1);
00003 
00004 #include "TinyGPSPlus.h"
00005 TinyGPSPlus gps_parser;
00006 static const int GPS_BAUD_RATE = 9600;
00007 Serial gps(PA_15, PB_7);
00008 char buf[128];
00009 
00010 void callback() {
00011     char c = gps.getc();
00012     switch (c) {
00013         case '\n':
00014             if (gps_parser.satellites.isValid() && gps_parser.satellites.value() > 3 && gps_parser.hdop.hdop() > 0) {
00015                 snprintf(buf, 128, "{\"lat\":%lf,\"lng\":%lf}", gps_parser.location.lat(), gps_parser.location.lng());
00016             } else {
00017                 snprintf(buf, 128, "Satellites: %lu, time: %04d-%02d-%02dT%02d:%02d:%02d.%02d",
00018                 gps_parser.satellites.value(), gps_parser.date.year(), gps_parser.date.month(), gps_parser.date.day(),
00019                 gps_parser.time.hour(), gps_parser.time.minute(), gps_parser.time.second(), gps_parser.time.centisecond());
00020             }
00021             printf("%s\r\n", buf);
00022             break;
00023         default:
00024             gps_parser.encode(c);
00025             break;
00026     }
00027     led1 = !led1;
00028 }
00029 
00030 int main()
00031 {
00032     gps.baud(GPS_BAUD_RATE);
00033     gps.format(8, Serial::None, 1);
00034     gps.attach(&callback, Serial::RxIrq);
00035     wait(osWaitForever);
00036 }