init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
20:5404841fdd2b
Parent:
19:22261767c87a
Child:
21:e0b866630c27
--- a/main.cpp	Tue Dec 18 21:41:39 2018 +0000
+++ b/main.cpp	Wed Dec 19 09:02:20 2018 +0000
@@ -8,6 +8,7 @@
 static void selftest(void);
 static void buttonPress(void);
 static void buttonRelease(void);
+static void saveEventTimes(void);
 
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
@@ -15,7 +16,7 @@
 char* GLOBAL_defaultApi = "b:gps2";
 bool GLOBAL_accel_healthy = false;
 bool GLOBAL_requireSoftReset = false;
-bool GLOBAL_motionFlagTriggered = false;
+bool GLOBAL_motionStopFlagTriggered = false;
 bool GLOBAL_debugLED = false;
 bool GLOBAL_needToConfigureLis3dh = false;
 bool GLOBAL_registeredOnNetwork = false;
@@ -61,6 +62,7 @@
 static time_t           RET_RTCunixtime                                 __attribute__((section("noinit"),zero_init));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
+static bool             RET_motionTriggeredInTXInterval                 __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStartTime                             __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStopTime                              __attribute__((section("noinit"),zero_init));
 static bool             RET_motionPendingOnState                        __attribute__((section("noinit"),zero_init));
@@ -95,7 +97,8 @@
 //PERIPHERALS
 //------------------------------------------------------------------------------ 
 //BLE myble;
-extern WatchdogTimer watchdog(240.0); //Do not set to less than 4500ms or can cause issues with softdevice
+WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
+void watchdogKick() {watchdog.kick();}
 //SERIAL DEBUG?
 #if CONSOLE_DEBUG
     Serial uart(PN_UART_TX, PN_UART_RX, 115200);
@@ -125,7 +128,7 @@
 }
 void gotoSleep(long sleep_milliseconds) {
     turnOffEverything();
-    watchdog.kick();
+    watchdogKick();
     if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false
         system_reset();
     }
@@ -233,6 +236,7 @@
     RET_buttonHoldTime = 0;
     //MOTION STATE
     RET_motionTriggered = 0;
+    RET_motionTriggeredInTXInterval = 0;
     RET_motionStartTime = 0;
     RET_motionStopTime = 0;
     RET_motionPendingOnState = 0;
@@ -297,6 +301,7 @@
         time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime);
         if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
             RET_motionState = true;
+            RET_motionTriggeredInTXInterval = true;
             if (GLOBAL_debugLED) LED1blink(10,100);
         }
     }
@@ -304,6 +309,7 @@
         time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime);
         if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
             RET_motionState = false;
+            GLOBAL_motionStopFlagTriggered = true;
             RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
             if (GLOBAL_debugLED) LED1blink(5,500);
         }
@@ -349,25 +355,7 @@
         
         if (critical_fail_count == 0) { 
             DEBUG("GOT SETTINGS OK\n"); 
-            
-            //SET EVENT TIMES
-            if(RET_setting_location_tx_interval_mins > 0) { 
-                RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
-                DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
-            }
-            if(RET_setting_location_tx_failsafe_hrs > 0) { 
-                RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
-                DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
-            }
-            if(RET_setting_activity_tx_interval_mins > 0) { 
-                RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_mins * 60));
-                DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
-            }
-            if(RET_eventTime_environmental_tx > 0) { 
-                RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
-                DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
-            }
-            
+            saveEventTimes();
             RET_haveSettings = true;     
             return true;
         } else {
@@ -380,7 +368,25 @@
         return false;
     }
 }
-
+void saveEventTimes() {
+    //SET EVENT TIMES
+    if(RET_setting_location_tx_interval_mins > 0) { 
+        RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
+        DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
+    }
+    if(RET_setting_location_tx_failsafe_hrs > 0) { 
+        RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
+        DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
+    }
+    if(RET_setting_activity_tx_interval_mins > 0) { 
+        RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_mins * 60));
+        DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
+    }
+    if(RET_eventTime_environmental_tx > 0) { 
+        RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
+        DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
+    }   
+}
 
 //------------------------------------------------------------------------------
 // SETUP
@@ -392,7 +398,7 @@
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
         DEBUG("imei: %lld \n",RET_imei);
