init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu May 23 11:39:28 2019 +0000
Revision:
51:9078e6928412
Parent:
50:a94e70c00fff
Child:
52:bd7678eade77
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pathfindr 0:94b743e06998 1 #include "main.h"
pathfindr 51:9078e6928412 2 // MUST USE MBED 5.10.4 2fd0c5cfbd
pathfindr 51:9078e6928412 3
pathfindr 51:9078e6928412 4 /*
pathfindr 51:9078e6928412 5 PRE DEPLOYMENT CHECKS!!
pathfindr 51:9078e6928412 6 • Ensure APP DATA address is correct
pathfindr 51:9078e6928412 7 */
pathfindr 51:9078e6928412 8
pathfindr 51:9078e6928412 9
pathfindr 51:9078e6928412 10 /*
pathfindr 51:9078e6928412 11 TO DO
pathfindr 51:9078e6928412 12 STOP FRASH WRITE FROM CRASHING IF THERE IS A PROBLEM
pathfindr 51:9078e6928412 13
pathfindr 51:9078e6928412 14 */
pathfindr 51:9078e6928412 15
pathfindr 21:e0b866630c27 16
pathfindr 21:e0b866630c27 17 //------------------------------------------------------------------------------
pathfindr 37:505ef618f06c 18 //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
pathfindr 0:94b743e06998 19 //------------------------------------------------------------------------------
pathfindr 45:6c7f5b556d74 20 //should really add these - will just add as and when needed for now
pathfindr 48:64b1613941d5 21 void event_activity_tx();
pathfindr 51:9078e6928412 22 void event_location_tx(bool failsafe);
pathfindr 0:94b743e06998 23
pathfindr 0:94b743e06998 24 //------------------------------------------------------------------------------
pathfindr 36:8e359069192b 25 // GLOBALS
pathfindr 51:9078e6928412 26 //------------------------------------------------------------------------------
pathfindr 48:64b1613941d5 27 bool GLOBAL_debugLED = false;
pathfindr 36:8e359069192b 28 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 36:8e359069192b 29 bool GLOBAL_accel_healthy = false;
pathfindr 36:8e359069192b 30 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 36:8e359069192b 31 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 36:8e359069192b 32 bool GLOBAL_registeredOnNetwork = false;
pathfindr 36:8e359069192b 33 bool GLOBAL_modemOn = false;
pathfindr 36:8e359069192b 34 bool GLOBAL_LEDSequenceinProgress = false;
pathfindr 36:8e359069192b 35 time_t GLOBAL_wakeTime = 0;
pathfindr 44:061509d1b38c 36 char GLOBAL_exceptionString[30];
pathfindr 36:8e359069192b 37 char GLOBAL_debug_buffer[200];
pathfindr 51:9078e6928412 38 char GLOBAL_failed_broadcasts[10][160];
pathfindr 51:9078e6928412 39 bool GLOBAL_failed_broadcast_slots[10];
pathfindr 34:4493c9f6d707 40
pathfindr 48:64b1613941d5 41 //SETTINGS
pathfindr 36:8e359069192b 42 int RET_setting_firmware;
pathfindr 36:8e359069192b 43 uint32_t RET_setting_minimumupdate_hrs;
pathfindr 36:8e359069192b 44 uint8_t RET_setting_location_mode;
pathfindr 36:8e359069192b 45 uint8_t RET_setting_location_accuracy;
pathfindr 36:8e359069192b 46 uint32_t RET_setting_location_tx_interval_mins;
pathfindr 36:8e359069192b 47 uint32_t RET_setting_location_tx_failsafe_hrs;
pathfindr 36:8e359069192b 48 uint16_t RET_setting_location_timeout;
pathfindr 36:8e359069192b 49 uint32_t RET_setting_activity_tx_interval_hrs;
pathfindr 45:6c7f5b556d74 50 uint8_t RET_setting_activity_mode;
pathfindr 36:8e359069192b 51 uint32_t RET_setting_environmental_tx_interval_mins;
pathfindr 36:8e359069192b 52 uint16_t RET_setting_motion_g;
pathfindr 36:8e359069192b 53 time_t RET_setting_motion_start_seconds;
pathfindr 36:8e359069192b 54 time_t RET_setting_motion_stop_seconds;
pathfindr 36:8e359069192b 55 uint16_t RET_setting_impact_g;
pathfindr 36:8e359069192b 56 uint8_t RET_setting_impact_alert;
pathfindr 36:8e359069192b 57 uint16_t RET_setting_connection_timeout;
pathfindr 36:8e359069192b 58 uint16_t RET_setting_beacon_interval_seconds;
pathfindr 36:8e359069192b 59 uint16_t RET_setting_beacon_scan;
pathfindr 13:29f67f256709 60 //STATE
pathfindr 51:9078e6928412 61 char RET_pf_identifier[7];
pathfindr 40:4270e09e59f2 62 uint8_t RET_coldBoot = 1;
pathfindr 39:f767b8037475 63 bool RET_asleep = false;
pathfindr 48:64b1613941d5 64 bool RET_busy = false;
pathfindr 36:8e359069192b 65 time_t RET_RTCunixtime;
pathfindr 36:8e359069192b 66 bool RET_haveSettings;
pathfindr 36:8e359069192b 67 uint8_t RET_state;
pathfindr 36:8e359069192b 68 uint8_t RET_state_prev;
pathfindr 36:8e359069192b 69 uint8_t RET_buttonPressCount;
pathfindr 36:8e359069192b 70 time_t RET_buttonPressTime;
pathfindr 49:15ddd84ec9fa 71 time_t RET_buttonReleaseTime;
pathfindr 51:9078e6928412 72 time_t RET_buttonReleaseTime_prev;
pathfindr 36:8e359069192b 73 time_t RET_buttonHoldTime;
pathfindr 36:8e359069192b 74 time_t RET_SetupRunAt;
pathfindr 36:8e359069192b 75 time_t RET_SettingsGotAt;
pathfindr 41:07e41dbd5304 76 bool RET_setupInProgress;
pathfindr 38:476a9b5629a1 77 bool RET_force2G;
pathfindr 39:f767b8037475 78 bool RET_watchdogfired;
pathfindr 45:6c7f5b556d74 79 bool RET_receivedNewSettings;
pathfindr 48:64b1613941d5 80 uint32_t RET_GPSFailCount;
pathfindr 48:64b1613941d5 81 uint32_t RET_NetworkFailCount;
pathfindr 51:9078e6928412 82 bool RET_debug = true;
pathfindr 51:9078e6928412 83 time_t RET_debug_offat;
pathfindr 51:9078e6928412 84 float RET_voltage;
pathfindr 51:9078e6928412 85 float RET_temperature;
pathfindr 51:9078e6928412 86 float RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 51:9078e6928412 87 float RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 16:3bf5f1a5f869 88 //MOTION STATE
pathfindr 40:4270e09e59f2 89 bool RET_motionTriggeredinFrame;
pathfindr 45:6c7f5b556d74 90 bool RET_motionTriggeredInLocTXInterval;
pathfindr 36:8e359069192b 91 time_t RET_motionStartTime;
pathfindr 36:8e359069192b 92 time_t RET_motionStopTime;
pathfindr 36:8e359069192b 93 bool RET_motionPendingOnState;
pathfindr 36:8e359069192b 94 bool RET_motionPendingOffState;
pathfindr 36:8e359069192b 95 bool RET_motionState;
pathfindr 36:8e359069192b 96 float RET_motionTotalActivityHours;
pathfindr 45:6c7f5b556d74 97 float RET_motionTotalActivityHoursSincePost;
pathfindr 36:8e359069192b 98 time_t RET_motionFrameStart;
pathfindr 45:6c7f5b556d74 99 char RET_activityData[ACTIVITY_BUFFERSIZE];
pathfindr 16:3bf5f1a5f869 100 //IMPACT
pathfindr 36:8e359069192b 101 bool RET_impactTriggered;
pathfindr 19:22261767c87a 102 //EVENTS LOGGING
pathfindr 36:8e359069192b 103 time_t RET_eventTime_location_log;
pathfindr 36:8e359069192b 104 time_t RET_eventTime_environmental_log;
pathfindr 19:22261767c87a 105 //EVENTS TX
pathfindr 36:8e359069192b 106 time_t RET_eventTime_location_tx;
pathfindr 36:8e359069192b 107 time_t RET_eventTime_location_failsafe_tx;
pathfindr 36:8e359069192b 108 time_t RET_eventTime_environmental_tx;
pathfindr 36:8e359069192b 109 time_t RET_eventTime_activity_tx;
pathfindr 36:8e359069192b 110 time_t RET_eventTime_wakeFromDormant;
pathfindr 4:8d8e9bfa82e4 111
pathfindr 0:94b743e06998 112 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 113 //GPIO
pathfindr 9:b0a1535b8ef2 114 //------------------------------------------------------------------------------
pathfindr 34:4493c9f6d707 115 InterruptIn button(PN_IN_BUTTON);
pathfindr 9:b0a1535b8ef2 116
pathfindr 9:b0a1535b8ef2 117 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 118 //PERIPHERALS
pathfindr 0:94b743e06998 119 //------------------------------------------------------------------------------
pathfindr 37:505ef618f06c 120 WatchdogTimer watchdog; //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 51:9078e6928412 121 void watchdogKick() { watchdog.kick();}
pathfindr 51:9078e6928412 122 //SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 34:4493c9f6d707 123 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 34:4493c9f6d707 124 Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 37:505ef618f06c 125 LowPowerTicker RTCticker;
pathfindr 51:9078e6928412 126 LowPowerTimer LPtimer;
pathfindr 9:b0a1535b8ef2 127
pathfindr 9:b0a1535b8ef2 128 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 129 //THREAD SEMAPHORES
pathfindr 9:b0a1535b8ef2 130 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 131 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 132
pathfindr 9:b0a1535b8ef2 133 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 134 // LOW LEVEL FUNCS
pathfindr 34:4493c9f6d707 135 //------------------------------------------------------------------------------
pathfindr 51:9078e6928412 136 void set_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 137 flags |= flag;
pathfindr 51:9078e6928412 138 }
pathfindr 51:9078e6928412 139 bool get_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 140 bool result = (flags & flag);
pathfindr 51:9078e6928412 141 return result;
pathfindr 51:9078e6928412 142 }
pathfindr 51:9078e6928412 143 void clr_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 144 flags &= ~flag;
pathfindr 51:9078e6928412 145 }
pathfindr 50:a94e70c00fff 146 void nrf_configureForSleep(){
pathfindr 50:a94e70c00fff 147 //Disable SPI pins to reduce power
pathfindr 50:a94e70c00fff 148 //nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL); //Don't need this one
pathfindr 34:4493c9f6d707 149 nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 150
pathfindr 50:a94e70c00fff 151 //disable modem control lines
pathfindr 50:a94e70c00fff 152 nrf_gpio_cfg_input(PN_GSM_PWR_KEY, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 153 nrf_gpio_cfg_input(PN_GSM_WAKE_DISABLE, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 154
pathfindr 51:9078e6928412 155 /*
pathfindr 50:a94e70c00fff 156 //ERRATA 89, shut down i2c channels properly
pathfindr 50:a94e70c00fff 157 //TWI0
pathfindr 50:a94e70c00fff 158 //NRF_TWI0->ENABLE=0;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 159 //NRF_TWI0->ENABLE=1;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 160 *(volatile uint32_t *)0x40003FFC = 0;
pathfindr 50:a94e70c00fff 161 *(volatile uint32_t *)0x40003FFC;
pathfindr 50:a94e70c00fff 162 *(volatile uint32_t *)0x40003FFC = 1;
pathfindr 50:a94e70c00fff 163 //TWI1
pathfindr 50:a94e70c00fff 164 //NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 165 *(volatile uint32_t *)0x40004FFC = 0;
pathfindr 50:a94e70c00fff 166 *(volatile uint32_t *)0x40004FFC;
pathfindr 50:a94e70c00fff 167 *(volatile uint32_t *)0x40004FFC = 1;
pathfindr 51:9078e6928412 168 */
pathfindr 6:388d3c7efdd9 169 }
pathfindr 19:22261767c87a 170 void setState(uint8_t state) {
pathfindr 19:22261767c87a 171 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 172 RET_state = state;
pathfindr 22:810425eb76e1 173 }
pathfindr 51:9078e6928412 174 void dumpSettings() {
pathfindr 51:9078e6928412 175 if(RET_debug){
pathfindr 34:4493c9f6d707 176 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_RTCunixtime:%u", RET_RTCunixtime);debug_exe();
pathfindr 34:4493c9f6d707 177 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_firmware:%d", RET_setting_firmware);debug_exe();
pathfindr 34:4493c9f6d707 178 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_minimumupdate_hrs:%d", RET_setting_minimumupdate_hrs);debug_exe();
pathfindr 34:4493c9f6d707 179 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_state:%d", RET_state);debug_exe();
pathfindr 34:4493c9f6d707 180 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_mode:%d", RET_setting_location_mode);debug_exe();
pathfindr 34:4493c9f6d707 181 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_accuracy:%d", RET_setting_location_accuracy);debug_exe();
pathfindr 34:4493c9f6d707 182 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 183 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 184 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_timeout:%d", RET_setting_location_timeout);debug_exe();
pathfindr 34:4493c9f6d707 185 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 45:6c7f5b556d74 186 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_mode:%d", RET_setting_activity_mode);debug_exe();
pathfindr 34:4493c9f6d707 187 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 188 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_g:%d", RET_setting_motion_g);debug_exe();
pathfindr 34:4493c9f6d707 189 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 190 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 191 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_g:%d", RET_setting_impact_g);debug_exe();
pathfindr 34:4493c9f6d707 192 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_alert:%d", RET_setting_impact_alert);debug_exe();
pathfindr 34:4493c9f6d707 193 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_connection_timeout:%d", RET_setting_connection_timeout);debug_exe();
pathfindr 34:4493c9f6d707 194 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 195 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe();
pathfindr 51:9078e6928412 196 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_pf_identifier:%s", RET_pf_identifier);debug_exe();
pathfindr 34:4493c9f6d707 197 }
pathfindr 24:c161db07557f 198 }
pathfindr 45:6c7f5b556d74 199 void recordFirmwareAsValid() {
pathfindr 51:9078e6928412 200 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RECORD FW %d AS VALID...", FW_VERSION);debug_exe();
pathfindr 45:6c7f5b556d74 201 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 202 bool write_app_data_to_flash_execute = false;
pathfindr 45:6c7f5b556d74 203 if(get_flag(&app_data, app_execution_flag) == true) {
pathfindr 45:6c7f5b556d74 204 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 205 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 206 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 207 }
pathfindr 45:6c7f5b556d74 208 if(app_data.current_firmware_version != FW_VERSION) {
pathfindr 45:6c7f5b556d74 209 app_data.current_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 210 app_data.target_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 211 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 212 }
pathfindr 45:6c7f5b556d74 213 if (write_app_data_to_flash_execute) {
pathfindr 45:6c7f5b556d74 214 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 215 //read back to check
pathfindr 51:9078e6928412 216 ThisThread::sleep_for(200); //need this delay
pathfindr 51:9078e6928412 217 read_app_data_from_flash(&app_data);
pathfindr 51:9078e6928412 218 if (app_data.current_firmware_version == FW_VERSION) {
pathfindr 51:9078e6928412 219 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OK");debug_exe();
pathfindr 51:9078e6928412 220 } else {
pathfindr 51:9078e6928412 221 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FAIL - IS BLE SHUTDOWN?");debug_exe();
pathfindr 51:9078e6928412 222 }
pathfindr 51:9078e6928412 223 }
pathfindr 45:6c7f5b556d74 224 }
pathfindr 51:9078e6928412 225 void updateBatteryV() {
pathfindr 19:22261767c87a 226 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 227 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 228 batteryIn.calibrate();
pathfindr 49:15ddd84ec9fa 229 //get initial reading, first reading is always low
pathfindr 49:15ddd84ec9fa 230 batteryIn.updateData(); batteryIn.getData()[0];
pathfindr 49:15ddd84ec9fa 231 float voltage_accumulator = 0.0;
pathfindr 49:15ddd84ec9fa 232 uint8_t readings = 0;
pathfindr 49:15ddd84ec9fa 233 for (uint8_t i = 1; i <= 50; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
pathfindr 19:22261767c87a 234 batteryIn.updateData();
pathfindr 49:15ddd84ec9fa 235 voltage_accumulator += (batteryIn.getData()[0])*(1.0/1024.0)*3.60;
pathfindr 49:15ddd84ec9fa 236 readings ++;
pathfindr 19:22261767c87a 237 }
pathfindr 51:9078e6928412 238 RET_voltage = (voltage_accumulator / (float)readings);
pathfindr 22:810425eb76e1 239 }
pathfindr 36:8e359069192b 240 float nrfTemperature() {
pathfindr 51:9078e6928412 241 float temperature = 0.0;
pathfindr 51:9078e6928412 242
pathfindr 36:8e359069192b 243 //INTERNAL NRF52 TEMP SENSOR
pathfindr 36:8e359069192b 244 uint32_t safetycounter = 0;
pathfindr 36:8e359069192b 245 NRF_TEMP->TASKS_START=1;
pathfindr 36:8e359069192b 246 while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000) {
pathfindr 36:8e359069192b 247 safetycounter ++;
pathfindr 36:8e359069192b 248 };
pathfindr 36:8e359069192b 249 NRF_TEMP->EVENTS_DATARDY=0;
pathfindr 36:8e359069192b 250 temperature = nrf_temp_read()/4.0;
pathfindr 36:8e359069192b 251 NRF_TEMP->TASKS_STOP=1;
pathfindr 51:9078e6928412 252
pathfindr 51:9078e6928412 253 //SOFTDEVICE METHOD - To use this method you need to include #include "nrf_soc.h"
pathfindr 51:9078e6928412 254 //THIS GAVE SOME INTERMITENT ODD RESULTS AND DOESNT SEEM AS RELIABLE
pathfindr 51:9078e6928412 255 /*
pathfindr 51:9078e6928412 256 int32_t t;
pathfindr 51:9078e6928412 257 sd_temp_get(&t);
pathfindr 51:9078e6928412 258 temperature = t/4.0;
pathfindr 51:9078e6928412 259 */
pathfindr 51:9078e6928412 260
pathfindr 36:8e359069192b 261 return temperature;
pathfindr 36:8e359069192b 262 }
pathfindr 22:810425eb76e1 263 float getTemperature() {
pathfindr 34:4493c9f6d707 264 float temperature;
pathfindr 34:4493c9f6d707 265 if (USE_NRF_TEMP_SENSOR) {
pathfindr 36:8e359069192b 266 temperature = nrfTemperature();
pathfindr 34:4493c9f6d707 267 } else {
pathfindr 51:9078e6928412 268 //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 269 }
pathfindr 22:810425eb76e1 270 return temperature;
pathfindr 19:22261767c87a 271 }
pathfindr 51:9078e6928412 272 void updateTemperatures() {
pathfindr 51:9078e6928412 273 //RET_temperature = getTemperature(); //not smoothed
pathfindr 51:9078e6928412 274 if (RET_temperature == 0.0f) {RET_temperature = getTemperature();}
pathfindr 51:9078e6928412 275 RET_temperature = (RET_temperature + (getTemperature() - RET_temperature) * 0.25f); //smoothed
pathfindr 51:9078e6928412 276
pathfindr 51:9078e6928412 277 if (RET_temperature < RET_temperature_min) {
pathfindr 51:9078e6928412 278 RET_temperature_min = RET_temperature;
pathfindr 51:9078e6928412 279 }
pathfindr 51:9078e6928412 280 if (RET_temperature > RET_temperature_max) {
pathfindr 51:9078e6928412 281 RET_temperature_max = RET_temperature;
pathfindr 51:9078e6928412 282 }
pathfindr 51:9078e6928412 283 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Temp:%.1f Min:%.1f Max:%.1f",RET_temperature,RET_temperature_min,RET_temperature_max);debug_exe();}
pathfindr 51:9078e6928412 284 }
pathfindr 27:fa76f5a08195 285 void addToExceptionString(char* value) {
pathfindr 45:6c7f5b556d74 286 if(strstr(GLOBAL_exceptionString, value) == false){
pathfindr 45:6c7f5b556d74 287 snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),"%s.",value);
pathfindr 45:6c7f5b556d74 288 }
pathfindr 27:fa76f5a08195 289 }
pathfindr 51:9078e6928412 290 void resetTemperatures() {
pathfindr 51:9078e6928412 291 RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 51:9078e6928412 292 RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 51:9078e6928412 293 }
pathfindr 19:22261767c87a 294 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 295 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 296 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 297 void buttonPress() {
pathfindr 51:9078e6928412 298 RET_buttonPressTime = LPtimer.read_ms();
pathfindr 19:22261767c87a 299 }
pathfindr 19:22261767c87a 300 void buttonRelease() {
pathfindr 51:9078e6928412 301 //debounce catch
pathfindr 51:9078e6928412 302 RET_buttonReleaseTime = LPtimer.read_ms();
pathfindr 51:9078e6928412 303 if ((RET_buttonReleaseTime - RET_buttonReleaseTime_prev) > 75) {
pathfindr 51:9078e6928412 304 RET_buttonPressCount ++;
pathfindr 51:9078e6928412 305 RET_buttonHoldTime = (RET_buttonReleaseTime - RET_buttonPressTime);
pathfindr 51:9078e6928412 306 //temporarily enable debugging and dump settings
pathfindr 51:9078e6928412 307 RET_debug = true;
pathfindr 51:9078e6928412 308 RET_debug_offat = (RET_RTCunixtime + 600); //debug on for 10 mins
pathfindr 51:9078e6928412 309 }
pathfindr 51:9078e6928412 310 RET_buttonReleaseTime_prev = RET_buttonReleaseTime;
pathfindr 19:22261767c87a 311 }
pathfindr 19:22261767c87a 312 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 313 // RTC TICKER
pathfindr 51:9078e6928412 314 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 315 void RTCtick() {
pathfindr 51:9078e6928412 316 //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars & comparisons etc
pathfindr 19:22261767c87a 317 RET_RTCunixtime += 1;
pathfindr 51:9078e6928412 318
pathfindr 51:9078e6928412 319 //button logic
pathfindr 51:9078e6928412 320 if (RET_buttonHoldTime >= 4000 && RET_buttonHoldTime <= 10000) {
pathfindr 49:15ddd84ec9fa 321 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 322 RET_buttonPressCount = 0;
pathfindr 51:9078e6928412 323 if (RET_state != STATE_DORMANT) {
pathfindr 51:9078e6928412 324 RET_state_prev = RET_state;
pathfindr 51:9078e6928412 325 RET_state = STATE_TURNOFF;
pathfindr 51:9078e6928412 326 } else {
pathfindr 51:9078e6928412 327 RET_state = STATE_TURNON;
pathfindr 51:9078e6928412 328 }
pathfindr 49:15ddd84ec9fa 329 mainthread.release();
pathfindr 51:9078e6928412 330 } else if (RET_buttonHoldTime >= 14000 && RET_buttonHoldTime <= 30000) {
pathfindr 49:15ddd84ec9fa 331 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 332 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 333 RET_state = STATE_SCORCHEDEARTH;
pathfindr 49:15ddd84ec9fa 334 mainthread.release();
pathfindr 49:15ddd84ec9fa 335 } else {
pathfindr 51:9078e6928412 336 if((LPtimer.read_ms() - RET_buttonReleaseTime) > 1500 && RET_buttonPressCount > 0) {
pathfindr 49:15ddd84ec9fa 337 if(RET_busy == true) {
pathfindr 49:15ddd84ec9fa 338 //RTCtick_ledflash_count = 4;// 2 flashes
pathfindr 49:15ddd84ec9fa 339 } else {
pathfindr 49:15ddd84ec9fa 340 RET_SetupRunAt = 0; //allow setup to run again
pathfindr 49:15ddd84ec9fa 341 switch (RET_buttonPressCount) { //double catches to help with debounce
pathfindr 49:15ddd84ec9fa 342 case 1 :
pathfindr 50:a94e70c00fff 343 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 344 RET_state = STATE_BUTTONPRESS1;
pathfindr 49:15ddd84ec9fa 345 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 346 mainthread.release();
pathfindr 49:15ddd84ec9fa 347 break;
pathfindr 49:15ddd84ec9fa 348 case 3 :
pathfindr 50:a94e70c00fff 349 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 350 RET_state = STATE_BUTTONPRESS3;
pathfindr 49:15ddd84ec9fa 351 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 352 mainthread.release();
pathfindr 49:15ddd84ec9fa 353 break;
pathfindr 49:15ddd84ec9fa 354 case 5 :
pathfindr 50:a94e70c00fff 355 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 356 RET_state = STATE_BUTTONPRESS5;
pathfindr 49:15ddd84ec9fa 357 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 358 mainthread.release();
pathfindr 49:15ddd84ec9fa 359 break;
pathfindr 49:15ddd84ec9fa 360 default :
pathfindr 49:15ddd84ec9fa 361 //do nothing
pathfindr 49:15ddd84ec9fa 362 break;
pathfindr 39:f767b8037475 363 }
pathfindr 9:b0a1535b8ef2 364 }
pathfindr 51:9078e6928412 365 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 366 }
pathfindr 49:15ddd84ec9fa 367 }
pathfindr 9:b0a1535b8ef2 368 }
pathfindr 51:9078e6928412 369
pathfindr 34:4493c9f6d707 370 void resetGlobals() {
pathfindr 34:4493c9f6d707 371 GLOBAL_accel_healthy = false;
pathfindr 34:4493c9f6d707 372 GLOBAL_motionStopFlagTriggered = false;
pathfindr 34:4493c9f6d707 373 memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
pathfindr 34:4493c9f6d707 374 }
pathfindr 38:476a9b5629a1 375 void healthCheck() {
pathfindr 39:f767b8037475 376 //check for watchdog fire
pathfindr 39:f767b8037475 377 if (RET_watchdogfired == true) {
pathfindr 39:f767b8037475 378 addToExceptionString("WD");
pathfindr 39:f767b8037475 379 RET_watchdogfired = false;
pathfindr 39:f767b8037475 380 }
pathfindr 41:07e41dbd5304 381 //check clock
pathfindr 41:07e41dbd5304 382 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 383 if (RET_RTCunixtime < 1547678732) {
pathfindr 41:07e41dbd5304 384 //go dormant for 72hrs and then resetup
pathfindr 41:07e41dbd5304 385 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 386 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 387 }
pathfindr 41:07e41dbd5304 388 }
pathfindr 41:07e41dbd5304 389 //check that we have a reasonable time to loc
pathfindr 41:07e41dbd5304 390 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 391 //check location tx delta is set
pathfindr 41:07e41dbd5304 392 if (RET_eventTime_location_tx == 0) {
pathfindr 51:9078e6928412 393 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
pathfindr 41:07e41dbd5304 394 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 395 }
pathfindr 41:07e41dbd5304 396 //check location failsafe tx delta is less that 2 weeks
pathfindr 41:07e41dbd5304 397 long location_failsafe_tx_delta = (RET_eventTime_location_failsafe_tx - RET_RTCunixtime);
pathfindr 41:07e41dbd5304 398 if (location_failsafe_tx_delta > TENDAYSINSECONDS) {
pathfindr 51:9078e6928412 399 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:location_failsafe_tx_delta too small");debug_exe();}
pathfindr 41:07e41dbd5304 400 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 401 }
pathfindr 41:07e41dbd5304 402 }
pathfindr 41:07e41dbd5304 403 if (RET_haveSettings == false) {
pathfindr 41:07e41dbd5304 404 setState(STATE_SETUP);
pathfindr 41:07e41dbd5304 405 }
pathfindr 38:476a9b5629a1 406 }
pathfindr 36:8e359069192b 407 void setDefaults() {
pathfindr 35:d9421d57d116 408 //STATE
pathfindr 39:f767b8037475 409 RET_asleep = false;
pathfindr 48:64b1613941d5 410 RET_busy = false;
pathfindr 41:07e41dbd5304 411 RET_haveSettings = false;
pathfindr 35:d9421d57d116 412 RET_state = STATE_SETUP;
pathfindr 35:d9421d57d116 413 RET_state_prev = RET_state;
pathfindr 35:d9421d57d116 414 RET_RTCunixtime = 0;
pathfindr 35:d9421d57d116 415 RET_SetupRunAt = 0;
pathfindr 35:d9421d57d116 416 RET_SettingsGotAt = 0;
pathfindr 38:476a9b5629a1 417 RET_force2G = DEFAULT_FORCE2G;
pathfindr 39:f767b8037475 418 RET_watchdogfired = false;
pathfindr 41:07e41dbd5304 419 RET_setupInProgress = false;
pathfindr 45:6c7f5b556d74 420 RET_receivedNewSettings = false;
pathfindr 48:64b1613941d5 421 RET_GPSFailCount = 0;
pathfindr 48:64b1613941d5 422 RET_NetworkFailCount = 0;
pathfindr 35:d9421d57d116 423 //SETTINGS
pathfindr 35:d9421d57d116 424 RET_setting_firmware = 0;
pathfindr 35:d9421d57d116 425 RET_setting_minimumupdate_hrs = 0;
pathfindr 35:d9421d57d116 426 RET_setting_location_mode = DEFAULT_LOCATION_MODE;
pathfindr 35:d9421d57d116 427 RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
pathfindr 35:d9421d57d116 428 RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
pathfindr 35:d9421d57d116 429 RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
pathfindr 35:d9421d57d116 430 RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
pathfindr 35:d9421d57d116 431 RET_setting_activity_tx_interval_hrs = 0;
pathfindr 45:6c7f5b556d74 432 RET_setting_activity_mode = 1;
pathfindr 35:d9421d57d116 433 RET_setting_environmental_tx_interval_mins = 0;
pathfindr 35:d9421d57d116 434 RET_setting_motion_g = DEFAULT_MOTION_G;
pathfindr 35:d9421d57d116 435 RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
pathfindr 35:d9421d57d116 436 RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
pathfindr 35:d9421d57d116 437 RET_setting_impact_g = 0;
pathfindr 35:d9421d57d116 438 RET_setting_impact_alert = 0;
pathfindr 35:d9421d57d116 439 RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
pathfindr 35:d9421d57d116 440 RET_setting_beacon_scan = 0;
pathfindr 35:d9421d57d116 441 RET_buttonPressCount = 0;
pathfindr 35:d9421d57d116 442 RET_buttonPressTime = 0;
pathfindr 49:15ddd84ec9fa 443 RET_buttonReleaseTime = 0;
pathfindr 35:d9421d57d116 444 RET_buttonHoldTime = 0;
pathfindr 35:d9421d57d116 445 //MOTION STATE
pathfindr 45:6c7f5b556d74 446 RET_motionTriggeredinFrame = false;
pathfindr 45:6c7f5b556d74 447 RET_motionTriggeredInLocTXInterval = false;
pathfindr 35:d9421d57d116 448 RET_motionStartTime = 0;
pathfindr 35:d9421d57d116 449 RET_motionStopTime = 0;
pathfindr 35:d9421d57d116 450 RET_motionPendingOnState = 0;
pathfindr 35:d9421d57d116 451 RET_motionPendingOffState = 0;
pathfindr 35:d9421d57d116 452 RET_motionState = 0;
pathfindr 35:d9421d57d116 453 RET_motionTotalActivityHours = 0.0;
pathfindr 45:6c7f5b556d74 454 RET_motionTotalActivityHoursSincePost = 0.0;
pathfindr 35:d9421d57d116 455 RET_motionFrameStart = 0;
pathfindr 35:d9421d57d116 456 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 35:d9421d57d116 457 //IMPACT
pathfindr 35:d9421d57d116 458 RET_impactTriggered = 0;
pathfindr 35:d9421d57d116 459 //EVENT HANDLING
pathfindr 35:d9421d57d116 460 RET_eventTime_location_log = 0;
pathfindr 35:d9421d57d116 461 RET_eventTime_location_tx = 0;
pathfindr 35:d9421d57d116 462 RET_eventTime_location_failsafe_tx = 0;
pathfindr 35:d9421d57d116 463 RET_eventTime_environmental_log = 0;
pathfindr 35:d9421d57d116 464 RET_eventTime_environmental_tx = 0;
pathfindr 35:d9421d57d116 465 RET_eventTime_activity_tx = 0;
pathfindr 35:d9421d57d116 466 RET_eventTime_wakeFromDormant = 0;
pathfindr 35:d9421d57d116 467 //PERIPHERAL RESET
pathfindr 35:d9421d57d116 468 lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
pathfindr 35:d9421d57d116 469 }
pathfindr 24:c161db07557f 470
pathfindr 22:810425eb76e1 471 bool selfTest() {
pathfindr 22:810425eb76e1 472 int test_count = 0;
pathfindr 22:810425eb76e1 473 int test_pass = 0;
pathfindr 22:810425eb76e1 474
pathfindr 8:7351f8c4cd60 475 //Accelerometer
pathfindr 22:810425eb76e1 476 test_count ++;
pathfindr 8:7351f8c4cd60 477 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 478 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 27:fa76f5a08195 479 if (lis3dh_id == 51) {
pathfindr 43:7b232f03628f 480 GLOBAL_accel_healthy = true;
pathfindr 27:fa76f5a08195 481 test_pass ++;
pathfindr 27:fa76f5a08195 482 } else {
pathfindr 43:7b232f03628f 483 GLOBAL_accel_healthy = false;
pathfindr 43:7b232f03628f 484 addToExceptionString("A");
pathfindr 27:fa76f5a08195 485 }
pathfindr 22:810425eb76e1 486
pathfindr 22:810425eb76e1 487 //Result
pathfindr 22:810425eb76e1 488 if (test_count == test_pass) {
pathfindr 22:810425eb76e1 489 return true;
pathfindr 22:810425eb76e1 490 } else {
pathfindr 48:64b1613941d5 491 addToExceptionString("HF");
pathfindr 22:810425eb76e1 492 return false;
pathfindr 9:b0a1535b8ef2 493 }
pathfindr 9:b0a1535b8ef2 494 }
pathfindr 9:b0a1535b8ef2 495
pathfindr 9:b0a1535b8ef2 496 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 497 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 498 //------------------------------------------------------------------------------
pathfindr 48:64b1613941d5 499 bool checkMotion() {
pathfindr 51:9078e6928412 500 if (RET_haveSettings) { //NEED THIS AS THINGS GO FUNKY WITHOUT RTC TIME SET CORRECTLY
pathfindr 51:9078e6928412 501 if (lis3dh_int2) {
pathfindr 51:9078e6928412 502 RET_motionTriggeredinFrame = true;
pathfindr 51:9078e6928412 503 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 51:9078e6928412 504 if (!RET_motionPendingOnState) {
pathfindr 51:9078e6928412 505 RET_motionPendingOnState = true;
pathfindr 51:9078e6928412 506 RET_motionPendingOffState = false;
pathfindr 51:9078e6928412 507 // Log start motion time
pathfindr 51:9078e6928412 508 RET_motionStartTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 509 }
pathfindr 51:9078e6928412 510 } else {
pathfindr 51:9078e6928412 511 RET_motionTriggeredinFrame = false;
pathfindr 51:9078e6928412 512 RET_motionPendingOnState = false;
pathfindr 51:9078e6928412 513 if (!RET_motionPendingOffState) {
pathfindr 51:9078e6928412 514 RET_motionPendingOffState = true;
pathfindr 51:9078e6928412 515 //log stop motion time
pathfindr 51:9078e6928412 516 RET_motionStopTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 517 }
pathfindr 9:b0a1535b8ef2 518 }
pathfindr 51:9078e6928412 519 //calculate motion state
pathfindr 51:9078e6928412 520 if (RET_motionFrameStart == 0) {RET_motionFrameStart = RET_RTCunixtime;}
pathfindr 51:9078e6928412 521 if (RET_motionPendingOnState) {
pathfindr 51:9078e6928412 522 //check if above threshold
pathfindr 51:9078e6928412 523 time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 51:9078e6928412 524 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOT:%u s",inMotionForSeconds);debug_exe();}
pathfindr 51:9078e6928412 525 if (inMotionForSeconds >= (RET_setting_motion_start_seconds + 15) && RET_motionState == false) {
pathfindr 51:9078e6928412 526 RET_motionState = true;
pathfindr 51:9078e6928412 527 RET_motionTriggeredInLocTXInterval = true;
pathfindr 51:9078e6928412 528 if (RET_setting_activity_mode == 2 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 45:6c7f5b556d74 529 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 45:6c7f5b556d74 530 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
pathfindr 45:6c7f5b556d74 531 }
pathfindr 23:a3b0ccf75ca5 532 }
pathfindr 9:b0a1535b8ef2 533 }
pathfindr 51:9078e6928412 534 if (RET_motionPendingOffState) {
pathfindr 51:9078e6928412 535 time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 51:9078e6928412 536 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOTLESS:%u s",noMotionForSeconds);debug_exe();}
pathfindr 51:9078e6928412 537 if (noMotionForSeconds >= (RET_setting_motion_stop_seconds + 15) && RET_motionState == true) {
pathfindr 51:9078e6928412 538 RET_motionPendingOffState = false;
pathfindr 51:9078e6928412 539 RET_motionState = false;
pathfindr 51:9078e6928412 540 GLOBAL_motionStopFlagTriggered = true;
pathfindr 51:9078e6928412 541 if (RET_setting_activity_mode > 0 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 51:9078e6928412 542 RET_motionTotalActivityHoursSincePost += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f);
pathfindr 51:9078e6928412 543 RET_motionTotalActivityHours += RET_motionTotalActivityHoursSincePost;
pathfindr 51:9078e6928412 544 if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 545 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 51:9078e6928412 546 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
pathfindr 51:9078e6928412 547 }
pathfindr 45:6c7f5b556d74 548 }
pathfindr 23:a3b0ccf75ca5 549 }
pathfindr 9:b0a1535b8ef2 550 }
pathfindr 9:b0a1535b8ef2 551 }
pathfindr 48:64b1613941d5 552 return RET_motionState;
pathfindr 8:7351f8c4cd60 553 }
pathfindr 8:7351f8c4cd60 554
pathfindr 14:9a54b1b65bc8 555 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 556 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 557 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 558 bool saveSettings(char* settingsBuffer) {
pathfindr 18:22edaa7e74b1 559 int matchCount = 0;
pathfindr 19:22261767c87a 560 int critical_fail_count = 0;
pathfindr 32:dff4858bdf37 561 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 51:9078e6928412 562 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; int TEMP_t = -1; char TEMP_u[7];
pathfindr 50:a94e70c00fff 563
pathfindr 51:9078e6928412 564 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RESP: %s",settingsBuffer);debug_exe();}
pathfindr 50:a94e70c00fff 565
pathfindr 51:9078e6928412 566 //exampple
pathfindr 51:9078e6928412 567 //+CUSD: 0,”1#a:12,b:1558020598,c:-1,d:-1,e:1,f:2,g:5,h:168,i:90,j:168,k:0,l:8,m:60,n:60,o:0,p:0,q:120,r:0,s:0,t:1,u:F859C1#”,15
pathfindr 51:9078e6928412 568
pathfindr 51:9078e6928412 569 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,t:%d,u:%s",
pathfindr 51:9078e6928412 570 &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,&TEMP_t,&TEMP_u) ) > 0 ) {
pathfindr 48:64b1613941d5 571
pathfindr 51:9078e6928412 572 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "PARAMS: 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,t:%d,u:%s\n",
pathfindr 51:9078e6928412 573 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,TEMP_t,TEMP_u);debug_exe();}
pathfindr 34:4493c9f6d707 574
pathfindr 41:07e41dbd5304 575 if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; }
pathfindr 50:a94e70c00fff 576 if(TEMP_b > 1552915630) {
pathfindr 50:a94e70c00fff 577 if (RET_RTCunixtime < 1552915630) {
pathfindr 50:a94e70c00fff 578 //assume this is first proper unixtime and set
pathfindr 50:a94e70c00fff 579 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 580 } else {
pathfindr 51:9078e6928412 581 //this is not a first unixtime, so check within safe range, convert to long long first to avoid -1 rollover issue
pathfindr 51:9078e6928412 582 long long unixtime_in = TEMP_b;
pathfindr 51:9078e6928412 583 long long unixtime_current = RET_RTCunixtime;
pathfindr 51:9078e6928412 584 if ((unixtime_in - unixtime_current) < 604800) { //allow 1 week difference
pathfindr 50:a94e70c00fff 585 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 586 } else {
pathfindr 50:a94e70c00fff 587 critical_fail_count++;
pathfindr 51:9078e6928412 588 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "unixtime too far ahead. C:%u N:%u",RET_RTCunixtime,TEMP_b);debug_exe();}
pathfindr 50:a94e70c00fff 589 }
pathfindr 50:a94e70c00fff 590 }
pathfindr 50:a94e70c00fff 591 } else {
pathfindr 50:a94e70c00fff 592 critical_fail_count++;
pathfindr 51:9078e6928412 593 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "unixtime too low");debug_exe();}
pathfindr 50:a94e70c00fff 594 }
pathfindr 41:07e41dbd5304 595 if(TEMP_c != -1) { RET_setting_firmware = TEMP_c; } else { RET_setting_firmware = -1;}
pathfindr 41:07e41dbd5304 596 if(TEMP_d != -1) { setState(TEMP_d); } else { setState(STATE_NORMAL);}
pathfindr 41:07e41dbd5304 597 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; }
pathfindr 34:4493c9f6d707 598 if(RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
pathfindr 41:07e41dbd5304 599 if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f; }
pathfindr 34:4493c9f6d707 600 if(RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
pathfindr 41:07e41dbd5304 601 if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g; }
pathfindr 41:07e41dbd5304 602 if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h; }
pathfindr 34:4493c9f6d707 603 if(RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
pathfindr 41:07e41dbd5304 604 if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i; }
pathfindr 27:fa76f5a08195 605 if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
pathfindr 41:07e41dbd5304 606 if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; }
pathfindr 41:07e41dbd5304 607 if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; }
pathfindr 41:07e41dbd5304 608 if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; }
pathfindr 43:7b232f03628f 609 if(RET_setting_motion_g < 4 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
pathfindr 41:07e41dbd5304 610 if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; }
pathfindr 41:07e41dbd5304 611 if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; }
pathfindr 41:07e41dbd5304 612 if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; }
pathfindr 41:07e41dbd5304 613 if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p; }
pathfindr 41:07e41dbd5304 614 if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q; }
pathfindr 47:cc6d4d0bf897 615 if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
pathfindr 41:07e41dbd5304 616 if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r; }
pathfindr 41:07e41dbd5304 617 if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s; }
pathfindr 45:6c7f5b556d74 618 if(TEMP_t != -1) { RET_setting_activity_mode = TEMP_t; }
pathfindr 51:9078e6928412 619 if(TEMP_u[0] != 0x00 && TEMP_u[1] != 0x00 && TEMP_u[2] != 0x00) { strcpy(RET_pf_identifier,TEMP_u); }
pathfindr 45:6c7f5b556d74 620
pathfindr 19:22261767c87a 621 if (critical_fail_count == 0) {
pathfindr 45:6c7f5b556d74 622 RET_receivedNewSettings = true;
pathfindr 51:9078e6928412 623 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "NEW SETTINGS");debug_exe();}
pathfindr 40:4270e09e59f2 624 dumpSettings();
pathfindr 32:dff4858bdf37 625 RET_haveSettings = true;
pathfindr 34:4493c9f6d707 626 GLOBAL_needToConfigureLis3dh = true;
pathfindr 32:dff4858bdf37 627 RET_SettingsGotAt = RET_RTCunixtime;
pathfindr 37:505ef618f06c 628 //check for firmware update
pathfindr 38:476a9b5629a1 629 if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) {
pathfindr 48:64b1613941d5 630 //Broadcast any activity data we have before fw update
pathfindr 48:64b1613941d5 631 event_activity_tx();
pathfindr 48:64b1613941d5 632 //prep for FW update
pathfindr 37:505ef618f06c 633 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 634 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 635 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 636 app_data.current_firmware_version = FW_VERSION;
pathfindr 37:505ef618f06c 637 app_data.target_firmware_version = RET_setting_firmware;
pathfindr 37:505ef618f06c 638 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 639 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OTA\n");debug_exe();}
pathfindr 48:64b1613941d5 640 ThisThread::sleep_for(200);
pathfindr 37:505ef618f06c 641 system_reset();
pathfindr 37:505ef618f06c 642 }
pathfindr 19:22261767c87a 643 return true;
pathfindr 19:22261767c87a 644 } else {
pathfindr 44:061509d1b38c 645 addToExceptionString("SCF");
pathfindr 19:22261767c87a 646 return false;
pathfindr 19:22261767c87a 647 }
pathfindr 14:9a54b1b65bc8 648 } else {
pathfindr 44:061509d1b38c 649 addToExceptionString("SPE");
pathfindr 15:7aad9a7f970c 650 return false;
pathfindr 14:9a54b1b65bc8 651 }
pathfindr 14:9a54b1b65bc8 652 }
pathfindr 24:c161db07557f 653
pathfindr 41:07e41dbd5304 654 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 655 // SET EVENT TIMES
pathfindr 41:07e41dbd5304 656 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 657 void setEventTime_Location() {
pathfindr 20:5404841fdd2b 658 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 659 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 51:9078e6928412 660 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC TX @ %u, MODE %d",RET_eventTime_location_tx, RET_setting_location_mode);debug_exe();}
pathfindr 20:5404841fdd2b 661 }
pathfindr 20:5404841fdd2b 662 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 663 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 51:9078e6928412 664 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC FS TX @ %u",RET_eventTime_location_failsafe_tx);debug_exe();}
pathfindr 20:5404841fdd2b 665 }
pathfindr 41:07e41dbd5304 666 }
pathfindr 41:07e41dbd5304 667 void setEventTime_Activity() {
pathfindr 22:810425eb76e1 668 if(RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 22:810425eb76e1 669 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600));
pathfindr 51:9078e6928412 670 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ACT TX @ %u",RET_eventTime_activity_tx);debug_exe();}
pathfindr 20:5404841fdd2b 671 }
pathfindr 41:07e41dbd5304 672 }
pathfindr 41:07e41dbd5304 673 void setEventTime_Environmental() {
pathfindr 20:5404841fdd2b 674 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 675 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 51:9078e6928412 676 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ENV TX @ %u",RET_eventTime_environmental_tx);debug_exe();}
pathfindr 34:4493c9f6d707 677 }
pathfindr 20:5404841fdd2b 678 }
pathfindr 41:07e41dbd5304 679 void setEventTimes() {
pathfindr 43:7b232f03628f 680 //SET ALL EVENT TIMES
pathfindr 41:07e41dbd5304 681 setEventTime_Location();
pathfindr 41:07e41dbd5304 682 setEventTime_Activity();
pathfindr 41:07e41dbd5304 683 setEventTime_Environmental();
pathfindr 41:07e41dbd5304 684 }
pathfindr 13:29f67f256709 685
pathfindr 13:29f67f256709 686 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 687 // ERROR LOG
pathfindr 40:4270e09e59f2 688 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 689 void sendErrorLog(char* error) {
pathfindr 51:9078e6928412 690 updateBatteryV();
pathfindr 40:4270e09e59f2 691 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 40:4270e09e59f2 692 char bytestosend[100];
pathfindr 40:4270e09e59f2 693 memset(bytestosend,0x00,sizeof(bytestosend));
pathfindr 51:9078e6928412 694 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s.%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,timetaken,error,GLOBAL_exceptionString);
pathfindr 43:7b232f03628f 695 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 696 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 697 modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi);
pathfindr 43:7b232f03628f 698 }
pathfindr 43:7b232f03628f 699 }
pathfindr 40:4270e09e59f2 700 ThisThread::sleep_for(250);
pathfindr 40:4270e09e59f2 701 }
pathfindr 40:4270e09e59f2 702
pathfindr 40:4270e09e59f2 703 //------------------------------------------------------------------------------
pathfindr 43:7b232f03628f 704 // EVENTS
pathfindr 13:29f67f256709 705 //------------------------------------------------------------------------------
pathfindr 51:9078e6928412 706 void failed_broadcast_log(char* eventstring) {
pathfindr 51:9078e6928412 707 //First available free slot
pathfindr 51:9078e6928412 708 bool saved = false;
pathfindr 51:9078e6928412 709 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 710 if (GLOBAL_failed_broadcast_slots[i] == false) {
pathfindr 51:9078e6928412 711 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i])); //clear row
pathfindr 51:9078e6928412 712 snprintf(GLOBAL_failed_broadcasts[i],sizeof(GLOBAL_failed_broadcasts[i]),"%s",eventstring); //save new row
pathfindr 51:9078e6928412 713 GLOBAL_failed_broadcast_slots[i] = true;
pathfindr 51:9078e6928412 714 saved = true;
pathfindr 51:9078e6928412 715 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast saved: %s",eventstring);debug_exe();}
pathfindr 51:9078e6928412 716 break;
pathfindr 51:9078e6928412 717 }
pathfindr 51:9078e6928412 718 }
pathfindr 51:9078e6928412 719 if (!saved) {
pathfindr 51:9078e6928412 720 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast save fail");debug_exe();}
pathfindr 51:9078e6928412 721 }
pathfindr 51:9078e6928412 722 return;
pathfindr 51:9078e6928412 723 }
pathfindr 51:9078e6928412 724 void failed_broadcasts_tx() {
pathfindr 51:9078e6928412 725 //check we have something to send...
pathfindr 51:9078e6928412 726 int numbertosend = 0;
pathfindr 51:9078e6928412 727 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 728 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 729 numbertosend ++;
pathfindr 51:9078e6928412 730 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Retry:%s",GLOBAL_failed_broadcasts[i]);debug_exe();}
pathfindr 51:9078e6928412 731 }
pathfindr 51:9078e6928412 732 }
pathfindr 51:9078e6928412 733 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcasts to send:%d",numbertosend);debug_exe();}
pathfindr 51:9078e6928412 734 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 735 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 736 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 737 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 738 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 739 char result[100];
pathfindr 51:9078e6928412 740 bool getResponse = false;
pathfindr 51:9078e6928412 741 snprintf(result,sizeof(result),"%s",modem.USSDmessage(GLOBAL_failed_broadcasts[i], getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 742 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 743 //clear row
pathfindr 51:9078e6928412 744 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i]));
pathfindr 51:9078e6928412 745 GLOBAL_failed_broadcast_slots[i] = false;
pathfindr 51:9078e6928412 746 }
pathfindr 51:9078e6928412 747 }
pathfindr 51:9078e6928412 748 }
pathfindr 51:9078e6928412 749 }
pathfindr 51:9078e6928412 750 }
pathfindr 51:9078e6928412 751 }
pathfindr 51:9078e6928412 752 }
pathfindr 43:7b232f03628f 753 bool event_setup(bool manualrun) {
pathfindr 51:9078e6928412 754 bool pass = false;
pathfindr 49:15ddd84ec9fa 755 LED1on(0);
pathfindr 48:64b1613941d5 756 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 48:64b1613941d5 757 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 41:07e41dbd5304 758 RET_setupInProgress = true;
pathfindr 48:64b1613941d5 759 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 760 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 761 RET_setting_connection_timeout = 180; //reset to longer value to setup run to help with connection
pathfindr 25:7adeb1a53360 762 bool selftestresult = selfTest();
pathfindr 45:6c7f5b556d74 763 int connectionStartTime = RET_RTCunixtime;
pathfindr 38:476a9b5629a1 764 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 765 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 766 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
pathfindr 45:6c7f5b556d74 767 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 43:7b232f03628f 768 if (manualrun) addToExceptionString("MAN");
pathfindr 17:ba55c026b1d6 769 char bytestosend[160];
pathfindr 51:9078e6928412 770 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,RET_temperature,timetaken_total,timetaken_connection,GLOBAL_exceptionString,SKU,HW_MAJORREVISION);
pathfindr 34:4493c9f6d707 771 char result[200];
pathfindr 51:9078e6928412 772 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, GLOBAL_defaultApi));
pathfindr 49:15ddd84ec9fa 773 RET_setupInProgress = false; //this turns off the flashing led
pathfindr 51:9078e6928412 774 if (strcmp(result, "sendokrecfail") == 0) {
pathfindr 51:9078e6928412 775 //send ok but receive fail
pathfindr 51:9078e6928412 776 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 51:9078e6928412 777 sendErrorLog("ERR-SU-NR");
pathfindr 51:9078e6928412 778 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 51:9078e6928412 779 //do nothing
pathfindr 51:9078e6928412 780 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 51:9078e6928412 781 } else {
pathfindr 51:9078e6928412 782 if (saveSettings(result) == true) {
pathfindr 51:9078e6928412 783 pass = true;
pathfindr 51:9078e6928412 784 } else {
pathfindr 19:22261767c87a 785 //something went critically wrong getting settings
pathfindr 48:64b1613941d5 786 sendErrorLog("ERR-SU-IR");
pathfindr 51:9078e6928412 787 }
pathfindr 16:3bf5f1a5f869 788 }
pathfindr 13:29f67f256709 789 }
pathfindr 13:29f67f256709 790 } else {
pathfindr 19:22261767c87a 791 //FAILUREMODE Modem failed to turn on
pathfindr 51:9078e6928412 792 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Modem Fail");debug_exe();}
pathfindr 13:29f67f256709 793 }
pathfindr 49:15ddd84ec9fa 794
pathfindr 49:15ddd84ec9fa 795 //IF WE PASSED THEN LETS DO OTHER POSTS
pathfindr 49:15ddd84ec9fa 796 if (pass == true) {
pathfindr 49:15ddd84ec9fa 797 GLOBAL_wakeTime = RET_RTCunixtime; //lets reset this here so that the following loc and act function have sensible values for total time
pathfindr 49:15ddd84ec9fa 798 //GET LOC DATA
pathfindr 49:15ddd84ec9fa 799 uint8_t previous_location_accuracy = RET_setting_location_accuracy;
pathfindr 49:15ddd84ec9fa 800 RET_setting_location_accuracy = 1; //set location mode to CL only
pathfindr 51:9078e6928412 801 event_location_tx(false);
pathfindr 49:15ddd84ec9fa 802 RET_setting_location_accuracy = previous_location_accuracy; //set location mode back to previous
pathfindr 49:15ddd84ec9fa 803 //SEND ANY ACTIVITY DATA
pathfindr 49:15ddd84ec9fa 804 event_activity_tx();
pathfindr 49:15ddd84ec9fa 805 LED1blink(4,500); //PASS AND END
pathfindr 49:15ddd84ec9fa 806 }
pathfindr 49:15ddd84ec9fa 807
pathfindr 49:15ddd84ec9fa 808 //TIDY UP = LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 809 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 49:15ddd84ec9fa 810 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 811 RET_NetworkFailCount = 0;
pathfindr 13:29f67f256709 812 //RESULT
pathfindr 19:22261767c87a 813 return pass;
pathfindr 13:29f67f256709 814 }
pathfindr 39:f767b8037475 815 void event_turnonofflog_tx(bool turnon) {
pathfindr 48:64b1613941d5 816 RET_NetworkFailCount = 0; //reset network blocker
pathfindr 48:64b1613941d5 817 RET_GPSFailCount = 0;
pathfindr 51:9078e6928412 818 updateBatteryV();
pathfindr 45:6c7f5b556d74 819 int connectionStartTime = RET_RTCunixtime;
pathfindr 39:f767b8037475 820 if (modem.on(RET_force2G)) {
pathfindr 39:f767b8037475 821 char locString[70];
pathfindr 45:6c7f5b556d74 822 int gpsStartTime = RET_RTCunixtime;
pathfindr 48:64b1613941d5 823 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 45:6c7f5b556d74 824 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 48:64b1613941d5 825 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 826 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 45:6c7f5b556d74 827 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 39:f767b8037475 828 char bytestosend[160];
pathfindr 39:f767b8037475 829 if (turnon) {
pathfindr 51:9078e6928412 830 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNON,e:%d,y:%d,x:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 831 } else {
pathfindr 51:9078e6928412 832 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,y:%d,x:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 833 }
pathfindr 39:f767b8037475 834 char result[180];
pathfindr 51:9078e6928412 835 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi));
pathfindr 39:f767b8037475 836 }
pathfindr 39:f767b8037475 837 }
pathfindr 39:f767b8037475 838 }
pathfindr 51:9078e6928412 839 void event_location_tx(bool failsafe) {
pathfindr 51:9078e6928412 840 //check if we have any outstanding to send
pathfindr 51:9078e6928412 841 failed_broadcasts_tx();
pathfindr 49:15ddd84ec9fa 842 bool selfTestResult = selfTest();
pathfindr 51:9078e6928412 843 char bytesToSend[160];
pathfindr 51:9078e6928412 844 //see if we are in range of ble detector, if so, no need to get gps and broadcast
pathfindr 48:64b1613941d5 845 //Set any network or GPS fail flags
pathfindr 48:64b1613941d5 846 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 48:64b1613941d5 847 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 48:64b1613941d5 848 //lets check to see if we've had repeated comms failings in this location, if so dont bother trying. reset by movment and timeout
pathfindr 48:64b1613941d5 849 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 48:64b1613941d5 850 if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); }
pathfindr 48:64b1613941d5 851 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 852 char locString[70];
pathfindr 48:64b1613941d5 853 int gpsStartTime = RET_RTCunixtime;
pathfindr 48:64b1613941d5 854 memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 48:64b1613941d5 855 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 51:9078e6928412 856
pathfindr 48:64b1613941d5 857 //SEND DATA
pathfindr 48:64b1613941d5 858 int connectionStartTime = RET_RTCunixtime;
pathfindr 48:64b1613941d5 859 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 48:64b1613941d5 860 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time.
pathfindr 48:64b1613941d5 861 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 48:64b1613941d5 862 //Check if we should wait for settings back
pathfindr 48:64b1613941d5 863 bool getSettings = true;
pathfindr 48:64b1613941d5 864 //work out if we need to get settings back
pathfindr 48:64b1613941d5 865 if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times
pathfindr 51:9078e6928412 866 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,y:%d,x:%d,j:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionTriggeredInLocTXInterval,getSettings,locString);
pathfindr 48:64b1613941d5 867 char result[180];
pathfindr 51:9078e6928412 868 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 869 if (getSettings == true && strcmp(result, "sendokrecfail") == 0) {
pathfindr 51:9078e6928412 870 //send ok but receive fail
pathfindr 51:9078e6928412 871 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 51:9078e6928412 872 sendErrorLog("ERR-LOC-NR");
pathfindr 51:9078e6928412 873 } else if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 874 //do nothing
pathfindr 51:9078e6928412 875 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 51:9078e6928412 876 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 51:9078e6928412 877 //need to log for retry
pathfindr 51:9078e6928412 878 } else {
pathfindr 48:64b1613941d5 879 if (getSettings) {
pathfindr 48:64b1613941d5 880 if (saveSettings(result) == false){sendErrorLog("ERR-LOC-IR");}
pathfindr 48:64b1613941d5 881 }
pathfindr 22:810425eb76e1 882 }
pathfindr 22:810425eb76e1 883 }
pathfindr 22:810425eb76e1 884 }
pathfindr 20:5404841fdd2b 885 }
pathfindr 20:5404841fdd2b 886 //RESETS
pathfindr 45:6c7f5b556d74 887 RET_motionTriggeredInLocTXInterval = false;
pathfindr 41:07e41dbd5304 888 setEventTime_Location();
pathfindr 19:22261767c87a 889 }
pathfindr 51:9078e6928412 890 void event_activity_tx() {
pathfindr 51:9078e6928412 891 //check if we have any outstanding to send
pathfindr 51:9078e6928412 892 failed_broadcasts_tx();
pathfindr 51:9078e6928412 893 //check we have something to send...
pathfindr 51:9078e6928412 894 bool sendok = false;
pathfindr 51:9078e6928412 895 if (RET_motionTotalActivityHoursSincePost > 0.0f && RET_setting_activity_mode > 0) {
pathfindr 51:9078e6928412 896 //Build data to send
pathfindr 51:9078e6928412 897 char bytesToSend[160];
pathfindr 51:9078e6928412 898 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 899 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f,c:1)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours);
pathfindr 51:9078e6928412 900 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 901 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost);
pathfindr 51:9078e6928412 902 }
pathfindr 51:9078e6928412 903 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 904 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 905 //SEND DATA
pathfindr 51:9078e6928412 906 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 907 char result[100];
pathfindr 51:9078e6928412 908 bool getResponse = false;
pathfindr 51:9078e6928412 909 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 910 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 911 sendok = true;
pathfindr 51:9078e6928412 912 //if(!RET_debug) {sendok = true;} //FOR TESTING
pathfindr 51:9078e6928412 913 }
pathfindr 51:9078e6928412 914 }
pathfindr 51:9078e6928412 915 }
pathfindr 51:9078e6928412 916 }
pathfindr 51:9078e6928412 917 //If send failed then log it for retry - only need to do this for mode 2, for mode the time just increments
pathfindr 51:9078e6928412 918 if (sendok == true) {
pathfindr 51:9078e6928412 919 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 920 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 921 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 922 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 923 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 924 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 925 }
pathfindr 51:9078e6928412 926 } else if (sendok == false) {
pathfindr 51:9078e6928412 927 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 928 //do nothing
pathfindr 51:9078e6928412 929 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 930 failed_broadcast_log(bytesToSend);
pathfindr 51:9078e6928412 931 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 932 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 933 }
pathfindr 51:9078e6928412 934 }
pathfindr 51:9078e6928412 935 }
pathfindr 51:9078e6928412 936 //RESETS
pathfindr 51:9078e6928412 937 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 51:9078e6928412 938 setEventTime_Activity();
pathfindr 48:64b1613941d5 939 }
pathfindr 51:9078e6928412 940 /*
pathfindr 48:64b1613941d5 941 void event_activity_tx() {
pathfindr 51:9078e6928412 942 bool sendok = false;
pathfindr 51:9078e6928412 943 //check if we have any outstanding to send
pathfindr 51:9078e6928412 944 failed_broadcasts_tx();
pathfindr 48:64b1613941d5 945
pathfindr 45:6c7f5b556d74 946 //check we have something to send...
pathfindr 48:64b1613941d5 947 if (RET_motionTotalActivityHoursSincePost > 0.0f && RET_setting_activity_mode > 0) {
pathfindr 48:64b1613941d5 948 //Build data to send
pathfindr 48:64b1613941d5 949 char bytesToSend[160];
pathfindr 48:64b1613941d5 950 if (RET_setting_activity_mode == 1) {
pathfindr 48:64b1613941d5 951 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f,c:1)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours);
pathfindr 48:64b1613941d5 952 } else if (RET_setting_activity_mode == 2) {
pathfindr 48:64b1613941d5 953 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost);
pathfindr 48:64b1613941d5 954 }
pathfindr 48:64b1613941d5 955 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 48:64b1613941d5 956 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 957 //SEND DATA
pathfindr 48:64b1613941d5 958 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 959 char result[100];
pathfindr 48:64b1613941d5 960 bool getResponse = false;
pathfindr 51:9078e6928412 961 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 962 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 963 //sendok = true;
pathfindr 51:9078e6928412 964 if(!RET_debug) {sendok = true;}
pathfindr 48:64b1613941d5 965 }
pathfindr 45:6c7f5b556d74 966 }
pathfindr 24:c161db07557f 967 }
pathfindr 48:64b1613941d5 968 modem.off(true);
pathfindr 51:9078e6928412 969 }
pathfindr 51:9078e6928412 970 //If send failed then log it for retry
pathfindr 51:9078e6928412 971 if (sendok == false) {
pathfindr 51:9078e6928412 972 failed_broadcast_log(bytesToSend);
pathfindr 24:c161db07557f 973 }
pathfindr 24:c161db07557f 974 }
pathfindr 24:c161db07557f 975 //RESETS
pathfindr 51:9078e6928412 976 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 977 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 51:9078e6928412 978 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; } //this is now safe to do as we are logging the fails
pathfindr 41:07e41dbd5304 979 setEventTime_Activity();
pathfindr 24:c161db07557f 980 }
pathfindr 51:9078e6928412 981 */
pathfindr 24:c161db07557f 982
pathfindr 7:e9a19750700d 983 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 984 // STATE ENGINE
pathfindr 7:e9a19750700d 985 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 986 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 987 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 988 case STATE_SETUP :
pathfindr 34:4493c9f6d707 989 { //need the curlies to avoid "transfer of control bypass init error warning"
pathfindr 48:64b1613941d5 990 RET_busy = true;
pathfindr 41:07e41dbd5304 991 //check that we havent run setup too recently
pathfindr 41:07e41dbd5304 992 time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt);
pathfindr 49:15ddd84ec9fa 993 if (RET_SetupRunAt == 0 || setupRunAt_delta >= ONEDAYINSECONDS) {
pathfindr 51:9078e6928412 994 updateBatteryV();
pathfindr 43:7b232f03628f 995 if (event_setup(false)) {
pathfindr 41:07e41dbd5304 996 // All good
pathfindr 41:07e41dbd5304 997 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 998 } else {
pathfindr 41:07e41dbd5304 999 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + ONEDAYINSECONDS);
pathfindr 41:07e41dbd5304 1000 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1001 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 41:07e41dbd5304 1002 }
pathfindr 19:22261767c87a 1003 } else {
pathfindr 41:07e41dbd5304 1004 time_t setupCanRunAt = (RET_RTCunixtime + (ONEDAYINSECONDS - setupRunAt_delta));
pathfindr 51:9078e6928412 1005 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP CAN RUN AGAIN @%u \n",setupCanRunAt);debug_exe();}
pathfindr 13:29f67f256709 1006 }
pathfindr 48:64b1613941d5 1007 RET_busy = false;
pathfindr 6:388d3c7efdd9 1008 break;
pathfindr 34:4493c9f6d707 1009 }
pathfindr 16:3bf5f1a5f869 1010 case STATE_NORMAL :
pathfindr 48:64b1613941d5 1011 {
pathfindr 48:64b1613941d5 1012 RET_busy = true;
pathfindr 24:c161db07557f 1013 //LOCATION EVENT
pathfindr 19:22261767c87a 1014 bool run_location_tx = false;
pathfindr 27:fa76f5a08195 1015 switch (RET_setting_location_mode) {
pathfindr 27:fa76f5a08195 1016 case 1: //INTERVAL POST
pathfindr 27:fa76f5a08195 1017 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 1018 break;
pathfindr 27:fa76f5a08195 1019 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 40:4270e09e59f2 1020 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 45:6c7f5b556d74 1021 if (RET_motionTriggeredInLocTXInterval == true) {
pathfindr 40:4270e09e59f2 1022 run_location_tx = true;
pathfindr 40:4270e09e59f2 1023 } else {
pathfindr 51:9078e6928412 1024 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX no mot\n");debug_exe();}
pathfindr 40:4270e09e59f2 1025 }
pathfindr 40:4270e09e59f2 1026 }
pathfindr 27:fa76f5a08195 1027 break;
pathfindr 27:fa76f5a08195 1028 case 3: //POST ON STOP MOTION
pathfindr 27:fa76f5a08195 1029 if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
pathfindr 27:fa76f5a08195 1030 break;
pathfindr 27:fa76f5a08195 1031 }
pathfindr 48:64b1613941d5 1032 if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) {
pathfindr 48:64b1613941d5 1033 RET_NetworkFailCount = 0; //reset to ensure connection
pathfindr 48:64b1613941d5 1034 RET_GPSFailCount = 0; // reset to ensure gps try
pathfindr 51:9078e6928412 1035 updateBatteryV();
pathfindr 51:9078e6928412 1036 event_location_tx(true);
pathfindr 51:9078e6928412 1037 } else {
pathfindr 51:9078e6928412 1038 if (run_location_tx) { updateBatteryV(); event_location_tx(false); }
pathfindr 48:64b1613941d5 1039 }
pathfindr 19:22261767c87a 1040
pathfindr 27:fa76f5a08195 1041
pathfindr 24:c161db07557f 1042 //ACTIVITY EVENT
pathfindr 24:c161db07557f 1043 bool run_activity_tx = false;
pathfindr 45:6c7f5b556d74 1044 if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
pathfindr 48:64b1613941d5 1045 if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0f) {
pathfindr 45:6c7f5b556d74 1046 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1047 }
pathfindr 45:6c7f5b556d74 1048 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > 1) {
pathfindr 45:6c7f5b556d74 1049 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1050 }
pathfindr 9:b0a1535b8ef2 1051 }
pathfindr 45:6c7f5b556d74 1052 //If in sendevent mode, check if buffer is close to full, if so send
pathfindr 45:6c7f5b556d74 1053 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > (ACTIVITY_BUFFERSIZE-20)) { run_activity_tx = true; }
pathfindr 48:64b1613941d5 1054 if (run_activity_tx) { event_activity_tx(); }
pathfindr 48:64b1613941d5 1055
pathfindr 48:64b1613941d5 1056 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1057 break;
pathfindr 34:4493c9f6d707 1058 }
pathfindr 16:3bf5f1a5f869 1059 case STATE_DORMANT :
pathfindr 34:4493c9f6d707 1060 {
pathfindr 50:a94e70c00fff 1061 RET_busy = true;
pathfindr 22:810425eb76e1 1062 if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
pathfindr 22:810425eb76e1 1063 if (RET_haveSettings) {
pathfindr 22:810425eb76e1 1064 setState(STATE_NORMAL);
pathfindr 22:810425eb76e1 1065 } else {
pathfindr 22:810425eb76e1 1066 setState(STATE_SETUP);
pathfindr 22:810425eb76e1 1067 }
pathfindr 7:e9a19750700d 1068 }
pathfindr 51:9078e6928412 1069 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u Prev S:%d\n",RET_eventTime_wakeFromDormant,RET_state_prev);debug_exe();}
pathfindr 50:a94e70c00fff 1070 RET_busy = false;
pathfindr 12:8345612bf867 1071 break;
pathfindr 34:4493c9f6d707 1072 }
pathfindr 16:3bf5f1a5f869 1073 case STATE_BUTTONPRESS1 :
pathfindr 34:4493c9f6d707 1074 {
pathfindr 50:a94e70c00fff 1075 RET_busy = true;
pathfindr 51:9078e6928412 1076 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
pathfindr 51:9078e6928412 1077 updateBatteryV();
pathfindr 51:9078e6928412 1078 if (RET_voltage < 2.5f) {
pathfindr 48:64b1613941d5 1079 LED1blink(3,500);
pathfindr 48:64b1613941d5 1080 } else if (selfTest() == false){
pathfindr 48:64b1613941d5 1081 LED1blink(4,500);
pathfindr 48:64b1613941d5 1082 } else {
pathfindr 48:64b1613941d5 1083 if(RET_state_prev == STATE_DORMANT) {
pathfindr 48:64b1613941d5 1084 LED1on(500);
pathfindr 48:64b1613941d5 1085 } else if (RET_state_prev == STATE_NORMAL) {
pathfindr 48:64b1613941d5 1086 LED1blink(2,500);
pathfindr 48:64b1613941d5 1087 }
pathfindr 48:64b1613941d5 1088 }
pathfindr 50:a94e70c00fff 1089 if (RET_state_prev != STATE_NORMAL || RET_state_prev != STATE_DORMANT) {
pathfindr 50:a94e70c00fff 1090 setState(STATE_NORMAL);
pathfindr 50:a94e70c00fff 1091 } else {
pathfindr 50:a94e70c00fff 1092 setState(RET_state_prev);
pathfindr 50:a94e70c00fff 1093 }
pathfindr 51:9078e6928412 1094 dumpSettings();
pathfindr 50:a94e70c00fff 1095 RET_busy = false;
pathfindr 28:24b02608fa5f 1096 break;
pathfindr 34:4493c9f6d707 1097 }
pathfindr 28:24b02608fa5f 1098 case STATE_BUTTONPRESS3 :
pathfindr 34:4493c9f6d707 1099 {
pathfindr 50:a94e70c00fff 1100 RET_busy = true;
pathfindr 48:64b1613941d5 1101 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1102 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
pathfindr 48:64b1613941d5 1103 LED1blink(3,500);
pathfindr 48:64b1613941d5 1104 LED1on(0);
pathfindr 51:9078e6928412 1105 updateBatteryV();
pathfindr 48:64b1613941d5 1106 event_setup(true);
pathfindr 48:64b1613941d5 1107 LED1off();
pathfindr 48:64b1613941d5 1108 }
pathfindr 48:64b1613941d5 1109 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1110 RET_busy = false;
pathfindr 48:64b1613941d5 1111 break;
pathfindr 48:64b1613941d5 1112 }
pathfindr 48:64b1613941d5 1113 case STATE_BUTTONPRESS5 :
pathfindr 48:64b1613941d5 1114 {
pathfindr 50:a94e70c00fff 1115 RET_busy = true;
pathfindr 48:64b1613941d5 1116 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1117 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();}
pathfindr 48:64b1613941d5 1118 LED1blink(5,500);
pathfindr 48:64b1613941d5 1119 LED1on(0);
pathfindr 51:9078e6928412 1120 updateBatteryV();
pathfindr 51:9078e6928412 1121 event_location_tx(false);
pathfindr 48:64b1613941d5 1122 LED1off();
pathfindr 48:64b1613941d5 1123 }
pathfindr 48:64b1613941d5 1124 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1125 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1126 break;
pathfindr 34:4493c9f6d707 1127 }
pathfindr 51:9078e6928412 1128 case STATE_TURNOFF :
pathfindr 34:4493c9f6d707 1129 {
pathfindr 50:a94e70c00fff 1130 RET_busy = true;
pathfindr 51:9078e6928412 1131 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1132 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
pathfindr 51:9078e6928412 1133 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1134 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING OFF\n");debug_exe();}
pathfindr 51:9078e6928412 1135 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 51:9078e6928412 1136 LED1on(5000);
pathfindr 51:9078e6928412 1137 //event_turnonofflog_tx(false);
pathfindr 51:9078e6928412 1138 RET_busy = false;
pathfindr 51:9078e6928412 1139 break;
pathfindr 51:9078e6928412 1140 }
pathfindr 51:9078e6928412 1141 case STATE_TURNON :
pathfindr 51:9078e6928412 1142 {
pathfindr 51:9078e6928412 1143 RET_busy = true;
pathfindr 51:9078e6928412 1144 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1145 if (RET_haveSettings) {
pathfindr 51:9078e6928412 1146 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 1147 } else {
pathfindr 51:9078e6928412 1148 setState(STATE_SETUP);
pathfindr 19:22261767c87a 1149 }
pathfindr 51:9078e6928412 1150 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
pathfindr 51:9078e6928412 1151 LED1blink(20,100);
pathfindr 50:a94e70c00fff 1152 RET_busy = false;
pathfindr 6:388d3c7efdd9 1153 break;
pathfindr 34:4493c9f6d707 1154 }
pathfindr 39:f767b8037475 1155 case STATE_SCORCHEDEARTH :
pathfindr 39:f767b8037475 1156 {
pathfindr 50:a94e70c00fff 1157 RET_busy = true;
pathfindr 51:9078e6928412 1158 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
pathfindr 39:f767b8037475 1159 setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
pathfindr 39:f767b8037475 1160 read_app_data_from_flash(&app_data);
pathfindr 39:f767b8037475 1161 app_data.current_firmware_version = 0;
pathfindr 39:f767b8037475 1162 app_data.target_firmware_version = 0;
pathfindr 39:f767b8037475 1163 write_app_data_to_flash(&app_data);
pathfindr 39:f767b8037475 1164 system_reset();
pathfindr 50:a94e70c00fff 1165 RET_busy = false;
pathfindr 39:f767b8037475 1166 break;
pathfindr 39:f767b8037475 1167 }
pathfindr 6:388d3c7efdd9 1168 default :
pathfindr 34:4493c9f6d707 1169 {
pathfindr 19:22261767c87a 1170 setState(STATE_SETUP);
pathfindr 34:4493c9f6d707 1171 }
pathfindr 6:388d3c7efdd9 1172 }
pathfindr 6:388d3c7efdd9 1173 }
pathfindr 6:388d3c7efdd9 1174
pathfindr 7:e9a19750700d 1175 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 1176 // MAIN
pathfindr 7:e9a19750700d 1177 //------------------------------------------------------------------------------
pathfindr 50:a94e70c00fff 1178 int main() {
pathfindr 24:c161db07557f 1179 //INIT
pathfindr 51:9078e6928412 1180 LED1off();
pathfindr 51:9078e6928412 1181 ThisThread::sleep_for(2000); //Initial pause, this is needed for softdevice to init, dont remove!! If we dont have this we get crashes
pathfindr 49:15ddd84ec9fa 1182 watchdog.configure(300.0); //5 mins
pathfindr 43:7b232f03628f 1183 modem.off(false);
pathfindr 24:c161db07557f 1184 RTCticker.attach(&RTCtick, 1.0);
pathfindr 51:9078e6928412 1185 LPtimer.start();
pathfindr 34:4493c9f6d707 1186 button.fall(&buttonPress);
pathfindr 24:c161db07557f 1187 button.rise(&buttonRelease);
pathfindr 43:7b232f03628f 1188 read_app_data_from_flash(&app_data);
pathfindr 34:4493c9f6d707 1189 dumpSettings();
pathfindr 4:8d8e9bfa82e4 1190
pathfindr 4:8d8e9bfa82e4 1191 //CHECK IF THIS IS RESET
pathfindr 43:7b232f03628f 1192 //0x00000008 == lock up //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 1193 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 43:7b232f03628f 1194 RET_coldBoot = 1;
pathfindr 4:8d8e9bfa82e4 1195 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 1196 case 0x00000001 :
pathfindr 51:9078e6928412 1197 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:HW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1198 break;
pathfindr 4:8d8e9bfa82e4 1199 case 0x00000002 :
pathfindr 51:9078e6928412 1200 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:WD\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1201 break;
pathfindr 4:8d8e9bfa82e4 1202 case 0x00000004 :
pathfindr 51:9078e6928412 1203 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:SW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1204 break;
pathfindr 43:7b232f03628f 1205 case 0x00000008 :
pathfindr 51:9078e6928412 1206 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:LU\n");debug_exe();}
pathfindr 43:7b232f03628f 1207 break;
pathfindr 4:8d8e9bfa82e4 1208 }
pathfindr 36:8e359069192b 1209 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 43:7b232f03628f 1210 } else {
pathfindr 43:7b232f03628f 1211 //if its not one of these reasons then its a power-on or a brown-out
pathfindr 51:9078e6928412 1212 //if(get_flag(&app_data, app_execution_flag) == false) {
pathfindr 43:7b232f03628f 1213 //THIS is a unexpected reset / brownout???
pathfindr 51:9078e6928412 1214 //}
pathfindr 4:8d8e9bfa82e4 1215 }
pathfindr 24:c161db07557f 1216
pathfindr 9:b0a1535b8ef2 1217 //CHECK FOR FIRST BOOT
pathfindr 34:4493c9f6d707 1218 if (RET_coldBoot == 1) {
pathfindr 36:8e359069192b 1219 setDefaults();
pathfindr 36:8e359069192b 1220 //check battery
pathfindr 51:9078e6928412 1221 updateBatteryV();
pathfindr 51:9078e6928412 1222 if (RET_voltage < 2.5f) {
pathfindr 36:8e359069192b 1223 //battery low
pathfindr 36:8e359069192b 1224 LED1errorCode(10,2);
pathfindr 36:8e359069192b 1225 }
pathfindr 43:7b232f03628f 1226 addToExceptionString("FR");
pathfindr 24:c161db07557f 1227 }
pathfindr 13:29f67f256709 1228
pathfindr 34:4493c9f6d707 1229 //MAIN LOOP
pathfindr 50:a94e70c00fff 1230 while(true) {
pathfindr 34:4493c9f6d707 1231 //WATCHDOG
pathfindr 38:476a9b5629a1 1232 watchdogKick();
pathfindr 39:f767b8037475 1233 RET_asleep = false;
pathfindr 51:9078e6928412 1234 updateTemperatures();
pathfindr 34:4493c9f6d707 1235
pathfindr 34:4493c9f6d707 1236 //INIT
pathfindr 38:476a9b5629a1 1237 resetGlobals();
pathfindr 39:f767b8037475 1238 healthCheck(); //this must be after resetGlobals
pathfindr 13:29f67f256709 1239 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 1240
pathfindr 40:4270e09e59f2 1241 //check and log motion
pathfindr 48:64b1613941d5 1242 if (RET_state == STATE_NORMAL || RET_state == STATE_SETUP) {
pathfindr 48:64b1613941d5 1243 if (checkMotion() == true) {
pathfindr 48:64b1613941d5 1244 //if motionstate is true, then reset network and gps fail counts, so that they are not skipped
pathfindr 48:64b1613941d5 1245 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 1246 RET_GPSFailCount = 0;
pathfindr 48:64b1613941d5 1247 }
pathfindr 48:64b1613941d5 1248 }
pathfindr 40:4270e09e59f2 1249
pathfindr 9:b0a1535b8ef2 1250 //MAIN LOGIC
pathfindr 51:9078e6928412 1251 if(RET_debug){
pathfindr 39:f767b8037475 1252 if (RET_state != 99) {
pathfindr 45:6c7f5b556d74 1253 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, SET:%d, MF:%d, MS:%d, MTX:%d, %u, L:%u, LFS:%u, A:%u", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, RET_motionState, RET_motionTriggeredInLocTXInterval, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
pathfindr 51:9078e6928412 1254 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT M:%d, HsP:%.2f, Ht:%.2f, D:%s",RET_setting_activity_mode,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours,RET_activityData);debug_exe();
pathfindr 39:f767b8037475 1255 } else {
pathfindr 43:7b232f03628f 1256 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
pathfindr 39:f767b8037475 1257 }
pathfindr 51:9078e6928412 1258 if (RET_coldBoot == false && RET_RTCunixtime > RET_debug_offat) {
pathfindr 51:9078e6928412 1259 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"DEBUGGING OFF\n");debug_exe();
pathfindr 51:9078e6928412 1260 RET_debug = false;
pathfindr 51:9078e6928412 1261 }
pathfindr 38:476a9b5629a1 1262 }
pathfindr 37:505ef618f06c 1263
pathfindr 42:f482f4cdb319 1264 //STATE ENGINE
pathfindr 9:b0a1535b8ef2 1265 mainStateEngine();
pathfindr 51:9078e6928412 1266
pathfindr 34:4493c9f6d707 1267 //LOG FIRST RUN - BOOTLOADER COMMS
pathfindr 34:4493c9f6d707 1268 if (RET_coldBoot) {
pathfindr 45:6c7f5b556d74 1269 recordFirmwareAsValid();
pathfindr 34:4493c9f6d707 1270 RET_coldBoot = 0;
pathfindr 34:4493c9f6d707 1271 }
pathfindr 51:9078e6928412 1272
pathfindr 9:b0a1535b8ef2 1273 //PRE-SLEEP ACTIONS
pathfindr 45:6c7f5b556d74 1274 LED1off();
pathfindr 51:9078e6928412 1275 if (GLOBAL_modemOn) {
pathfindr 51:9078e6928412 1276 modem.off(true); //SOFT SHUT DOWN - THIS IS THE ONLY MODEM OFF, DONT REMOVE
pathfindr 51:9078e6928412 1277 } else {
pathfindr 51:9078e6928412 1278 modem.off(false); //HARD SHUT DOWN - THIS IS THE ONLY MODEM OFF, DONT REMOVE
pathfindr 51:9078e6928412 1279 }
pathfindr 40:4270e09e59f2 1280 RET_motionTriggeredinFrame = false;
pathfindr 34:4493c9f6d707 1281 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
pathfindr 50:a94e70c00fff 1282 nrf_configureForSleep();
pathfindr 34:4493c9f6d707 1283 NRFuart_uninit();
pathfindr 34:4493c9f6d707 1284 watchdogKick();
pathfindr 22:810425eb76e1 1285
pathfindr 34:4493c9f6d707 1286 //SLEEP
pathfindr 39:f767b8037475 1287 RET_asleep = true;
pathfindr 26:fa3579737329 1288 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 34:4493c9f6d707 1289 }
pathfindr 7:e9a19750700d 1290 }