Quicksand micro-electronics / Mbed 2 deprecated QW-GPS

Dependencies:   mbed

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

Files at this revision

API Documentation at this revision

Comitter:
quicksand
Date:
Wed Mar 02 12:14:48 2016 +0000
Parent:
0:49858c4c3500
Commit message:
First version of a GPS example

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Oct 30 14:10:26 2015 +0000
+++ b/main.cpp	Wed Mar 02 12:14:48 2016 +0000
@@ -8,77 +8,107 @@
 InterruptIn SW2(PA_8);
 
 Ticker hartbeat;
-
-typedef struct{
-   bool busy;
-   char button;
-} TX_BUTTON;
+Ticker position_update;
 
-TX_BUTTON Button_tx;
-
-void beat() {
-    LED_0 = !LED_0;
-}
- 
 //Virtual serial port over USB
 Serial pc(USBTX, USBRX);
 Serial modem(PA_9, PA_10);
 
-void sw1interrupt(){
-        pc.printf("Button 1 pressed, sending sigfox message (command is AT$SF=42 55 54 54 4f 4e 20 31 00 00 00 00,2,0)\n");
-        modem.printf("AT$SF=42 55 54 54 4f 4e 20 31 00 00 00 00,2,0\n");
-        LED_1 = 0;
-        wait(0.25);
-        // Flush the echo of the command:
-        while(modem.readable()) modem.getc();
-        Button_tx.busy = true;
-        Button_tx.button = 1;
-} 
+char * response = "OK";
+char * responsePtr;
+bool commandogiven = false;
+bool commandofailed = true;
+int updateinterval_s = 30;
+
+// Send command and check if ok
+void command(char * commando)
+{
+    LED_1=0;
+    modem.printf(commando);
+    commandogiven = true;
+    commandofailed = true;
+}
+
+// Blinking LED Ticker
+void beat()
+{
+    LED_0 = !LED_0;
+}
+
+// Position transmission ticker
+void txpos()
+{
+    command("AT$GSND\n");
+}
 
-void sw2interrupt(){
-        pc.printf("Button 2 pressed, sending sigfox message (command is AT$SF=42 55 54 54 4f 4e 20 32 00 00 00 00,2,0)\n");
-        modem.printf("AT$SF=42 55 54 54 4f 4e 20 32 00 00 00 00,2,0\n");
+void sw1interrupt()
+{
+    //command("AT$GPS=1,16,0,65535,1,1\n");
+    if(updateinterval_s == 30)
+    {   
+        position_update.detach();
+        updateinterval_s = 600; // Updateinterval = 10 minutes
+        LED_3 = 1;
         LED_2 = 0;
-        wait(0.25);
-        // Flush the echo of the command:
-        while(modem.readable()) modem.getc();
-        Button_tx.busy = true;
-        Button_tx.button = 2;           
-} 
+        position_update.attach(&txpos, updateinterval_s);
+    }
+    else
+    {   
+        position_update.detach();
+        updateinterval_s = 30; // Updateinterval = 30 seconds
+        LED_3 = 0;
+        LED_2 = 1;
+        position_update.attach(&txpos, updateinterval_s);
+    }
+}
 
-int main() {
-    
+void sw2interrupt()
+{
+    command("AT$GSND\n");
+}
+
+int main()
+{
+    wait(3);
     LED_0 = 1;
     LED_1 = 1;
     LED_2 = 1;
-    LED_3 = 1;
+    LED_3 = 0;
     hartbeat.attach(&beat, 0.5);
-    Button_tx.busy = false;
-    Button_tx.button = 0;    
+    position_update.attach(&txpos, updateinterval_s);
     SW2.fall(&sw1interrupt);
     SW1.fall(&sw2interrupt);
-    char responsebuffer[2];
-    char c; 
-    while(1) {   
-        if(pc.readable()) {
-            modem.putc(pc.getc());
-        }
-        
-        if(modem.readable()) {
-            c = modem.getc();
-            responsebuffer[0] = responsebuffer[1];
-            responsebuffer[1] = c;
-            if(Button_tx.busy)
-            {
-                if(responsebuffer[0] == 'O' && responsebuffer[1] == 'K' )
-                {
-                    // Everything went fine, turn off the LED
-                    Button_tx.busy = false;
-                    if(Button_tx.button == 1) LED_1 = 1;
-                    if(Button_tx.button == 2) LED_2 = 1;
-                }    
-            }    
-            pc.putc(c);
+    command("AT$GPS=1,16,0,65535,1,1\n");
+    while(1) {
+        if(!commandogiven) {
+            if(pc.readable()) {
+                modem.putc(pc.getc());
+            }
+
+            if(modem.readable()) {
+                pc.putc(modem.getc());
+            }
+        } else {
+            int c, i;
+            while ((c = modem.getc()) >= 0 && commandogiven && i < 10000) {
+                if ((char) c == *responsePtr)
+                    responsePtr++;
+                else
+                    responsePtr = response;
+                if (*responsePtr == 0) {
+                    LED_1=1;
+                    commandogiven = false;
+                }
+                i++;
+            }
+            if(commandogiven == true) {
+                commandogiven = false;
+                commandofailed = true;
+                LED_1=1;
+            } else {
+                commandofailed = false;
+            }
+
         }
     }
 }