init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Feb 07 00:34:30 2019 +0000
Revision:
49:15ddd84ec9fa
Parent:
48:64b1613941d5
Child:
50:a94e70c00fff
update to led logic, and moved location check back in the setup sequence

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