init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
18:22edaa7e74b1
Parent:
17:ba55c026b1d6
Child:
19:22261767c87a
--- a/main.cpp	Mon Dec 17 23:36:10 2018 +0000
+++ b/main.cpp	Tue Dec 18 12:43:32 2018 +0000
@@ -58,7 +58,7 @@
 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             RET_RTCmicros                                   __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));
@@ -99,7 +99,7 @@
         
 //------------------------------------------------------------------------------
 //TIMERS
-//------------------------------------------------------------------------------ 
+//-----------------------------------------------mi------------------------------- 
 LowPowerTicker RTCticker; //no impact on power consumption
 
 //------------------------------------------------------------------------------
@@ -128,8 +128,8 @@
     mainthread.wait(sleep_milliseconds);
 }
 void RTCtick() {
-    RET_RTCmicros += 100;
-    RET_RTCunixtime = (RET_RTCmicros / 1000);
+    RET_RTCmillis += 100;
+    RET_RTCunixtime = (RET_RTCmillis / 1000);
     GLOBAL_RTCunixtime = RET_RTCunixtime;
     //button logic - check for hold
     if (RET_buttonHoldTime > 4000) {
@@ -139,7 +139,7 @@
         RET_state = STATE_BUTTONHOLD;
         mainthread.release();
     } else {
-        if((RET_RTCmicros - RET_buttonPressTime) > 500) {
+        if((RET_RTCmillis - RET_buttonPressTime) > 500) {
             switch (RET_buttonPressCount) {   
                 case 1 :
                     RET_state_prev = RET_state;
@@ -187,13 +187,11 @@
     RET_state = STATE_SETUP;
     RET_state_prev = RET_state;
     RET_RTCunixtime = 0;
-    RET_RTCmicros = 0;
+    RET_RTCmillis = 0;
     RET_buttonPressCount = 0;
     RET_buttonPressTime = 0;
     RET_buttonReleaseTime = 0;
     RET_buttonHoldTime = 0;
-    RET_RTCunixtime = 0;
-    RET_RTCmicros = 0;
     //MOTION STATE
     RET_motionTriggered = 0;
     RET_motionStartTime = 0;
@@ -234,12 +232,12 @@
 // USER BUTTON HANDLING
 //------------------------------------------------------------------------------ 
 void buttonPress() {
-    RET_buttonPressTime = RET_RTCmicros;
+    RET_buttonPressTime = RET_RTCmillis;
 }
 void buttonRelease() {
-    RET_buttonHoldTime = (RET_RTCmicros - RET_buttonPressTime);
+    RET_buttonHoldTime = (RET_RTCmillis - RET_buttonPressTime);
     //debounce catch
-    if((RET_RTCmicros - RET_buttonPressTime) >= 100) {
+    if((RET_RTCmillis - RET_buttonPressTime) >= 100) {
         RET_buttonPressCount ++;
     }
 }
@@ -292,13 +290,37 @@
 //------------------------------------------------------------------------------ 
 bool saveSettings(char* settingsBuffer) {
     //process result
-    int matchCount = 0; 
+    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;
     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",
         TEMP_a,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);
+        
+        //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(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)
+        
         RET_haveSettings = true;
         return true;
     } else {
@@ -336,10 +358,6 @@
         //FAILUREMODE Modem failed to turn on   
     }
      
-    while (1) {
-        Thread::wait(60000);
-        watchdog.kick();
-    }
     
     //RESULT
     if (pass) {
@@ -360,7 +378,6 @@
             factoryReset();
             if (setup()) {
                 copyRETtoGLOBAL();
-                RET_state = STATE_NORMAL;
             }
             break;
         case STATE_NORMAL :
@@ -382,7 +399,7 @@
                 Filesystem filesystem(BD_PAGE_ADDRESS,BD_TOTAL_SIZE);
                 SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
                 float temperature_c = temperature.getTemperature();
-                DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c);
+                DEBUG("Temperature log: %lld,%f \n", RET_RTCunixtime,temperature_c);
                 filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c);
             }
             if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
@@ -414,6 +431,7 @@
             break;
         default :
             RET_state = STATE_SETUP;
+            DEBUG("DEFAULT STATE");
     }
 }
 
@@ -448,7 +466,7 @@
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot == 1) factoryReset();
     
-    //COPY ESSENTIAL VALUES FROM RET TO GLOBAL
+    //COPY ESSENTIAL VALUES FROM RET TO GLOBAL AFTER RESET
     copyRETtoGLOBAL();
     
     while(true) {
@@ -460,13 +478,11 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        DEBUG("State: %i  time: %i, %i, %i, %i \n", RET_state, RET_RTCmicros, 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);
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
-        if (GLOBAL_needToConfigureLis3dh) { //ONLY RESET ACCELL IF WE NEED TO
-            lis3dh_configureForSleep(10,127);
-        }
+        if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(10,127); }
         RET_coldBoot = 0;
     }
 }
\ No newline at end of file