init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Thu Jan 17 15:20:14 2019 +0000
Parent:
38:476a9b5629a1
Child:
40:4270e09e59f2
Commit message:
tweaks

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
modem.cpp Show annotated file Show diff for this revision Revisions of this file
states.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jan 16 23:06:39 2019 +0000
+++ b/main.cpp	Thu Jan 17 15:20:14 2019 +0000
@@ -3,7 +3,7 @@
 //------------------------------------------------------------------------------
 //DEFINES
 //------------------------------------------------------------------------------ 
-#define FW_VERSION          11
+#define FW_VERSION          14
 #define SKU                 "GPSPLUS"
 #define HW_MAJORREVISION    "001"
 
@@ -70,6 +70,7 @@
 uint16_t         RET_setting_beacon_interval_seconds;
 uint16_t         RET_setting_beacon_scan;
 //STATE
+bool             RET_asleep = false;
 uint8_t          RET_coldBoot = 1;
 time_t           RET_RTCunixtime;
 bool             RET_haveSettings;
@@ -81,6 +82,7 @@
 time_t           RET_SetupRunAt;
 time_t           RET_SettingsGotAt;
 bool             RET_force2G;
+bool             RET_watchdogfired;
 //MOTION STATE
 bool             RET_motionTriggered;
 bool             RET_motionTriggeredInTXInterval;
@@ -233,45 +235,54 @@
 void RTCtick() {
     //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
     RET_RTCunixtime += 1;
-    
-    //button logic - check for hold
-    if (RET_buttonHoldTime > 4) {
-        RET_buttonHoldTime = 0;
-        RET_buttonPressCount = 0;
-        RET_state_prev = RET_state;
-        RET_state = STATE_BUTTONHOLD;
-        mainthread.release();
-    } else {
-        if((RET_RTCunixtime - RET_buttonPressTime) > 1) {
-            switch (RET_buttonPressCount) {   
-                case 1 :
-                    if (RET_state == STATE_NORMAL) {
-                        RET_state = STATE_BUTTONPRESS1;
-                        mainthread.release();
-                    }
-                    break;
-                case 3 :
-                    if (RET_state == STATE_NORMAL) {
-                        RET_state = STATE_BUTTONPRESS2;
-                        mainthread.release();
-                    }
-                    break;
-                case 5 :
-                    if (RET_state == STATE_NORMAL) {
-                        RET_state = STATE_BUTTONPRESS3;
-                        mainthread.release();
-                    }
-                    break;
-                default :
-                    //do nothing
-                    break;
+
+    //button logic, only when device asleep
+    if (RET_asleep == true) {
+        //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) {
+                switch (RET_buttonPressCount) {   
+                    case 1 :
+                        if (RET_state == STATE_NORMAL) {
+                            RET_state = STATE_BUTTONPRESS1;
+                            mainthread.release();
+                        }
+                        break;
+                    case 3 :
+                        if (RET_state == STATE_NORMAL) {
+                            RET_state = STATE_BUTTONPRESS2;
+                            mainthread.release();
+                        }
+                        break;
+                    case 5 :
+                        if (RET_state == STATE_NORMAL) {
+                            RET_state = STATE_BUTTONPRESS3;
+                            mainthread.release();
+                        }
+                        break;
+                    default :
+                        //do nothing
+                        break;
+                }
+                RET_buttonPressCount = 0;
             }
-            RET_buttonPressCount = 0;
         }
-    }
-    //SETUP STATE VISUALISE
-    if (!GLOBAL_LEDSequenceinProgress && RET_state == STATE_SETUP) {
-        led1 = !led1; 
+        //SETUP STATE VISUALISE
+        if (!GLOBAL_LEDSequenceinProgress && RET_state == STATE_SETUP) {
+            led1 = !led1; 
+        }
     }
 }
 void resetGlobals() {
@@ -281,15 +292,22 @@
 }
 void healthCheck() {
     //check clock
-    if(RET_havesettings == 1) {
-        if (RET_RTCunixtime < 1547678732) { 
+    if(RET_haveSettings == 1) {
+        if (RET_RTCunixtime < 1547678732) {
             //go dormant for 72hrs and then resetup
-            RET_havesettings = 0; setState(STATE_DORMANT);
+            RET_haveSettings = 0; 
+            setState(STATE_DORMANT);
         }
     }
+    //check for watchdog fire
+    if (RET_watchdogfired == true) {
+        addToExceptionString("WD");
+        RET_watchdogfired = false;
+    }
 }
 void setDefaults() {
     //STATE
+    RET_asleep = false;
     RET_haveSettings = 0;
     RET_state = STATE_SETUP;
     RET_state_prev = RET_state;
@@ -297,6 +315,7 @@
     RET_SetupRunAt = 0;
     RET_SettingsGotAt = 0;
     RET_force2G = DEFAULT_FORCE2G;
+    RET_watchdogfired = false;
     //SETTINGS
     RET_setting_firmware = 0;
     RET_setting_minimumupdate_hrs = 0;
@@ -625,6 +644,35 @@
     saveEventTimes();
 }
 
+void event_turnonofflog_tx(bool turnon) {
+    DEBUG("TEST EVENT\n",false);
+    float temperature = getTemperature();
+    float voltage = getBatteryV();
+    if (modem.on(RET_force2G)) {
+        char locString[70];
+        memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
+        if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
+            int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.;
+            char bytestosend[160];
+            if (turnon) {
+                snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,t:%.1f,v:%.2f,z:TURNON,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken,locString);
+            } else {
+                snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken,locString);
+            }
+            char result[180];
+            bool getSettings = false;
+            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
+        } else {
+            modem.off(false);  
+        }
+    } else {
+        modem.off(false);
+    }
+    modem.off(false);
+    //RESETS
+    RET_motionTriggeredInTXInterval = 0;
+}
+
 void event_location_tx() {
     DEBUG("LOCATION TX\n",false);
     float temperature = getTemperature();
@@ -797,6 +845,7 @@
                 DEBUG("TURNING OFF\n",false);
                 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
                 LED1on(5000);
+                event_turnonofflog_tx(false);
             } else {
                 if (RET_haveSettings) {
                     setState(STATE_NORMAL);
@@ -805,9 +854,20 @@
                 }
                 DEBUG("TURNING ON\n",false);
                 LED1blink(20,100);
+                event_turnonofflog_tx(true);
             }
             break;
         }
