init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Tue Feb 05 14:30:57 2019 +0000
Revision:
48:64b1613941d5
Parent:
47:cc6d4d0bf897
Child:
49:15ddd84ec9fa
init

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