init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
49:15ddd84ec9fa
Parent:
48:64b1613941d5
Child:
50:a94e70c00fff
--- a/main.cpp	Tue Feb 05 14:30:57 2019 +0000
+++ b/main.cpp	Thu Feb 07 00:34:30 2019 +0000
@@ -5,6 +5,7 @@
 //------------------------------------------------------------------------------ 
 //should really add these - will just add as and when needed for now
 void event_activity_tx();
+void event_location_tx();
 
 //------------------------------------------------------------------------------
 // GLOBALS
@@ -50,6 +51,7 @@
 uint8_t          RET_state_prev;
 uint8_t          RET_buttonPressCount;
 time_t           RET_buttonPressTime;
+time_t           RET_buttonReleaseTime;
 time_t           RET_buttonHoldTime;
 time_t           RET_SetupRunAt;
 time_t           RET_SettingsGotAt;
@@ -160,11 +162,16 @@
     NRF52_SAADC batteryIn;
     batteryIn.addChannel(9); // vdd for battery
     batteryIn.calibrate();
-    float voltage = 0.0;
-    for (uint8_t i = 1; i <= 2; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
+    //get initial reading, first reading is always low
+    batteryIn.updateData(); batteryIn.getData()[0];
+    float voltage_accumulator = 0.0;
+    uint8_t readings = 0;
+    for (uint8_t i = 1; i <= 50; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
         batteryIn.updateData();
-        voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
+        voltage_accumulator += (batteryIn.getData()[0])*(1.0/1024.0)*3.60;
+        readings ++;
     }
+    float voltage = (voltage_accumulator / (float)readings);
     return voltage;
 }
 float nrfTemperature() {
@@ -202,9 +209,9 @@
     RET_buttonPressTime = RET_RTCunixtime;
 }
 void buttonRelease() {
-    RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime);
+    RET_buttonReleaseTime = RET_RTCunixtime;
+    RET_buttonHoldTime = (RET_buttonReleaseTime - RET_buttonPressTime);
     RET_buttonPressCount ++;
-    led1 = 1; //led off
 }
 //------------------------------------------------------------------------------
 // RTC TICKER
@@ -215,65 +222,72 @@
     RET_RTCunixtime += 1;
 
     //button logic, only when device asleep
