init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Diff: main.cpp
- Revision:
- 48:64b1613941d5
- Parent:
- 47:cc6d4d0bf897
- Child:
- 49:15ddd84ec9fa
--- a/main.cpp Mon Jan 21 19:43:17 2019 +0000 +++ b/main.cpp Tue Feb 05 14:30:57 2019 +0000 @@ -4,15 +4,15 @@ //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES //------------------------------------------------------------------------------ //should really add these - will just add as and when needed for now -void event_activity_tx(uint8_t activity_mode); +void event_activity_tx(); //------------------------------------------------------------------------------ // GLOBALS //------------------------------------------------------------------------------ +bool GLOBAL_debugLED = false; char* GLOBAL_defaultApi = "b:gps2"; bool GLOBAL_accel_healthy = false; bool GLOBAL_motionStopFlagTriggered = false; -bool GLOBAL_debugLED = true; bool GLOBAL_needToConfigureLis3dh = false; bool GLOBAL_registeredOnNetwork = false; bool GLOBAL_modemOn = false; @@ -21,7 +21,7 @@ char GLOBAL_exceptionString[30]; char GLOBAL_debug_buffer[200]; - +//SETTINGS int RET_setting_firmware; uint32_t RET_setting_minimumupdate_hrs; uint8_t RET_setting_location_mode; @@ -43,6 +43,7 @@ //STATE uint8_t RET_coldBoot = 1; bool RET_asleep = false; +bool RET_busy = false; time_t RET_RTCunixtime; bool RET_haveSettings; uint8_t RET_state; @@ -56,6 +57,8 @@ bool RET_force2G; bool RET_watchdogfired; bool RET_receivedNewSettings; +uint32_t RET_GPSFailCount; +uint32_t RET_NetworkFailCount; //MOTION STATE bool RET_motionTriggeredinFrame; bool RET_motionTriggeredInLocTXInterval; @@ -206,12 +209,13 @@ //------------------------------------------------------------------------------ // RTC TICKER //------------------------------------------------------------------------------ +uint8_t RTCtick_ledflash_count = 0; 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, only when device asleep - if (RET_asleep == true) { + //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; @@ -229,29 +233,30 @@ RET_state = STATE_SCORCHEDEARTH; mainthread.release(); } else { - if((RET_RTCunixtime - RET_buttonPressTime) > 1) { - switch (RET_buttonPressCount) { - case 1 : - if(RET_state != STATE_DORMANT) { + 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 : - if(RET_state != STATE_DORMANT) { - RET_state = STATE_BUTTONPRESS2; - mainthread.release(); - } - break; - case 5 : - if(RET_state != STATE_DORMANT) { + break; + case 3 : + RET_state_prev = RET_state; RET_state = STATE_BUTTONPRESS3; mainthread.release(); - } - break; - default : - //do nothing - break; + break; + case 5 : + RET_state_prev = RET_state; + RET_state = STATE_BUTTONPRESS5; + mainthread.release(); + break; + default : + //do nothing + break; + } } RET_SetupRunAt = 0; //allow setup to run again RET_buttonPressCount = 0; @@ -260,8 +265,15 @@ //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; + led1 = 1; //turn off led } - } + //} } void resetGlobals() { GLOBAL_accel_healthy = false; @@ -303,6 +315,7 @@ void setDefaults() { //STATE RET_asleep = false; + RET_busy = false; RET_haveSettings = false; RET_state = STATE_SETUP; RET_state_prev = RET_state; @@ -313,6 +326,8 @@ RET_watchdogfired = false; RET_setupInProgress = false; RET_receivedNewSettings = false; + RET_GPSFailCount = 0; + RET_NetworkFailCount = 0; //SETTINGS RET_setting_firmware = 0; RET_setting_minimumupdate_hrs = 0; @@ -394,10 +409,10 @@ //Result if (test_count == test_pass) { - addToExceptionString("OK"); + //addToExceptionString("HOK"); //dont need this, only add fails return true; } else { - addToExceptionString("FAIL"); + addToExceptionString("HF"); return false; } } @@ -405,9 +420,9 @@ //------------------------------------------------------------------------------ // MOTION FUNCS //------------------------------------------------------------------------------ -void checkMotion() { +bool checkMotion() { if (lis3dh_int2) { - //if (GLOBAL_debugLED) LED1blink(1,50); + if (GLOBAL_debugLED) LED1blink(2,50); RET_motionTriggeredinFrame = true; GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it if (!RET_motionPendingOnState) { @@ -417,6 +432,7 @@ RET_motionStartTime = RET_RTCunixtime; } } else { + if (GLOBAL_debugLED) LED1blink(1,50); RET_motionTriggeredinFrame = false; RET_motionPendingOnState = false; if (!RET_motionPendingOffState) { @@ -450,7 +466,7 @@ RET_motionPendingOffState = false; RET_motionState = false; GLOBAL_motionStopFlagTriggered = true; - if (RET_setting_location_tx_failsafe_hrs > 0) { + if (RET_setting_activity_mode > 0 && RET_setting_location_tx_failsafe_hrs > 0) { RET_motionTotalActivityHoursSincePost += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f); RET_motionTotalActivityHours += RET_motionTotalActivityHoursSincePost; if (RET_setting_activity_mode == 2) { @@ -460,6 +476,10 @@ } } } + + if (GLOBAL_debugLED && RET_motionState == true) LED1on(1000); + + return RET_motionState; } //------------------------------------------------------------------------------ @@ -470,9 +490,10 @@ int critical_fail_count = 0; int TEMP_a = -1; 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 TEMP_t = -1; - int varTotal = 19; //change this if var number changes!!!!!!!! + //int varTotal = 19; //change this if var number changes!!!!!!!! if ( (matchCount = sscanf(settingsBuffer,"a:%d,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,t:%d", &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,&TEMP_t) ) > 0 ) { + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FROMSERVER: a:%d,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,t:%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,TEMP_t);debug_exe();} @@ -512,6 +533,9 @@ RET_SettingsGotAt = RET_RTCunixtime; //check for firmware update if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) { + //Broadcast any activity data we have before fw update + event_activity_tx(); + //prep for FW update read_app_data_from_flash(&app_data); clr_flag(&app_data, app_execution_flag); clr_flag(&app_data, first_run_flag); @@ -519,7 +543,7 @@ app_data.target_firmware_version = RET_setting_firmware; write_app_data_to_flash(&app_data); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OTA\n");debug_exe();} - ThisThread::sleep_for(250); + ThisThread::sleep_for(200); system_reset(); } return true; @@ -580,7 +604,7 @@ memset(bytestosend,0x00,sizeof(bytestosend)); snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s.%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken,error,GLOBAL_exceptionString); if (modem.on(RET_force2G)) { - if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) { modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi); } } @@ -591,7 +615,11 @@ // EVENTS //------------------------------------------------------------------------------ bool event_setup(bool manualrun) { + 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; float temperature = getTemperature(); float voltage = getBatteryV(); @@ -601,9 +629,9 @@ if (modem.on(RET_force2G)) { char locString[70]; int gpsStartTime = RET_RTCunixtime; - memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70); + 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*1000))) { + 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"); @@ -615,26 +643,26 @@ if (saveSettings(result) == false) { //something went critically wrong getting settings pass = false; - sendErrorLog("ERR:SU-IR"); + sendErrorLog("ERR-SU-IR"); } } else { //Response error - sendErrorLog("ERR:SU-NR"); + sendErrorLog("ERR-SU-NR"); } } else { //FAILUREMODE modem failed to register on network modem.off(true); - if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 + LED1errorCode(3,5); //ERROR 3 pass = false; } } else { //FAILUREMODE Modem failed to turn on modem.off(true); - if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2 + LED1errorCode(2,5); //ERROR 2 pass = false; } //SEND ANY ACTIVITY DATA - event_activity_tx(RET_setting_activity_mode); + event_activity_tx(); modem.off(true); //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP @@ -646,15 +674,17 @@ } void event_turnonofflog_tx(bool turnon) { + RET_NetworkFailCount = 0; //reset network blocker + RET_GPSFailCount = 0; float temperature = getTemperature(); float voltage = getBatteryV(); 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), 70); + 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*1000))) { + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) { int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime); int timetaken_connection = (RET_RTCunixtime - connectionStartTime); char bytestosend[160]; @@ -678,39 +708,47 @@ float temperature = getTemperature(); float voltage = getBatteryV(); int selfTestResult = selfTest(); - int connectionStartTime = RET_RTCunixtime; - if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); } - if (modem.on(RET_force2G)) { - char locString[70]; - int gpsStartTime = RET_RTCunixtime; - memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70); - int timetaken_gps = (RET_RTCunixtime - gpsStartTime); - //SEND DATA - if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { - int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time. - int timetaken_connection = (RET_RTCunixtime - connectionStartTime); - //Check if we should wait for settings back - bool getSettings = true; - //work out if we need to get settings back - if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times - char bytesToSend[160]; - snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,y:%d,x:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,getSettings,locString); - char result[180]; - snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi)); - if (result != "err") { - if (getSettings) { - if (saveSettings(result) == false){sendErrorLog("ERR:LOC-IR");} + //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"); + //lets check to see if we've had repeated comms failings in this location, if so dont bother trying. reset by movment and timeout + if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) { + if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); } + if (modem.on(RET_force2G)) { + char locString[70]; + int gpsStartTime = RET_RTCunixtime; + memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString)); + int timetaken_gps = (RET_RTCunixtime - gpsStartTime); + //SEND DATA + int connectionStartTime = RET_RTCunixtime; + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) { + int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time. + int timetaken_connection = (RET_RTCunixtime - connectionStartTime); + //Check if we should wait for settings back + bool getSettings = true; + //work out if we need to get settings back + if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times + char bytesToSend[160]; + snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,y:%d,x:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,getSettings,locString); + char result[180]; + snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi)); + if (result != "err") { + if (getSettings) { + if (saveSettings(result) == false){sendErrorLog("ERR-LOC-IR");} + } + } else { + sendErrorLog("ERR-LOC-NR"); } } else { - sendErrorLog("ERR:LOC-NR"); + //Modem fails to register on network + modem.off(true); + LED1errorCode(3,5); //ERROR 3 } } else { + //Modem fails to turn on modem.off(true); - if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 + LED1errorCode(2,5); //ERROR 2 } - } else { - modem.off(true); - if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2 } modem.off(true); //RESETS @@ -718,38 +756,53 @@ setEventTime_Location(); } -void event_activity_tx(uint8_t activity_mode) { +void event_activity_log(char* eventstring) { + //STUB + return; +} +void event_activity_tx() { //SEND ACTIVITY DATA + //check we have something to send... - if (RET_motionTotalActivityHoursSincePost > 0.0 && activity_mode > 0) { - float temperature = getTemperature(); - float voltage = getBatteryV(); - if (modem.on(RET_force2G)) { - //SEND DATA - if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { - char bytesToSend[160]; - if (activity_mode == 1) { - snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f,c:1)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours); - } else if (activity_mode == 2) { - snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost); - } - char result[180]; - bool getResponse = false; - snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 2, GLOBAL_defaultApi)); - if (result != "err") { - //RESET ACTIVITY FRAME - memset(RET_activityData,0,sizeof(RET_activityData)); - if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; } + if (RET_motionTotalActivityHoursSincePost > 0.0f && RET_setting_activity_mode > 0) { + //Build data to send + char bytesToSend[160]; + if (RET_setting_activity_mode == 1) { + snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f,c:1)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours); + } else if (RET_setting_activity_mode == 2) { + snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost); + } + + if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) { + float temperature = getTemperature(); + float voltage = getBatteryV(); + if (modem.on(RET_force2G)) { + //SEND DATA + if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) { + char result[180]; + bool getResponse = false; + snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 3, GLOBAL_defaultApi)); + if (result != "err") { + //RESET ACTIVITY FRAME + memset(RET_activityData,0,sizeof(RET_activityData)); + if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; } + } else { + //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); - if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 } + modem.off(true); } else { - modem.off(true); - if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 3 + //TODO: LETS LOG THE DATA INSTEAD + event_activity_log(bytesToSend); } - modem.off(true); } //RESETS setEventTime_Activity(); @@ -762,6 +815,7 @@ switch(RET_state) { case STATE_SETUP : { //need the curlies to avoid "transfer of control bypass init error warning" + 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) { @@ -777,10 +831,12 @@ time_t setupCanRunAt = (RET_RTCunixtime + (ONEDAYINSECONDS - setupRunAt_delta)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP CAN RUN AGAIN @%u \n",setupCanRunAt);debug_exe();} } + RET_busy = false; break; } case STATE_NORMAL : - { + { + RET_busy = true; //LOCATION EVENT bool run_location_tx = false; switch (RET_setting_location_mode) { @@ -800,14 +856,18 @@ if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; } break; } - if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; } + if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { + RET_NetworkFailCount = 0; //reset to ensure connection + RET_GPSFailCount = 0; // reset to ensure gps try + run_location_tx = true; + } if (run_location_tx) { event_location_tx(); } //ACTIVITY EVENT bool run_activity_tx = false; if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) { - if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0) { + if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0f) { run_activity_tx = true; } if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > 1) { @@ -816,7 +876,9 @@ } //If in sendevent mode, check if buffer is close to full, if so send if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > (ACTIVITY_BUFFERSIZE-20)) { run_activity_tx = true; } - if (run_activity_tx) { event_activity_tx(RET_setting_activity_mode); } + if (run_activity_tx) { event_activity_tx(); } + + RET_busy = false; break; } case STATE_DORMANT : @@ -833,29 +895,43 @@ } case STATE_BUTTONPRESS1 : { - setState(STATE_NORMAL); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();} - LED1blink(1,300); - break; - } - case STATE_BUTTONPRESS2 : - { - setState(STATE_NORMAL); - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 2\n");debug_exe();} - LED1blink(3,300); - LED1on(0); - event_setup(true); - LED1off(); + if (getBatteryV() < 2.5f) { + LED1blink(3,500); + } else if (selfTest() == false){ + LED1blink(4,500); + } else { + if(RET_state_prev == STATE_DORMANT) { + LED1on(500); + } else if (RET_state_prev == STATE_NORMAL) { + LED1blink(2,500); + } + } + setState(RET_state_prev); break; } case STATE_BUTTONPRESS3 : { - setState(STATE_NORMAL); - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();} - LED1blink(5,300); - LED1on(0); - event_location_tx(); - LED1off(); + if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) { + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();} + LED1blink(3,500); + LED1on(0); + event_setup(true); + LED1off(); + } + setState(STATE_NORMAL); //turns device back on + break; + } + case STATE_BUTTONPRESS5 : + { + if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) { + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();} + LED1blink(5,500); + LED1on(0); + event_location_tx(); + LED1off(); + } + setState(STATE_NORMAL); //turns device back on break; } case STATE_BUTTONHOLD : @@ -881,7 +957,6 @@ } case STATE_SCORCHEDEARTH : { - LED1blink(40,50); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();} setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state read_app_data_from_flash(&app_data); @@ -943,16 +1018,12 @@ if (RET_coldBoot == 1) { setDefaults(); //check battery - float voltage = getBatteryV(); - if (voltage > 2.5f) { - //battery ok - LED1errorCode(6,2); - } else { + if (getBatteryV() < 2.5f) { //battery low LED1errorCode(10,2); + addToExceptionString("BA"); } addToExceptionString("FR"); - mainthread.wait(2000); } //MAIN LOOP @@ -967,7 +1038,13 @@ GLOBAL_wakeTime = RET_RTCunixtime; //check and log motion - if (RET_state == STATE_NORMAL) { checkMotion(); } + if (RET_state == STATE_NORMAL || RET_state == STATE_SETUP) { + if (checkMotion() == true) { + //if motionstate is true, then reset network and gps fail counts, so that they are not skipped + RET_NetworkFailCount = 0; + RET_GPSFailCount = 0; + } + } //MAIN LOGIC if(DEBUG_ON){ @@ -981,7 +1058,7 @@ /* if (modem.on(RET_force2G)) { - if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) { modem.USSDsend("(b:gps2,a:act,e:1.7!0.15!,t:1547937626,r:0.13,c:1)\0",2); } }