Shigeru Fujiwara
/
TinyGPSPlus-example
An example for TinyGPSPlus
main.cpp@1:351d281f437f, 2019-02-02 (annotated)
- Committer:
- aoba
- Date:
- Sat Feb 02 07:17:19 2019 +0000
- Revision:
- 1:351d281f437f
- Parent:
- 0:eff8db5e950d
updated mbed to mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aoba | 0:eff8db5e950d | 1 | #include "mbed.h" |
aoba | 0:eff8db5e950d | 2 | DigitalOut led1(LED1); |
aoba | 0:eff8db5e950d | 3 | |
aoba | 0:eff8db5e950d | 4 | #include "TinyGPSPlus.h" |
aoba | 0:eff8db5e950d | 5 | TinyGPSPlus gps_parser; |
aoba | 0:eff8db5e950d | 6 | static const int GPS_BAUD_RATE = 9600; |
aoba | 0:eff8db5e950d | 7 | Serial gps(PA_15, PB_7); |
aoba | 0:eff8db5e950d | 8 | char buf[128]; |
aoba | 0:eff8db5e950d | 9 | |
aoba | 0:eff8db5e950d | 10 | void callback() { |
aoba | 0:eff8db5e950d | 11 | char c = gps.getc(); |
aoba | 0:eff8db5e950d | 12 | switch (c) { |
aoba | 0:eff8db5e950d | 13 | case '\n': |
aoba | 0:eff8db5e950d | 14 | if (gps_parser.satellites.isValid() && gps_parser.satellites.value() > 3 && gps_parser.hdop.hdop() > 0) { |
aoba | 0:eff8db5e950d | 15 | snprintf(buf, 128, "{\"lat\":%lf,\"lng\":%lf}", gps_parser.location.lat(), gps_parser.location.lng()); |
aoba | 0:eff8db5e950d | 16 | } else { |
aoba | 0:eff8db5e950d | 17 | snprintf(buf, 128, "Satellites: %lu, time: %04d-%02d-%02dT%02d:%02d:%02d.%02d", |
aoba | 0:eff8db5e950d | 18 | gps_parser.satellites.value(), gps_parser.date.year(), gps_parser.date.month(), gps_parser.date.day(), |
aoba | 0:eff8db5e950d | 19 | gps_parser.time.hour(), gps_parser.time.minute(), gps_parser.time.second(), gps_parser.time.centisecond()); |
aoba | 0:eff8db5e950d | 20 | } |
aoba | 0:eff8db5e950d | 21 | printf("%s\r\n", buf); |
aoba | 0:eff8db5e950d | 22 | break; |
aoba | 0:eff8db5e950d | 23 | default: |
aoba | 0:eff8db5e950d | 24 | gps_parser.encode(c); |
aoba | 0:eff8db5e950d | 25 | break; |
aoba | 0:eff8db5e950d | 26 | } |
aoba | 0:eff8db5e950d | 27 | led1 = !led1; |
aoba | 0:eff8db5e950d | 28 | } |
aoba | 0:eff8db5e950d | 29 | |
aoba | 0:eff8db5e950d | 30 | int main() |
aoba | 0:eff8db5e950d | 31 | { |
aoba | 0:eff8db5e950d | 32 | gps.baud(GPS_BAUD_RATE); |
aoba | 0:eff8db5e950d | 33 | gps.format(8, Serial::None, 1); |
aoba | 0:eff8db5e950d | 34 | gps.attach(&callback, Serial::RxIrq); |
aoba | 1:351d281f437f | 35 | wait(osWaitForever); |
aoba | 0:eff8db5e950d | 36 | } |