-    //if (RET_asleep == true) {
-        //led indicator
-        //if (RET_buttonPressTime > 0 && (RET_RTCunixtime - RET_buttonPressTime) > 4) led1 = 0;
-        //if (RET_buttonPressTime > 0 && (RET_RTCunixtime - RET_buttonPressTime) > 14) led1 = 1;
-        //hold check
-        if (RET_buttonHoldTime > 4 && RET_buttonHoldTime < 10) {
-            RET_buttonHoldTime = 0;
-            RET_buttonPressCount = 0;
-            RET_state_prev = RET_state;
-            RET_state = STATE_BUTTONHOLD;
-            mainthread.release();
-        } else if (RET_buttonHoldTime > 14 && RET_buttonHoldTime < 30) {
-            RET_buttonHoldTime = 0;
-            RET_buttonPressCount = 0;
-            RET_state_prev = RET_state;
-            RET_state = STATE_SCORCHEDEARTH;
-            mainthread.release();
-        } else {
-            if((RET_RTCunixtime - RET_buttonPressTime) > 1 && RET_buttonPressCount > 0) {
-                if(RET_busy == true) { 
-                    RTCtick_ledflash_count = 16;// 8 flashes
-                } else {
-                    switch (RET_buttonPressCount) {   
-                        case 1 :
-                            RET_state_prev = RET_state;
-                            RET_state = STATE_BUTTONPRESS1;
-                            mainthread.release();
-                            break;
-                        case 3 :
-                            RET_state_prev = RET_state;
-                            RET_state = STATE_BUTTONPRESS3;
-                            mainthread.release();
-                            break;
-                        case 5 :
-                            RET_state_prev = RET_state;
-                            RET_state = STATE_BUTTONPRESS5;
-                            mainthread.release();
-                            break;
-                        default :
-                            //do nothing
-                            break;
-                    }
+    //hold check
+    if (RET_buttonHoldTime >= 4 && RET_buttonHoldTime <= 10) {
+        RET_buttonHoldTime = 0;
+        RET_buttonPressCount = 0;
+        RET_state_prev = RET_state;
+        RET_state = STATE_BUTTONHOLD;
+        mainthread.release();
+    } else if (RET_buttonHoldTime >= 14 && RET_buttonHoldTime <= 30) {
+        RET_buttonHoldTime = 0;
+        RET_buttonPressCount = 0;
+        RET_state_prev = RET_state;
+        RET_state = STATE_SCORCHEDEARTH;
+        mainthread.release();
+    } else {
+        if((RET_RTCunixtime - RET_buttonReleaseTime) > 1 && RET_buttonPressCount > 0) {
+            if(RET_busy == true) { 
+                //RTCtick_ledflash_count = 4;// 2 flashes
+            } else {
+                RET_SetupRunAt = 0; //allow setup to run again
+                switch (RET_buttonPressCount) {   //double catches to help with debounce
+                    case 1 :
+                    case 2 :
+                        RET_state_prev = RET_state;
+                        RET_state = STATE_BUTTONPRESS1;
+                        RET_buttonPressCount = 0;
+                        mainthread.release();
+                        break;
+                    case 3 :
+                    case 4 :
+                        RET_state_prev = RET_state;
+                        RET_state = STATE_BUTTONPRESS3;
+                        RET_buttonPressCount = 0;
+                        mainthread.release();
+                        break;
+                    case 5 :
+                    case 6 :
+                        RET_state_prev = RET_state;
+                        RET_state = STATE_BUTTONPRESS5;
+                        RET_buttonPressCount = 0;
+                        mainthread.release();
+                        break;
+                    default :
+                        //do nothing
+                        break;
                 }
-                RET_SetupRunAt = 0; //allow setup to run again
-                RET_buttonPressCount = 0;
             }
         }
-        //SETUP STATE VISUALISE
-        if (!GLOBAL_LEDSequenceinProgress && RET_setupInProgress) {
-            led1 = !led1; 
-        } else if (!GLOBAL_LEDSequenceinProgress && RTCtick_ledflash_count > 0) {
-            //IN OPERATION VISUALISE
-            led1 = !led1;
-            RTCtick_ledflash_count --;
-        } else if (!GLOBAL_LEDSequenceinProgress && RTCtick_ledflash_count <= 0) {
-            RTCtick_ledflash_count = 0;
+    }
+    
+    /*
+    if (RET_setupInProgress) {
+        led1 = 0;
+    } else if (!GLOBAL_LEDSequenceinProgress) {
+        led1 = 1;
+    }
+    */
+    /*
+    if (RTCtick_ledflash_count > 0) {
+        led1 = !led1;
+        RTCtick_ledflash_count --;
+        if (RTCtick_ledflash_count <= 0) {
+            RTCtick_ledflash_count = 0;   
             led1 = 1; //turn off led
         }
-    //}
+    }
+    */
 }
 void resetGlobals() {
     GLOBAL_accel_healthy = false;
@@ -349,6 +363,7 @@
     RET_setting_beacon_scan = 0;
     RET_buttonPressCount = 0;
     RET_buttonPressTime = 0;
+    RET_buttonReleaseTime = 0;
     RET_buttonHoldTime = 0;
     //MOTION STATE
     RET_motionTriggeredinFrame = false;
@@ -615,60 +630,72 @@
 // EVENTS
 //------------------------------------------------------------------------------ 
 bool event_setup(bool manualrun) {
+    bool pass = true;
+    float voltage = getBatteryV();
+    LED1on(0);
     if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
     if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
     RET_setupInProgress = true;
     RET_NetworkFailCount = 0;
     RET_GPSFailCount = 0;
-    bool pass = true;
+    RET_setting_connection_timeout = 180; //reset to longer value to setup run to help with connection
     float temperature = getTemperature();
-    float voltage = getBatteryV();
     bool selftestresult = selfTest();
-    if (selftestresult == false) { LED1errorCode(4,10); } //ERROR 4
     int connectionStartTime = RET_RTCunixtime;
     if (modem.on(RET_force2G)) {
-        char locString[70];
-        int gpsStartTime = RET_RTCunixtime;
-        memcpy(locString, modem.getLocation(1, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
-        int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
             int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
             int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
             if (manualrun) addToExceptionString("MAN");
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,x:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken_total,timetaken_connection,timetaken_gps,GLOBAL_exceptionString,SKU,HW_MAJORREVISION,locString);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken_total,timetaken_connection,GLOBAL_exceptionString,SKU,HW_MAJORREVISION);
             char result[200];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
+            RET_setupInProgress = false; //this turns off the flashing led
             if (strcmp(result, "err") != 0) {
                 if (saveSettings(result) == false) {
                     //something went critically wrong getting settings 
                     pass = false;
                     sendErrorLog("ERR-SU-IR");
-                }                      
+                }                   
             } else {
                 //Response error
                 sendErrorLog("ERR-SU-NR");
             }
         } else {
             //FAILUREMODE modem failed to register on network
-            modem.off(true);
-            LED1errorCode(3,5); //ERROR 3
+            //LED1errorCode(3,1); //ERROR 3
             pass = false;
         }
     } else {
         //FAILUREMODE Modem failed to turn on  
-        modem.off(true);
-        LED1errorCode(2,5); //ERROR 2
+        LED1errorCode(2,1); //ERROR 2
         pass = false;
     }
-    //SEND ANY ACTIVITY DATA
-    event_activity_tx();
+    
+    //IF WE PASSED THEN LETS DO OTHER POSTS
+    if (pass == true) {
+        GLOBAL_wakeTime = RET_RTCunixtime; //lets reset this here so that the following loc and act function have sensible values for total time
+    
+        //GET LOC DATA
+        uint8_t previous_location_accuracy = RET_setting_location_accuracy;
+        RET_setting_location_accuracy = 1; //set location mode to CL only
+        event_location_tx();
+        RET_setting_location_accuracy = previous_location_accuracy; //set location mode back to previous
+    
+        //SEND ANY ACTIVITY DATA
+        event_activity_tx();
+        
+        LED1blink(4,500); //PASS AND END
+    }
+    
+    //TIDY UP = LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
     modem.off(true);
-    
-    //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
     RET_SetupRunAt = RET_RTCunixtime;
     setEventTimes();
-    RET_setupInProgress = false; //this turns off the flashing led
+    
+    RET_GPSFailCount = 0;
+    RET_NetworkFailCount = 0;
     //RESULT
     return pass;
 }
@@ -676,8 +703,8 @@
 void event_turnonofflog_tx(bool turnon) {
     RET_NetworkFailCount = 0; //reset network blocker
     RET_GPSFailCount = 0;
+    float voltage = getBatteryV();
     float temperature = getTemperature();
-    float voltage = getBatteryV();
     int connectionStartTime = RET_RTCunixtime;
     if (modem.on(RET_force2G)) {
         char locString[70];
@@ -695,19 +722,15 @@
             }
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi));
-        } else {
-            modem.off(true);  
         }
