init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Fri Jan 18 00:07:45 2019 +0000
Revision:
42:f482f4cdb319
Parent:
41:07e41dbd5304
Child:
43:7b232f03628f
state engine fix

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