Graham Cantin / MODGPS

Dependents:   Telnet_server

Fork of MODGPS by Andy K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example1.cpp Source File

example1.cpp

00001 #ifdef COMPILE_EXAMPLE_CODE_MODGPS
00002 
00003 #include "mbed.h"
00004 #include "GPS.h"
00005 
00006 Serial pc(USBTX, USBRX);
00007 DigitalOut led1(LED1);
00008 
00009 // SET THIS.
00010 // Create an instance of the GPS object. You will need to
00011 // set p25 to whichever Serial RX pin you have connected
00012 // your GPS module to.
00013 GPS gps(NC, GPSRX);
00014 
00015 // 0.1 second flash of LED2
00016 DigitalOut led2(LED2);
00017 Timeout t2;
00018 void t2out(void) { led2 = 0; }
00019 void blip2(void) { led2 = 1; t2.attach(&t2out, 0.1); }
00020 
00021 // 0.1 second flash of LED3
00022 DigitalOut led3(LED3);
00023 Timeout t3;
00024 void t3out(void) { led3 = 0; }
00025 void blip3(void) { led3 = 1; t3.attach(&t3out, 0.1); }
00026 
00027 // 0.1 second flash of LED4
00028 DigitalOut led4(LED4);
00029 
00030 Timeout t4;
00031 void t4out(void) { led4 = 0; }
00032 void blip4(void) { led4 = 1; t4.attach(&t4out, 0.1); }
00033 
00034 int main() {
00035     GPS_Time q1;
00036     
00037     // SET THIS.
00038     // Ensure you set the baud rate to match your serial
00039     // communications to your PC/Max/Linux host so you
00040     // can read the messages.
00041     pc.baud(PCBAUD);
00042     
00043     // SET THIS.
00044     // Most GPS modules use 9600,8,n,1 so that's what
00045     // we default to here. Ensure your GPS module matches
00046     // this, otherwise set it to match.
00047     gps.baud(GPSBUAD);
00048     gps.format(8, GPS::None, 1);
00049     
00050     // OPTIONAL
00051     // If you GPS has a 1 pulse per second output you can
00052     // connect it to an Mbed pin. Here you specify what pin
00053     // and on what "edge" teh signal is active. If your GPS
00054     // module has a rising edge at the one second point then
00055     // use GPS::ppsRise
00056     #ifdef PPSPIN
00057     gps.ppsAttach(PPSPIN, GPS::ppsFall );
00058     #endif
00059 
00060     // Sample of a callback to a function when the 1PPS activates.
00061     // For this example, we flash LED2 for 0.1 second.
00062     gps.attach_pps(&blip2);
00063     
00064     // Sample of a callback to a function when a NMEA GGA message is recieved.
00065     // For this example, we flash LED2 for 0.1 second.
00066     gps.attach_gga(&blip3);
00067     
00068     // Sample of a callback to a function when a NMEA RMC message is recieved.
00069     // For this example, we flash LED2 for 0.1 second.
00070     gps.attach_rmc(&blip4);
00071 
00072     while(1) {
00073         // Every 3 seconds, flip LED1 and print the basic GPS info.
00074         wait(3);
00075         led1 = 1;
00076         
00077         // Demonstrate how to find out the GPS location co-ords.
00078         pc.printf("Method 1. Lat = %.4f ", gps.latitude());
00079         pc.printf("Lon = %.4f ", gps.longitude());
00080         pc.printf("Alt = %.4f ", gps.altitude());
00081         
00082         // Gran a snapshot of the current time.
00083         gps.timeNow(&q1);
00084         pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n", 
00085             q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year);
00086             
00087         // Alternative method that does the same thing.        
00088         pc.printf("Method 2. Lat = %.4f ", gps.latitude());
00089         pc.printf("Lon = %.4f ", gps.longitude());
00090         pc.printf("Alt = %.4f ", gps.altitude());
00091         
00092         GPS_Time *q2 = gps.timeNow();
00093         pc.printf("%02d:%02d:%02d %02d/%02d/%04d\r\n\n", 
00094             q2->hour, q2->minute, q2->second, q2->day, q2->month, q2->year);
00095         delete(q2);        
00096         led1 = 0;
00097     }
00098 }
00099 
00100 #endif