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:
44:cf1070af05db
Parent:
43:c900cd8ea072
Child:
45:db9580357bc6
--- a/main.cpp	Thu Sep 06 17:42:09 2018 +0000
+++ b/main.cpp	Tue Sep 11 20:44:06 2018 +0000
@@ -15,18 +15,38 @@
  * limitations under the License.
  */
 
-//#define TARGET_LRAT 1
+//#define TARGET_LRAT1 1
+//#define TARGET_LRAT2 1
 #define TARGET_DISCO1 1
 //#define TARGET_DISCO2 1
-//#define SENSOR_TEMP 1
 
+//#define ENABLE_TMPSENSOR 1
+//#define ENABLE_USBSERIAL 1
+
+#include "stm32EEPROM.h"
 #include "foo.h"
 int weGetSignal = 0;
 
 #define DCHILL 600  // Cycle delay in chill mode.
 #define DPANIC 60   // Cycle delay in panic mode.
+//#define DGRACE 3    // Sensor timeout in seconds.
 #define NPANIC 5    // Number of unprovoked panic cycles before cooldown.
 
+#if defined(TARGET_LRAT1) or defined(TARGET_LRAT2)
+#define TARGET_LRAT 1
+#endif
+#if defined(TARGET_DISCO1) or defined(TARGET_DISCO2)
+#define TARGET_DISCO 1
+#endif
+
+#if (defined(TARGET_LRAT) or defined(TARGET_DISCO2)) and defined(ENABLE_USBSERIAL)
+#include "USBSerial.h"
+USBSerial serial;
+FileHandle* mbed::mbed_override_console(int) {
+    return &serial;
+}
+#endif
+
 #include <stdio.h>
 #include "mbed.h"
 
@@ -39,9 +59,6 @@
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
-// HUOT ADDITION -- 08/21/2018 -- Import of EEPROM Library
-#include "stm32EEPROM.h"
-
 #include "mbed-trace/mbed_trace.h"
 #define TRACE_GROUP "MAIN"
 
@@ -111,16 +128,6 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
-/*
-#if defined(TARGET_LRAT) or defined(TARGET_DISCO2)
-#include "USBSerial.h"
-USBSerial serial;
-FileHandle* mbed::mbed_override_console(int) {
-    return &serial;
-}
-#endif
-*/
-
 uint32_t mytime = 0;
 uint8_t  mybatt = 0;
 double   mylat = 0;
@@ -206,10 +213,17 @@
 #define EEPROM_MAX 0x17FF
 
 #if defined(TARGET_LRAT)
-    #define LEDR PB_6
-    #define LEDG PB_7
-    #define LEDB PB_5
-    #define LEDW PB_2
+    #if defined(TARGET_LRAT2)
+        #define LEDR PB_7
+        #define LEDG PB_6
+        #define LEDB PB_2
+        #define LEDW PB_5
+    #else
+        #define LEDR PB_6
+        #define LEDG PB_7
+        #define LEDB PB_5
+        #define LEDW PB_2
+    #endif
     #define PIN_ACC PB_14
     #define PIN_MAG PB_12
     #define PIN_BTN PA_5
@@ -448,16 +462,8 @@
         i2c.read(LSM303_ADR_MAG, &ret, 1);
 }
 