-    } else {
-        modem.off(true);
     }
     modem.off(true);
 }
 
 void event_location_tx() {
+    float voltage = getBatteryV();
     float temperature = getTemperature();
-    float voltage = getBatteryV();
-    int selfTestResult = selfTest();
+    bool selfTestResult = selfTest();
     //Set any network or GPS fail flags
     if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
     if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
@@ -739,15 +762,7 @@
                 } else {
                     sendErrorLog("ERR-LOC-NR");
                 }
-            } else {
-                //Modem fails to register on network
-                modem.off(true);
-                LED1errorCode(3,5); //ERROR 3   
             }
-        } else {
-            //Modem fails to turn on
-            modem.off(true);
-            LED1errorCode(2,5); //ERROR 2
         }
     }
     modem.off(true);
@@ -790,13 +805,7 @@
                         //TODO: LETS LOG THE DATA INSTEAD   
                         event_activity_log(bytesToSend);
                     }
-                } else {
-                    //Modem fails to register on network
-                    modem.off(true);
                 }
-            } else {
-                //Modem fails to turn on
-                modem.off(true);
             }
             modem.off(true);
         } else {
@@ -818,7 +827,7 @@
             RET_busy = true;
             //check that we havent run setup too recently
             time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt);
-            if (RET_SetupRunAt == 0 || setupRunAt_delta > ONEDAYINSECONDS) {
+            if (RET_SetupRunAt == 0 || setupRunAt_delta >= ONEDAYINSECONDS) {
                 if (event_setup(false)) {
                     // All good
                     setState(STATE_NORMAL);
@@ -978,7 +987,7 @@
 //------------------------------------------------------------------------------ 
 int main() {    
     //INIT
-    watchdog.configure(240.0); //4 mins
+    watchdog.configure(300.0); //5 mins
     LED1off();
     modem.off(false);
     RTCticker.attach(&RTCtick, 1.0);