init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sun Jan 20 00:15:03 2019 +0000
Revision:
43:7b232f03628f
Parent:
42:f482f4cdb319
Child:
44:061509d1b38c
performance tweaks

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