init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Mon Jan 21 19:43:17 2019 +0000
Revision:
47:cc6d4d0bf897
Parent:
46:b95fe3484d88
Child:
48:64b1613941d5
rc1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pathfindr 0:94b743e06998 1 #include "main.h"
pathfindr 21:e0b866630c27 2
pathfindr 21:e0b866630c27 3 //------------------------------------------------------------------------------
pathfindr 37:505ef618f06c 4 //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
pathfindr 0:94b743e06998 5 //------------------------------------------------------------------------------
pathfindr 45:6c7f5b556d74 6 //should really add these - will just add as and when needed for now
pathfindr 45:6c7f5b556d74 7 void event_activity_tx(uint8_t activity_mode);
pathfindr 0:94b743e06998 8
pathfindr 0:94b743e06998 9 //------------------------------------------------------------------------------
pathfindr 36:8e359069192b 10 // GLOBALS
pathfindr 0:94b743e06998 11 //------------------------------------------------------------------------------
pathfindr 36:8e359069192b 12 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 36:8e359069192b 13 bool GLOBAL_accel_healthy = false;
pathfindr 36:8e359069192b 14 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 36:8e359069192b 15 bool GLOBAL_debugLED = true;
pathfindr 36:8e359069192b 16 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 36:8e359069192b 17 bool GLOBAL_registeredOnNetwork = false;
pathfindr 36:8e359069192b 18 bool GLOBAL_modemOn = false;
pathfindr 36:8e359069192b 19 bool GLOBAL_LEDSequenceinProgress = false;
pathfindr 36:8e359069192b 20 time_t GLOBAL_wakeTime = 0;
pathfindr 44:061509d1b38c 21 char GLOBAL_exceptionString[30];
pathfindr 36:8e359069192b 22 char GLOBAL_debug_buffer[200];
pathfindr 34:4493c9f6d707 23
pathfindr 0:94b743e06998 24
pathfindr 36:8e359069192b 25 int RET_setting_firmware;
pathfindr 36:8e359069192b 26 uint32_t RET_setting_minimumupdate_hrs;
pathfindr 36:8e359069192b 27 uint8_t RET_setting_location_mode;
pathfindr 36:8e359069192b 28 uint8_t RET_setting_location_accuracy;
pathfindr 36:8e359069192b 29 uint32_t RET_setting_location_tx_interval_mins;
pathfindr 36:8e359069192b 30 uint32_t RET_setting_location_tx_failsafe_hrs;
pathfindr 36:8e359069192b 31 uint16_t RET_setting_location_timeout;
pathfindr 36:8e359069192b 32 uint32_t RET_setting_activity_tx_interval_hrs;
pathfindr 45:6c7f5b556d74 33 uint8_t RET_setting_activity_mode;
pathfindr 36:8e359069192b 34 uint32_t RET_setting_environmental_tx_interval_mins;
pathfindr 36:8e359069192b 35 uint16_t RET_setting_motion_g;
pathfindr 36:8e359069192b 36 time_t RET_setting_motion_start_seconds;
pathfindr 36:8e359069192b 37 time_t RET_setting_motion_stop_seconds;
pathfindr 36:8e359069192b 38 uint16_t RET_setting_impact_g;
pathfindr 36:8e359069192b 39 uint8_t RET_setting_impact_alert;
pathfindr 36:8e359069192b 40 uint16_t RET_setting_connection_timeout;
pathfindr 36:8e359069192b 41 uint16_t RET_setting_beacon_interval_seconds;
pathfindr 36:8e359069192b 42 uint16_t RET_setting_beacon_scan;
pathfindr 13:29f67f256709 43 //STATE
pathfindr 40:4270e09e59f2 44 uint8_t RET_coldBoot = 1;
pathfindr 39:f767b8037475 45 bool RET_asleep = false;
pathfindr 36:8e359069192b 46 time_t RET_RTCunixtime;
pathfindr 36:8e359069192b 47 bool RET_haveSettings;
pathfindr 36:8e359069192b 48 uint8_t RET_state;
pathfindr 36:8e359069192b 49 uint8_t RET_state_prev;
pathfindr 36:8e359069192b 50 uint8_t RET_buttonPressCount;
pathfindr 36:8e359069192b 51 time_t RET_buttonPressTime;
pathfindr 36:8e359069192b 52 time_t RET_buttonHoldTime;
pathfindr 36:8e359069192b 53 time_t RET_SetupRunAt;
pathfindr 36:8e359069192b 54 time_t RET_SettingsGotAt;
pathfindr 41:07e41dbd5304 55 bool RET_setupInProgress;
pathfindr 38:476a9b5629a1 56 bool RET_force2G;
pathfindr 39:f767b8037475 57 bool RET_watchdogfired;
pathfindr 45:6c7f5b556d74 58 bool RET_receivedNewSettings;
pathfindr 16:3bf5f1a5f869 59 //MOTION STATE
pathfindr 40:4270e09e59f2 60 bool RET_motionTriggeredinFrame;
pathfindr 45:6c7f5b556d74 61 bool RET_motionTriggeredInLocTXInterval;
pathfindr 36:8e359069192b 62 time_t RET_motionStartTime;
pathfindr 36:8e359069192b 63 time_t RET_motionStopTime;
pathfindr 36:8e359069192b 64 bool RET_motionPendingOnState;
pathfindr 36:8e359069192b 65 bool RET_motionPendingOffState;
pathfindr 36:8e359069192b 66 bool RET_motionState;
pathfindr 36:8e359069192b 67 float RET_motionTotalActivityHours;
pathfindr 45:6c7f5b556d74 68 float RET_motionTotalActivityHoursSincePost;
pathfindr 36:8e359069192b 69 time_t RET_motionFrameStart;
pathfindr 45:6c7f5b556d74 70 char RET_activityData[ACTIVITY_BUFFERSIZE];
pathfindr 16:3bf5f1a5f869 71 //IMPACT
pathfindr 36:8e359069192b 72 bool RET_impactTriggered;
pathfindr 19:22261767c87a 73 //EVENTS LOGGING
pathfindr 36:8e359069192b 74 time_t RET_eventTime_location_log;
pathfindr 36:8e359069192b 75 time_t RET_eventTime_environmental_log;
pathfindr 19:22261767c87a 76 //EVENTS TX
pathfindr 36:8e359069192b 77 time_t RET_eventTime_location_tx;
pathfindr 36:8e359069192b 78 time_t RET_eventTime_location_failsafe_tx;
pathfindr 36:8e359069192b 79 time_t RET_eventTime_environmental_tx;
pathfindr 36:8e359069192b 80 time_t RET_eventTime_activity_tx;
pathfindr 36:8e359069192b 81 time_t RET_eventTime_wakeFromDormant;
pathfindr 4:8d8e9bfa82e4 82
pathfindr 0:94b743e06998 83 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 84 //GPIO
pathfindr 9:b0a1535b8ef2 85 //------------------------------------------------------------------------------
pathfindr 34:4493c9f6d707 86 InterruptIn button(PN_IN_BUTTON);
pathfindr 9:b0a1535b8ef2 87
pathfindr 9:b0a1535b8ef2 88 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 89 //PERIPHERALS
pathfindr 0:94b743e06998 90 //------------------------------------------------------------------------------
pathfindr 4:8d8e9bfa82e4 91 //BLE myble;
pathfindr 37:505ef618f06c 92 WatchdogTimer watchdog; //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 20:5404841fdd2b 93 void watchdogKick() {watchdog.kick();}
pathfindr 34:4493c9f6d707 94 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 34:4493c9f6d707 95 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 34:4493c9f6d707 96 Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 37:505ef618f06c 97 LowPowerTicker RTCticker;
pathfindr 9:b0a1535b8ef2 98
pathfindr 9:b0a1535b8ef2 99 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 100 //THREAD SEMAPHORES
pathfindr 9:b0a1535b8ef2 101 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 102 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 103
pathfindr 9:b0a1535b8ef2 104 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 105 // LOW LEVEL FUNCS
pathfindr 34:4493c9f6d707 106 //------------------------------------------------------------------------------
pathfindr 34:4493c9f6d707 107 void nrf_configureGPIOForSleep(){
pathfindr 34:4493c9f6d707 108 nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);
pathfindr 34:4493c9f6d707 109 nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
pathfindr 6:388d3c7efdd9 110 }
pathfindr 19:22261767c87a 111 void setState(uint8_t state) {
pathfindr 19:22261767c87a 112 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 113 RET_state = state;
pathfindr 22:810425eb76e1 114 }
pathfindr 34:4493c9f6d707 115 void dumpSettings() {
pathfindr 34:4493c9f6d707 116 if(DEBUG_ON){
pathfindr 34:4493c9f6d707 117 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_RTCunixtime:%u", RET_RTCunixtime);debug_exe();
pathfindr 34:4493c9f6d707 118 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_firmware:%d", RET_setting_firmware);debug_exe();
pathfindr 34:4493c9f6d707 119 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_minimumupdate_hrs:%d", RET_setting_minimumupdate_hrs);debug_exe();
pathfindr 34:4493c9f6d707 120 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_state:%d", RET_state);debug_exe();
pathfindr 34:4493c9f6d707 121 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_mode:%d", RET_setting_location_mode);debug_exe();
pathfindr 34:4493c9f6d707 122 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_accuracy:%d", RET_setting_location_accuracy);debug_exe();
pathfindr 34:4493c9f6d707 123 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_interval_mins:%d", RET_setting_location_tx_interval_mins);debug_exe();
pathfindr 34:4493c9f6d707 124 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_failsafe_hrs:%d", RET_setting_location_tx_failsafe_hrs);debug_exe();
pathfindr 34:4493c9f6d707 125 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_timeout:%d", RET_setting_location_timeout);debug_exe();
pathfindr 34:4493c9f6d707 126 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_tx_interval_hrs:%d", RET_setting_activity_tx_interval_hrs);debug_exe();
pathfindr 45:6c7f5b556d74 127 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_mode:%d", RET_setting_activity_mode);debug_exe();
pathfindr 34:4493c9f6d707 128 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_environmental_tx_interval_mins:%d", RET_setting_environmental_tx_interval_mins);debug_exe();
pathfindr 34:4493c9f6d707 129 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_g:%d", RET_setting_motion_g);debug_exe();
pathfindr 34:4493c9f6d707 130 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_start_seconds:%d", RET_setting_motion_start_seconds);debug_exe();
pathfindr 34:4493c9f6d707 131 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_stop_seconds:%d", RET_setting_motion_stop_seconds);debug_exe();
pathfindr 34:4493c9f6d707 132 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_g:%d", RET_setting_impact_g);debug_exe();
pathfindr 34:4493c9f6d707 133 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_alert:%d", RET_setting_impact_alert);debug_exe();
pathfindr 34:4493c9f6d707 134 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_connection_timeout:%d", RET_setting_connection_timeout);debug_exe();
pathfindr 34:4493c9f6d707 135 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_interval_seconds:%d", RET_setting_beacon_interval_seconds);debug_exe();
pathfindr 34:4493c9f6d707 136 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe();
pathfindr 34:4493c9f6d707 137 }
pathfindr 24:c161db07557f 138 }
pathfindr 45:6c7f5b556d74 139 void recordFirmwareAsValid() {
pathfindr 45:6c7f5b556d74 140 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 141 bool write_app_data_to_flash_execute = false;
pathfindr 45:6c7f5b556d74 142 if(get_flag(&app_data, app_execution_flag) == true) {
pathfindr 45:6c7f5b556d74 143 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 144 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 145 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 146 }
pathfindr 45:6c7f5b556d74 147 if(app_data.current_firmware_version != FW_VERSION) {
pathfindr 45:6c7f5b556d74 148 app_data.current_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 149 app_data.target_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 150 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 151 }
pathfindr 45:6c7f5b556d74 152 if (write_app_data_to_flash_execute) {
pathfindr 45:6c7f5b556d74 153 write_app_data_to_flash(&app_data);
pathfindr 45:6c7f5b556d74 154 }
pathfindr 45:6c7f5b556d74 155 }
pathfindr 34:4493c9f6d707 156 float getBatteryV() {
pathfindr 19:22261767c87a 157 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 158 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 159 batteryIn.calibrate();
pathfindr 25:7adeb1a53360 160 float voltage = 0.0;
pathfindr 25:7adeb1a53360 161 for (uint8_t i = 1; i <= 2; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
pathfindr 19:22261767c87a 162 batteryIn.updateData();
pathfindr 25:7adeb1a53360 163 voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
pathfindr 19:22261767c87a 164 }
pathfindr 25:7adeb1a53360 165 return voltage;
pathfindr 22:810425eb76e1 166 }
pathfindr 36:8e359069192b 167 float nrfTemperature() {
pathfindr 36:8e359069192b 168 //INTERNAL NRF52 TEMP SENSOR
pathfindr 36:8e359069192b 169 uint32_t safetycounter = 0;
pathfindr 36:8e359069192b 170 float temperature = 0.0;
pathfindr 36:8e359069192b 171 NRF_TEMP->TASKS_START=1;
pathfindr 36:8e359069192b 172 while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000) {
pathfindr 36:8e359069192b 173 safetycounter ++;
pathfindr 36:8e359069192b 174 };
pathfindr 36:8e359069192b 175 NRF_TEMP->EVENTS_DATARDY=0;
pathfindr 36:8e359069192b 176 temperature = nrf_temp_read()/4.0;
pathfindr 36:8e359069192b 177 NRF_TEMP->TASKS_STOP=1;
pathfindr 36:8e359069192b 178 return temperature;
pathfindr 36:8e359069192b 179 }
pathfindr 22:810425eb76e1 180 float getTemperature() {
pathfindr 34:4493c9f6d707 181 float temperature;
pathfindr 34:4493c9f6d707 182 if (USE_NRF_TEMP_SENSOR) {
pathfindr 34:4493c9f6d707 183 //INTERNAL NRF52 TEMP SENSOR
pathfindr 36:8e359069192b 184 temperature = nrfTemperature();
pathfindr 34:4493c9f6d707 185 } else {
pathfindr 34:4493c9f6d707 186 temperature = si7060.getTemperature(); //currently disabled because its causing a high current 450ua sleep, most likely due to sensor not sleeping correctly, or i2c sleep issue
pathfindr 34:4493c9f6d707 187 }
pathfindr 22:810425eb76e1 188 return temperature;
pathfindr 19:22261767c87a 189 }
pathfindr 27:fa76f5a08195 190 void addToExceptionString(char* value) {
pathfindr 45:6c7f5b556d74 191 if(strstr(GLOBAL_exceptionString, value) == false){
pathfindr 45:6c7f5b556d74 192 snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),"%s.",value);
pathfindr 45:6c7f5b556d74 193 }
pathfindr 27:fa76f5a08195 194 }
pathfindr 19:22261767c87a 195 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 196 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 197 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 198 void buttonPress() {
pathfindr 19:22261767c87a 199 RET_buttonPressTime = RET_RTCunixtime;
pathfindr 19:22261767c87a 200 }
pathfindr 19:22261767c87a 201 void buttonRelease() {
pathfindr 19:22261767c87a 202 RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime);
pathfindr 19:22261767c87a 203 RET_buttonPressCount ++;
pathfindr 43:7b232f03628f 204 led1 = 1; //led off
pathfindr 19:22261767c87a 205 }
pathfindr 19:22261767c87a 206 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 207 // RTC TICKER
pathfindr 19:22261767c87a 208 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 209 void RTCtick() {
pathfindr 19:22261767c87a 210 //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
pathfindr 19:22261767c87a 211 RET_RTCunixtime += 1;
pathfindr 39:f767b8037475 212
pathfindr 39:f767b8037475 213 //button logic, only when device asleep
pathfindr 39:f767b8037475 214 if (RET_asleep == true) {
pathfindr 44:061509d1b38c 215 //led indicator
pathfindr 44:061509d1b38c 216 //if (RET_buttonPressTime > 0 && (RET_RTCunixtime - RET_buttonPressTime) > 4) led1 = 0;
pathfindr 44:061509d1b38c 217 //if (RET_buttonPressTime > 0 && (RET_RTCunixtime - RET_buttonPressTime) > 14) led1 = 1;
pathfindr 39:f767b8037475 218 //hold check
pathfindr 39:f767b8037475 219 if (RET_buttonHoldTime > 4 && RET_buttonHoldTime < 10) {
pathfindr 39:f767b8037475 220 RET_buttonHoldTime = 0;
pathfindr 39:f767b8037475 221 RET_buttonPressCount = 0;
pathfindr 39:f767b8037475 222 RET_state_prev = RET_state;
pathfindr 39:f767b8037475 223 RET_state = STATE_BUTTONHOLD;
pathfindr 39:f767b8037475 224 mainthread.release();
pathfindr 39:f767b8037475 225 } else if (RET_buttonHoldTime > 14 && RET_buttonHoldTime < 30) {
pathfindr 39:f767b8037475 226 RET_buttonHoldTime = 0;
pathfindr 39:f767b8037475 227 RET_buttonPressCount = 0;
pathfindr 39:f767b8037475 228 RET_state_prev = RET_state;
pathfindr 39:f767b8037475 229 RET_state = STATE_SCORCHEDEARTH;
pathfindr 39:f767b8037475 230 mainthread.release();
pathfindr 39:f767b8037475 231 } else {
pathfindr 39:f767b8037475 232 if((RET_RTCunixtime - RET_buttonPressTime) > 1) {
pathfindr 39:f767b8037475 233 switch (RET_buttonPressCount) {
pathfindr 39:f767b8037475 234 case 1 :
pathfindr 42:f482f4cdb319 235 if(RET_state != STATE_DORMANT) {
pathfindr 42:f482f4cdb319 236 RET_state = STATE_BUTTONPRESS1;
pathfindr 42:f482f4cdb319 237 mainthread.release();
pathfindr 42:f482f4cdb319 238 }
pathfindr 39:f767b8037475 239 break;
pathfindr 39:f767b8037475 240 case 3 :
pathfindr 42:f482f4cdb319 241 if(RET_state != STATE_DORMANT) {
pathfindr 42:f482f4cdb319 242 RET_state = STATE_BUTTONPRESS2;
pathfindr 42:f482f4cdb319 243 mainthread.release();
pathfindr 42:f482f4cdb319 244 }
pathfindr 39:f767b8037475 245 break;
pathfindr 39:f767b8037475 246 case 5 :
pathfindr 42:f482f4cdb319 247 if(RET_state != STATE_DORMANT) {
pathfindr 42:f482f4cdb319 248 RET_state = STATE_BUTTONPRESS3;
pathfindr 42:f482f4cdb319 249 mainthread.release();
pathfindr 42:f482f4cdb319 250 }
pathfindr 39:f767b8037475 251 break;
pathfindr 39:f767b8037475 252 default :
pathfindr 39:f767b8037475 253 //do nothing
pathfindr 39:f767b8037475 254 break;
pathfindr 39:f767b8037475 255 }
pathfindr 44:061509d1b38c 256 RET_SetupRunAt = 0; //allow setup to run again
pathfindr 39:f767b8037475 257 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 258 }
pathfindr 9:b0a1535b8ef2 259 }
pathfindr 39:f767b8037475 260 //SETUP STATE VISUALISE
pathfindr 41:07e41dbd5304 261 if (!GLOBAL_LEDSequenceinProgress && RET_setupInProgress) {
pathfindr 39:f767b8037475 262 led1 = !led1;
pathfindr 39:f767b8037475 263 }
pathfindr 19:22261767c87a 264 }
pathfindr 9:b0a1535b8ef2 265 }
pathfindr 34:4493c9f6d707 266 void resetGlobals() {
pathfindr 34:4493c9f6d707 267 GLOBAL_accel_healthy = false;
pathfindr 34:4493c9f6d707 268 GLOBAL_motionStopFlagTriggered = false;
pathfindr 34:4493c9f6d707 269 memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
pathfindr 34:4493c9f6d707 270 }
pathfindr 38:476a9b5629a1 271 void healthCheck() {
pathfindr 39:f767b8037475 272 //check for watchdog fire
pathfindr 39:f767b8037475 273 if (RET_watchdogfired == true) {
pathfindr 39:f767b8037475 274 addToExceptionString("WD");
pathfindr 39:f767b8037475 275 RET_watchdogfired = false;
pathfindr 39:f767b8037475 276 }
pathfindr 41:07e41dbd5304 277 //check clock
pathfindr 41:07e41dbd5304 278 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 279 if (RET_RTCunixtime < 1547678732) {
pathfindr 41:07e41dbd5304 280 //go dormant for 72hrs and then resetup
pathfindr 41:07e41dbd5304 281 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 282 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 283 }
pathfindr 41:07e41dbd5304 284 }
pathfindr 41:07e41dbd5304 285 //check that we have a reasonable time to loc
pathfindr 41:07e41dbd5304 286 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 287 //check location tx delta is set
pathfindr 41:07e41dbd5304 288 if (RET_eventTime_location_tx == 0) {
pathfindr 41:07e41dbd5304 289 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
pathfindr 41:07e41dbd5304 290 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 291 }
pathfindr 41:07e41dbd5304 292 //check location failsafe tx delta is less that 2 weeks
pathfindr 41:07e41dbd5304 293 long location_failsafe_tx_delta = (RET_eventTime_location_failsafe_tx - RET_RTCunixtime);
pathfindr 41:07e41dbd5304 294 if (location_failsafe_tx_delta > TENDAYSINSECONDS) {
pathfindr 41:07e41dbd5304 295 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:location_failsafe_tx_delta too small");debug_exe();}
pathfindr 41:07e41dbd5304 296 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 297 }
pathfindr 41:07e41dbd5304 298 }
pathfindr 41:07e41dbd5304 299 if (RET_haveSettings == false) {
pathfindr 41:07e41dbd5304 300 setState(STATE_SETUP);
pathfindr 41:07e41dbd5304 301 }
pathfindr 38:476a9b5629a1 302 }
pathfindr 36:8e359069192b 303 void setDefaults() {
pathfindr 35:d9421d57d116 304 //STATE
pathfindr 39:f767b8037475 305 RET_asleep = false;
pathfindr 41:07e41dbd5304 306 RET_haveSettings = false;
pathfindr 35:d9421d57d116 307 RET_state = STATE_SETUP;
pathfindr 35:d9421d57d116 308 RET_state_prev = RET_state;
pathfindr 35:d9421d57d116 309 RET_RTCunixtime = 0;
pathfindr 35:d9421d57d116 310 RET_SetupRunAt = 0;
pathfindr 35:d9421d57d116 311 RET_SettingsGotAt = 0;
pathfindr 38:476a9b5629a1 312 RET_force2G = DEFAULT_FORCE2G;
pathfindr 39:f767b8037475 313 RET_watchdogfired = false;
pathfindr 41:07e41dbd5304 314 RET_setupInProgress = false;
pathfindr 45:6c7f5b556d74 315 RET_receivedNewSettings = false;
pathfindr 35:d9421d57d116 316 //SETTINGS
pathfindr 35:d9421d57d116 317 RET_setting_firmware = 0;
pathfindr 35:d9421d57d116 318 RET_setting_minimumupdate_hrs = 0;
pathfindr 35:d9421d57d116 319 RET_setting_location_mode = DEFAULT_LOCATION_MODE;
pathfindr 35:d9421d57d116 320 RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
pathfindr 35:d9421d57d116 321 RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
pathfindr 35:d9421d57d116 322 RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
pathfindr 35:d9421d57d116 323 RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
pathfindr 35:d9421d57d116 324 RET_setting_activity_tx_interval_hrs = 0;
pathfindr 45:6c7f5b556d74 325 RET_setting_activity_mode = 1;
pathfindr 35:d9421d57d116 326 RET_setting_environmental_tx_interval_mins = 0;
pathfindr 35:d9421d57d116 327 RET_setting_motion_g = DEFAULT_MOTION_G;
pathfindr 35:d9421d57d116 328 RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
pathfindr 35:d9421d57d116 329 RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
pathfindr 35:d9421d57d116 330 RET_setting_impact_g = 0;
pathfindr 35:d9421d57d116 331 RET_setting_impact_alert = 0;
pathfindr 35:d9421d57d116 332 RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
pathfindr 35:d9421d57d116 333 RET_setting_beacon_interval_seconds = DEFAULT_BEACON_INTERVAL_SECONDS;
pathfindr 35:d9421d57d116 334 RET_setting_beacon_scan = 0;
pathfindr 35:d9421d57d116 335 RET_buttonPressCount = 0;
pathfindr 35:d9421d57d116 336 RET_buttonPressTime = 0;
pathfindr 35:d9421d57d116 337 RET_buttonHoldTime = 0;
pathfindr 35:d9421d57d116 338 //MOTION STATE
pathfindr 45:6c7f5b556d74 339 RET_motionTriggeredinFrame = false;
pathfindr 45:6c7f5b556d74 340 RET_motionTriggeredInLocTXInterval = false;
pathfindr 35:d9421d57d116 341 RET_motionStartTime = 0;
pathfindr 35:d9421d57d116 342 RET_motionStopTime = 0;
pathfindr 35:d9421d57d116 343 RET_motionPendingOnState = 0;
pathfindr 35:d9421d57d116 344 RET_motionPendingOffState = 0;
pathfindr 35:d9421d57d116 345 RET_motionState = 0;
pathfindr 35:d9421d57d116 346 RET_motionTotalActivityHours = 0.0;
pathfindr 45:6c7f5b556d74 347 RET_motionTotalActivityHoursSincePost = 0.0;
pathfindr 35:d9421d57d116 348 RET_motionFrameStart = 0;
pathfindr 35:d9421d57d116 349 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 35:d9421d57d116 350 //IMPACT
pathfindr 35:d9421d57d116 351 RET_impactTriggered = 0;
pathfindr 35:d9421d57d116 352 //EVENT HANDLING
pathfindr 35:d9421d57d116 353 RET_eventTime_location_log = 0;
pathfindr 35:d9421d57d116 354 RET_eventTime_location_tx = 0;
pathfindr 35:d9421d57d116 355 RET_eventTime_location_failsafe_tx = 0;
pathfindr 35:d9421d57d116 356 RET_eventTime_environmental_log = 0;
pathfindr 35:d9421d57d116 357 RET_eventTime_environmental_tx = 0;
pathfindr 35:d9421d57d116 358 RET_eventTime_activity_tx = 0;
pathfindr 35:d9421d57d116 359 RET_eventTime_wakeFromDormant = 0;
pathfindr 35:d9421d57d116 360 //PERIPHERAL RESET
pathfindr 35:d9421d57d116 361 lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
pathfindr 35:d9421d57d116 362 }
pathfindr 24:c161db07557f 363
pathfindr 22:810425eb76e1 364 bool selfTest() {
pathfindr 22:810425eb76e1 365 int test_count = 0;
pathfindr 22:810425eb76e1 366 int test_pass = 0;
pathfindr 22:810425eb76e1 367
pathfindr 8:7351f8c4cd60 368 //Accelerometer
pathfindr 22:810425eb76e1 369 test_count ++;
pathfindr 8:7351f8c4cd60 370 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 371 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 27:fa76f5a08195 372 if (lis3dh_id == 51) {
pathfindr 43:7b232f03628f 373 GLOBAL_accel_healthy = true;
pathfindr 27:fa76f5a08195 374 test_pass ++;
pathfindr 27:fa76f5a08195 375 } else {
pathfindr 43:7b232f03628f 376 GLOBAL_accel_healthy = false;
pathfindr 43:7b232f03628f 377 addToExceptionString("A");
pathfindr 27:fa76f5a08195 378 }
pathfindr 22:810425eb76e1 379
pathfindr 22:810425eb76e1 380 //Temperature
pathfindr 22:810425eb76e1 381 test_count ++;
pathfindr 22:810425eb76e1 382 float temperature;
pathfindr 34:4493c9f6d707 383 if (USE_NRF_TEMP_SENSOR) {
pathfindr 34:4493c9f6d707 384 //INTERNAL NRF52 TEMP SENSOR
pathfindr 36:8e359069192b 385 temperature = nrfTemperature();
pathfindr 34:4493c9f6d707 386 } else {
pathfindr 34:4493c9f6d707 387 temperature = si7060.getTemperature();
pathfindr 34:4493c9f6d707 388 }
pathfindr 27:fa76f5a08195 389 if (temperature > -40 && temperature < 60) {
pathfindr 27:fa76f5a08195 390 test_pass ++;
pathfindr 27:fa76f5a08195 391 } else {
pathfindr 43:7b232f03628f 392 addToExceptionString("T");
pathfindr 27:fa76f5a08195 393 }
pathfindr 22:810425eb76e1 394
pathfindr 22:810425eb76e1 395 //Result
pathfindr 22:810425eb76e1 396 if (test_count == test_pass) {
pathfindr 43:7b232f03628f 397 addToExceptionString("OK");
pathfindr 22:810425eb76e1 398 return true;
pathfindr 22:810425eb76e1 399 } else {
pathfindr 43:7b232f03628f 400 addToExceptionString("FAIL");
pathfindr 22:810425eb76e1 401 return false;
pathfindr 9:b0a1535b8ef2 402 }
pathfindr 9:b0a1535b8ef2 403 }
pathfindr 9:b0a1535b8ef2 404
pathfindr 9:b0a1535b8ef2 405 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 406 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 407 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 408 void checkMotion() {
pathfindr 9:b0a1535b8ef2 409 if (lis3dh_int2) {
pathfindr 29:059fc7324328 410 //if (GLOBAL_debugLED) LED1blink(1,50);
pathfindr 40:4270e09e59f2 411 RET_motionTriggeredinFrame = true;
pathfindr 13:29f67f256709 412 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 9:b0a1535b8ef2 413 if (!RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 414 RET_motionPendingOnState = true;
pathfindr 9:b0a1535b8ef2 415 RET_motionPendingOffState = false;
pathfindr 9:b0a1535b8ef2 416 // Log start motion time
pathfindr 13:29f67f256709 417 RET_motionStartTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 418 }
pathfindr 9:b0a1535b8ef2 419 } else {
pathfindr 40:4270e09e59f2 420 RET_motionTriggeredinFrame = false;
pathfindr 9:b0a1535b8ef2 421 RET_motionPendingOnState = false;
pathfindr 9:b0a1535b8ef2 422 if (!RET_motionPendingOffState) {
pathfindr 9:b0a1535b8ef2 423 RET_motionPendingOffState = true;
pathfindr 9:b0a1535b8ef2 424 //log stop motion time
pathfindr 13:29f67f256709 425 RET_motionStopTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 426 }
pathfindr 9:b0a1535b8ef2 427 }
pathfindr 9:b0a1535b8ef2 428 //calculate motion state
pathfindr 9:b0a1535b8ef2 429 if (RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 430 //check if above threshold
pathfindr 45:6c7f5b556d74 431 time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 45:6c7f5b556d74 432 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOT:%u s",inMotionForSeconds);debug_exe();}
pathfindr 24:c161db07557f 433 if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) {
pathfindr 9:b0a1535b8ef2 434 RET_motionState = true;
pathfindr 45:6c7f5b556d74 435 RET_motionTriggeredInLocTXInterval = true;
pathfindr 41:07e41dbd5304 436 //if (GLOBAL_debugLED) LED1blink(1,50);
pathfindr 45:6c7f5b556d74 437 if (RET_setting_activity_mode == 2) {
pathfindr 45:6c7f5b556d74 438 if (RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 45:6c7f5b556d74 439 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 45:6c7f5b556d74 440 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
pathfindr 45:6c7f5b556d74 441 }
pathfindr 23:a3b0ccf75ca5 442 }
pathfindr 9:b0a1535b8ef2 443 }
pathfindr 9:b0a1535b8ef2 444 }
pathfindr 9:b0a1535b8ef2 445 if (RET_motionPendingOffState) {
pathfindr 45:6c7f5b556d74 446 time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 45:6c7f5b556d74 447 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOTLESS:%u s",noMotionForSeconds);debug_exe();}
pathfindr 24:c161db07557f 448 if (noMotionForSeconds >= RET_setting_motion_stop_seconds && RET_motionState == true) {
pathfindr 41:07e41dbd5304 449 //if (GLOBAL_debugLED) LED1blink(2,50);
pathfindr 24:c161db07557f 450 RET_motionPendingOffState = false;
pathfindr 9:b0a1535b8ef2 451 RET_motionState = false;
pathfindr 20:5404841fdd2b 452 GLOBAL_motionStopFlagTriggered = true;
pathfindr 24:c161db07557f 453 if (RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 45:6c7f5b556d74 454 RET_motionTotalActivityHoursSincePost += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f);
pathfindr 45:6c7f5b556d74 455 RET_motionTotalActivityHours += RET_motionTotalActivityHoursSincePost;
pathfindr 45:6c7f5b556d74 456 if (RET_setting_activity_mode == 2) {
pathfindr 45:6c7f5b556d74 457 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 45:6c7f5b556d74 458 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
pathfindr 45:6c7f5b556d74 459 }
pathfindr 23:a3b0ccf75ca5 460 }
pathfindr 9:b0a1535b8ef2 461 }
pathfindr 9:b0a1535b8ef2 462 }
pathfindr 8:7351f8c4cd60 463 }
pathfindr 8:7351f8c4cd60 464
pathfindr 14:9a54b1b65bc8 465 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 466 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 467 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 468 bool saveSettings(char* settingsBuffer) {
pathfindr 18:22edaa7e74b1 469 int matchCount = 0;
pathfindr 19:22261767c87a 470 int critical_fail_count = 0;
pathfindr 32:dff4858bdf37 471 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;
pathfindr 45:6c7f5b556d74 472 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;
pathfindr 45:6c7f5b556d74 473 int varTotal = 19; //change this if var number changes!!!!!!!!
pathfindr 45:6c7f5b556d74 474 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",
pathfindr 45:6c7f5b556d74 475 &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 ) {
pathfindr 45:6c7f5b556d74 476 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",
pathfindr 45:6c7f5b556d74 477 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();}
pathfindr 34:4493c9f6d707 478
pathfindr 41:07e41dbd5304 479 if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; }
pathfindr 41:07e41dbd5304 480 if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b; } else { critical_fail_count++; }
pathfindr 41:07e41dbd5304 481 if(TEMP_c != -1) { RET_setting_firmware = TEMP_c; } else { RET_setting_firmware = -1;}
pathfindr 41:07e41dbd5304 482 if(TEMP_d != -1) { setState(TEMP_d); } else { setState(STATE_NORMAL);}
pathfindr 41:07e41dbd5304 483 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; }
pathfindr 34:4493c9f6d707 484 if(RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
pathfindr 41:07e41dbd5304 485 if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f; }
pathfindr 34:4493c9f6d707 486 if(RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
pathfindr 41:07e41dbd5304 487 if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g; }
pathfindr 41:07e41dbd5304 488 if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h; }
pathfindr 34:4493c9f6d707 489 if(RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
pathfindr 41:07e41dbd5304 490 if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i; }
pathfindr 27:fa76f5a08195 491 if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
pathfindr 41:07e41dbd5304 492 if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; }
pathfindr 41:07e41dbd5304 493 if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; }
pathfindr 41:07e41dbd5304 494 if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; }
pathfindr 43:7b232f03628f 495 if(RET_setting_motion_g < 4 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
pathfindr 41:07e41dbd5304 496 if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; }
pathfindr 41:07e41dbd5304 497 if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; }
pathfindr 41:07e41dbd5304 498 if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; }
pathfindr 41:07e41dbd5304 499 if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p; }
pathfindr 41:07e41dbd5304 500 if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q; }
pathfindr 47:cc6d4d0bf897 501 if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
pathfindr 41:07e41dbd5304 502 if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r; }
pathfindr 41:07e41dbd5304 503 if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s; }
pathfindr 45:6c7f5b556d74 504 if(TEMP_t != -1) { RET_setting_activity_mode = TEMP_t; }
pathfindr 45:6c7f5b556d74 505
pathfindr 19:22261767c87a 506 if (critical_fail_count == 0) {
pathfindr 45:6c7f5b556d74 507 RET_receivedNewSettings = true;
pathfindr 44:061509d1b38c 508 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SETTINGS OK");debug_exe();}
pathfindr 40:4270e09e59f2 509 dumpSettings();
pathfindr 32:dff4858bdf37 510 RET_haveSettings = true;
pathfindr 34:4493c9f6d707 511 GLOBAL_needToConfigureLis3dh = true;
pathfindr 32:dff4858bdf37 512 RET_SettingsGotAt = RET_RTCunixtime;
pathfindr 37:505ef618f06c 513 //check for firmware update
pathfindr 38:476a9b5629a1 514 if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) {
pathfindr 37:505ef618f06c 515 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 516 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 517 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 518 app_data.current_firmware_version = FW_VERSION;
pathfindr 37:505ef618f06c 519 app_data.target_firmware_version = RET_setting_firmware;
pathfindr 37:505ef618f06c 520 write_app_data_to_flash(&app_data);
pathfindr 44:061509d1b38c 521 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OTA\n");debug_exe();}
pathfindr 38:476a9b5629a1 522 ThisThread::sleep_for(250);
pathfindr 37:505ef618f06c 523 system_reset();
pathfindr 37:505ef618f06c 524 }
pathfindr 19:22261767c87a 525 return true;
pathfindr 19:22261767c87a 526 } else {
pathfindr 34:4493c9f6d707 527 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
pathfindr 34:4493c9f6d707 528 dumpSettings();
pathfindr 44:061509d1b38c 529 addToExceptionString("SCF");
pathfindr 44:061509d1b38c 530 //dont set RET_haveSettings = false; here, if we already have settings continue to use them
pathfindr 19:22261767c87a 531 return false;
pathfindr 19:22261767c87a 532 }
pathfindr 14:9a54b1b65bc8 533 } else {
pathfindr 44:061509d1b38c 534 //dont set RET_haveSettings = false; here, if we already have settings continue to use them
pathfindr 44:061509d1b38c 535 addToExceptionString("SPE");
pathfindr 15:7aad9a7f970c 536 return false;
pathfindr 14:9a54b1b65bc8 537 }
pathfindr 14:9a54b1b65bc8 538 }
pathfindr 24:c161db07557f 539
pathfindr 41:07e41dbd5304 540 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 541 // SET EVENT TIMES
pathfindr 41:07e41dbd5304 542 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 543 void setEventTime_Location() {
pathfindr 20:5404841fdd2b 544 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 545 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 44:061509d1b38c 546 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC TX @ %u, MODE %d",RET_eventTime_location_tx, RET_setting_location_mode);debug_exe();}
pathfindr 20:5404841fdd2b 547 }
pathfindr 20:5404841fdd2b 548 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 549 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 44:061509d1b38c 550 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC FS TX @ %u",RET_eventTime_location_failsafe_tx);debug_exe();}
pathfindr 20:5404841fdd2b 551 }
pathfindr 41:07e41dbd5304 552 }
pathfindr 41:07e41dbd5304 553 void setEventTime_Activity() {
pathfindr 22:810425eb76e1 554 if(RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 24:c161db07557f 555 RET_motionFrameStart = RET_RTCunixtime; //SET START FRAME INITAL
pathfindr 22:810425eb76e1 556 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600));
pathfindr 44:061509d1b38c 557 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ACT TX @ %u",RET_eventTime_activity_tx);debug_exe();}
pathfindr 20:5404841fdd2b 558 }
pathfindr 41:07e41dbd5304 559 }
pathfindr 41:07e41dbd5304 560 void setEventTime_Environmental() {
pathfindr 20:5404841fdd2b 561 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 562 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 44:061509d1b38c 563 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ENV TX @ %u",RET_eventTime_environmental_tx);debug_exe();}
pathfindr 34:4493c9f6d707 564 }
pathfindr 20:5404841fdd2b 565 }
pathfindr 41:07e41dbd5304 566 void setEventTimes() {
pathfindr 43:7b232f03628f 567 //SET ALL EVENT TIMES
pathfindr 41:07e41dbd5304 568 setEventTime_Location();
pathfindr 41:07e41dbd5304 569 setEventTime_Activity();
pathfindr 41:07e41dbd5304 570 setEventTime_Environmental();
pathfindr 41:07e41dbd5304 571 }
pathfindr 13:29f67f256709 572
pathfindr 13:29f67f256709 573 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 574 // ERROR LOG
pathfindr 40:4270e09e59f2 575 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 576 void sendErrorLog(char* error) {
pathfindr 40:4270e09e59f2 577 float voltage = getBatteryV();
pathfindr 40:4270e09e59f2 578 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 40:4270e09e59f2 579 char bytestosend[100];
pathfindr 40:4270e09e59f2 580 memset(bytestosend,0x00,sizeof(bytestosend));
pathfindr 44:061509d1b38c 581 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);
pathfindr 43:7b232f03628f 582 if (modem.on(RET_force2G)) {
pathfindr 43:7b232f03628f 583 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 43:7b232f03628f 584 modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi);
pathfindr 43:7b232f03628f 585 }
pathfindr 43:7b232f03628f 586 }
pathfindr 40:4270e09e59f2 587 ThisThread::sleep_for(250);
pathfindr 40:4270e09e59f2 588 }
pathfindr 40:4270e09e59f2 589
pathfindr 40:4270e09e59f2 590 //------------------------------------------------------------------------------
pathfindr 43:7b232f03628f 591 // EVENTS
pathfindr 13:29f67f256709 592 //------------------------------------------------------------------------------
pathfindr 43:7b232f03628f 593 bool event_setup(bool manualrun) {
pathfindr 41:07e41dbd5304 594 RET_setupInProgress = true;
pathfindr 13:29f67f256709 595 bool pass = true;
pathfindr 34:4493c9f6d707 596 float temperature = getTemperature();
pathfindr 25:7adeb1a53360 597 float voltage = getBatteryV();
pathfindr 25:7adeb1a53360 598 bool selftestresult = selfTest();
pathfindr 45:6c7f5b556d74 599 if (selftestresult == false) { LED1errorCode(4,10); } //ERROR 4
pathfindr 45:6c7f5b556d74 600 int connectionStartTime = RET_RTCunixtime;
pathfindr 38:476a9b5629a1 601 if (modem.on(RET_force2G)) {
pathfindr 28:24b02608fa5f 602 char locString[70];
pathfindr 45:6c7f5b556d74 603 int gpsStartTime = RET_RTCunixtime;
pathfindr 28:24b02608fa5f 604 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
pathfindr 45:6c7f5b556d74 605 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 28:24b02608fa5f 606 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 45:6c7f5b556d74 607 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
pathfindr 45:6c7f5b556d74 608 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 43:7b232f03628f 609 if (manualrun) addToExceptionString("MAN");
pathfindr 17:ba55c026b1d6 610 char bytestosend[160];
pathfindr 45:6c7f5b556d74 611 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,x:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken_total,timetaken_connection,timetaken_gps,GLOBAL_exceptionString,SKU,HW_MAJORREVISION,locString);
pathfindr 34:4493c9f6d707 612 char result[200];
pathfindr 17:ba55c026b1d6 613 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
pathfindr 34:4493c9f6d707 614 if (strcmp(result, "err") != 0) {
pathfindr 43:7b232f03628f 615 if (saveSettings(result) == false) {
pathfindr 19:22261767c87a 616 //something went critically wrong getting settings
pathfindr 19:22261767c87a 617 pass = false;
pathfindr 44:061509d1b38c 618 sendErrorLog("ERR:SU-IR");
pathfindr 40:4270e09e59f2 619 }
pathfindr 34:4493c9f6d707 620 } else {
pathfindr 34:4493c9f6d707 621 //Response error
pathfindr 44:061509d1b38c 622 sendErrorLog("ERR:SU-NR");
pathfindr 16:3bf5f1a5f869 623 }
pathfindr 13:29f67f256709 624 } else {
pathfindr 16:3bf5f1a5f869 625 //FAILUREMODE modem failed to register on network
pathfindr 43:7b232f03628f 626 modem.off(true);
pathfindr 34:4493c9f6d707 627 if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3
pathfindr 19:22261767c87a 628 pass = false;
pathfindr 13:29f67f256709 629 }
pathfindr 13:29f67f256709 630 } else {
pathfindr 19:22261767c87a 631 //FAILUREMODE Modem failed to turn on
pathfindr 43:7b232f03628f 632 modem.off(true);
pathfindr 34:4493c9f6d707 633 if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2
pathfindr 13:29f67f256709 634 pass = false;
pathfindr 13:29f67f256709 635 }
pathfindr 45:6c7f5b556d74 636 //SEND ANY ACTIVITY DATA
pathfindr 45:6c7f5b556d74 637 event_activity_tx(RET_setting_activity_mode);
pathfindr 43:7b232f03628f 638 modem.off(true);
pathfindr 45:6c7f5b556d74 639
pathfindr 21:e0b866630c27 640 //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 641 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 41:07e41dbd5304 642 setEventTimes();
pathfindr 41:07e41dbd5304 643 RET_setupInProgress = false; //this turns off the flashing led
pathfindr 13:29f67f256709 644 //RESULT
pathfindr 19:22261767c87a 645 return pass;
pathfindr 13:29f67f256709 646 }
pathfindr 13:29f67f256709 647
pathfindr 39:f767b8037475 648 void event_turnonofflog_tx(bool turnon) {
pathfindr 39:f767b8037475 649 float temperature = getTemperature();
pathfindr 39:f767b8037475 650 float voltage = getBatteryV();
pathfindr 45:6c7f5b556d74 651 int connectionStartTime = RET_RTCunixtime;
pathfindr 39:f767b8037475 652 if (modem.on(RET_force2G)) {
pathfindr 39:f767b8037475 653 char locString[70];
pathfindr 45:6c7f5b556d74 654 int gpsStartTime = RET_RTCunixtime;
pathfindr 39:f767b8037475 655 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
pathfindr 45:6c7f5b556d74 656 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 39:f767b8037475 657 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 45:6c7f5b556d74 658 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 45:6c7f5b556d74 659 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 39:f767b8037475 660 char bytestosend[160];
pathfindr 39:f767b8037475 661 if (turnon) {
pathfindr 45:6c7f5b556d74 662 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNON,e:%d,y:%d,x:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 663 } else {
pathfindr 45:6c7f5b556d74 664 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,y:%d,x:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 665 }
pathfindr 39:f767b8037475 666 char result[180];
pathfindr 40:4270e09e59f2 667 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi));
pathfindr 39:f767b8037475 668 } else {
pathfindr 43:7b232f03628f 669 modem.off(true);
pathfindr 39:f767b8037475 670 }
pathfindr 39:f767b8037475 671 } else {
pathfindr 43:7b232f03628f 672 modem.off(true);
pathfindr 39:f767b8037475 673 }
pathfindr 43:7b232f03628f 674 modem.off(true);
pathfindr 39:f767b8037475 675 }
pathfindr 39:f767b8037475 676
pathfindr 22:810425eb76e1 677 void event_location_tx() {
pathfindr 22:810425eb76e1 678 float temperature = getTemperature();
pathfindr 34:4493c9f6d707 679 float voltage = getBatteryV();
pathfindr 27:fa76f5a08195 680 int selfTestResult = selfTest();
pathfindr 45:6c7f5b556d74 681 int connectionStartTime = RET_RTCunixtime;
pathfindr 45:6c7f5b556d74 682 if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); }
pathfindr 38:476a9b5629a1 683 if (modem.on(RET_force2G)) {
pathfindr 23:a3b0ccf75ca5 684 char locString[70];
pathfindr 45:6c7f5b556d74 685 int gpsStartTime = RET_RTCunixtime;
pathfindr 31:c84fc6d8eaa3 686 memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70);
pathfindr 45:6c7f5b556d74 687 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 22:810425eb76e1 688 //SEND DATA
pathfindr 28:24b02608fa5f 689 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 45:6c7f5b556d74 690 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
pathfindr 45:6c7f5b556d74 691 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 32:dff4858bdf37 692 //Check if we should wait for settings back
pathfindr 32:dff4858bdf37 693 bool getSettings = true;
pathfindr 45:6c7f5b556d74 694 //work out if we need to get settings back
pathfindr 45:6c7f5b556d74 695 if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times
pathfindr 23:a3b0ccf75ca5 696 char bytesToSend[160];
pathfindr 45:6c7f5b556d74 697 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);
pathfindr 22:810425eb76e1 698 char result[180];
pathfindr 23:a3b0ccf75ca5 699 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
pathfindr 22:810425eb76e1 700 if (result != "err") {
pathfindr 22:810425eb76e1 701 if (getSettings) {
pathfindr 44:061509d1b38c 702 if (saveSettings(result) == false){sendErrorLog("ERR:LOC-IR");}
pathfindr 22:810425eb76e1 703 }
pathfindr 40:4270e09e59f2 704 } else {
pathfindr 44:061509d1b38c 705 sendErrorLog("ERR:LOC-NR");
pathfindr 22:810425eb76e1 706 }
pathfindr 33:760005331b4c 707 } else {
pathfindr 43:7b232f03628f 708 modem.off(true);
pathfindr 43:7b232f03628f 709 if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3
pathfindr 22:810425eb76e1 710 }
pathfindr 31:c84fc6d8eaa3 711 } else {
pathfindr 43:7b232f03628f 712 modem.off(true);
pathfindr 43:7b232f03628f 713 if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2
pathfindr 20:5404841fdd2b 714 }
pathfindr 43:7b232f03628f 715 modem.off(true);
pathfindr 20:5404841fdd2b 716 //RESETS
pathfindr 45:6c7f5b556d74 717 RET_motionTriggeredInLocTXInterval = false;
pathfindr 41:07e41dbd5304 718 setEventTime_Location();
pathfindr 19:22261767c87a 719 }
pathfindr 24:c161db07557f 720
pathfindr 45:6c7f5b556d74 721 void event_activity_tx(uint8_t activity_mode) {
pathfindr 45:6c7f5b556d74 722 //SEND ACTIVITY DATA
pathfindr 45:6c7f5b556d74 723 //check we have something to send...
pathfindr 45:6c7f5b556d74 724 if (RET_motionTotalActivityHoursSincePost > 0.0 && activity_mode > 0) {
pathfindr 45:6c7f5b556d74 725 float temperature = getTemperature();
pathfindr 45:6c7f5b556d74 726 float voltage = getBatteryV();
pathfindr 45:6c7f5b556d74 727 if (modem.on(RET_force2G)) {
pathfindr 45:6c7f5b556d74 728 //SEND DATA
pathfindr 45:6c7f5b556d74 729 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 45:6c7f5b556d74 730 char bytesToSend[160];
pathfindr 45:6c7f5b556d74 731 if (activity_mode == 1) {
pathfindr 45:6c7f5b556d74 732 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f,c:1)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours);
pathfindr 45:6c7f5b556d74 733 } else if (activity_mode == 2) {
pathfindr 45:6c7f5b556d74 734 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost);
pathfindr 45:6c7f5b556d74 735 }
pathfindr 45:6c7f5b556d74 736 char result[180];
pathfindr 45:6c7f5b556d74 737 bool getResponse = false;
pathfindr 45:6c7f5b556d74 738 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 2, GLOBAL_defaultApi));
pathfindr 45:6c7f5b556d74 739 if (result != "err") {
pathfindr 45:6c7f5b556d74 740 //RESET ACTIVITY FRAME
pathfindr 45:6c7f5b556d74 741 memset(RET_activityData,0,sizeof(RET_activityData));
pathfindr 45:6c7f5b556d74 742 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 45:6c7f5b556d74 743 }
pathfindr 45:6c7f5b556d74 744 } else {
pathfindr 45:6c7f5b556d74 745 modem.off(true);
pathfindr 45:6c7f5b556d74 746 if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3
pathfindr 24:c161db07557f 747 }
pathfindr 33:760005331b4c 748 } else {
pathfindr 43:7b232f03628f 749 modem.off(true);
pathfindr 45:6c7f5b556d74 750 if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 3
pathfindr 24:c161db07557f 751 }
pathfindr 43:7b232f03628f 752 modem.off(true);
pathfindr 24:c161db07557f 753 }
pathfindr 24:c161db07557f 754 //RESETS
pathfindr 41:07e41dbd5304 755 setEventTime_Activity();
pathfindr 24:c161db07557f 756 }
pathfindr 24:c161db07557f 757
pathfindr 7:e9a19750700d 758 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 759 // STATE ENGINE
pathfindr 7:e9a19750700d 760 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 761 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 762 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 763 case STATE_SETUP :
pathfindr 34:4493c9f6d707 764 { //need the curlies to avoid "transfer of control bypass init error warning"
pathfindr 41:07e41dbd5304 765 //check that we havent run setup too recently
pathfindr 41:07e41dbd5304 766 time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt);
pathfindr 41:07e41dbd5304 767 if (RET_SetupRunAt == 0 || setupRunAt_delta > ONEDAYINSECONDS) {
pathfindr 43:7b232f03628f 768 if (event_setup(false)) {
pathfindr 41:07e41dbd5304 769 // All good
pathfindr 41:07e41dbd5304 770 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 771 } else {
pathfindr 41:07e41dbd5304 772 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + ONEDAYINSECONDS);
pathfindr 41:07e41dbd5304 773 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 774 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 41:07e41dbd5304 775 }
pathfindr 19:22261767c87a 776 } else {
pathfindr 41:07e41dbd5304 777 time_t setupCanRunAt = (RET_RTCunixtime + (ONEDAYINSECONDS - setupRunAt_delta));
pathfindr 41:07e41dbd5304 778 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP CAN RUN AGAIN @%u \n",setupCanRunAt);debug_exe();}
pathfindr 13:29f67f256709 779 }
pathfindr 6:388d3c7efdd9 780 break;
pathfindr 34:4493c9f6d707 781 }
pathfindr 16:3bf5f1a5f869 782 case STATE_NORMAL :
pathfindr 41:07e41dbd5304 783 {
pathfindr 24:c161db07557f 784 //LOCATION EVENT
pathfindr 19:22261767c87a 785 bool run_location_tx = false;
pathfindr 27:fa76f5a08195 786 switch (RET_setting_location_mode) {
pathfindr 27:fa76f5a08195 787 case 1: //INTERVAL POST
pathfindr 27:fa76f5a08195 788 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 789 break;
pathfindr 27:fa76f5a08195 790 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 40:4270e09e59f2 791 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 45:6c7f5b556d74 792 if (RET_motionTriggeredInLocTXInterval == true) {
pathfindr 40:4270e09e59f2 793 run_location_tx = true;
pathfindr 40:4270e09e59f2 794 } else {
pathfindr 43:7b232f03628f 795 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX no mot\n");debug_exe();}
pathfindr 40:4270e09e59f2 796 }
pathfindr 40:4270e09e59f2 797 }
pathfindr 27:fa76f5a08195 798 break;
pathfindr 27:fa76f5a08195 799 case 3: //POST ON STOP MOTION
pathfindr 27:fa76f5a08195 800 if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
pathfindr 27:fa76f5a08195 801 break;
pathfindr 27:fa76f5a08195 802 }
pathfindr 22:810425eb76e1 803 if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
pathfindr 37:505ef618f06c 804 if (run_location_tx) { event_location_tx(); }
pathfindr 19:22261767c87a 805
pathfindr 27:fa76f5a08195 806
pathfindr 24:c161db07557f 807 //ACTIVITY EVENT
pathfindr 24:c161db07557f 808 bool run_activity_tx = false;
pathfindr 45:6c7f5b556d74 809 if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
pathfindr 45:6c7f5b556d74 810 if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0) {
pathfindr 45:6c7f5b556d74 811 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 812 }
pathfindr 45:6c7f5b556d74 813 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > 1) {
pathfindr 45:6c7f5b556d74 814 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 815 }
pathfindr 9:b0a1535b8ef2 816 }
pathfindr 45:6c7f5b556d74 817 //If in sendevent mode, check if buffer is close to full, if so send
pathfindr 45:6c7f5b556d74 818 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > (ACTIVITY_BUFFERSIZE-20)) { run_activity_tx = true; }
pathfindr 45:6c7f5b556d74 819 if (run_activity_tx) { event_activity_tx(RET_setting_activity_mode); }
pathfindr 9:b0a1535b8ef2 820 break;
pathfindr 34:4493c9f6d707 821 }
pathfindr 16:3bf5f1a5f869 822 case STATE_DORMANT :
pathfindr 34:4493c9f6d707 823 {
pathfindr 22:810425eb76e1 824 if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
pathfindr 22:810425eb76e1 825 if (RET_haveSettings) {
pathfindr 22:810425eb76e1 826 setState(STATE_NORMAL);
pathfindr 22:810425eb76e1 827 } else {
pathfindr 22:810425eb76e1 828 setState(STATE_SETUP);
pathfindr 22:810425eb76e1 829 }
pathfindr 7:e9a19750700d 830 }
pathfindr 41:07e41dbd5304 831 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 12:8345612bf867 832 break;
pathfindr 34:4493c9f6d707 833 }
pathfindr 16:3bf5f1a5f869 834 case STATE_BUTTONPRESS1 :
pathfindr 34:4493c9f6d707 835 {
pathfindr 19:22261767c87a 836 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 837 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
pathfindr 12:8345612bf867 838 LED1blink(1,300);
pathfindr 34:4493c9f6d707 839 break;
pathfindr 34:4493c9f6d707 840 }
pathfindr 16:3bf5f1a5f869 841 case STATE_BUTTONPRESS2 :
pathfindr 34:4493c9f6d707 842 {
pathfindr 19:22261767c87a 843 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 844 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 2\n");debug_exe();}
pathfindr 44:061509d1b38c 845 LED1blink(3,300);
pathfindr 27:fa76f5a08195 846 LED1on(0);
pathfindr 43:7b232f03628f 847 event_setup(true);
pathfindr 28:24b02608fa5f 848 LED1off();
pathfindr 28:24b02608fa5f 849 break;
pathfindr 34:4493c9f6d707 850 }
pathfindr 28:24b02608fa5f 851 case STATE_BUTTONPRESS3 :
pathfindr 34:4493c9f6d707 852 {
pathfindr 28:24b02608fa5f 853 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 854 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
pathfindr 44:061509d1b38c 855 LED1blink(5,300);
pathfindr 28:24b02608fa5f 856 LED1on(0);
pathfindr 43:7b232f03628f 857 event_location_tx();
pathfindr 27:fa76f5a08195 858 LED1off();
pathfindr 9:b0a1535b8ef2 859 break;
pathfindr 34:4493c9f6d707 860 }
pathfindr 16:3bf5f1a5f869 861 case STATE_BUTTONHOLD :
pathfindr 34:4493c9f6d707 862 {
pathfindr 41:07e41dbd5304 863 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 19:22261767c87a 864 if (RET_state_prev == STATE_NORMAL) {
pathfindr 41:07e41dbd5304 865 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
pathfindr 19:22261767c87a 866 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 867 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING OFF\n");debug_exe();}
pathfindr 41:07e41dbd5304 868 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 19:22261767c87a 869 LED1on(5000);
pathfindr 39:f767b8037475 870 event_turnonofflog_tx(false);
pathfindr 19:22261767c87a 871 } else {
pathfindr 19:22261767c87a 872 if (RET_haveSettings) {
pathfindr 19:22261767c87a 873 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 874 } else {
pathfindr 19:22261767c87a 875 setState(STATE_SETUP);
pathfindr 19:22261767c87a 876 }
pathfindr 41:07e41dbd5304 877 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
pathfindr 19:22261767c87a 878 LED1blink(20,100);
pathfindr 19:22261767c87a 879 }
pathfindr 6:388d3c7efdd9 880 break;
pathfindr 34:4493c9f6d707 881 }
pathfindr 39:f767b8037475 882 case STATE_SCORCHEDEARTH :
pathfindr 39:f767b8037475 883 {
pathfindr 43:7b232f03628f 884 LED1blink(40,50);
pathfindr 42:f482f4cdb319 885 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
pathfindr 39:f767b8037475 886 setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
pathfindr 39:f767b8037475 887 read_app_data_from_flash(&app_data);
pathfindr 39:f767b8037475 888 app_data.current_firmware_version = 0;
pathfindr 39:f767b8037475 889 app_data.target_firmware_version = 0;
pathfindr 39:f767b8037475 890 write_app_data_to_flash(&app_data);
pathfindr 39:f767b8037475 891 system_reset();
pathfindr 39:f767b8037475 892 break;
pathfindr 39:f767b8037475 893 }
pathfindr 6:388d3c7efdd9 894 default :
pathfindr 34:4493c9f6d707 895 {
pathfindr 19:22261767c87a 896 setState(STATE_SETUP);
pathfindr 34:4493c9f6d707 897 }
pathfindr 6:388d3c7efdd9 898 }
pathfindr 6:388d3c7efdd9 899 }
pathfindr 6:388d3c7efdd9 900
pathfindr 7:e9a19750700d 901 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 902 // MAIN
pathfindr 7:e9a19750700d 903 //------------------------------------------------------------------------------
pathfindr 38:476a9b5629a1 904 int main() {
pathfindr 24:c161db07557f 905 //INIT
pathfindr 38:476a9b5629a1 906 watchdog.configure(240.0); //4 mins
pathfindr 34:4493c9f6d707 907 LED1off();
pathfindr 43:7b232f03628f 908 modem.off(false);
pathfindr 24:c161db07557f 909 RTCticker.attach(&RTCtick, 1.0);
pathfindr 34:4493c9f6d707 910 button.fall(&buttonPress);
pathfindr 24:c161db07557f 911 button.rise(&buttonRelease);
pathfindr 43:7b232f03628f 912 read_app_data_from_flash(&app_data);
pathfindr 34:4493c9f6d707 913 dumpSettings();
pathfindr 4:8d8e9bfa82e4 914
pathfindr 4:8d8e9bfa82e4 915 //CHECK IF THIS IS RESET
pathfindr 43:7b232f03628f 916 //0x00000008 == lock up //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 917 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 43:7b232f03628f 918 RET_coldBoot = 1;
pathfindr 4:8d8e9bfa82e4 919 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 920 case 0x00000001 :
pathfindr 47:cc6d4d0bf897 921 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:HW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 922 break;
pathfindr 4:8d8e9bfa82e4 923 case 0x00000002 :
pathfindr 43:7b232f03628f 924 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:WD\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 925 break;
pathfindr 4:8d8e9bfa82e4 926 case 0x00000004 :
pathfindr 43:7b232f03628f 927 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:SW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 928 break;
pathfindr 43:7b232f03628f 929 case 0x00000008 :
pathfindr 43:7b232f03628f 930 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:LU\n");debug_exe();}
pathfindr 43:7b232f03628f 931 break;
pathfindr 4:8d8e9bfa82e4 932 }
pathfindr 36:8e359069192b 933 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 43:7b232f03628f 934 } else {
pathfindr 43:7b232f03628f 935 //if its not one of these reasons then its a power-on or a brown-out
pathfindr 43:7b232f03628f 936 if(get_flag(&app_data, app_execution_flag) == false) {
pathfindr 43:7b232f03628f 937 //THIS is a unexpected reset / brownout???
pathfindr 43:7b232f03628f 938 //THIS NEEDS TESTING
pathfindr 43:7b232f03628f 939 }
pathfindr 4:8d8e9bfa82e4 940 }
pathfindr 24:c161db07557f 941
pathfindr 9:b0a1535b8ef2 942 //CHECK FOR FIRST BOOT
pathfindr 34:4493c9f6d707 943 if (RET_coldBoot == 1) {
pathfindr 36:8e359069192b 944 setDefaults();
pathfindr 36:8e359069192b 945 //check battery
pathfindr 36:8e359069192b 946 float voltage = getBatteryV();
pathfindr 40:4270e09e59f2 947 if (voltage > 2.5f) {
pathfindr 36:8e359069192b 948 //battery ok
pathfindr 36:8e359069192b 949 LED1errorCode(6,2);
pathfindr 36:8e359069192b 950 } else {
pathfindr 36:8e359069192b 951 //battery low
pathfindr 36:8e359069192b 952 LED1errorCode(10,2);
pathfindr 36:8e359069192b 953 }
pathfindr 43:7b232f03628f 954 addToExceptionString("FR");
pathfindr 36:8e359069192b 955 mainthread.wait(2000);
pathfindr 24:c161db07557f 956 }
pathfindr 13:29f67f256709 957
pathfindr 34:4493c9f6d707 958 //MAIN LOOP
pathfindr 34:4493c9f6d707 959 while(true) {
pathfindr 34:4493c9f6d707 960 //WATCHDOG
pathfindr 38:476a9b5629a1 961 watchdogKick();
pathfindr 39:f767b8037475 962 RET_asleep = false;
pathfindr 34:4493c9f6d707 963
pathfindr 34:4493c9f6d707 964 //INIT
pathfindr 38:476a9b5629a1 965 resetGlobals();
pathfindr 39:f767b8037475 966 healthCheck(); //this must be after resetGlobals
pathfindr 13:29f67f256709 967 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 968
pathfindr 40:4270e09e59f2 969 //check and log motion
pathfindr 40:4270e09e59f2 970 if (RET_state == STATE_NORMAL) { checkMotion(); }
pathfindr 40:4270e09e59f2 971
pathfindr 9:b0a1535b8ef2 972 //MAIN LOGIC
pathfindr 38:476a9b5629a1 973 if(DEBUG_ON){
pathfindr 39:f767b8037475 974 if (RET_state != 99) {
pathfindr 45:6c7f5b556d74 975 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, SET:%d, MF:%d, MS:%d, MTX:%d, %u, L:%u, LFS:%u, A:%u", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, RET_motionState, RET_motionTriggeredInLocTXInterval, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
pathfindr 45:6c7f5b556d74 976 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT M:%d, HsP:%.2f, Ht:%.2f D:%s\n",RET_setting_activity_mode,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours,RET_activityData);debug_exe();
pathfindr 39:f767b8037475 977 } else {
pathfindr 43:7b232f03628f 978 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();
pathfindr 39:f767b8037475 979 }
pathfindr 38:476a9b5629a1 980 }
pathfindr 43:7b232f03628f 981
pathfindr 43:7b232f03628f 982 /*
pathfindr 43:7b232f03628f 983 if (modem.on(RET_force2G)) {
pathfindr 43:7b232f03628f 984 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 43:7b232f03628f 985 modem.USSDsend("(b:gps2,a:act,e:1.7!0.15!,t:1547937626,r:0.13,c:1)\0",2);
pathfindr 43:7b232f03628f 986 }
pathfindr 43:7b232f03628f 987 }
pathfindr 43:7b232f03628f 988 */
pathfindr 37:505ef618f06c 989
pathfindr 42:f482f4cdb319 990 //STATE ENGINE
pathfindr 9:b0a1535b8ef2 991 mainStateEngine();
pathfindr 34:4493c9f6d707 992
pathfindr 34:4493c9f6d707 993 //LOG FIRST RUN - BOOTLOADER COMMS
pathfindr 34:4493c9f6d707 994 if (RET_coldBoot) {
pathfindr 45:6c7f5b556d74 995 recordFirmwareAsValid();
pathfindr 34:4493c9f6d707 996 RET_coldBoot = 0;
pathfindr 34:4493c9f6d707 997 }
pathfindr 9:b0a1535b8ef2 998
pathfindr 9:b0a1535b8ef2 999 //PRE-SLEEP ACTIONS
pathfindr 45:6c7f5b556d74 1000 LED1off();
pathfindr 45:6c7f5b556d74 1001 modem.off(false);
pathfindr 40:4270e09e59f2 1002 RET_motionTriggeredinFrame = false;
pathfindr 34:4493c9f6d707 1003 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
pathfindr 45:6c7f5b556d74 1004 nrf_configureGPIOForSleep();
pathfindr 34:4493c9f6d707 1005 NRFuart_uninit();
pathfindr 34:4493c9f6d707 1006 watchdogKick();
pathfindr 22:810425eb76e1 1007
pathfindr 34:4493c9f6d707 1008 //SLEEP
pathfindr 39:f767b8037475 1009 RET_asleep = true;
pathfindr 26:fa3579737329 1010 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 34:4493c9f6d707 1011 }
pathfindr 7:e9a19750700d 1012 }