init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Jan 17 23:40:03 2019 +0000
Revision:
41:07e41dbd5304
Parent:
40:4270e09e59f2
Child:
42:f482f4cdb319
updates

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