-/**
- * Entry point for application
- */
-int main (void)
+void bootFlash()
 {
-    //i2c.frequency(400000);
-    wait(4);
-    printf("\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
- 
-    // Boot Flash
     for (int i = 0; i <= 64; i++)
     {
         myLedR = i & 0x01;
@@ -466,9 +472,73 @@
         myLedW = i & 0x08;
         wait(0.01);
     }
-    wait(4);
+}
+
+void byteFlash(DigitalOut *scl, DigitalOut *sda, uint8_t dat)
+{
+    *sda = 0;
+    *scl = 1;
+    for (int i = 0; i < 8; i++)
+    {
+        *sda = (dat >> (7 - i)) & 0x01;
+        wait(0.125);
+    }
+    *sda = 0;
+    *scl = 0;
+}
+
+void quadFlash(DigitalOut *scl, DigitalOut *sda, uint32_t dat)
+{
+    *sda = 0;
+    *scl = 1;
+    for (int i = 0; i < 32; i++)
+    {
+        *sda = (dat >> (31 - i)) & 0x01;
+        wait(0.125);
+    }
+    *sda = 0;
+    *scl = 0;
+}
+
+void quadFlash2(DigitalOut *scl, DigitalOut *sdaL, DigitalOut *sdaH, uint32_t dat)
+{
+    *sdaL = 0;
+    *sdaH = 0;
+    *scl = 1;
+    for (int i = 0; i < 32; i++)
+    {
+        *sdaH = (dat >> (31 - i)) & 0x01;
+        *sdaL = !*sdaH;
+        wait(0.125);
+    }
+    *sdaL = 0;
+    *sdaH = 0;
+    *scl = 0;
+}
+
+/**
+ * Entry point for application
+ */
+int main (void)
+{
+    //i2c.frequency(400000);
+    for (int i = 0; i < 5; i++)
+    {
+        myLedR = i == 0;
+        myLedG = i == 1;
+        myLedB = i == 2;
+        myLedW = i == 3;
+        wait(0.25);
+    }
+    quadFlash(&myLedW,&myLedB,0xFF0F3355);
+    printf("\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
+ 
+    // Boot Flash
+    bootFlash();
+    wait(1);
 //    btnPin.rise(&onBtnIrq);
     printf("Boot flash complete.\r\n");
+    
     // setup tracing
     setup_trace();
 
@@ -481,19 +551,23 @@
     
     /* I2C init */
     ret = 0x00;
+#ifndef TARGET_LRAT2
     magDumpCfg();
     accDumpCfg();
     magInitSequence();
     accInitSequence();
     
     // HUOT ADDITION -- 08/30/2018 -- RESET GPS TO DEFAULT BEFORE MOVING FORWARD
+    printf("Resetting GPS config...\r\n");
     gpsExecuteCommand(cmdResetDefaults, sizeof(cmdResetDefaults));
-    
+    printf("Performing initial read...\r\n");
     gpsInitSequence();
     magDumpCfg();
     accDumpCfg();
     printf("Post-sensor init.\r\n");
+#endif
     
+    /*
     cfg = 0x00;
 #if defined(TARGET_LRAT)
     cmd[0] = LSM303_REG_ACC_CTRL_REG1_A;
@@ -532,6 +606,7 @@
         accShift = 6;
 #endif
     printf("Quality: %02x AccShift: %d AccScale: %d\r\n", cfg, accShift, accScale);
+    */
 
     time_t tNow;
     /*
@@ -539,7 +614,7 @@
 //    {
         tNow = time(NULL);
         printf("RTC: %08X\r\n", tNow);
-#if defined(SENSOR_TEMP)
+#if defined(ENABLE_TMPSENSOR)
         tmpRead();
 #endif
         magRead();
@@ -551,14 +626,8 @@
 //    }
     */
     
-    for (int i = 0; i <= 64; i++)
-    {
-        myLedR = i & 0x01;
-        myLedG = i & 0x02;
-        myLedB = i & 0x04;
-        myLedW = i & 0x08;
-        wait(0.01);
-    }
+    wait(1);
+    bootFlash();
     
 //    uint8_t crcA = 0;
 //    uint8_t crcB = 0;
@@ -583,9 +652,11 @@
     
 //    printf("LPM INIT \r\n");
 //    gpsExecuteCommand(lowPowerMode, sizeof(lowPowerMode));
-//    
+//  
+    /*  
     printf("Putting to sleep... Good Luck ================ \r\n");
     gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep));
+    */
     
 //    while(1)
 //    {
@@ -605,16 +676,34 @@
 //        printf("=== New loop incoming === \r\n");
 //    }
     
+    wait(1);
+    uint32_t myRand = 0;
+    uint8_t myTemp = 0;
+    while(1)
+    {
+        myRand = radio.random();
+        myTemp = radio.getTemp();
+        printf("RND: %08X\r\n", myRand);
+        printf("TMP: %02X\r\n", myTemp);
+        quadFlash2(&myLedB,&myLedR,&myLedG,myRand);
+        byteFlash(&myLedW,&myLedB,myTemp);
+        wait(1);
+    }
+
+/*
     // TEST SEND -- SPECTRUM ANALYZER
-    printf("Will send packet in 10 seconds... \r\n");
+    printf("Sending in 5 seconds... \r\n");
         
-    for (int i = 9; i > 0; i--)
+    for (int i = 0; i < 5; i++)
     {
-        wait(1);
-        printf("%d ... \r\n", i);
+        myLedW = 1;
+        wait(0.5);
+        myLedW = 0;
+        wait(0.5);
     }
     
     wait(1);
+    */
     myLedG = 1;
     
     
@@ -652,6 +741,8 @@
 
     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
+    myLedB = 1;
+
     retcode = lorawan.connect();
 
     if (retcode == LORAWAN_STATUS_OK ||
@@ -666,6 +757,17 @@
     // make your event queue dispatching events forever
     //ev_queue.dispatch_forever();
     ev_queue.dispatch();
+    
+    myLedB = 0;
+    wait(0.5);
+    for (int i = 0; i <= 64; i++)
+    {
+        myLedR = i & 0x01;
+        myLedG = i & 0x02;
+        myLedB = i & 0x04;
+        myLedW = i & 0x08;
+        wait(0.01);
+    }
     printf("\r\n- = - = - = - = - = - DISPATCH 1 COMPLETE - = - = - = - = - = -\r\n");
     /*
     printf("CONTROL LOOP GOES HERE!\r\n");
@@ -739,6 +841,7 @@
     weGetSignal = 0;
     printf("Head Cleared ACK flag: %d\r\n", weGetSignal);
     prepBacklogSend();
+    myLedB = 1;
     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN,
                         MSG_CONFIRMED_FLAG);
     if (retcode < 0) {
@@ -798,7 +901,7 @@
     weGetSignal = 0;
     printf("Head Cleared ACK flag: %d\r\n", weGetSignal);
     
-    #if defined(SENSOR_TEMP)
+    #if defined(ENABLE_TMPSENSOR)
         tmpRead();
     #endif
         magRead();
@@ -844,7 +947,7 @@
             printf("%d ... \r\n", i);
         }
         wait(1);
-        myLedG = 1; 
+        myLedB = 1; 
     
         retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN,
                                MSG_CONFIRMED_FLAG);
@@ -928,7 +1031,13 @@
     tr_debug("In lora_event_handler(%d)...", event);
     switch (event) {
         case CONNECTED:
+            myLedB = 0;
+            myLedG = 1;
+            myLedW = weGetSignal;
             printf("\r\n Connection - Successful \r\n");
+            wait(1);
+            myLedG = 0;
+            myLedW = 0;
             ev_queue.break_dispatch();
             /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
@@ -943,6 +1052,9 @@
             printf("\r\n Disconnected Successfully \r\n");
             break;
         case TX_DONE:
+            myLedB = 0;
+            myLedG = 1;
+            myLedW = weGetSignal;
             printf("\r\n Message Sent to Network Server \r\n");
             printf("Dumping ACK Flag: %d\r\n", weGetSignal);
             if (weGetSignal == 0)
@@ -960,22 +1072,24 @@
             wasEEPROM = false;
             weGetSignal = 0;
             printf("Tail Cleared ACK Flag: %d\r\n", weGetSignal);
+            wait(1);
+            myLedG = 0;
+            myLedW = 0;
             ev_queue.break_dispatch();
             /*
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
             */
-            
-            myLedG = 0;
-            
             break;
         case TX_TIMEOUT:
         case TX_ERROR:
         case TX_CRYPTO_ERROR:
         case TX_SCHEDULING_ERROR:
+            myLedB = 0;
             myLedR = 1;
-            wait(1);
+            myLedW = weGetSignal;
+            //wait(1);
             
             printf("\r\n Transmission Error - EventCode = %d \r\n", event);
             
@@ -991,8 +1105,10 @@
             if (wasEEPROM)
                 continueSending = false;
             
-            myLedG = 0;
+            //myLedG = 0;
+            wait(1);
             myLedR = 0;
+            myLedW = 0;
             
             break;
         case RX_DONE:
@@ -1004,6 +1120,9 @@
             printf("\r\n Error in reception - Code = %d \r\n", event);
             break;
         case JOIN_FAILURE:
+            myLedB = 0;
+            myLedR = 1;
+            myLedW = weGetSignal;
             printf("\r\n OTAA Failed - Check Keys \r\n");
             break;
         case UPLINK_REQUIRED:
@@ -1037,9 +1156,9 @@
 
     for (int i = 0; i < 2; i++) {
         myLedB = 1;
-        wait(0.3);
+        wait(0.25);
         myLedB = 0;
-        wait(0.3);
+        wait(0.25);
     }
     
 #if defined(TARGET_LRAT)
@@ -1052,7 +1171,7 @@
     cmd[0] = LSM303_REG_MAG_CTRL_REG4_M;
     cmd[1] = 0x0C;  // Ultra-High Performance Mode on Z axis
     i2c.write(LSM303_ADR_MAG, cmd, 2);
-    #if defined(SENSOR_TEMP)
+    #if defined(ENABLE_TMPSENSOR)
         // Enable Temp Sensor
         cmd[0] = LSM303_REG_MAG_CTRL_REG1_M;
         i2c.write(LSM303_ADR_MAG, cmd, 1);
@@ -1078,7 +1197,7 @@
     cmd[1] = 0x40; // Mag enable interrupt on pin
     i2c.write(LSM303_ADR_MAG, cmd, 2);
 #endif
-    
+/*
     for (int i = 0; i < 2; i++) {
         myLedR = 1;
         myLedG = 1;
@@ -1089,6 +1208,9 @@
         myLedB = 0;
         wait(0.5);
     }
+*/
+    myLedG = 0;
+    myLedR = 0;
 #if defined(TARGET_LRAT)
     // LRAT MAG IRQ SETUP GOES HERE
 #else
@@ -1124,9 +1246,9 @@
     
     for (int i = 0; i < 2; i++) {
         myLedB = 1;
-        wait(0.3);
+        wait(0.25);
         myLedB = 0;
-        wait(0.3);
+        wait(0.25);
     }
     
 #if defined(TARGET_LRAT)
@@ -1197,7 +1319,7 @@
     cmd[1] = ret | 0x08;    // High Resolution
     i2c.write(LSM303_ADR_ACC, cmd, 2);
     
-    #if defined(SENSOR_TEMP)
+    #if defined(ENABLE_TMPSENSOR)
         // Enable Temp Sensor
         cmd[0] = LSM303_REG_ACC_TEMP_CFG_REG_A;
         cmd[1] = 0xC0;
@@ -1212,6 +1334,7 @@
 #endif
     
     //LED Confirmation Output - ACC INIT COMPLETE
+/*
     for (int i = 0; i < 2; i++) {
         myLedR = 1;
         myLedG = 1;
@@ -1222,6 +1345,9 @@
         myLedB = 0;
         wait(0.5);
     }
+*/
+    myLedG = 0;
+    myLedR = 0;
     
     // Set Full Scale to 4g
     /*
@@ -1306,9 +1432,9 @@
     
     for (int i = 0; i < 2; i++) {
         myLedB = 1;
-        wait(0.3);
+        wait(0.25);
         myLedB = 0;
-        wait(0.3);
+        wait(0.25);
     }
     myLedG = 0;
     myLedR = 0;
@@ -1339,8 +1465,9 @@
     command[(length - 1)] = crcB;
     printf("UBX CRC: %02X %02X\r\n", crcA, crcB);
     i2c.write(NEOM8M_ADR_GPS, command, length);
+    ubxRead();
     printf("Command Executed \r\n");
-    wait(1);
+    //wait(1);
 }
 
 void magRead()
@@ -1623,7 +1750,10 @@
                             sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%f,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldTrk, &fldDat) != 9)
                             printf("[\u001b[33mWARN\u001b[0m] Invalid GNRMC packet detected.\r\n");
                         //sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%7[^,],%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, fldFoo, &fldDat);
-                        printf("Sec: %.2f, Sts: %c, Lat: %.5f %c, Lon: %.5f %c, Spd: %.3f, Dat: %06d\r\n", fldTim, fldSts, fldLat, fldN_S, fldLon, fldE_W, fldSpd, fldDat);
+                        if (fldN_S == 'N' || fldN_S == 'S' && fldE_W == 'E' || fldE_W == 'W')
+                            printf("Sec: %.2f, Sts: %c, Lat: %.5f %c, Lon: %.5f %c, Spd: %.3f, Dat: %06d\r\n", fldTim, fldSts, fldLat, fldN_S, fldLon, fldE_W, fldSpd, fldDat);
+                        else
+                            printf("\r\n");
                         if (clr == '2')
                         {
                             tTime = (uint32_t)fldTim;
@@ -1715,7 +1845,7 @@
     while (ret == 0xFF)
     {
         i2c.read(NEOM8M_ADR_GPS, &ret, 1);
-    }   
+    }
     while (ret != 0xFF)
     {
         buf[pos++] = ret;