+        case STATE_SCORCHEDEARTH :
+        {
+            setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
+            read_app_data_from_flash(&app_data);
+            app_data.current_firmware_version = 0;
+            app_data.target_firmware_version = 0;
+            write_app_data_to_flash(&app_data);
+            system_reset();
+            break;
+        }
         default :
         {
             setState(STATE_SETUP);
@@ -834,15 +894,15 @@
     if (NRF_POWER->RESETREAS != 0xffffffff) {
         switch(NRF_POWER->RESETREAS) {
             case 0x00000001  :
-                DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);
+                //DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);
                 RET_coldBoot = 1;
-                dumpSettings();
             break;
             case 0x00000002  :
-                DEBUG("Watchdog ST:%d\n",RET_state);
+                //DEBUG("Watchdog ST:%d\n",RET_state);
+                RET_watchdogfired = true;
             break;
             case 0x00000004  :
-                DEBUG("Soft ST:%d\n",RET_state);
+                //DEBUG("Soft ST:%d\n",RET_state);
             break;
         }
         NRF_POWER->RESETREAS = 0xffffffff;
@@ -867,18 +927,23 @@
     while(true) {
         //WATCHDOG
         watchdogKick();
+        RET_asleep = false;
         
         //INIT
         resetGlobals();
-        healthCheck();
+        healthCheck(); //this must be after resetGlobals
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //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);
         if(DEBUG_ON){
-            debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
-            //debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACTIVITY:%s\n",RET_activityData);debug_exe();
+            if (RET_state != 99) {
+                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, SETTINGS:%d, MOT: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
+                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT:%s\n",RET_activityData);debug_exe();
+            } else {
+                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, WAKE@:%u, \n", RET_state, RET_eventTime_wakeFromDormant);debug_exe();
+            }
         }
 
         mainStateEngine();
@@ -911,6 +976,7 @@
         watchdogKick();
         
         //SLEEP
+        RET_asleep = true;
         mainthread.wait(DEFAULT_SLEEP_FRAME);
     }
 }
\ No newline at end of file
--- a/modem.cpp	Wed Jan 16 23:06:39 2019 +0000
+++ b/modem.cpp	Thu Jan 17 15:20:14 2019 +0000
@@ -96,19 +96,23 @@
         if (ATwaitForWord("RDY",ATTIMEOUT_MED)) {
             
             //TURN OFF ECHO
-            //ATsendCMD("ATE0");
-            //ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            ATsendCMD("ATE0");
+            ATwaitForWord("OK",ATTIMEOUT_SHORT);
              
             //ENABLE AIRPLANE MODE CONTROL WITH PIN
             ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
             ATwaitForWord("OK",ATTIMEOUT_SHORT);
                         
             //CONNECTION TYPE
+            //ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
             //ATsendCMD("AT+QCFG=\"nwscanmode\",2"); //3G only connection
+            //ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
             if (force2G) {
                 ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
             } else {
+                ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
+                ATwaitForWord("OK",ATTIMEOUT_SHORT);
                 //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tirangulation)
                 ATsendCMD("AT+QCFG=\"nwscanseq\",1"); //2G priority
                 //ATsendCMD("AT+QCFG=\"nwscanseq\",2"); //3G priority
@@ -274,7 +278,7 @@
     int messageLength = strlen(message);
     if (messageLength > USSD_MAXLENGTH) {
         char message_failsafe[100];
-        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,z:TOOBIG,s:1,c:%d)\0",api,messageIndex);
+        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,a:error,z:TOOBIG,s:1,c:%d)\0",api,messageIndex);
         result = USSDsend(message_failsafe, maxAttempts);
     } else {
         result = USSDsend(message, maxAttempts);
--- a/states.h	Wed Jan 16 23:06:39 2019 +0000
+++ b/states.h	Thu Jan 17 15:20:14 2019 +0000
@@ -4,4 +4,5 @@
 #define STATE_BUTTONPRESS1       81
 #define STATE_BUTTONPRESS2       82
 #define STATE_BUTTONPRESS3       83
-#define STATE_BUTTONHOLD         89
\ No newline at end of file
+#define STATE_BUTTONHOLD         89
+#define STATE_SCORCHEDEARTH      90
\ No newline at end of file