-        if (modem.registerOnNetwork(3,60000)) {
+        if (modem.registerOnNetwork(2,180000)) {
             char bytestosend[160];
             snprintf(bytestosend,sizeof(bytestosend),"(%s,im:%lld,v:%.2f,fr:1,z:SETUP,c:1)\0",GLOBAL_defaultApi,GLOBAL_imei,GLOBAL_voltage);
             char result[180];
@@ -421,8 +427,21 @@
 // EVENTS
 //------------------------------------------------------------------------------ 
 void event_location_tx() {
-    getBatteryV();
+    DEBUG("PERFORM LOCATION TX\n");
+    getBatteryV();  
+    
+    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    if (modem.on()) {
         
+        //RET_setting_location_accuracy
+        modem.getLocation(true, 3, true, RET_setting_location_timeout);
+        
+    }
+    
+    
+    //RESETS
+    RET_motionTriggeredInTXInterval = 0;
+    saveEventTimes();
 }
 //------------------------------------------------------------------------------
 // STATE ENGINE
@@ -453,28 +472,35 @@
             bool run_location_tx = false;
             switch (RET_setting_location_mode) {
                 case 1: //INTERVAL POST
-                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
-                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
+                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { 
+                        DEBUG("INTERVAL LOC TX...\n");
+                        run_location_tx = true; 
+                    }
                     break;
-                
                 case 2: //INTERVAL POST WITH MOTION CHECK
-                    
+                    if(RET_motionTriggeredInTXInterval && RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
+                        DEBUG("INTERVAL LOC TX WMC...\n"); 
+                        run_location_tx = true; 
+                    }
                     break;
-                    
                 case 3: //POST ON STOP MOTION
-                    
+                    if (GLOBAL_motionStopFlagTriggered) {
+                        DEBUG("MOTION STOP LOC TX...\n");
+                        run_location_tx = true;
+                        GLOBAL_motionStopFlagTriggered = false;
+                    }
                     break;   
             }
+            //Failsafe timer catchall
+            if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { 
+                run_location_tx = true; 
+            }
             if (run_location_tx) {
                 event_location_tx();
             }
             
             
             
-            
-            
-            //Location TX
-            
             //Environmental Log
             if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
                 /*
@@ -487,14 +513,13 @@
             }
             //Environmental TX
             if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
-                getBatteryV();
+                //getBatteryV();
                 //event_environmental_bc();
                 //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
             }
             //Activity TX
             if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
-                getBatteryV();
-                //event_activity_bc();
+                //event_activity_tx();
             }
             break;
         case STATE_DORMANT :
@@ -543,12 +568,8 @@
 // MAIN
 //------------------------------------------------------------------------------ 
 int main() {
-    RTCticker.attach(&RTCtick, 1.0);
     turnOffEverything();
     
-    button.fall(&buttonPress); //does this affect power?
-    button.rise(&buttonRelease);
-
     //CHECK IF THIS IS RESET
     //0x00000004 == soft reset  //0x00000002 == watchdog  //0x00000001 == button/hardreset 
     if (NRF_POWER->RESETREAS != 0xffffffff) {
@@ -567,15 +588,18 @@
         NRF_POWER->RESETREAS = 0xffffffff;
     }
     //CHECK FOR FIRST BOOT
-    if (RET_coldBoot == 1) factoryReset();
+    if (RET_coldBoot == 1) { factoryReset(); }
     
-    //COPY ESSENTIAL VALUES FROM RET TO GLOBAL AFTER RESET
+    //INIT
+    RTCticker.attach(&RTCtick, 1.0);
+    button.fall(&buttonPress); //does this affect power?
+    button.rise(&buttonRelease);
     copyRETtoGLOBAL();
     
     while(true) {
         //SLEEP
         if (RET_coldBoot != 1) gotoSleep(10000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
-        watchdog.kick();
+        watchdogKick();
         
         //LOG START TIME
         GLOBAL_wakeTime = RET_RTCunixtime;
@@ -584,6 +608,11 @@
         //DEBUG("STATE: %d,  RTC_MILLIS: %lld,  RTC_SECONDS:%lld,  BUTT_T:%d,  BUTT_C,%d \n", RET_state, RET_RTCmillis, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
         DEBUG("STATE: %d,  RTC_SECONDS:%u\n", RET_state, RET_RTCunixtime);
 
+        
+        event_location_tx();
+        while(1) {   
+        }
+
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS