init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Tue Dec 18 21:41:39 2018 +0000
Parent:
18:22edaa7e74b1
Child:
20:5404841fdd2b
Commit message:
9

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
targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_ARM_STD/nRF52832.sct Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Dec 18 12:43:32 2018 +0000
+++ b/main.cpp	Tue Dec 18 21:41:39 2018 +0000
@@ -12,7 +12,7 @@
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
 //------------------------------------------------------------------------------ 
-char* GLOBAL_defaultApi = "b:gps2,d:3";
+char* GLOBAL_defaultApi = "b:gps2";
 bool GLOBAL_accel_healthy = false;
 bool GLOBAL_requireSoftReset = false;
 bool GLOBAL_motionFlagTriggered = false;
@@ -30,9 +30,10 @@
 #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
 //IDENTITY
 static long long        RET_imei                                        __attribute__((section("noinit"),zero_init));
-static char             RET_pf_identifier[6]                            __attribute__((section("noinit"),zero_init));
+static char             RET_pf_identifier[7]                            __attribute__((section("noinit"),zero_init));
 //SETTINGS
 static int              RET_setting_firmware                            __attribute__((section("noinit"),zero_init));
+static uint8_t          RET_setting_location_mode                       __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_setting_location_accuracy                   __attribute__((section("noinit"),zero_init));
 static uint32_t         RET_setting_location_tx_interval_mins           __attribute__((section("noinit"),zero_init));
 static uint32_t         RET_setting_location_tx_failsafe_hrs            __attribute__((section("noinit"),zero_init));
@@ -58,7 +59,6 @@
 static time_t           RET_buttonReleaseTime                           __attribute__((section("noinit"),zero_init));
 static time_t           RET_buttonHoldTime                              __attribute__((section("noinit"),zero_init));
 static time_t           RET_RTCunixtime                                 __attribute__((section("noinit"),zero_init));
-//static long long        RET_RTCmillis                                   __attribute__((section("noinit"),zero_init));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStartTime                             __attribute__((section("noinit"),zero_init));
@@ -67,13 +67,17 @@
 static bool             RET_motionPendingOffState                       __attribute__((section("noinit"),zero_init));
 static bool             RET_motionState                                 __attribute__((section("noinit"),zero_init));
 static float            RET_motionTotalActivityHours                    __attribute__((section("noinit"),zero_init));
-static char             RET_activityData[100]                             __attribute__((section("noinit"),zero_init));
+static char             RET_activityData[130]                           __attribute__((section("noinit"),zero_init));
+//TO MAKE THE ABOVE BIGGER YOU NEED TO ALLOCATE MORE NOINIT MEMORY!!!!!
+
 //IMPACT
 static bool             RET_impactTriggered                             __attribute__((section("noinit"),zero_init));
-//EVENT HANDLING
+//EVENTS LOGGING
 static time_t           RET_eventTime_location_log                      __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_environmental_log                 __attribute__((section("noinit"),zero_init));
+//EVENTS TX
 static time_t           RET_eventTime_location_tx                       __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_environmental_log                 __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_location_failsafe_tx              __attribute__((section("noinit"),zero_init));
 static time_t           RET_eventTime_environmental_tx                  __attribute__((section("noinit"),zero_init));
 static time_t           RET_eventTime_activity_tx                       __attribute__((section("noinit"),zero_init));
 static time_t           RET_eventTime_wakeFromDormant                   __attribute__((section("noinit"),zero_init));
@@ -127,29 +131,59 @@
     }
     mainthread.wait(sleep_milliseconds);
 }
