Examplecode for GPS functionality on the QW-GPS-DEVKIT.

Dependencies:   mbed

Fork of HelloWorld - QW Development kit by Quicksand micro-electronics

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut LED_0 (PB_6);
00004 DigitalOut LED_1 (PA_7);
00005 DigitalOut LED_2 (PA_6);
00006 DigitalOut LED_3 (PA_5);
00007 InterruptIn SW1(PB_10);
00008 InterruptIn SW2(PA_8);
00009 
00010 Ticker hartbeat;
00011 Ticker position_update;
00012 
00013 //Virtual serial port over USB
00014 Serial pc(USBTX, USBRX);
00015 Serial modem(PA_9, PA_10);
00016 
00017 char * response = "OK";
00018 char * responsePtr;
00019 bool commandogiven = false;
00020 bool commandofailed = true;
00021 int updateinterval_s = 30;
00022 
00023 // Send command and check if ok
00024 void command(char * commando)
00025 {
00026     LED_1=0;
00027     modem.printf(commando);
00028     commandogiven = true;
00029     commandofailed = true;
00030 }
00031 
00032 // Blinking LED Ticker
00033 void beat()
00034 {
00035     LED_0 = !LED_0;
00036 }
00037 
00038 // Position transmission ticker
00039 void txpos()
00040 {
00041     command("AT$GSND\n");
00042 }
00043 
00044 void sw1interrupt()
00045 {
00046     //command("AT$GPS=1,16,0,65535,1,1\n");
00047     if(updateinterval_s == 30)
00048     {   
00049         position_update.detach();
00050         updateinterval_s = 600; // Updateinterval = 10 minutes
00051         LED_3 = 1;
00052         LED_2 = 0;
00053         position_update.attach(&txpos, updateinterval_s);
00054     }
00055     else
00056     {   
00057         position_update.detach();
00058         updateinterval_s = 30; // Updateinterval = 30 seconds
00059         LED_3 = 0;
00060         LED_2 = 1;
00061         position_update.attach(&txpos, updateinterval_s);
00062     }
00063 }
00064 
00065 void sw2interrupt()
00066 {
00067     command("AT$GSND\n");
00068 }
00069 
00070 int main()
00071 {
00072     wait(3);
00073     LED_0 = 1;
00074     LED_1 = 1;
00075     LED_2 = 1;
00076     LED_3 = 0;
00077     hartbeat.attach(&beat, 0.5);
00078     position_update.attach(&txpos, updateinterval_s);
00079     SW2.fall(&sw1interrupt);
00080     SW1.fall(&sw2interrupt);
00081     command("AT$GPS=1,16,0,65535,1,1\n");
00082     while(1) {
00083         if(!commandogiven) {
00084             if(pc.readable()) {
00085                 modem.putc(pc.getc());
00086             }
00087 
00088             if(modem.readable()) {
00089                 pc.putc(modem.getc());
00090             }
00091         } else {
00092             int c, i;
00093             while ((c = modem.getc()) >= 0 && commandogiven && i < 10000) {
00094                 if ((char) c == *responsePtr)
00095                     responsePtr++;
00096                 else
00097                     responsePtr = response;
00098                 if (*responsePtr == 0) {
00099                     LED_1=1;
00100                     commandogiven = false;
00101                 }
00102                 i++;
00103             }
00104             if(commandogiven == true) {
00105                 commandogiven = false;
00106                 commandofailed = true;
00107                 LED_1=1;
00108             } else {
00109                 commandofailed = false;
00110             }
00111 
00112         }
00113     }
00114 }