Adafruit GPS library sample with Xadow GPS and Xadow Oled

Dependencies:   MBed_Adafruit-GPS-Library SSD1308_128x64_I2C USBDevice mbed

Fork of AVC_gps by AVR Competition

Committer:
whatnick
Date:
Fri Nov 21 13:13:38 2014 +0000
Revision:
1:2387ce3e58c8
Parent:
0:59cfe30c337c
Initial commit of GPS parsing with Adafruit GPS library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gerardo_carmona 0:59cfe30c337c 1 #include "mbed.h"
whatnick 1:2387ce3e58c8 2 #include "pinmap.h"
whatnick 1:2387ce3e58c8 3 #include "SSD1308.h"
whatnick 1:2387ce3e58c8 4 #include "MBed_Adafruit_GPS.h"
whatnick 1:2387ce3e58c8 5
whatnick 1:2387ce3e58c8 6 Serial gps(P0_19,P0_18);
whatnick 1:2387ce3e58c8 7
whatnick 1:2387ce3e58c8 8 #define DEBUG
whatnick 1:2387ce3e58c8 9
whatnick 1:2387ce3e58c8 10 #ifdef DEBUG
whatnick 1:2387ce3e58c8 11 #include "USBSerial.h" // To use USB virtual serial, a driver is needed, check http://mbed.org/handbook/USBSerial
whatnick 1:2387ce3e58c8 12 #define LOG(args...) pc.printf(args)
whatnick 1:2387ce3e58c8 13 USBSerial pc;
whatnick 1:2387ce3e58c8 14 #else
whatnick 1:2387ce3e58c8 15 #define LOG(args...)
whatnick 1:2387ce3e58c8 16 #endif
whatnick 1:2387ce3e58c8 17
whatnick 1:2387ce3e58c8 18 #define HARD_SPI 1
whatnick 1:2387ce3e58c8 19 #define I2C_FREQ 100000
gerardo_carmona 0:59cfe30c337c 20
whatnick 1:2387ce3e58c8 21 I2C i2c(I2C_SDA, I2C_SCL);
whatnick 1:2387ce3e58c8 22 //Use Xadow OLED for display
whatnick 1:2387ce3e58c8 23 SSD1308 oled = SSD1308(i2c, SSD1308_SA0);
whatnick 1:2387ce3e58c8 24
whatnick 1:2387ce3e58c8 25 //GPS Data
whatnick 1:2387ce3e58c8 26
gerardo_carmona 0:59cfe30c337c 27
whatnick 1:2387ce3e58c8 28 int main()
whatnick 1:2387ce3e58c8 29 {
whatnick 1:2387ce3e58c8 30 Adafruit_GPS myGPS(&gps);
whatnick 1:2387ce3e58c8 31 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
whatnick 1:2387ce3e58c8 32 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
whatnick 1:2387ce3e58c8 33 const int refresh_Time = 1000; //refresh time in ms
whatnick 1:2387ce3e58c8 34
whatnick 1:2387ce3e58c8 35 myGPS.begin(9600);
whatnick 1:2387ce3e58c8 36 oled.clearDisplay();
whatnick 1:2387ce3e58c8 37 oled.writeString(0,0,"GPS Test");
whatnick 1:2387ce3e58c8 38 oled.writeString(1,0,"GPS Start");
whatnick 1:2387ce3e58c8 39
whatnick 1:2387ce3e58c8 40 wait(1);
whatnick 1:2387ce3e58c8 41
whatnick 1:2387ce3e58c8 42 refresh_Timer.start(); //starts the clock on the timer
whatnick 1:2387ce3e58c8 43
gerardo_carmona 0:59cfe30c337c 44 while (true) {
whatnick 1:2387ce3e58c8 45 c = myGPS.read(); //queries the GPS
whatnick 1:2387ce3e58c8 46
whatnick 1:2387ce3e58c8 47 if (c) {
whatnick 1:2387ce3e58c8 48 pc.printf("%c", c); //this line will echo the GPS data if not paused
whatnick 1:2387ce3e58c8 49 }
whatnick 1:2387ce3e58c8 50
whatnick 1:2387ce3e58c8 51 //check if we recieved a new message from GPS, if so, attempt to parse it,
whatnick 1:2387ce3e58c8 52 if ( myGPS.newNMEAreceived() ) {
whatnick 1:2387ce3e58c8 53 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
whatnick 1:2387ce3e58c8 54 continue;
gerardo_carmona 0:59cfe30c337c 55 }
gerardo_carmona 0:59cfe30c337c 56 }
whatnick 1:2387ce3e58c8 57
whatnick 1:2387ce3e58c8 58
whatnick 1:2387ce3e58c8 59
whatnick 1:2387ce3e58c8 60 //check if enough time has passed to warrant printing GPS info to screen
whatnick 1:2387ce3e58c8 61 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
whatnick 1:2387ce3e58c8 62 if (refresh_Timer.read_ms() >= refresh_Time) {
whatnick 1:2387ce3e58c8 63 refresh_Timer.reset();
whatnick 1:2387ce3e58c8 64 pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
whatnick 1:2387ce3e58c8 65 pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
whatnick 1:2387ce3e58c8 66 pc.printf("Fix: %d\n", (int) myGPS.fix);
whatnick 1:2387ce3e58c8 67 pc.printf("Quality: %d\n", (int) myGPS.fixquality);
whatnick 1:2387ce3e58c8 68 if (myGPS.fix) {
whatnick 1:2387ce3e58c8 69 pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
whatnick 1:2387ce3e58c8 70 pc.printf("Speed: %5.2f knots\n", myGPS.speed);
whatnick 1:2387ce3e58c8 71 pc.printf("Angle: %5.2f\n", myGPS.angle);
whatnick 1:2387ce3e58c8 72 pc.printf("Altitude: %5.2f\n", myGPS.altitude);
whatnick 1:2387ce3e58c8 73 pc.printf("Satellites: %d\n", myGPS.satellites);
whatnick 1:2387ce3e58c8 74 }
whatnick 1:2387ce3e58c8 75
whatnick 1:2387ce3e58c8 76 if (myGPS.fix) {
whatnick 1:2387ce3e58c8 77 char oled_str[20];
whatnick 1:2387ce3e58c8 78 oled.writeString(1,0,"GPS Lock ");
whatnick 1:2387ce3e58c8 79 sprintf(oled_str,"LAT:%f",myGPS.latitude);
whatnick 1:2387ce3e58c8 80 oled.writeString(2,0,oled_str);
whatnick 1:2387ce3e58c8 81 sprintf(oled_str,"LON:%f",myGPS.longitude);
whatnick 1:2387ce3e58c8 82 oled.writeString(3,0,oled_str);
whatnick 1:2387ce3e58c8 83 sprintf(oled_str,"ALT:%f",myGPS.altitude);
whatnick 1:2387ce3e58c8 84 oled.writeString(4,0,oled_str);
whatnick 1:2387ce3e58c8 85 } else {
whatnick 1:2387ce3e58c8 86 oled.writeString(1,0,"GPS Lost ");
whatnick 1:2387ce3e58c8 87 }
whatnick 1:2387ce3e58c8 88 }
gerardo_carmona 0:59cfe30c337c 89 }
whatnick 1:2387ce3e58c8 90 }