init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Jan 17 15:20:14 2019 +0000
Revision:
39:f767b8037475
Parent:
38:476a9b5629a1
Child:
40:4270e09e59f2
tweaks

Who changed what in which revision?

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