init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Wed Jan 16 23:06:39 2019 +0000
Revision:
38:476a9b5629a1
Parent:
37:505ef618f06c
Child:
39:f767b8037475
updates

Who changed what in which revision?

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