init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Diff: main.cpp
- Revision:
- 43:7b232f03628f
- Parent:
- 42:f482f4cdb319
- Child:
- 44:061509d1b38c
--- a/main.cpp Fri Jan 18 00:07:45 2019 +0000 +++ b/main.cpp Sun Jan 20 00:15:03 2019 +0000 @@ -1,19 +1,9 @@ #include "main.h" //------------------------------------------------------------------------------ -//DEFINES -//------------------------------------------------------------------------------ - - -//------------------------------------------------------------------------------ //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES //------------------------------------------------------------------------------ -void mainStateEngine(void); -void selftest(void); -void buttonPress(void); -void buttonRelease(void); -void dumpSettings(void); -//void setEventTimes(void); +//should really add these //------------------------------------------------------------------------------ // GLOBALS @@ -27,7 +17,7 @@ bool GLOBAL_modemOn = false; bool GLOBAL_LEDSequenceinProgress = false; time_t GLOBAL_wakeTime = 0; -char GLOBAL_exceptionString[15]; +char GLOBAL_exceptionString[32]; char GLOBAL_debug_buffer[200]; @@ -188,6 +178,7 @@ void buttonRelease() { RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime); RET_buttonPressCount ++; + led1 = 1; //led off } //------------------------------------------------------------------------------ // RTC TICKER @@ -200,12 +191,14 @@ if (RET_asleep == true) { //hold check if (RET_buttonHoldTime > 4 && RET_buttonHoldTime < 10) { + led1 = 0; // led on 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) { + led1 = 1; //led off RET_buttonHoldTime = 0; RET_buttonPressCount = 0; RET_state_prev = RET_state; @@ -349,9 +342,11 @@ uint8_t lis3dh_id; lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id); if (lis3dh_id == 51) { + GLOBAL_accel_healthy = true; test_pass ++; } else { - addToExceptionString("A."); + GLOBAL_accel_healthy = false; + addToExceptionString("A"); } //Temperature @@ -366,15 +361,15 @@ if (temperature > -40 && temperature < 60) { test_pass ++; } else { - addToExceptionString("T."); + addToExceptionString("T"); } //Result if (test_count == test_pass) { - addToExceptionString("OK."); + addToExceptionString("OK"); return true; } else { - addToExceptionString("FAIL."); + addToExceptionString("FAIL"); return false; } } @@ -426,7 +421,7 @@ RET_motionState = false; GLOBAL_motionStopFlagTriggered = true; if (RET_setting_location_tx_failsafe_hrs > 0) { - RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0f); + RET_motionTotalActivityHours += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f); time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60); sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins); } @@ -463,7 +458,7 @@ if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; } if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; } if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; } - if(RET_setting_motion_g < 6 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;} + if(RET_setting_motion_g < 4 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;} if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; } if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; } if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; } @@ -479,7 +474,6 @@ RET_haveSettings = true; GLOBAL_needToConfigureLis3dh = true; RET_SettingsGotAt = RET_RTCunixtime; - //check for firmware update if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) { read_app_data_from_flash(&app_data); @@ -489,7 +483,6 @@ ThisThread::sleep_for(250); system_reset(); } - return true; } else { if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();} @@ -530,7 +523,7 @@ } } void setEventTimes() { - //SET EVENT TIMES + //SET ALL EVENT TIMES setEventTime_Location(); setEventTime_Activity(); setEventTime_Environmental(); @@ -544,15 +537,19 @@ int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime); char bytestosend[100]; memset(bytestosend,0x00,sizeof(bytestosend)); - snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,v:%.2f,e:%d,z:%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken,error); - modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi); + snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken,error); + if (modem.on(RET_force2G)) { + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { + modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi); + } + } ThisThread::sleep_for(250); } //------------------------------------------------------------------------------ -// SETUP +// EVENTS //------------------------------------------------------------------------------ -bool setup() { +bool event_setup(bool manualrun) { RET_setupInProgress = true; bool pass = true; float temperature = getTemperature(); @@ -564,90 +561,45 @@ 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. + if (manualrun) addToExceptionString("MAN"); char bytestosend[160]; snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,SKU,HW_MAJORREVISION,locString); char result[200]; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi)); if (strcmp(result, "err") != 0) { - if (!saveSettings(result)) { + if (saveSettings(result) == false) { //something went critically wrong getting settings pass = false; - modem.off(false); + modem.off(true); if(GLOBAL_debugLED) LED1errorCode(5,10); //ERROR 5 - sendErrorLog("SETTINGS"); + sendErrorLog("ERRSETTINGS"); } } else { //Response error - sendErrorLog("RESPONSE"); + sendErrorLog("ERRRESP"); } } else { //FAILUREMODE modem failed to register on network - modem.off(false); + modem.off(true); if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 pass = false; } } else { //FAILUREMODE Modem failed to turn on - modem.off(false); + modem.off(true); if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2 pass = false; } - modem.off(false); - + modem.off(true); //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP RET_SetupRunAt = RET_RTCunixtime; - //SET ALL EVENT TIMES setEventTimes(); - RET_setupInProgress = false; //this turns off the flashing led - //RESULT return pass; } -//------------------------------------------------------------------------------ -// EVENTS -//------------------------------------------------------------------------------ -void event_connectiontest_tx(int location_accuracy) { - float temperature = getTemperature(); - float voltage = getBatteryV(); - int selftestresult = selfTest(); - if (modem.on(RET_force2G)) { - char locString[70]; - memcpy(locString, modem.getLocation(location_accuracy, 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]; - snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString); - char result[180]; - bool getSettings = true; - snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi)); - if (result != "err") { - if (getSettings) { - if (saveSettings(result)) { - if(GLOBAL_debugLED) LED1blink(5,250); - } else { - sendErrorLog("SETTINGS"); - } - } - } else { - sendErrorLog("RESPONSE"); - } - } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 - } - } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 2 - } - modem.off(false); - //RESETS - RET_motionTriggeredInTXInterval = 0; - setEventTimes(); -} - void event_turnonofflog_tx(bool turnon) { float temperature = getTemperature(); float voltage = getBatteryV(); @@ -658,19 +610,19 @@ 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); + snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,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); + snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,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]; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi)); } else { - modem.off(false); + modem.off(true); } } else { - modem.off(false); + modem.off(true); } - modem.off(false); + modem.off(true); //RESETS RET_motionTriggeredInTXInterval = 0; } @@ -694,22 +646,20 @@ snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi)); if (result != "err") { if (getSettings) { - saveSettings(result); - } else { - sendErrorLog("SETTINGS"); + if (saveSettings(result) == false){sendErrorLog("ERRSETTINGS");} } } else { - sendErrorLog("RESPONSE"); + sendErrorLog("ERRRESP"); } } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 + modem.off(true); + if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 } } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 2 + modem.off(true); + if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2 } - modem.off(false); + modem.off(true); //RESETS RET_motionTriggeredInTXInterval = 0; setEventTime_Location(); @@ -732,14 +682,14 @@ if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; } } } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 + modem.off(true); + if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 } } else { - modem.off(false); - if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 3 + modem.off(true); + if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 3 } - modem.off(false); + modem.off(true); //RESETS RET_motionTriggeredInTXInterval = 0; setEventTime_Activity(); @@ -749,14 +699,13 @@ // STATE ENGINE //------------------------------------------------------------------------------ void mainStateEngine() { - switch(RET_state) { case STATE_SETUP : { //need the curlies to avoid "transfer of control bypass init error warning" //check that we havent run setup too recently time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt); if (RET_SetupRunAt == 0 || setupRunAt_delta > ONEDAYINSECONDS) { - if (setup()) { + if (event_setup(false)) { // All good setState(STATE_NORMAL); } else { @@ -783,7 +732,7 @@ if (RET_motionTriggeredInTXInterval) { run_location_tx = true; } else { - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX due to no motion\n");debug_exe();} + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX no mot\n");debug_exe();} } } break; @@ -800,9 +749,8 @@ if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0 && strlen(RET_activityData) > 1) { run_activity_tx = true; } - if (strlen(RET_activityData) > 130) { run_activity_tx = true; } + if (strlen(RET_activityData) > 8) { run_activity_tx = true; } //was 130 if (run_activity_tx) { event_activity_tx();} - break; } case STATE_DORMANT : @@ -830,7 +778,7 @@ if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 2\n");debug_exe();} LED1blink(2,300); LED1on(0); - event_connectiontest_tx(1); + event_setup(true); LED1off(); break; } @@ -840,7 +788,7 @@ if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();} LED1blink(3,300); LED1on(0); - event_connectiontest_tx(2); + event_location_tx(); LED1off(); break; } @@ -862,12 +810,12 @@ } if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();} LED1blink(20,100); - event_turnonofflog_tx(true); } break; } 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); @@ -891,29 +839,38 @@ //INIT watchdog.configure(240.0); //4 mins LED1off(); + modem.off(false); RTCticker.attach(&RTCtick, 1.0); button.fall(&buttonPress); button.rise(&buttonRelease); - + read_app_data_from_flash(&app_data); dumpSettings(); //CHECK IF THIS IS RESET - //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset + //0x00000008 == lock up //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset if (NRF_POWER->RESETREAS != 0xffffffff) { + RET_coldBoot = 1; switch(NRF_POWER->RESETREAS) { case 0x00000001 : - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);debug_exe();} - RET_coldBoot = 1; + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:PI\n");debug_exe();} break; case 0x00000002 : - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Watchdog ST:%d\n",RET_state);debug_exe();} - RET_watchdogfired = true; + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:WD\n");debug_exe();} break; case 0x00000004 : - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Soft ST:%d\n",RET_state);debug_exe();} + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:SW\n");debug_exe();} break; + case 0x00000008 : + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:LU\n");debug_exe();} + break; } NRF_POWER->RESETREAS = 0xffffffff; + } else { + //if its not one of these reasons then its a power-on or a brown-out + if(get_flag(&app_data, app_execution_flag) == false) { + //THIS is a unexpected reset / brownout??? + //THIS NEEDS TESTING + } } //CHECK FOR FIRST BOOT @@ -928,6 +885,7 @@ //battery low LED1errorCode(10,2); } + addToExceptionString("FR"); mainthread.wait(2000); } @@ -948,12 +906,20 @@ //MAIN LOGIC if(DEBUG_ON){ if (RET_state != 99) { - debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FW:%d,ST:%d, SET:%d, MFR:%d, MST: %d, RTC:%u, L:%u, LFS:%u, A:%u", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, 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), "F:%d,S:%d, SET:%d, MF:%d, MS: %d, %u, L:%u, LFS:%u, A:%u", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, 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), "FW:%d, ST:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe(); + debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe(); } } + + /* + if (modem.on(RET_force2G)) { + if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { + modem.USSDsend("(b:gps2,a:act,e:1.7!0.15!,t:1547937626,r:0.13,c:1)\0",2); + } + } + */ //STATE ENGINE mainStateEngine(); @@ -964,6 +930,7 @@ bool write_app_data_to_flash_execute = false; if(get_flag(&app_data, app_execution_flag) == true) { clr_flag(&app_data, app_execution_flag); + clr_flag(&app_data, first_run_flag); write_app_data_to_flash_execute = true; } if(app_data.current_firmware_version != FW_VERSION) {