default folder relocation

Dependencies:   ublox-cellular-driver-gen gnss ublox-cellular-base ublox-at-cellular-interface-ext

Revision:
4:13a84f6cc800
Parent:
1:628f51c3511f
Child:
5:540b2d19fc5c
--- a/main.cpp	Wed Jun 07 23:06:04 2017 +0000
+++ b/main.cpp	Thu Jun 08 09:11:45 2017 +0100
@@ -25,7 +25,7 @@
 // Network credentials.  You should set this according to your
 // network/SIM card.  For C030 boards, leave the parameters as NULL
 // otherwise, if you do not know the APN for your network, you may
-// either try the failry common "internet" for the APN (and leave the
+// either try the fairly common "internet" for the APN (and leave the
 // username and password NULL), or you may leave all three as NULL and then
 // a lookup will be attempted for a small number of known networks
 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
@@ -38,14 +38,37 @@
 DigitalOut ledGreen(LED2, 1);
 DigitalOut ledBlue(LED3, 1);
 
-static void pulseBlue() {
+// The user button
+volatile bool buttonPressed = false;
+
+static void good() {
+    ledGreen = 0;
+    ledBlue = 1;
+    ledRed = 1;
+}
+
+static void bad() {
+    ledRed = 0;
+    ledGreen = 1;
+    ledBlue = 1;
+}
+
+static void event() {
     ledBlue = 0;
     ledRed = 1;
     ledGreen = 1;
+}
+
+static void pulseEvent() {
+    event();
     wait_ms(500);
-    ledGreen = 0;
+    good();
+}
+
+static void ledOff() {
     ledBlue = 1;
     ledRed = 1;
+    ledGreen = 1;
 }
 
 static void printCellLocateData(UbloxATCellularInterfaceExt::CellLocData *pData)
@@ -81,10 +104,16 @@
     printf("  direction:          %d degree(s)\n", pData->direction);
     printf("  vertical accuracy:  %d metre(s)/second\n", pData->speed);
     printf("  satellite(s) used:  %d\n", pData->svUsed);
-    printf("I am here:            "
+    printf("I am here: "
            "https://maps.google.com/?q=%.5f,%.5f\n", pData->latitude, pData->longitude);       
 }
 
+static void cbButton()
+{
+    buttonPressed = true;
+    pulseEvent();
+}
+
 /* This example program for the u-blox C030 and C027 boards instantiates
  * the UbloxAtCellularInterface to do FTP, HTTP and CellLocate operations.
  * It uses test.rebex.net for FTP testing and mbed.org for HTTP GET testing.
@@ -103,30 +132,29 @@
     int httpProfile;
     int numRes;
     GnssSerial gnssSerial;
+    InterruptIn userButton(SW0);
+    
+    // Attach a function to the user button
+    userButton.rise(&cbButton);
     
     // Power up GNSS for the Cell Locate bit
     gnssSerial.init();
-    
-    ledGreen = 0;
-    ledBlue = 1;
-    ledRed = 1;
- 
-    printf ("Starting up, please wait up to 180 seconds for network registration to complete...\n");
+    good();
+    printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
     if (interface->init(PIN)) {
-        pulseBlue();
-        printf ("Connecting to the packet network...\n");
+        pulseEvent();
+        printf("Registered, connecting to the packet network...\n");
         for (int x = 0; interface->connect(PIN, APN, USERNAME, PASSWORD) != 0; x++) {
             if (x > 0) {
-                printf ("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
-                ledRed = 0;
-                ledGreen = 1;
-                ledBlue = 1;
+                bad();
+                printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
             }
         }
-        pulseBlue();
+        pulseEvent();
 
         // FTP OPERATIONS
         // Reset FTP parameters to default then set things up
+        printf("=== FTP ===\n");
         interface->ftpResetPar();
         interface->ftpSetTimeout(60000);
         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_SERVER_NAME, "test.rebex.net");
@@ -135,36 +163,37 @@
         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_MODE, "1");
 
         // Log into the FTP server
-        printf ("Logging into FTP server \"test.rebex.net\"...\n");
+        printf("Logging into FTP server \"test.rebex.net\"...\n");
         err = interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_LOGIN);
         if (err == NULL) {
-            pulseBlue();
+            pulseEvent();
              // Get a directory listing from the server
              if (interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_LS,
                                       NULL, NULL, 0, buf, sizeof (buf)) == NULL) {
-                pulseBlue();
-                printf ("Directory listing of FTP server:\n%s", buf);
+                pulseEvent();
+                printf ("Directory listing of FTP server:\n"
+                        "--------------------------------\n%s"
+                        "--------------------------------\n", buf);
              }
              // FTP GET a file known to be on test.rebex.net into the module file system
              if (interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_GET_FILE, "readme.txt") == NULL) {
-                pulseBlue();
+                pulseEvent();
                 // Read the file from the module file system into buf
                 if (interface->readFile("readme.txt", buf, sizeof (buf))) {
-                    printf ("FTP GET of file \"readme.txt\" completed.  The file contains:\n"
-                            "-------------------------------------------------------------\n%s"
-                            "-------------------------------------------------------------\n", buf);
+                    printf("FTP GET of file \"readme.txt\" completed.  The file contained:\n"
+                           "-------------------------------------------------------------\n%s"
+                           "-------------------------------------------------------------\n", buf);
                 }
              }            
         } else {
+            bad();
             printf ("Unable to log in, error class %d, error code %d.\n", err->eClass, err->eCode);
-            ledRed = 0;
-            ledGreen = 1;
-            ledBlue = 1;
         }
         
         // HTTP OPERATIONS
         // Set up HTTP parameters
-        printf ("Performing HTTP GET on \"mbed.org\"...\n");
+        printf("=== HTTP ===\n");
+        printf("Performing HTTP GET on \"mbed.org\"...\n");
         httpProfile = interface->httpAllocProfile();
         interface->httpSetTimeout(httpProfile, 30000);
         interface->httpSetPar(httpProfile, UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "mbed.org");
@@ -175,24 +204,23 @@
                                      NULL, NULL, 0, NULL,
                                      buf, sizeof (buf));
         if (err == NULL) {
-            pulseBlue();
-            printf ("HTTP GET of \"/media/uploads/mbed_official/hello.txt\" completed.  The file contains:\n"
-                    "------------------------------------------------------------------------------------\n%s"
-                    "------------------------------------------------------------------------------------\n", buf);
+            pulseEvent();
+            printf("HTTP GET of \"/media/uploads/mbed_official/hello.txt\" completed.  The response contained:\n"
+                   "------------------------------------------------------------------------------------\n%s"
+                   "------------------------------------------------------------------------------------\n", buf);
         } else {
-            printf ("Unable to get \"/media/uploads/mbed_official/hello.txt\" from \"mbed.org\", "
-                    "error class %d, error code %d.\n", err->eClass, err->eCode);
-            ledRed = 0;
-            ledGreen = 1;
-            ledBlue = 1;
+            bad();
+            printf("Unable to get \"/media/uploads/mbed_official/hello.txt\" from \"mbed.org\", "
+                   "error class %d, error code %d.\n", err->eClass, err->eCode);
         }
 
         // CELL LOCATE OPERATIONS (in a loop)
-        printf ("Sending Cell Locate requests in a loop...\n");
-        while (1) {
+        printf("=== Cell Locate ===\n");
+        printf("Sending Cell Locate requests in a loop until user button is pressed...\n");
+        while (!buttonPressed) {
             interface->cellLocSrvUdp();
             interface->cellLocConfig(1); // Deep scan mode
-            printf ("Sending Cell Locate request...\n");
+            printf("Sending Cell Locate request...\n");
             if (interface->cellLocRequest(UbloxATCellularInterfaceExt::CELL_HYBRID, 10, 100,
                                          (UbloxATCellularInterfaceExt::CellRespType) 1, 1)) {
                 // Wait for the response
@@ -204,28 +232,28 @@
                 if (numRes > 0) {
                     interface->cellLocGetData(&data);
                     if (data.validData) {
-                        pulseBlue();
+                        pulseEvent();
                         printCellLocateData(&data);
                     }
                 } else {
-                    printf ("No response from Cell Locate server.\n");
-                    ledRed = 0;
-                    ledGreen = 1;
-                    ledBlue = 1;
+                    bad();
+                    printf("No response from Cell Locate server.\n");
                 }
             }
             wait_ms(5000);
         }
         
+        pulseEvent();
+        printf("User button was pressed, stopping...\n");
+        gnssSerial.powerOff();
+        interface->disconnect();
+        interface->deinit();
+        ledOff();
+        printf("Stopped.\n");
     } else {
+        bad();
         printf("Unable to initialise the interface.\n");
     }
-    
-    ledRed = 0;
-    ledGreen = 1;
-    ledBlue = 1;
-    printf("Should never get here.\n");
-    MBED_ASSERT(false);
 }
 
 // End Of File
\ No newline at end of file