+void setState(uint8_t state) {
+    RET_state_prev = RET_state;
+    RET_state = state;
+    DEBUG("STATEREADBACK %d\n",RET_state)
+}
+void getBatteryV() {
+    NRF52_SAADC batteryIn;
+    batteryIn.addChannel(9); // vdd for battery
+    batteryIn.calibrate();
+    for (uint8_t i = 1; i < 3; i++) { // need to get it 3 times to get accurate data, first one is always low for some reason
+        batteryIn.updateData();
+        GLOBAL_voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
+    }
+}
+//------------------------------------------------------------------------------
+// USER BUTTON HANDLING
+//------------------------------------------------------------------------------ 
+void buttonPress() {
+    RET_buttonPressTime = RET_RTCunixtime;
+}
+void buttonRelease() {
+    RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime);
+    RET_buttonPressCount ++;
+}
+//------------------------------------------------------------------------------
+// RTC TICKER
+//------------------------------------------------------------------------------ 
 void RTCtick() {
-    RET_RTCmillis += 100;
-    RET_RTCunixtime = (RET_RTCmillis / 1000);
+    //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
+    RET_RTCunixtime += 1;
     GLOBAL_RTCunixtime = RET_RTCunixtime;
+    
     //button logic - check for hold
-    if (RET_buttonHoldTime > 4000) {
+    if (RET_buttonHoldTime > 4) {
         RET_buttonHoldTime = 0;
         RET_buttonPressCount = 0;
         RET_state_prev = RET_state;
         RET_state = STATE_BUTTONHOLD;
         mainthread.release();
     } else {
-        if((RET_RTCmillis - RET_buttonPressTime) > 500) {
+        if((RET_RTCunixtime - RET_buttonPressTime) > 1) {
             switch (RET_buttonPressCount) {   
                 case 1 :
-                    RET_state_prev = RET_state;
-                    RET_state = STATE_BUTTONPRESS1;
-                    mainthread.release();
+                    if (RET_state == STATE_NORMAL) {
+                        RET_state = STATE_BUTTONPRESS1;
+                        mainthread.release();
+                    }
                     break;
                 case 2 :
-                    RET_state_prev = RET_state;
-                    RET_state = STATE_BUTTONPRESS2;
-                    mainthread.release();
+                    if (RET_state == STATE_NORMAL) {
+                        RET_state = STATE_BUTTONPRESS2;
+                        mainthread.release();
+                    }
                     break;
                 default :
                     //do nothing
@@ -158,6 +192,10 @@
             RET_buttonPressCount = 0;
         }
     }
+    //SETUP STATE VISUALISE
+    if (RET_state == STATE_SETUP) {
+        led1 = !led1;
+    }
 }
 void factoryReset() {
     DEBUG("Factory Reset \n");
@@ -165,8 +203,15 @@
     //IDENTITY 
     RET_imei = 0;
     memset(RET_pf_identifier,0,sizeof(RET_pf_identifier));
+    //STATE
+    RET_coldBoot = 1;
+    RET_haveSettings = 0;
+    RET_state = STATE_SETUP;
+    RET_state_prev = RET_state;
+    RET_RTCunixtime = 0;
     //SETTINGS
     RET_setting_firmware = 0;
+    RET_setting_location_mode = 2;
     RET_setting_location_accuracy = 1;
     RET_setting_location_tx_interval_mins = 1440;
     RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
@@ -181,13 +226,7 @@
     RET_setting_connection_timeout = 180;
     RET_setting_beacon_interval_seconds = 10;
     RET_setting_beacon_scan = 0;
-    //STATE
-    RET_coldBoot = 1;
-    RET_haveSettings = 0;
-    RET_state = STATE_SETUP;
-    RET_state_prev = RET_state;
-    RET_RTCunixtime = 0;
-    RET_RTCmillis = 0;
+    //RET_RTCmillis = 0;
     RET_buttonPressCount = 0;
     RET_buttonPressTime = 0;
     RET_buttonReleaseTime = 0;
@@ -229,20 +268,6 @@
 }
 
 //------------------------------------------------------------------------------
-// USER BUTTON HANDLING
-//------------------------------------------------------------------------------ 
-void buttonPress() {
-    RET_buttonPressTime = RET_RTCmillis;
-}
-void buttonRelease() {
-    RET_buttonHoldTime = (RET_RTCmillis - RET_buttonPressTime);
-    //debounce catch
-    if((RET_RTCmillis - RET_buttonPressTime) >= 100) {
-        RET_buttonPressCount ++;
-    }
-}
-
-//------------------------------------------------------------------------------
 // MOTION FUNCS
 //------------------------------------------------------------------------------ 
 void checkMotion() {
@@ -291,8 +316,9 @@
 bool saveSettings(char* settingsBuffer) {
     //process result
     int matchCount = 0;
-    char TEMP_a[6]; 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; int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1;
+    int critical_fail_count = 0;
+    char TEMP_a[7]; 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; 
+    int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1;
     if ( (matchCount = sscanf(settingsBuffer,"a:%c%c%c%c%c%c,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d",
     &TEMP_a[0],&TEMP_a[1],&TEMP_a[2],&TEMP_a[3],&TEMP_a[4],&TEMP_a[5],&TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) {
         DEBUG("VALUES: a:%s,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n",
@@ -300,29 +326,55 @@
         
         //FAILUREMODE need to be checking thse against checksums
         char changed;
-        if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b;                                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_RTCunixtime:%lld..%c\n",RET_RTCunixtime,changed);
-        if(TEMP_b != 0) { RET_RTCmillis = ((long long)TEMP_b * 1000);              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_RTCmillis:%lld..%c\n",RET_RTCmillis,changed);
         //FAILUREMODE need to verify the identifier against a reg exp or similar
-        if(1==1) { memcpy(RET_pf_identifier, TEMP_a, sizeof(TEMP_a));              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_pf_identifier:%s..%c\n",RET_pf_identifier,changed);
+        if(1==1) { memcpy(RET_pf_identifier, TEMP_a, sizeof(TEMP_a));              changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_pf_identifier:%s..%c\n",RET_pf_identifier,changed);
+        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'; }; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
-        if(TEMP_d != -1) { RET_state = TEMP_d; RET_state_prev = RET_state;         changed = 'Y'; } else { RET_state = STATE_DORMANT; changed = 'N'; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
-        if(TEMP_e != -1) { RET_setting_location_accuracy = TEMP_e;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
-        if(TEMP_f != -1) { RET_setting_location_tx_interval_mins = TEMP_f;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_interval_mins:%d..%c\n",RET_setting_location_tx_interval_mins,changed);
-        if(TEMP_g != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_g;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed)
-        if(TEMP_h != -1) { RET_setting_location_timeout = TEMP_h;                  changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed)
-        if(TEMP_i != -1) { RET_setting_activity_tx_interval_mins = TEMP_i;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,changed)
-        if(TEMP_j != -1) { RET_setting_environmental_tx_interval_mins = TEMP_j;    changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed)
-        if(TEMP_k != -1) { RET_setting_motion_g = TEMP_k;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed)
-        if(TEMP_l != -1) { RET_setting_motion_start_seconds = TEMP_l;              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed)
-        if(TEMP_m != -1) { RET_setting_motion_stop_seconds = TEMP_m;               changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed)
-        if(TEMP_n != -1) { RET_setting_impact_g = TEMP_n;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed)
-        if(TEMP_o != -1) { RET_setting_impact_alert = TEMP_o;                      changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed)
-        if(TEMP_p != -1) { RET_setting_connection_timeout = TEMP_p;                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed)
-        if(TEMP_q != -1) { RET_setting_beacon_interval_seconds = TEMP_q;           changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed)
-        if(TEMP_r != -1) { RET_setting_beacon_scan = TEMP_r;                       changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,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);
+        if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                     changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
+        if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
+        if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_interval_mins:%d..%c\n",RET_setting_location_tx_interval_mins,changed);
+        if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed)
+        if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i;                  changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed)
+        if(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,changed)
+        if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k;    changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed)
+        if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed)
+        if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m;              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed)
+        if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n;               changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed)
+        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(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)
         
-        RET_haveSettings = true;
-        return true;
+        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);
+            }
+            
+            RET_haveSettings = true;     
+            return true;
+        } else {
+            DEBUG("CRITICAL FAILS:%d\n",critical_fail_count);
+            RET_haveSettings = false; 
+            return false;
+        }
     } else {
         RET_haveSettings = false;
         return false;
@@ -334,7 +386,6 @@
 // SETUP
 //------------------------------------------------------------------------------ 
 bool setup() {
-    DEBUG("SETUP\n");
     bool pass = true;
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
     if (modem.on()) {
@@ -347,91 +398,143 @@
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
-                DEBUG("result: %s \n",result);
-                saveSettings(result);
+                DEBUG("fromserver: %s \n",result);
+                if (!saveSettings(result)) { 
+                    //something went critically wrong getting settings 
+                    pass = false;
+                }              
             }
         } else {
             //FAILUREMODE modem failed to register on network
+            pass = false;
         }
     } else {
+        //FAILUREMODE Modem failed to turn on  
         pass = false;
-        //FAILUREMODE Modem failed to turn on   
     }
-     
     
     //RESULT
-    if (pass) {
-        return true;
-    } else {
-        return false;   
-    }
+    return pass;
 }
 
-
+//------------------------------------------------------------------------------
+// EVENTS
+//------------------------------------------------------------------------------ 
+void event_location_tx() {
+    getBatteryV();
+        
+}
 //------------------------------------------------------------------------------
 // STATE ENGINE
 //------------------------------------------------------------------------------ 
 void mainStateEngine() {
-    DEBUG("StateEngine:%d \n",RET_state);
     switch(RET_state) {
         case STATE_SETUP :
-            factoryReset();
+            DEBUG("STATE:SETUP\n");
+            getBatteryV();
             if (setup()) {
                 copyRETtoGLOBAL();
+            } else {
+                RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (72*3600)); //72hrs
+                setState(STATE_DORMANT);
+                DEBUG("SETSTATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
             }
             break;
         case STATE_NORMAL :
+            DEBUG("STATE:NORMAL\n");
             //check and log motion
             checkMotion();
             
             //EVENTS
-            //Location
-            if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) {
-                //getdata_environmental();
-                //event_location_log();
+            
+            //LOCATION
+            //Location Log
+            //if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) { }
+            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; }
+                    break;
+                
+                case 2: //INTERVAL POST WITH MOTION CHECK
+                    
+                    break;
+                    
+                case 3: //POST ON STOP MOTION
+                    
+                    break;   
             }
-            if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
-                //getdata_battery();
-                //event_location_broadcast();
+            if (run_location_tx) {
+                event_location_tx();
             }
-            //Environmental
+            
+            
+            
+            
+            
+            //Location TX
+            
+            //Environmental Log
             if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
+                /*
                 Filesystem filesystem(BD_PAGE_ADDRESS,BD_TOTAL_SIZE);
                 SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
                 float temperature_c = temperature.getTemperature();
-                DEBUG("Temperature log: %lld,%f \n", RET_RTCunixtime,temperature_c);
+                DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c);
                 filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c);
+                */
             }
+            //Environmental TX
             if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
+                getBatteryV();
                 //event_environmental_bc();
                 //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
             }
-            //Activity
+            //Activity TX
             if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
+                getBatteryV();
                 //event_activity_bc();
             }
             break;
         case STATE_DORMANT :
             if (RET_RTCunixtime > RET_eventTime_wakeFromDormant) {
-                RET_state = STATE_NORMAL;
+                setState(STATE_NORMAL);
+                DEBUG("WAKING UP FROM DORMANT\n");
             }
+            DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
             break;
         case STATE_BUTTONPRESS1 :
+            setState(STATE_NORMAL);
+            DEBUG("STATE:BUTTONPRESS1\n");
             LED1blink(1,300);
-            RET_state = RET_state_prev;
             break; 
         case STATE_BUTTONPRESS2 :
-            LED1blink(3,300);
-            RET_state = RET_state_prev;
+            setState(STATE_NORMAL);
+            DEBUG("STATE:BUTTONPRESS2\n");
+            LED1blink(2,300);
             break;  
         case STATE_BUTTONHOLD :
-            LED1blink(10,300);
-            RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
-            RET_state = STATE_DORMANT;
+            DEBUG("STATE:BUTTONHOLD\n");
+            if (RET_state_prev == STATE_NORMAL) {
+                setState(STATE_DORMANT);
+                RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
+                DEBUG("TURNING OFF\n");
+                DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
+                LED1on(5000);
+            } else {
+                if (RET_haveSettings) {
+                    setState(STATE_NORMAL);
+                } else {
+                    setState(STATE_SETUP);
+                }
+                DEBUG("TURNING ON\n");
+                LED1blink(20,100);
+            }
             break;
         default :
-            RET_state = STATE_SETUP;
-            DEBUG("DEFAULT STATE");
+            setState(STATE_SETUP);
+            DEBUG("DEFAULT STATE\n");
     }
 }
 
