init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
26:fa3579737329
Parent:
25:7adeb1a53360
Child:
27:fa76f5a08195
--- a/main.cpp	Sun Dec 23 20:24:34 2018 +0000
+++ b/main.cpp	Sun Dec 23 21:13:19 2018 +0000
@@ -4,7 +4,20 @@
 //DEFINES
 //------------------------------------------------------------------------------ 
 #define FW_VERSION 1
-#define SLEEP_FRAME 30000
+
+#define DEFAULT_SLEEP_FRAME                 30000
+#define DEFAULT_LOCATION_MODE               2
+#define DEFAULT_LOCATION_ACCURACY           2  // 0 = no location, 1 = cl only, 2 = gps then cl
+#define DEFAULT_LOCATION_TX_INTERVAL_MINS   1440
+#define DEFAULT_LOCATION_TX_FAILSAFE_HRS    168
+#define DEFAULT_LOCATION_TIMEOUT            180
+#define DEFAULT_MOTION_G                    11
+#define DEFAULT_MOTION_START_SECONDS        120
+#define DEFAULT_MOTION_STOP_SECONDS         120
+#define DEFAULT_IMPACT_G                    127
+#define DEFAULT_CONNECTION_TIMEOUT          180
+#define DEFAULT_BEACON_INTERVAL_SECONDS     10
+
 
 //------------------------------------------------------------------------------
 //FUNCTION PROTOTYPES
@@ -190,7 +203,7 @@
 float getTemperature() {
     SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
     float temperature = si7060.getTemperature();
-    DEBUG("Temperature:%.2f\n",temperature);
+    //DEBUG("Temperature:%.2f\n",temperature);
     return temperature;  
 }
 //------------------------------------------------------------------------------
@@ -264,20 +277,20 @@
     RET_SetupRunAt = 0;
     //SETTINGS
     RET_setting_firmware = 0;
-    RET_setting_location_mode = 2;
-    RET_setting_location_accuracy = 2; // 0 = no location, 1 = cl only, 2 = gps then cl
-    RET_setting_location_tx_interval_mins = 1440;
-    RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
-    RET_setting_location_timeout = 240;
+    RET_setting_location_mode = DEFAULT_LOCATION_MODE;
+    RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
+    RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
+    RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
+    RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
     RET_setting_activity_tx_interval_hrs = 0;
     RET_setting_environmental_tx_interval_mins = 0;
-    RET_setting_motion_g = 11;
-    RET_setting_motion_start_seconds = 120;
-    RET_setting_motion_stop_seconds = 120;
+    RET_setting_motion_g = DEFAULT_MOTION_G;
+    RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
+    RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
     RET_setting_impact_g = 0;
     RET_setting_impact_alert = 0;
-    RET_setting_connection_timeout = 180;
-    RET_setting_beacon_interval_seconds = 10;
+    RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
+    RET_setting_beacon_interval_seconds = DEFAULT_BEACON_INTERVAL_SECONDS;
     RET_setting_beacon_scan = 0;
     //RET_RTCmillis = 0;
     RET_buttonPressCount = 0;
@@ -309,7 +322,7 @@
     GLOBAL_requireSoftReset = false;   
     
     //PERIPHERAL RESET
-    lis3dh_configureForSleep(10,127);
+    lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
 }
 bool selfTest() {
     int test_count = 0;
@@ -394,7 +407,6 @@
 // UPDATE OPERATING SETTINGS
 //------------------------------------------------------------------------------ 
 bool saveSettings(char* settingsBuffer) {
-    //process result
     int matchCount = 0;
     int critical_fail_count = 0;
     time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 
@@ -406,7 +418,6 @@
         
         //FAILUREMODE need to be checking thse against checksums
         char changed;
-        //FAILUREMODE need to verify the identifier against a reg exp or similar
         if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b;                                changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                          changed = 'Y'; } else { changed = 'N'; RET_setting_firmware = -1;}; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
         if(TEMP_d != -1) { setState(TEMP_d);                                       changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
@@ -423,6 +434,7 @@
         if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed);
         if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p;                      changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed);
         if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q;                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed);
+        if (RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
         if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r;           changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed);
         if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s;                       changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,changed);
         
@@ -476,10 +488,10 @@
         //RET_imei = modem.getIMEI();
         //DEBUG("imei: %lld \n",RET_imei);
         //char* modemModel = modem.getModemModel();
-        if (modem.registerOnNetwork(2,180000)) {
+        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.2f,e:%d,z:SETUP,c:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP,c:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken);
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
@@ -515,10 +527,10 @@
     bool selftestresult = selfTest();
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
-        if (modem.registerOnNetwork(2,180000)) {
+        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:SHAKERTEST-%d,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:SHAKERTEST-%d,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
@@ -548,10 +560,10 @@
             //DEBUG("locString:%s-\n",locString);
         }
         //SEND DATA
-        if (modem.registerOnNetwork(2,180000)) {
+        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytesToSend[160];
-            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:OK%d,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken,locString);
+            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:OK%d,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken,locString);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
@@ -574,7 +586,7 @@
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
         //SEND DATA
-        if (modem.registerOnNetwork(2,180000)) {
+        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             char bytesToSend[160];
             snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHours);
             char result[180];
@@ -716,7 +728,7 @@
         RET_sleepToggle = 0; 
         NRF_UART0->TASKS_SUSPEND = 1;
         NRF_UART0->ENABLE = 0; //NO MORE SERIAL OR DEBUG OUTPUTS AFTER THIS
-        mainthread.wait(SLEEP_FRAME);
+        mainthread.wait(DEFAULT_SLEEP_FRAME);
         system_reset();
     }
     
@@ -728,25 +740,17 @@
         //MAIN LOGIC
         DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
         DEBUG("ACTIVITY:%s\n",RET_activityData);
-        float temperature = getTemperature();
-        DEBUG("temp:%.2f\n",temperature);
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
         if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(10,127); }
         turnOffEverything();
-        /*if (GLOBAL_modemOn) {
-            DEBUG("SHUTTING DOWN MODEM\n");
-            Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
-            modem.off(true);
-        }*/
-        
         
         //SLEEP DIFFERENT FOR THIS SHAKER TEST VERSION - AS IS WILL GIVE 450ua sleep if in motion
         /*
         NRF_UART0->TASKS_SUSPEND = 1;
         NRF_UART0->ENABLE = 0; //NO MORE SERIAL OR DEBUG OUTPUTS AFTER THIS
-        mainthread.wait(SLEEP_FRAME);
+        mainthread.wait(DEFAULT_SLEEP_FRAME);
         */
         
         RET_coldBoot = 0;