SAIT ARIS / LRAT-example-lorawan-REFACTOR-and-CLEAN-Branch

Dependencies:   Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM

Fork of LRAT-example-lorawan by SAIT ARIS

Revision:
36:dcc6f89fa39a
Parent:
35:73b3963c6dd3
Child:
37:df69df1bcb1a
--- a/main.cpp	Mon Aug 27 15:09:11 2018 +0000
+++ b/main.cpp	Mon Aug 27 21:06:30 2018 +0000
@@ -14,10 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 //#define TARGET_LRAT 1
 #define TARGET_DISCO1 1
 //#define TARGET_DISCO2 1
 //#define SENSOR_TEMP 1
+
 #include <stdio.h>
 #include "mbed.h"
 
@@ -292,6 +294,7 @@
 time_t tBump = 0;
 time_t tLast = 0;
 time_t tNext = 0;
+time_t tSend = 0;
 
 DigitalOut myLedR(LEDR);
 DigitalOut myLedG(LEDG);
@@ -305,29 +308,24 @@
 void magRead();
 void accRead();
 void gpsRead();
+void send_message();
 
 void onBtnIrq()
 {
     btnHFire++;
-//    tBump = time(NULL);
-//    tNext = tBump;
-    tNext = 0;
+    tSend = 0;
 }
 
 void onAccIrq()
 {
     accHFire++;
-//    tBump = time(NULL);
-//    tNext = tBump;
-    tNext = 0;
+    tSend = 0;
 }
 
 void onMagIrq()
 {
     magHFire++;
-//    tBump = time(NULL);
-//    tNext = tBump;
-    tNext = 0;
+    tSend = 0;
 }
 
 void accDumpCfg()
@@ -458,28 +456,6 @@
 //    }
     */
     
-    printf("CONTROL LOOP GOES HERE!\r\n");
-
-#if defined(TARGET_LRAT)
-#else
-    for (int i = 0; i < 6; i++)
-    {
-        tNow = time(NULL);
-        printf("RTC: %08X\r\n", tNow);
-        tNext = tNow + 10;
-        printf("NXT: %08X\r\n", tNext);
-        // Clear any pending IRQs
-        cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
-        i2c.write(LSM303_ADR_MAG, cmd, 1);
-        i2c.read(LSM303_ADR_MAG, &ret, 1);
-        while (time(NULL) < tNext)
-        {
-            wait(1);
-        }
-        printf("Timer #%d complete.\r\n", i);
-    }
-#endif
-    
     for (int i = 0; i <= 64; i++)
     {
         myLedR = i & 0x01;
@@ -496,8 +472,8 @@
     }
 
     printf("\r\n Mbed LoRaWANStack initialized \r\n");
-    printf("MBED_CONF_LORA_APP_PORT: %d", MBED_CONF_LORA_APP_PORT);
-    printf("MBED_CONF_LORA_DUTY_CYCLE_ON: %d", MBED_CONF_LORA_DUTY_CYCLE_ON);
+    printf("MBED_CONF_LORA_APP_PORT: %d\r\n", MBED_CONF_LORA_APP_PORT);
+    printf("MBED_CONF_LORA_DUTY_CYCLE_ON: %d\r\n", MBED_CONF_LORA_DUTY_CYCLE_ON);
 
     // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -533,7 +509,45 @@
     printf("\r\n Connection - In Progress ...\r\n");
 
     // make your event queue dispatching events forever
-    ev_queue.dispatch_forever();
+    //ev_queue.dispatch_forever();
+    ev_queue.dispatch();
+    printf("\r\n- = - = - = - = - = - DISPATCH 1 COMPLETE - = - = - = - = - = -\r\n");
+    /*
+    printf("CONTROL LOOP GOES HERE!\r\n");
+#if defined(TARGET_LRAT)
+#else
+    for (int i = 0; i < 6; i++)
+    {
+        tNow = time(NULL);
+        printf("RTC: %08X\r\n", tNow);
+        tNext = tNow + 10;
+        printf("NXT: %08X\r\n", tNext);
+        // Clear any pending IRQs
+        cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
+        i2c.write(LSM303_ADR_MAG, cmd, 1);
+        i2c.read(LSM303_ADR_MAG, &ret, 1);
+        while (time(NULL) < tNext)
+        {
+            wait(1);
+        }
+        printf("Timer #%d complete.\r\n", i);
+    }
+#endif
+    */
+    tSend = time(NULL) + 1;
+    while (1) {
+        while (time(NULL) < tSend)
+            wait(0.1);
+        printf("IT'S PACKET TIME!\r\n");
+        ev_queue.call(send_message);
+        ev_queue.dispatch();
+        printf("\r\n- = - = - = - = - = - DISPATCH 2 COMPLETE - = - = - = - = - = -\r\n");
+        tSend = time(NULL) + 300;
+        // Clear any pending IRQs
+        cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
+        i2c.write(LSM303_ADR_MAG, cmd, 1);
+        i2c.read(LSM303_ADR_MAG, &ret, 1);
+    }
 
     return 0;
 }
@@ -543,6 +557,7 @@
  */
 static void send_message()
 {
+    printf("In send_message()...\r\n");
     uint16_t packet_len;
     int16_t retcode;
     float sensor_value;
@@ -623,6 +638,7 @@
  */
 static void receive_message()
 {
+    printf("In receive_message()...\r\n");
     int16_t retcode;
     retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
                               sizeof(rx_buffer),
@@ -666,12 +682,14 @@
     switch (event) {
         case CONNECTED:
             printf("\r\n Connection - Successful \r\n");
+            ev_queue.break_dispatch();
+            /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             } else {
                 ev_queue.call_every(TX_TIMER, send_message);
             }
-
+            */
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
@@ -679,9 +697,12 @@
             break;
         case TX_DONE:
             printf("\r\n Message Sent to Network Server \r\n");
+            ev_queue.break_dispatch();
+            /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
+            */
             break;
         case TX_TIMEOUT:
         case TX_ERROR:
@@ -689,9 +710,11 @@
         case TX_SCHEDULING_ERROR:
             printf("\r\n Transmission Error - EventCode = %d \r\n", event);
             // try again
+            /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
+            */
             break;
         case RX_DONE:
             printf("\r\n Received message from Network Server \r\n");
@@ -706,9 +729,11 @@
             break;
         case UPLINK_REQUIRED:
             printf("\r\n Uplink required by NS \r\n");
+            /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
+            */
             break;
         default:
             MBED_ASSERT("Unknown Event");