@@ -440,7 +543,7 @@
 // MAIN
 //------------------------------------------------------------------------------ 
 int main() {
-    RTCticker.attach(&RTCtick, 0.1);
+    RTCticker.attach(&RTCtick, 1.0);
     turnOffEverything();
     
     button.fall(&buttonPress); //does this affect power?
@@ -451,14 +554,14 @@
     if (NRF_POWER->RESETREAS != 0xffffffff) {
         switch(NRF_POWER->RESETREAS) {
             case 0x00000001  :
-                DEBUG("reset_reason: 0x%08x. - Hard Reset\n",NRF_POWER->RESETREAS);
+                DEBUG("reset_reason: 0x%08x. - Hard Reset - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
                 RET_coldBoot = 1;
             break;
             case 0x00000002  :
-                DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS);
+                DEBUG("reset_reason: 0x%08x. - Watchdog - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
             break;
             case 0x00000004  :
-                DEBUG("reset_reason: 0x%08x. - Soft reset\n",NRF_POWER->RESETREAS);
+                DEBUG("reset_reason: 0x%08x. - Soft reset - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
             break;
         }
         NRF_POWER->RESETREAS = 0xffffffff;
@@ -478,7 +581,9 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        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_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);
+
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
--- a/modem.cpp	Tue Dec 18 12:43:32 2018 +0000
+++ b/modem.cpp	Tue Dec 18 21:41:39 2018 +0000
@@ -156,7 +156,7 @@
 {
     bool received = false;
     uint32_t timeout = 15000;
-    int bufferIndex = 0;
+    //int bufferIndex = 0;
     int USSDmessageIndex = 0;
     char ATinBuffer[180];
     int matchCount = 0;
@@ -196,7 +196,7 @@
     int messageLength = strlen(message);
     if (messageLength > USSD_MAXLENGTH) {
         char message_failsafe[100];
-        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,im:%lld,z:TOOBIG,c:%d)\0",api,messageIndex,GLOBAL_imei,messageIndex);
+        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,im:%lld,z:TOOBIG,c:%d)\0",api,GLOBAL_imei,messageIndex);
         result = USSDsend(message_failsafe, maxAttempts);
     } else {
         result = USSDsend(message, maxAttempts);
--- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_ARM_STD/nRF52832.sct	Tue Dec 18 12:43:32 2018 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_ARM_STD/nRF52832.sct	Tue Dec 18 21:41:39 2018 +0000
@@ -26,27 +26,31 @@
 #define MBED_RAM_SIZE   (MBED_RAM_PHYSICAL_SIZE - MBED_RAM_SOFT_DEVICE_SIZE)
 #endif
 
+
 #define MBED_RAM0_START MBED_RAM_START
 #define MBED_RAM0_SIZE  0xE0
+
 #define MBED_RAM1_START (MBED_RAM0_START + MBED_RAM0_SIZE)
 #define MBED_RAM1_SIZE  MBED_RAM_UNINIT_AREA_SIZE
+
 #define MBED_RAM2_START (MBED_RAM1_START + MBED_RAM1_SIZE)
 #define MBED_RAM2_SIZE  (MBED_RAM_SIZE - MBED_RAM0_SIZE - MBED_RAM1_SIZE)
 
+
 LR_IROM1 MBED_APP_START MBED_APP_SIZE {
-ER_IROM1 MBED_APP_START MBED_APP_SIZE {
-*.o (RESET, +First)
-*(InRoot$$Sections) 
-.ANY (+RO)
-}
+	ER_IROM1 MBED_APP_START MBED_APP_SIZE {
+		*.o (RESET, +First)
+		*(InRoot$$Sections) 
+		.ANY (+RO)
+	}
 
-RW_IRAM0 MBED_RAM0_START UNINIT MBED_RAM0_SIZE { ;no init section
-*(*nvictable)
+	RW_IRAM0 MBED_RAM0_START UNINIT MBED_RAM0_SIZE { ;no init section
+		*(*nvictable)
+	}
+	RW_IRAM1 MBED_RAM1_START UNINIT MBED_RAM1_SIZE { ;no init section
+		*(*noinit)
+	}
+	RW_IRAM2 MBED_RAM2_START MBED_RAM2_SIZE {
+		.ANY (+RW +ZI)
+	}
 }
-RW_IRAM1 MBED_RAM1_START UNINIT MBED_RAM1_SIZE { ;no init section
-*(*noinit)
-}
-RW_IRAM2 MBED_RAM2_START MBED_RAM2_SIZE {
-.ANY (+RW +ZI)
-}
-}