init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Wed May 29 23:59:56 2019 +0000
Revision:
53:c6942af186d7
Parent:
52:bd7678eade77
Child:
54:2840f2d50734
added humidity sensor support

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 TO DO
pathfindr 51:9078e6928412 11 STOP FRASH WRITE FROM CRASHING IF THERE IS A PROBLEM
pathfindr 51:9078e6928412 12 */
pathfindr 51:9078e6928412 13
pathfindr 21:e0b866630c27 14 //------------------------------------------------------------------------------
pathfindr 37:505ef618f06c 15 //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
pathfindr 0:94b743e06998 16 //------------------------------------------------------------------------------
pathfindr 45:6c7f5b556d74 17 //should really add these - will just add as and when needed for now
pathfindr 48:64b1613941d5 18 void event_activity_tx();
pathfindr 51:9078e6928412 19 void event_location_tx(bool failsafe);
pathfindr 52:bd7678eade77 20 #if BLE_ENABLED
pathfindr 52:bd7678eade77 21 void bleShutdown();
pathfindr 52:bd7678eade77 22 #endif
pathfindr 0:94b743e06998 23
pathfindr 0:94b743e06998 24 //------------------------------------------------------------------------------
pathfindr 36:8e359069192b 25 // GLOBALS
pathfindr 51:9078e6928412 26 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 27 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 52:bd7678eade77 28 bool GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 29 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 52:bd7678eade77 30 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 52:bd7678eade77 31 bool GLOBAL_registeredOnNetwork = false;
pathfindr 52:bd7678eade77 32 bool GLOBAL_modemOn = false;
pathfindr 52:bd7678eade77 33 bool GLOBAL_LEDSequenceinProgress = false;
pathfindr 52:bd7678eade77 34 time_t GLOBAL_wakeTime = 0;
pathfindr 44:061509d1b38c 35 char GLOBAL_exceptionString[30];
pathfindr 52:bd7678eade77 36 char GLOBAL_debug_buffer[DEBUG_BUFFERSIZE];
pathfindr 51:9078e6928412 37 char GLOBAL_failed_broadcasts[10][160];
pathfindr 51:9078e6928412 38 bool GLOBAL_failed_broadcast_slots[10];
pathfindr 52:bd7678eade77 39 char GLOBAL_GPSlocString_prev[70];
pathfindr 52:bd7678eade77 40 bool GLOBAL_have_GPSlocString_prev = false;
pathfindr 34:4493c9f6d707 41
pathfindr 48:64b1613941d5 42 //SETTINGS
pathfindr 52:bd7678eade77 43 int RET_setting_firmware = 0;
pathfindr 52:bd7678eade77 44 uint32_t RET_setting_minimumupdate_hrs = 0;
pathfindr 52:bd7678eade77 45 uint8_t RET_setting_location_mode = DEFAULT_LOCATION_MODE;
pathfindr 52:bd7678eade77 46 uint8_t RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
pathfindr 52:bd7678eade77 47 uint32_t RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
pathfindr 52:bd7678eade77 48 uint32_t RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
pathfindr 52:bd7678eade77 49 uint16_t RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
pathfindr 52:bd7678eade77 50 uint32_t RET_setting_activity_tx_interval_hrs = 0;
pathfindr 52:bd7678eade77 51 uint8_t RET_setting_activity_mode = 1;
pathfindr 52:bd7678eade77 52 uint32_t RET_setting_environmental_tx_interval_mins = 0;
pathfindr 52:bd7678eade77 53 uint16_t RET_setting_motion_g = DEFAULT_MOTION_G;
pathfindr 52:bd7678eade77 54 time_t RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
pathfindr 52:bd7678eade77 55 time_t RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
pathfindr 52:bd7678eade77 56 uint16_t RET_setting_impact_g = 0;
pathfindr 52:bd7678eade77 57 uint8_t RET_setting_impact_alert = 0;
pathfindr 52:bd7678eade77 58 uint16_t RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
pathfindr 52:bd7678eade77 59 uint16_t RET_setting_beacon_interval_seconds = 0;
pathfindr 52:bd7678eade77 60 uint8_t RET_setting_beacon_scan = 0;
pathfindr 13:29f67f256709 61 //STATE
pathfindr 51:9078e6928412 62 char RET_pf_identifier[7];
pathfindr 52:bd7678eade77 63 uint8_t RET_coldBoot = 1;
pathfindr 52:bd7678eade77 64 bool RET_asleep = false;
pathfindr 52:bd7678eade77 65 bool RET_busy = false;
pathfindr 52:bd7678eade77 66 time_t RET_RTCunixtime = 0;
pathfindr 52:bd7678eade77 67 bool RET_haveSettings = false;
pathfindr 52:bd7678eade77 68 uint8_t RET_state = STATE_SETUP;
pathfindr 52:bd7678eade77 69 uint8_t RET_state_prev = RET_state;
pathfindr 52:bd7678eade77 70 uint8_t RET_buttonPressCount = 0;
pathfindr 52:bd7678eade77 71 time_t RET_buttonPressTime = 0;
pathfindr 52:bd7678eade77 72 time_t RET_buttonReleaseTime = 0;
pathfindr 52:bd7678eade77 73 time_t RET_buttonReleaseTime_prev = 0;
pathfindr 52:bd7678eade77 74 time_t RET_buttonHoldTime = 0;
pathfindr 52:bd7678eade77 75 time_t RET_SetupRunAt = 0;
pathfindr 52:bd7678eade77 76 time_t RET_SettingsGotAt = 0;
pathfindr 52:bd7678eade77 77 bool RET_setupInProgress = false;
pathfindr 52:bd7678eade77 78 bool RET_force2G = DEFAULT_FORCE2G;
pathfindr 52:bd7678eade77 79 bool RET_watchdogfired = false;
pathfindr 52:bd7678eade77 80 bool RET_receivedNewSettings = false;
pathfindr 52:bd7678eade77 81 uint32_t RET_GPSFailCount = 0;
pathfindr 52:bd7678eade77 82 uint32_t RET_NetworkFailCount = 0;
pathfindr 52:bd7678eade77 83 bool RET_debug = true;
pathfindr 52:bd7678eade77 84 time_t RET_debug_offat = 0;
pathfindr 52:bd7678eade77 85 float RET_voltage = 0.0;
pathfindr 52:bd7678eade77 86 float RET_temperature = 0.0;
pathfindr 53:c6942af186d7 87 float RET_humidity = 0.0;
pathfindr 52:bd7678eade77 88 float RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 52:bd7678eade77 89 float RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 52:bd7678eade77 90 uint32_t RET_modemBrownOutCountInSession = 0;
pathfindr 16:3bf5f1a5f869 91 //MOTION STATE
pathfindr 52:bd7678eade77 92 bool RET_motionTriggeredinFrame = false;
pathfindr 52:bd7678eade77 93 bool RET_motionStateOnInLocTXInterval = false;
pathfindr 52:bd7678eade77 94 time_t RET_motionStartTime = 0;
pathfindr 52:bd7678eade77 95 time_t RET_motionStopTime = 0;
pathfindr 52:bd7678eade77 96 bool RET_motionPendingOnState = false;
pathfindr 52:bd7678eade77 97 bool RET_motionPendingOffState = false;
pathfindr 52:bd7678eade77 98 bool RET_motionState = false;
pathfindr 52:bd7678eade77 99 float RET_motionTotalActivityHours = 0.0f;
pathfindr 52:bd7678eade77 100 float RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 52:bd7678eade77 101 time_t RET_motionFrameStart = 0;
pathfindr 45:6c7f5b556d74 102 char RET_activityData[ACTIVITY_BUFFERSIZE];
pathfindr 16:3bf5f1a5f869 103 //IMPACT
pathfindr 52:bd7678eade77 104 bool RET_impactTriggered = 0;
pathfindr 19:22261767c87a 105 //EVENTS LOGGING
pathfindr 52:bd7678eade77 106 time_t RET_eventTime_location_log = 0;
pathfindr 52:bd7678eade77 107 time_t RET_eventTime_environmental_log = 0;
pathfindr 19:22261767c87a 108 //EVENTS TX
pathfindr 52:bd7678eade77 109 time_t RET_eventTime_location_tx = 0;
pathfindr 52:bd7678eade77 110 time_t RET_eventTime_location_failsafe_tx = 0;
pathfindr 52:bd7678eade77 111 time_t RET_eventTime_environmental_tx = 0;
pathfindr 52:bd7678eade77 112 time_t RET_eventTime_activity_tx = 0;
pathfindr 52:bd7678eade77 113 time_t RET_eventTime_wakeFromDormant = 0;
pathfindr 52:bd7678eade77 114 //BLE
pathfindr 52:bd7678eade77 115 #if BLE_ENABLED
pathfindr 52:bd7678eade77 116 bool RET_bleBroadcasting = false;
pathfindr 52:bd7678eade77 117 uint8_t RET_bleAdv_flags = 0;
pathfindr 52:bd7678eade77 118 char RET_closestLocationTag_id[17];
pathfindr 52:bd7678eade77 119 int RET_closestLocationTag_rssi = 999;
pathfindr 53:c6942af186d7 120 int RET_closestBT04Tag_rssi = 999;
pathfindr 52:bd7678eade77 121 bool RET_locationTag_present = false;
pathfindr 52:bd7678eade77 122 bool RET_detector_present = false;
pathfindr 52:bd7678eade77 123 const uint8_t bleAdv_motionTriggeredinFrame_flag = 0b01000000;
pathfindr 52:bd7678eade77 124 const uint8_t bleAdv_motionState_flag = 0b00100000;
pathfindr 52:bd7678eade77 125 const uint8_t bleAdv_impact_flag = 0b00010000;
pathfindr 52:bd7678eade77 126 #pragma pack(1)
pathfindr 52:bd7678eade77 127 struct bleData_t {
pathfindr 52:bd7678eade77 128 uint16_t applicationSpecificId;
pathfindr 52:bd7678eade77 129 uint8_t firmware;
pathfindr 52:bd7678eade77 130 uint8_t flags;
pathfindr 52:bd7678eade77 131 uint16_t voltage;
pathfindr 52:bd7678eade77 132 uint8_t buttonpresses;
pathfindr 52:bd7678eade77 133 int16_t temperature;
pathfindr 52:bd7678eade77 134 uint8_t humidity;
pathfindr 52:bd7678eade77 135 uint8_t lux;
pathfindr 52:bd7678eade77 136 int8_t accel_x;
pathfindr 52:bd7678eade77 137 int8_t accel_y;
pathfindr 52:bd7678eade77 138 int8_t accel_z;
pathfindr 52:bd7678eade77 139 /*char id_1; //cant use this and local name as it pushes us over size limit
pathfindr 52:bd7678eade77 140 char id_2;
pathfindr 52:bd7678eade77 141 char id_3;
pathfindr 52:bd7678eade77 142 char id_4;
pathfindr 52:bd7678eade77 143 char id_5;
pathfindr 52:bd7678eade77 144 char id_6;*/
pathfindr 52:bd7678eade77 145 };
pathfindr 52:bd7678eade77 146 #pragma pack()
pathfindr 52:bd7678eade77 147 #endif
pathfindr 0:94b743e06998 148 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 149 //GPIO
pathfindr 9:b0a1535b8ef2 150 //------------------------------------------------------------------------------
pathfindr 34:4493c9f6d707 151 InterruptIn button(PN_IN_BUTTON);
pathfindr 9:b0a1535b8ef2 152
pathfindr 9:b0a1535b8ef2 153 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 154 //PERIPHERALS
pathfindr 0:94b743e06998 155 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 156 #if BLE_ENABLED
pathfindr 52:bd7678eade77 157 BLE myble;
pathfindr 52:bd7678eade77 158 #endif
pathfindr 52:bd7678eade77 159 WatchdogTimer watchdog;
pathfindr 34:4493c9f6d707 160 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 52:bd7678eade77 161 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 34:4493c9f6d707 162 Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 37:505ef618f06c 163 LowPowerTicker RTCticker;
pathfindr 51:9078e6928412 164 LowPowerTimer LPtimer;
pathfindr 9:b0a1535b8ef2 165
pathfindr 9:b0a1535b8ef2 166 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 167 //SEMAPHORES
pathfindr 9:b0a1535b8ef2 168 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 169 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 170
pathfindr 9:b0a1535b8ef2 171 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 172 // LOW LEVEL FUNCS
pathfindr 34:4493c9f6d707 173 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 174 void watchdogKick() {
pathfindr 52:bd7678eade77 175 watchdog.kick();
pathfindr 52:bd7678eade77 176 }
pathfindr 51:9078e6928412 177 void set_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 178 flags |= flag;
pathfindr 51:9078e6928412 179 }
pathfindr 51:9078e6928412 180 bool get_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 181 bool result = (flags & flag);
pathfindr 51:9078e6928412 182 return result;
pathfindr 51:9078e6928412 183 }
pathfindr 51:9078e6928412 184 void clr_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 185 flags &= ~flag;
pathfindr 51:9078e6928412 186 }
pathfindr 50:a94e70c00fff 187 void nrf_configureForSleep(){
pathfindr 50:a94e70c00fff 188 //Disable SPI pins to reduce power
pathfindr 50:a94e70c00fff 189 //nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL); //Don't need this one
pathfindr 34:4493c9f6d707 190 nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 191
pathfindr 50:a94e70c00fff 192 //disable modem control lines
pathfindr 50:a94e70c00fff 193 nrf_gpio_cfg_input(PN_GSM_PWR_KEY, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 194 nrf_gpio_cfg_input(PN_GSM_WAKE_DISABLE, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 195
pathfindr 50:a94e70c00fff 196 //TWI0
pathfindr 52:bd7678eade77 197 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 198 *(volatile uint32_t *)0x40003FFC = 0;
pathfindr 50:a94e70c00fff 199 *(volatile uint32_t *)0x40003FFC;
pathfindr 50:a94e70c00fff 200 *(volatile uint32_t *)0x40003FFC = 1;
pathfindr 50:a94e70c00fff 201 //TWI1
pathfindr 52:bd7678eade77 202 NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 203 *(volatile uint32_t *)0x40004FFC = 0;
pathfindr 50:a94e70c00fff 204 *(volatile uint32_t *)0x40004FFC;
pathfindr 50:a94e70c00fff 205 *(volatile uint32_t *)0x40004FFC = 1;
pathfindr 6:388d3c7efdd9 206 }
pathfindr 19:22261767c87a 207 void setState(uint8_t state) {
pathfindr 19:22261767c87a 208 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 209 RET_state = state;
pathfindr 22:810425eb76e1 210 }
pathfindr 51:9078e6928412 211 void dumpSettings() {
pathfindr 51:9078e6928412 212 if(RET_debug){
pathfindr 34:4493c9f6d707 213 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_RTCunixtime:%u", RET_RTCunixtime);debug_exe();
pathfindr 34:4493c9f6d707 214 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_firmware:%d", RET_setting_firmware);debug_exe();
pathfindr 34:4493c9f6d707 215 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_minimumupdate_hrs:%d", RET_setting_minimumupdate_hrs);debug_exe();
pathfindr 34:4493c9f6d707 216 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_state:%d", RET_state);debug_exe();
pathfindr 34:4493c9f6d707 217 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_mode:%d", RET_setting_location_mode);debug_exe();
pathfindr 34:4493c9f6d707 218 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_accuracy:%d", RET_setting_location_accuracy);debug_exe();
pathfindr 34:4493c9f6d707 219 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 220 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 221 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_timeout:%d", RET_setting_location_timeout);debug_exe();
pathfindr 34:4493c9f6d707 222 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 223 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_mode:%d", RET_setting_activity_mode);debug_exe();
pathfindr 34:4493c9f6d707 224 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 225 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_g:%d", RET_setting_motion_g);debug_exe();
pathfindr 34:4493c9f6d707 226 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 227 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 228 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_g:%d", RET_setting_impact_g);debug_exe();
pathfindr 34:4493c9f6d707 229 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_alert:%d", RET_setting_impact_alert);debug_exe();
pathfindr 34:4493c9f6d707 230 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_connection_timeout:%d", RET_setting_connection_timeout);debug_exe();
pathfindr 34:4493c9f6d707 231 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 232 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe();
pathfindr 51:9078e6928412 233 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_pf_identifier:%s", RET_pf_identifier);debug_exe();
pathfindr 34:4493c9f6d707 234 }
pathfindr 24:c161db07557f 235 }
pathfindr 45:6c7f5b556d74 236 void recordFirmwareAsValid() {
pathfindr 52:bd7678eade77 237 #if BLE_ENABLED
pathfindr 52:bd7678eade77 238 bleShutdown();
pathfindr 52:bd7678eade77 239 #endif
pathfindr 51:9078e6928412 240 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RECORD FW %d AS VALID...", FW_VERSION);debug_exe();
pathfindr 45:6c7f5b556d74 241 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 242 bool write_app_data_to_flash_execute = false;
pathfindr 45:6c7f5b556d74 243 if(get_flag(&app_data, app_execution_flag) == true) {
pathfindr 45:6c7f5b556d74 244 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 245 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 246 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 247 }
pathfindr 45:6c7f5b556d74 248 if(app_data.current_firmware_version != FW_VERSION) {
pathfindr 45:6c7f5b556d74 249 app_data.current_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 250 app_data.target_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 251 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 252 }
pathfindr 45:6c7f5b556d74 253 if (write_app_data_to_flash_execute) {
pathfindr 45:6c7f5b556d74 254 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 255 //read back to check
pathfindr 51:9078e6928412 256 ThisThread::sleep_for(200); //need this delay
pathfindr 51:9078e6928412 257 read_app_data_from_flash(&app_data);
pathfindr 51:9078e6928412 258 if (app_data.current_firmware_version == FW_VERSION) {
pathfindr 51:9078e6928412 259 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OK");debug_exe();
pathfindr 51:9078e6928412 260 } else {
pathfindr 51:9078e6928412 261 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FAIL - IS BLE SHUTDOWN?");debug_exe();
pathfindr 51:9078e6928412 262 }
pathfindr 51:9078e6928412 263 }
pathfindr 45:6c7f5b556d74 264 }
pathfindr 51:9078e6928412 265 void updateBatteryV() {
pathfindr 19:22261767c87a 266 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 267 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 268 batteryIn.calibrate();
pathfindr 49:15ddd84ec9fa 269 //get initial reading, first reading is always low
pathfindr 49:15ddd84ec9fa 270 batteryIn.updateData(); batteryIn.getData()[0];
pathfindr 49:15ddd84ec9fa 271 float voltage_accumulator = 0.0;
pathfindr 49:15ddd84ec9fa 272 uint8_t readings = 0;
pathfindr 49:15ddd84ec9fa 273 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 274 batteryIn.updateData();
pathfindr 49:15ddd84ec9fa 275 voltage_accumulator += (batteryIn.getData()[0])*(1.0/1024.0)*3.60;
pathfindr 49:15ddd84ec9fa 276 readings ++;
pathfindr 19:22261767c87a 277 }
pathfindr 51:9078e6928412 278 RET_voltage = (voltage_accumulator / (float)readings);
pathfindr 22:810425eb76e1 279 }
pathfindr 52:bd7678eade77 280 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 281 // TEMPERATURE
pathfindr 52:bd7678eade77 282 //------------------------------------------------------------------------------
pathfindr 22:810425eb76e1 283 float getTemperature() {
pathfindr 52:bd7678eade77 284 return si7060.getTemperature();
pathfindr 19:22261767c87a 285 }
pathfindr 51:9078e6928412 286 void updateTemperatures() {
pathfindr 52:bd7678eade77 287 if (RET_temperature == 0.0) {RET_temperature = getTemperature();}
pathfindr 51:9078e6928412 288 RET_temperature = (RET_temperature + (getTemperature() - RET_temperature) * 0.25f); //smoothed
pathfindr 52:bd7678eade77 289 if (RET_temperature < RET_temperature_min) {RET_temperature_min = RET_temperature;}
pathfindr 52:bd7678eade77 290 if (RET_temperature > RET_temperature_max) {RET_temperature_max = RET_temperature;}
pathfindr 51:9078e6928412 291 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 292 }
pathfindr 27:fa76f5a08195 293 void addToExceptionString(char* value) {
pathfindr 45:6c7f5b556d74 294 if(strstr(GLOBAL_exceptionString, value) == false){
pathfindr 52:bd7678eade77 295 snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),".%s",value);
pathfindr 45:6c7f5b556d74 296 }
pathfindr 27:fa76f5a08195 297 }
pathfindr 51:9078e6928412 298 void resetTemperatures() {
pathfindr 51:9078e6928412 299 RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 51:9078e6928412 300 RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 51:9078e6928412 301 }
pathfindr 19:22261767c87a 302 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 303 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 304 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 305 void buttonPress() {
pathfindr 51:9078e6928412 306 RET_buttonPressTime = LPtimer.read_ms();
pathfindr 19:22261767c87a 307 }
pathfindr 19:22261767c87a 308 void buttonRelease() {
pathfindr 51:9078e6928412 309 //debounce catch
pathfindr 51:9078e6928412 310 RET_buttonReleaseTime = LPtimer.read_ms();
pathfindr 51:9078e6928412 311 if ((RET_buttonReleaseTime - RET_buttonReleaseTime_prev) > 75) {
pathfindr 51:9078e6928412 312 RET_buttonPressCount ++;
pathfindr 51:9078e6928412 313 RET_buttonHoldTime = (RET_buttonReleaseTime - RET_buttonPressTime);
pathfindr 51:9078e6928412 314 //temporarily enable debugging and dump settings
pathfindr 51:9078e6928412 315 RET_debug = true;
pathfindr 52:bd7678eade77 316 RET_debug_offat = (RET_RTCunixtime + 3600); //debug on for 1hr
pathfindr 51:9078e6928412 317 }
pathfindr 51:9078e6928412 318 RET_buttonReleaseTime_prev = RET_buttonReleaseTime;
pathfindr 19:22261767c87a 319 }
pathfindr 19:22261767c87a 320 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 321 // RTC TICKER
pathfindr 51:9078e6928412 322 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 323 void RTCtick() {
pathfindr 51:9078e6928412 324 //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 325 RET_RTCunixtime += 1;
pathfindr 51:9078e6928412 326
pathfindr 51:9078e6928412 327 //button logic
pathfindr 51:9078e6928412 328 if (RET_buttonHoldTime >= 4000 && RET_buttonHoldTime <= 10000) {
pathfindr 49:15ddd84ec9fa 329 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 330 RET_buttonPressCount = 0;
pathfindr 51:9078e6928412 331 if (RET_state != STATE_DORMANT) {
pathfindr 51:9078e6928412 332 RET_state_prev = RET_state;
pathfindr 51:9078e6928412 333 RET_state = STATE_TURNOFF;
pathfindr 51:9078e6928412 334 } else {
pathfindr 51:9078e6928412 335 RET_state = STATE_TURNON;
pathfindr 51:9078e6928412 336 }
pathfindr 49:15ddd84ec9fa 337 mainthread.release();
pathfindr 51:9078e6928412 338 } else if (RET_buttonHoldTime >= 14000 && RET_buttonHoldTime <= 30000) {
pathfindr 49:15ddd84ec9fa 339 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 340 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 341 RET_state = STATE_SCORCHEDEARTH;
pathfindr 49:15ddd84ec9fa 342 mainthread.release();
pathfindr 49:15ddd84ec9fa 343 } else {
pathfindr 51:9078e6928412 344 if((LPtimer.read_ms() - RET_buttonReleaseTime) > 1500 && RET_buttonPressCount > 0) {
pathfindr 49:15ddd84ec9fa 345 if(RET_busy == true) {
pathfindr 49:15ddd84ec9fa 346 //RTCtick_ledflash_count = 4;// 2 flashes
pathfindr 49:15ddd84ec9fa 347 } else {
pathfindr 49:15ddd84ec9fa 348 RET_SetupRunAt = 0; //allow setup to run again
pathfindr 49:15ddd84ec9fa 349 switch (RET_buttonPressCount) { //double catches to help with debounce
pathfindr 49:15ddd84ec9fa 350 case 1 :
pathfindr 50:a94e70c00fff 351 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 352 RET_state = STATE_BUTTONPRESS1;
pathfindr 49:15ddd84ec9fa 353 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 354 mainthread.release();
pathfindr 49:15ddd84ec9fa 355 break;
pathfindr 49:15ddd84ec9fa 356 case 3 :
pathfindr 50:a94e70c00fff 357 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 358 RET_state = STATE_BUTTONPRESS3;
pathfindr 49:15ddd84ec9fa 359 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 360 mainthread.release();
pathfindr 49:15ddd84ec9fa 361 break;
pathfindr 49:15ddd84ec9fa 362 case 5 :
pathfindr 50:a94e70c00fff 363 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 364 RET_state = STATE_BUTTONPRESS5;
pathfindr 49:15ddd84ec9fa 365 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 366 mainthread.release();
pathfindr 49:15ddd84ec9fa 367 break;
pathfindr 49:15ddd84ec9fa 368 default :
pathfindr 49:15ddd84ec9fa 369 //do nothing
pathfindr 49:15ddd84ec9fa 370 break;
pathfindr 39:f767b8037475 371 }
pathfindr 9:b0a1535b8ef2 372 }
pathfindr 51:9078e6928412 373 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 374 }
pathfindr 49:15ddd84ec9fa 375 }
pathfindr 52:bd7678eade77 376
pathfindr 52:bd7678eade77 377 if(RET_debug && GLOBAL_LEDSequenceinProgress == false){
pathfindr 52:bd7678eade77 378 if (RET_motionState) {
pathfindr 52:bd7678eade77 379 led1 = 0;
pathfindr 52:bd7678eade77 380 } else {
pathfindr 52:bd7678eade77 381 led1 = 1;
pathfindr 52:bd7678eade77 382 }
pathfindr 52:bd7678eade77 383 }
pathfindr 9:b0a1535b8ef2 384 }
pathfindr 52:bd7678eade77 385 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 386 // GLOBALS
pathfindr 52:bd7678eade77 387 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 388 void resetSessionVars() {
pathfindr 34:4493c9f6d707 389 GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 390 RET_motionTriggeredinFrame = false;
pathfindr 52:bd7678eade77 391 RET_modemBrownOutCountInSession = 0;
pathfindr 53:c6942af186d7 392 RET_humidity = 99.0f;
pathfindr 34:4493c9f6d707 393 memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
pathfindr 34:4493c9f6d707 394 }
pathfindr 38:476a9b5629a1 395 void healthCheck() {
pathfindr 39:f767b8037475 396 //check for watchdog fire
pathfindr 39:f767b8037475 397 if (RET_watchdogfired == true) {
pathfindr 39:f767b8037475 398 addToExceptionString("WD");
pathfindr 39:f767b8037475 399 RET_watchdogfired = false;
pathfindr 39:f767b8037475 400 }
pathfindr 41:07e41dbd5304 401 //check clock
pathfindr 41:07e41dbd5304 402 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 403 if (RET_RTCunixtime < 1547678732) {
pathfindr 41:07e41dbd5304 404 //go dormant for 72hrs and then resetup
pathfindr 41:07e41dbd5304 405 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 406 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 407 }
pathfindr 41:07e41dbd5304 408 }
pathfindr 41:07e41dbd5304 409 //check that we have a reasonable time to loc
pathfindr 41:07e41dbd5304 410 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 411 //check location tx delta is set
pathfindr 41:07e41dbd5304 412 if (RET_eventTime_location_tx == 0) {
pathfindr 51:9078e6928412 413 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
pathfindr 41:07e41dbd5304 414 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 415 }
pathfindr 41:07e41dbd5304 416 //check location failsafe tx delta is less that 2 weeks
pathfindr 41:07e41dbd5304 417 long location_failsafe_tx_delta = (RET_eventTime_location_failsafe_tx - RET_RTCunixtime);
pathfindr 41:07e41dbd5304 418 if (location_failsafe_tx_delta > TENDAYSINSECONDS) {
pathfindr 51:9078e6928412 419 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 420 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 421 }
pathfindr 41:07e41dbd5304 422 }
pathfindr 41:07e41dbd5304 423 if (RET_haveSettings == false) {
pathfindr 41:07e41dbd5304 424 setState(STATE_SETUP);
pathfindr 41:07e41dbd5304 425 }
pathfindr 38:476a9b5629a1 426 }
pathfindr 24:c161db07557f 427
pathfindr 22:810425eb76e1 428 bool selfTest() {
pathfindr 22:810425eb76e1 429 int test_count = 0;
pathfindr 22:810425eb76e1 430 int test_pass = 0;
pathfindr 22:810425eb76e1 431
pathfindr 8:7351f8c4cd60 432 //Accelerometer
pathfindr 22:810425eb76e1 433 test_count ++;
pathfindr 8:7351f8c4cd60 434 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 435 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 27:fa76f5a08195 436 if (lis3dh_id == 51) {
pathfindr 43:7b232f03628f 437 GLOBAL_accel_healthy = true;
pathfindr 27:fa76f5a08195 438 test_pass ++;
pathfindr 27:fa76f5a08195 439 } else {
pathfindr 43:7b232f03628f 440 GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 441 addToExceptionString("AF");
pathfindr 27:fa76f5a08195 442 }
pathfindr 22:810425eb76e1 443
pathfindr 22:810425eb76e1 444 //Result
pathfindr 22:810425eb76e1 445 if (test_count == test_pass) {
pathfindr 22:810425eb76e1 446 return true;
pathfindr 22:810425eb76e1 447 } else {
pathfindr 52:bd7678eade77 448 //addToExceptionString("HF");
pathfindr 22:810425eb76e1 449 return false;
pathfindr 9:b0a1535b8ef2 450 }
pathfindr 9:b0a1535b8ef2 451 }
pathfindr 52:bd7678eade77 452 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 453 // BLE FUNCS
pathfindr 52:bd7678eade77 454 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 455 #if BLE_ENABLED
pathfindr 52:bd7678eade77 456 void bleBuildData(bleData_t &bleData)
pathfindr 52:bd7678eade77 457 {
pathfindr 52:bd7678eade77 458 bleData.applicationSpecificId = BLE_SERVICEID_GPSPLUS;
pathfindr 52:bd7678eade77 459 bleData.firmware = FW_VERSION;
pathfindr 52:bd7678eade77 460 //set flags
pathfindr 52:bd7678eade77 461 if (RET_motionTriggeredinFrame) {
pathfindr 52:bd7678eade77 462 set_8bit_flag(RET_bleAdv_flags, bleAdv_motionTriggeredinFrame_flag);
pathfindr 52:bd7678eade77 463 } else {
pathfindr 52:bd7678eade77 464 clr_8bit_flag(RET_bleAdv_flags, bleAdv_motionTriggeredinFrame_flag);
pathfindr 52:bd7678eade77 465 }
pathfindr 52:bd7678eade77 466 if (RET_motionState) {
pathfindr 52:bd7678eade77 467 set_8bit_flag(RET_bleAdv_flags, bleAdv_motionState_flag);
pathfindr 52:bd7678eade77 468 } else {
pathfindr 52:bd7678eade77 469 clr_8bit_flag(RET_bleAdv_flags, bleAdv_motionState_flag);
pathfindr 52:bd7678eade77 470 }
pathfindr 52:bd7678eade77 471 clr_8bit_flag(RET_bleAdv_flags, bleAdv_impact_flag);
pathfindr 52:bd7678eade77 472 bleData.flags = RET_bleAdv_flags;
pathfindr 52:bd7678eade77 473 bleData.voltage = uint16_t(RET_voltage * 100);
pathfindr 52:bd7678eade77 474 bleData.temperature = uint16_t(getTemperature() * 10);
pathfindr 52:bd7678eade77 475 bleData.humidity = 0xFF;
pathfindr 52:bd7678eade77 476 bleData.lux = 0xFF;
pathfindr 52:bd7678eade77 477 bleData.accel_x = 0xFF;
pathfindr 52:bd7678eade77 478 bleData.accel_y = 0xFF;
pathfindr 52:bd7678eade77 479 bleData.accel_z = 0xFF;
pathfindr 52:bd7678eade77 480 /*
pathfindr 52:bd7678eade77 481 bleData.id_1 = RET_pf_identifier[0]; //dont use this just stick it in the local name
pathfindr 52:bd7678eade77 482 bleData.id_2 = RET_pf_identifier[1];
pathfindr 52:bd7678eade77 483 bleData.id_3 = RET_pf_identifier[2];
pathfindr 52:bd7678eade77 484 bleData.id_4 = RET_pf_identifier[3];
pathfindr 52:bd7678eade77 485 bleData.id_5 = RET_pf_identifier[4];
pathfindr 52:bd7678eade77 486 bleData.id_6 = RET_pf_identifier[5];
pathfindr 52:bd7678eade77 487 */
pathfindr 52:bd7678eade77 488 }
pathfindr 52:bd7678eade77 489 void bleUpdateAndAdvertise(void)
pathfindr 52:bd7678eade77 490 {
pathfindr 52:bd7678eade77 491 int AdvertisingInterval = (RET_setting_beacon_interval_seconds * 1000);
pathfindr 52:bd7678eade77 492 bleData_t bleData;
pathfindr 52:bd7678eade77 493 bleBuildData(bleData);
pathfindr 52:bd7678eade77 494 myble.gap().stopAdvertising();
pathfindr 52:bd7678eade77 495 myble.gap().setTxPower(DEFAULT_BEACON_POWER); // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
pathfindr 52:bd7678eade77 496 myble.gap().accumulateAdvertisingPayloadTxPower(DEFAULT_BEACON_POWER); // (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm)
pathfindr 52:bd7678eade77 497 myble.gap().setAdvertisingInterval(AdvertisingInterval);
pathfindr 52:bd7678eade77 498 myble.gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 499 //myble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 500 myble.gap().startAdvertising();
pathfindr 52:bd7678eade77 501 RET_bleBroadcasting = true;
pathfindr 52:bd7678eade77 502 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Adv Start");debug_exe();}
pathfindr 52:bd7678eade77 503 }
pathfindr 52:bd7678eade77 504 void bleSetupAdvertising(BLE::InitializationCompleteCallbackContext *params)
pathfindr 52:bd7678eade77 505 {
pathfindr 52:bd7678eade77 506 if (RET_pf_identifier[0] != 0x00 && RET_pf_identifier[1] != 0x00 && RET_pf_identifier[2] != 0x00) {
pathfindr 52:bd7678eade77 507 bleData_t bleData;
pathfindr 52:bd7678eade77 508 bleBuildData(bleData);
pathfindr 52:bd7678eade77 509 char localname[6];
pathfindr 52:bd7678eade77 510 memcpy(localname,RET_pf_identifier,6);
pathfindr 52:bd7678eade77 511 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
pathfindr 52:bd7678eade77 512 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)localname, sizeof(localname));
pathfindr 52:bd7678eade77 513 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 514 //myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 515 myble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
pathfindr 52:bd7678eade77 516 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Setup...");debug_exe();}
pathfindr 52:bd7678eade77 517 }
pathfindr 52:bd7678eade77 518 }
pathfindr 52:bd7678eade77 519 void bleStopAdvertising(void)
pathfindr 52:bd7678eade77 520 {
pathfindr 52:bd7678eade77 521 myble.gap().stopAdvertising();
pathfindr 52:bd7678eade77 522 RET_bleBroadcasting = false;
pathfindr 52:bd7678eade77 523 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Adv Stop");debug_exe();}
pathfindr 52:bd7678eade77 524 }
pathfindr 52:bd7678eade77 525 void bleAdvertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
pathfindr 52:bd7678eade77 526 struct AdvertisingData_t {
pathfindr 52:bd7678eade77 527 uint8_t length;
pathfindr 52:bd7678eade77 528 GapAdvertisingData::DataType_t dataType;
pathfindr 52:bd7678eade77 529 uint8_t data[0];
pathfindr 52:bd7678eade77 530 } PACKED;
pathfindr 52:bd7678eade77 531 struct bleData_t {
pathfindr 52:bd7678eade77 532 uint16_t applicationSpecificId;
pathfindr 52:bd7678eade77 533 } PACKED;
pathfindr 52:bd7678eade77 534
pathfindr 52:bd7678eade77 535 //LOOKING FOR (SIAEC BETA)
pathfindr 52:bd7678eade77 536 //14f64b977c250854
pathfindr 52:bd7678eade77 537 //442ce31996801546
pathfindr 52:bd7678eade77 538
pathfindr 52:bd7678eade77 539 char beta_location_tag_0[17] = {'4','1','1','7','6','9','0','5','6','4','6','8','3','f','6','8',0x00}; //bens
pathfindr 52:bd7678eade77 540 char beta_location_tag_1[17] = {'1','4','f','6','4','b','9','7','7','c','2','5','0','8','5','4',0x00}; //singapore
pathfindr 52:bd7678eade77 541 char beta_location_tag_2[17] = {'4','4','2','c','e','3','1','9','9','6','8','0','1','5','4','6',0x00}; //singapore
pathfindr 52:bd7678eade77 542
pathfindr 52:bd7678eade77 543 //Portal demos
pathfindr 52:bd7678eade77 544 char beta_location_tag_3[17] = {'1','5','f','c','6','d','d','5','c','3','7','f','1','f','4','a',0x00};
pathfindr 52:bd7678eade77 545 char beta_location_tag_4[17] = {'b','a','a','e','c','d','6','8','b','d','7','e','e','a','6','5',0x00};
pathfindr 52:bd7678eade77 546 char beta_location_tag_5[17] = {'7','e','4','b','7','8','e','0','f','a','3','3','0','8','a','0',0x00};
pathfindr 52:bd7678eade77 547 char beta_location_tag_6[17] = {'e','1','1','3','c','d','e','a','a','e','f','f','1','4','d','9',0x00};
pathfindr 52:bd7678eade77 548 char beta_location_tag_7[17] = {'8','c','9','2','4','5','f','f','0','c','f','a','b','9','2','f',0x00};
pathfindr 52:bd7678eade77 549 char beta_location_tag_8[17] = {'0','7','e','9','9','1','d','3','b','e','b','6','4','0','4','8',0x00};
pathfindr 52:bd7678eade77 550 char beta_location_tag_9[17] = {'9','5','6','3','a','0','8','6','2','1','e','8','e','c','1','4',0x00};
pathfindr 52:bd7678eade77 551
pathfindr 52:bd7678eade77 552 //Search for the manufacturer specific data with matching application-ID
pathfindr 52:bd7678eade77 553 AdvertisingData_t *pAdvData;
pathfindr 52:bd7678eade77 554 for (size_t index = 0; index < params->advertisingDataLen; index += (pAdvData->length + 1)) {
pathfindr 52:bd7678eade77 555 pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
pathfindr 52:bd7678eade77 556 const bleData_t *pbleData = (const bleData_t *)pAdvData->data;
pathfindr 52:bd7678eade77 557
pathfindr 52:bd7678eade77 558 //ESTIMOTE BEACON
pathfindr 52:bd7678eade77 559 //example data 020104 0303 9afe1716 9afe 22 02c887042a33f9f0 00efebc60146f8ffffffff //spaces just to show identifier
pathfindr 52:bd7678eade77 560
pathfindr 52:bd7678eade77 561 //BLE TYPE SWITCH
pathfindr 52:bd7678eade77 562 switch(pbleData->applicationSpecificId) {
pathfindr 52:bd7678eade77 563 case BLE_SERVICEID_PFBEACON1 :
pathfindr 52:bd7678eade77 564 {
pathfindr 52:bd7678eade77 565 char beacon_identifier[17];
pathfindr 52:bd7678eade77 566 snprintf(beacon_identifier, sizeof(beacon_identifier), "%02x%02x%02x%02x%02x%02x%02x%02x",
pathfindr 52:bd7678eade77 567 params->advertisingData[12],
pathfindr 52:bd7678eade77 568 params->advertisingData[13],
pathfindr 52:bd7678eade77 569 params->advertisingData[14],
pathfindr 52:bd7678eade77 570 params->advertisingData[15],
pathfindr 52:bd7678eade77 571 params->advertisingData[16],
pathfindr 52:bd7678eade77 572 params->advertisingData[17],
pathfindr 52:bd7678eade77 573 params->advertisingData[18],
pathfindr 52:bd7678eade77 574 params->advertisingData[19]);
pathfindr 52:bd7678eade77 575 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " Mac: [%02x%02x%02x%02x%02x%02x], rssi: %d, AType: %u, S: %04x, Id: %s",params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],params->rssi, params->type, pbleData->applicationSpecificId, beacon_identifier);debug_exe();}
pathfindr 52:bd7678eade77 576 //check to see if its one of the two location beta beacons we are looking for...
pathfindr 52:bd7678eade77 577 if (strcmp(beacon_identifier, beta_location_tag_0) == 0
pathfindr 52:bd7678eade77 578 || strcmp(beacon_identifier, beta_location_tag_1) == 0
pathfindr 52:bd7678eade77 579 || strcmp(beacon_identifier, beta_location_tag_2) == 0
pathfindr 52:bd7678eade77 580 || strcmp(beacon_identifier, beta_location_tag_3) == 0
pathfindr 52:bd7678eade77 581 || strcmp(beacon_identifier, beta_location_tag_4) == 0
pathfindr 52:bd7678eade77 582 || strcmp(beacon_identifier, beta_location_tag_5) == 0
pathfindr 52:bd7678eade77 583 || strcmp(beacon_identifier, beta_location_tag_6) == 0
pathfindr 52:bd7678eade77 584 || strcmp(beacon_identifier, beta_location_tag_7) == 0
pathfindr 52:bd7678eade77 585 || strcmp(beacon_identifier, beta_location_tag_8) == 0
pathfindr 52:bd7678eade77 586 || strcmp(beacon_identifier, beta_location_tag_9) == 0
pathfindr 52:bd7678eade77 587 ) {
pathfindr 52:bd7678eade77 588 //dump advertising data
pathfindr 52:bd7678eade77 589 //for (unsigned index = 0; index < params->advertisingDataLen; index++) { if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "%02x",params->advertisingData[index]);debug_exe(false);}}
pathfindr 52:bd7678eade77 590 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " MATCH");debug_exe();}
pathfindr 52:bd7678eade77 591 int tag_rssi = params->rssi * -1; //to convert to positive number
pathfindr 52:bd7678eade77 592 if (tag_rssi < RET_closestLocationTag_rssi) {
pathfindr 52:bd7678eade77 593 RET_closestLocationTag_rssi = tag_rssi;
pathfindr 52:bd7678eade77 594 memcpy(RET_closestLocationTag_id, beacon_identifier, sizeof(RET_closestLocationTag_id));
pathfindr 52:bd7678eade77 595 RET_locationTag_present = true;
pathfindr 53:c6942af186d7 596 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Location Tag Present: %s, rssi: %d",RET_closestLocationTag_id, RET_closestLocationTag_rssi);debug_exe();}
pathfindr 52:bd7678eade77 597 }
pathfindr 52:bd7678eade77 598 }
pathfindr 52:bd7678eade77 599 break;
pathfindr 52:bd7678eade77 600 }
pathfindr 52:bd7678eade77 601 case BLE_SERVICEID_BT04 :
pathfindr 52:bd7678eade77 602 {
pathfindr 53:c6942af186d7 603 //get serial
pathfindr 53:c6942af186d7 604 //char serial[9];
pathfindr 53:c6942af186d7 605 //snprintf(serial, sizeof(serial), "%02x%02x%02x%02x", params->advertisingData[11], params->advertisingData[12], params->advertisingData[13], params->advertisingData[14]);
pathfindr 53:c6942af186d7 606 //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Serial: %s", serial);debug_exe();}
pathfindr 53:c6942af186d7 607
pathfindr 53:c6942af186d7 608 //get battery
pathfindr 53:c6942af186d7 609 //int battery = params->advertisingData[15];
pathfindr 53:c6942af186d7 610 //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Battery: %i", battery);debug_exe();}
pathfindr 53:c6942af186d7 611
pathfindr 53:c6942af186d7 612 //get temperature positive
pathfindr 53:c6942af186d7 613 /*
pathfindr 53:c6942af186d7 614 bool temppositive = !(params->advertisingData[17] & 0b01000000);
pathfindr 53:c6942af186d7 615 float temperature =
pathfindr 53:c6942af186d7 616 (
pathfindr 53:c6942af186d7 617 ((params->advertisingData[17] & 0b00111111) << 8) |
pathfindr 53:c6942af186d7 618 ((params->advertisingData[18] & 0b11111111))
pathfindr 53:c6942af186d7 619 ) / 100.0;
pathfindr 53:c6942af186d7 620 if (!temppositive) temperature = -temperature;
pathfindr 53:c6942af186d7 621 //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Temp: %f", temperature);debug_exe();}
pathfindr 53:c6942af186d7 622 */
pathfindr 53:c6942af186d7 623
pathfindr 53:c6942af186d7 624 //get humidity
pathfindr 53:c6942af186d7 625 int tag_rssi = params->rssi * -1; //to convert to positive number
pathfindr 53:c6942af186d7 626 if (tag_rssi < RET_closestBT04Tag_rssi) {
pathfindr 53:c6942af186d7 627 RET_humidity =
pathfindr 53:c6942af186d7 628 (
pathfindr 53:c6942af186d7 629 ((params->advertisingData[19] & 0b01111111) << 8) |
pathfindr 53:c6942af186d7 630 ((params->advertisingData[20] & 0b11111111))
pathfindr 53:c6942af186d7 631 ) / 100.0;
pathfindr 53:c6942af186d7 632 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Humidity Tag Present: %f",RET_humidity);debug_exe();}
pathfindr 53:c6942af186d7 633 }
pathfindr 53:c6942af186d7 634 //rssi
pathfindr 53:c6942af186d7 635 //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Rssi: %i", params->rssi);debug_exe();}
pathfindr 53:c6942af186d7 636
pathfindr 52:bd7678eade77 637 break;
pathfindr 52:bd7678eade77 638 }
pathfindr 52:bd7678eade77 639 case BLE_SERVICEID_PFDETECTOR1 :
pathfindr 52:bd7678eade77 640 {
pathfindr 52:bd7678eade77 641 char ble_advdata[100];
pathfindr 52:bd7678eade77 642 snprintf(ble_advdata, sizeof(ble_advdata), "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
pathfindr 52:bd7678eade77 643 params->advertisingData[0],
pathfindr 52:bd7678eade77 644 params->advertisingData[1],
pathfindr 52:bd7678eade77 645 params->advertisingData[2],
pathfindr 52:bd7678eade77 646 params->advertisingData[3],
pathfindr 52:bd7678eade77 647 params->advertisingData[4],
pathfindr 52:bd7678eade77 648 params->advertisingData[5],
pathfindr 52:bd7678eade77 649 params->advertisingData[6],
pathfindr 52:bd7678eade77 650 params->advertisingData[7],
pathfindr 52:bd7678eade77 651 params->advertisingData[8],
pathfindr 52:bd7678eade77 652 params->advertisingData[9],
pathfindr 52:bd7678eade77 653 params->advertisingData[10],
pathfindr 52:bd7678eade77 654 params->advertisingData[11],
pathfindr 52:bd7678eade77 655 params->advertisingData[12],
pathfindr 52:bd7678eade77 656 params->advertisingData[13],
pathfindr 52:bd7678eade77 657 params->advertisingData[14],
pathfindr 52:bd7678eade77 658 params->advertisingData[15],
pathfindr 52:bd7678eade77 659 params->advertisingData[16],
pathfindr 52:bd7678eade77 660 params->advertisingData[17],
pathfindr 52:bd7678eade77 661 params->advertisingData[18],
pathfindr 52:bd7678eade77 662 params->advertisingData[19],
pathfindr 52:bd7678eade77 663 params->advertisingData[20]);
pathfindr 52:bd7678eade77 664 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " Mac: [%02x%02x%02x%02x%02x%02x], rssi: %d, AType: %u, Id: %s",params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],params->rssi, pAdvData->dataType, ble_advdata);debug_exe();}
pathfindr 53:c6942af186d7 665 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Detector @rssi: %d",params->rssi);debug_exe();}
pathfindr 52:bd7678eade77 666 if (params->rssi >= -75) {
pathfindr 52:bd7678eade77 667 RET_detector_present = true;
pathfindr 52:bd7678eade77 668 }
pathfindr 52:bd7678eade77 669 break;
pathfindr 52:bd7678eade77 670 }
pathfindr 52:bd7678eade77 671 }
pathfindr 52:bd7678eade77 672 }
pathfindr 52:bd7678eade77 673 }
pathfindr 52:bd7678eade77 674 void bleConfigureScan(void)
pathfindr 52:bd7678eade77 675 {
pathfindr 52:bd7678eade77 676 //configure scan
pathfindr 53:c6942af186d7 677 myble.gap().setScanParams(60,30); //scan interval //scan window
pathfindr 53:c6942af186d7 678 //myble.gap().setScanParams(100,100); //scan interval //scan window
pathfindr 52:bd7678eade77 679 myble.gap().startScan(bleAdvertisementCallback);
pathfindr 52:bd7678eade77 680 }
pathfindr 52:bd7678eade77 681 void bleStopScan(void)
pathfindr 52:bd7678eade77 682 {
pathfindr 52:bd7678eade77 683 myble.gap().stopScan();
pathfindr 52:bd7678eade77 684 }
pathfindr 52:bd7678eade77 685 void bleShutdown(void)
pathfindr 52:bd7678eade77 686 {
pathfindr 52:bd7678eade77 687 bleStopScan();
pathfindr 52:bd7678eade77 688 bleStopAdvertising();
pathfindr 52:bd7678eade77 689 myble.shutdown();
pathfindr 52:bd7678eade77 690 }
pathfindr 52:bd7678eade77 691 #endif
pathfindr 9:b0a1535b8ef2 692
pathfindr 9:b0a1535b8ef2 693 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 694 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 695 //------------------------------------------------------------------------------
pathfindr 48:64b1613941d5 696 bool checkMotion() {
pathfindr 51:9078e6928412 697 if (RET_haveSettings) { //NEED THIS AS THINGS GO FUNKY WITHOUT RTC TIME SET CORRECTLY
pathfindr 51:9078e6928412 698 if (lis3dh_int2) {
pathfindr 51:9078e6928412 699 RET_motionTriggeredinFrame = true;
pathfindr 51:9078e6928412 700 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 52:bd7678eade77 701 if(RET_debug) {
pathfindr 52:bd7678eade77 702 LED1on(100);
pathfindr 52:bd7678eade77 703 }
pathfindr 51:9078e6928412 704 if (!RET_motionPendingOnState) {
pathfindr 51:9078e6928412 705 RET_motionPendingOnState = true;
pathfindr 51:9078e6928412 706 RET_motionPendingOffState = false;
pathfindr 51:9078e6928412 707 // Log start motion time
pathfindr 51:9078e6928412 708 RET_motionStartTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 709 }
pathfindr 51:9078e6928412 710 } else {
pathfindr 51:9078e6928412 711 RET_motionTriggeredinFrame = false;
pathfindr 51:9078e6928412 712 RET_motionPendingOnState = false;
pathfindr 51:9078e6928412 713 if (!RET_motionPendingOffState) {
pathfindr 51:9078e6928412 714 RET_motionPendingOffState = true;
pathfindr 51:9078e6928412 715 //log stop motion time
pathfindr 51:9078e6928412 716 RET_motionStopTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 717 }
pathfindr 9:b0a1535b8ef2 718 }
pathfindr 51:9078e6928412 719 //calculate motion state
pathfindr 51:9078e6928412 720 if (RET_motionFrameStart == 0) {RET_motionFrameStart = RET_RTCunixtime;}
pathfindr 51:9078e6928412 721 if (RET_motionPendingOnState) {
pathfindr 51:9078e6928412 722 //check if above threshold
pathfindr 51:9078e6928412 723 time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 52:bd7678eade77 724 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "M:%u",inMotionForSeconds);debug_exe();}
pathfindr 52:bd7678eade77 725 if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
pathfindr 52:bd7678eade77 726 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MotionOn");debug_exe();}
pathfindr 51:9078e6928412 727 RET_motionState = true;
pathfindr 52:bd7678eade77 728 RET_motionStateOnInLocTXInterval = true;
pathfindr 52:bd7678eade77 729 if (RET_motionState == false) { //If this is first after motionState 0
pathfindr 52:bd7678eade77 730 if (RET_setting_activity_mode == 2 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 52:bd7678eade77 731 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 52:bd7678eade77 732 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
pathfindr 52:bd7678eade77 733 }
pathfindr 45:6c7f5b556d74 734 }
pathfindr 23:a3b0ccf75ca5 735 }
pathfindr 9:b0a1535b8ef2 736 }
pathfindr 51:9078e6928412 737 if (RET_motionPendingOffState) {
pathfindr 51:9078e6928412 738 time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 52:bd7678eade77 739 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "NM:%u",noMotionForSeconds);debug_exe();}
pathfindr 52:bd7678eade77 740 if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
pathfindr 52:bd7678eade77 741 if (RET_motionState == true) {
pathfindr 52:bd7678eade77 742 RET_motionPendingOffState = false;
pathfindr 52:bd7678eade77 743 RET_motionState = false;
pathfindr 52:bd7678eade77 744 GLOBAL_motionStopFlagTriggered = true;
pathfindr 52:bd7678eade77 745 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MotionStop");debug_exe();}
pathfindr 52:bd7678eade77 746 if (RET_setting_activity_mode > 0 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 52:bd7678eade77 747 RET_motionTotalActivityHoursSincePost += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f);
pathfindr 52:bd7678eade77 748 RET_motionTotalActivityHours += RET_motionTotalActivityHoursSincePost;
pathfindr 52:bd7678eade77 749 if (RET_setting_activity_mode == 2) {
pathfindr 52:bd7678eade77 750 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 52:bd7678eade77 751 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
pathfindr 52:bd7678eade77 752 }
pathfindr 51:9078e6928412 753 }
pathfindr 45:6c7f5b556d74 754 }
pathfindr 23:a3b0ccf75ca5 755 }
pathfindr 9:b0a1535b8ef2 756 }
pathfindr 9:b0a1535b8ef2 757 }
pathfindr 48:64b1613941d5 758 return RET_motionState;
pathfindr 8:7351f8c4cd60 759 }
pathfindr 8:7351f8c4cd60 760
pathfindr 14:9a54b1b65bc8 761 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 762 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 763 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 764 bool saveSettings(char* settingsBuffer) {
pathfindr 18:22edaa7e74b1 765 int matchCount = 0;
pathfindr 19:22261767c87a 766 int critical_fail_count = 0;
pathfindr 32:dff4858bdf37 767 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 768 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 769
pathfindr 51:9078e6928412 770 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RESP: %s",settingsBuffer);debug_exe();}
pathfindr 50:a94e70c00fff 771
pathfindr 51:9078e6928412 772 //exampple
pathfindr 51:9078e6928412 773 //+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 774
pathfindr 51:9078e6928412 775 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 776 &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 777
pathfindr 51:9078e6928412 778 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 779 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 780
pathfindr 41:07e41dbd5304 781 if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; }
pathfindr 50:a94e70c00fff 782 if(TEMP_b > 1552915630) {
pathfindr 50:a94e70c00fff 783 if (RET_RTCunixtime < 1552915630) {
pathfindr 50:a94e70c00fff 784 //assume this is first proper unixtime and set
pathfindr 50:a94e70c00fff 785 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 786 } else {
pathfindr 51:9078e6928412 787 //this is not a first unixtime, so check within safe range, convert to long long first to avoid -1 rollover issue
pathfindr 51:9078e6928412 788 long long unixtime_in = TEMP_b;
pathfindr 51:9078e6928412 789 long long unixtime_current = RET_RTCunixtime;
pathfindr 51:9078e6928412 790 if ((unixtime_in - unixtime_current) < 604800) { //allow 1 week difference
pathfindr 50:a94e70c00fff 791 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 792 } else {
pathfindr 50:a94e70c00fff 793 critical_fail_count++;
pathfindr 51:9078e6928412 794 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 795 }
pathfindr 50:a94e70c00fff 796 }
pathfindr 50:a94e70c00fff 797 } else {
pathfindr 50:a94e70c00fff 798 critical_fail_count++;
pathfindr 51:9078e6928412 799 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "unixtime too low");debug_exe();}
pathfindr 50:a94e70c00fff 800 }
pathfindr 41:07e41dbd5304 801 if(TEMP_c != -1) { RET_setting_firmware = TEMP_c; } else { RET_setting_firmware = -1;}
pathfindr 41:07e41dbd5304 802 if(TEMP_d != -1) { setState(TEMP_d); } else { setState(STATE_NORMAL);}
pathfindr 41:07e41dbd5304 803 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; }
pathfindr 52:bd7678eade77 804 if(RET_setting_location_mode > 4) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
pathfindr 41:07e41dbd5304 805 if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f; }
pathfindr 34:4493c9f6d707 806 if(RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
pathfindr 41:07e41dbd5304 807 if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g; }
pathfindr 41:07e41dbd5304 808 if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h; }
pathfindr 34:4493c9f6d707 809 if(RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
pathfindr 41:07e41dbd5304 810 if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i; }
pathfindr 27:fa76f5a08195 811 if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
pathfindr 41:07e41dbd5304 812 if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; }
pathfindr 41:07e41dbd5304 813 if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; }
pathfindr 41:07e41dbd5304 814 if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; }
pathfindr 43:7b232f03628f 815 if(RET_setting_motion_g < 4 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
pathfindr 41:07e41dbd5304 816 if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; }
pathfindr 41:07e41dbd5304 817 if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; }
pathfindr 41:07e41dbd5304 818 if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; }
pathfindr 41:07e41dbd5304 819 if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p; }
pathfindr 41:07e41dbd5304 820 if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q; }
pathfindr 47:cc6d4d0bf897 821 if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
pathfindr 41:07e41dbd5304 822 if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r; }
pathfindr 41:07e41dbd5304 823 if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s; }
pathfindr 45:6c7f5b556d74 824 if(TEMP_t != -1) { RET_setting_activity_mode = TEMP_t; }
pathfindr 51:9078e6928412 825 if(TEMP_u[0] != 0x00 && TEMP_u[1] != 0x00 && TEMP_u[2] != 0x00) { strcpy(RET_pf_identifier,TEMP_u); }
pathfindr 45:6c7f5b556d74 826
pathfindr 19:22261767c87a 827 if (critical_fail_count == 0) {
pathfindr 45:6c7f5b556d74 828 RET_receivedNewSettings = true;
pathfindr 51:9078e6928412 829 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "NEW SETTINGS");debug_exe();}
pathfindr 40:4270e09e59f2 830 dumpSettings();
pathfindr 32:dff4858bdf37 831 RET_haveSettings = true;
pathfindr 34:4493c9f6d707 832 GLOBAL_needToConfigureLis3dh = true;
pathfindr 32:dff4858bdf37 833 RET_SettingsGotAt = RET_RTCunixtime;
pathfindr 37:505ef618f06c 834 //check for firmware update
pathfindr 38:476a9b5629a1 835 if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) {
pathfindr 48:64b1613941d5 836 //Broadcast any activity data we have before fw update
pathfindr 48:64b1613941d5 837 event_activity_tx();
pathfindr 48:64b1613941d5 838 //prep for FW update
pathfindr 52:bd7678eade77 839 #if BLE_ENABLED
pathfindr 52:bd7678eade77 840 bleShutdown();
pathfindr 52:bd7678eade77 841 #endif
pathfindr 37:505ef618f06c 842 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 843 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 844 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 845 app_data.current_firmware_version = FW_VERSION;
pathfindr 37:505ef618f06c 846 app_data.target_firmware_version = RET_setting_firmware;
pathfindr 37:505ef618f06c 847 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 848 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OTA\n");debug_exe();}
pathfindr 48:64b1613941d5 849 ThisThread::sleep_for(200);
pathfindr 37:505ef618f06c 850 system_reset();
pathfindr 37:505ef618f06c 851 }
pathfindr 19:22261767c87a 852 return true;
pathfindr 19:22261767c87a 853 } else {
pathfindr 44:061509d1b38c 854 addToExceptionString("SCF");
pathfindr 19:22261767c87a 855 return false;
pathfindr 19:22261767c87a 856 }
pathfindr 14:9a54b1b65bc8 857 } else {
pathfindr 44:061509d1b38c 858 addToExceptionString("SPE");
pathfindr 15:7aad9a7f970c 859 return false;
pathfindr 14:9a54b1b65bc8 860 }
pathfindr 14:9a54b1b65bc8 861 }
pathfindr 24:c161db07557f 862
pathfindr 41:07e41dbd5304 863 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 864 // SET EVENT TIMES
pathfindr 41:07e41dbd5304 865 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 866 void setEventTime_Location() {
pathfindr 20:5404841fdd2b 867 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 868 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 51:9078e6928412 869 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 870 }
pathfindr 20:5404841fdd2b 871 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 872 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 51:9078e6928412 873 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 874 }
pathfindr 41:07e41dbd5304 875 }
pathfindr 41:07e41dbd5304 876 void setEventTime_Activity() {
pathfindr 22:810425eb76e1 877 if(RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 22:810425eb76e1 878 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600));
pathfindr 51:9078e6928412 879 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 880 }
pathfindr 41:07e41dbd5304 881 }
pathfindr 41:07e41dbd5304 882 void setEventTime_Environmental() {
pathfindr 20:5404841fdd2b 883 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 884 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 51:9078e6928412 885 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 886 }
pathfindr 20:5404841fdd2b 887 }
pathfindr 41:07e41dbd5304 888 void setEventTimes() {
pathfindr 43:7b232f03628f 889 //SET ALL EVENT TIMES
pathfindr 41:07e41dbd5304 890 setEventTime_Location();
pathfindr 41:07e41dbd5304 891 setEventTime_Activity();
pathfindr 41:07e41dbd5304 892 setEventTime_Environmental();
pathfindr 41:07e41dbd5304 893 }
pathfindr 13:29f67f256709 894
pathfindr 13:29f67f256709 895 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 896 // ERROR LOG
pathfindr 40:4270e09e59f2 897 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 898 void sendErrorLog(char* error) {
pathfindr 51:9078e6928412 899 updateBatteryV();
pathfindr 40:4270e09e59f2 900 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 40:4270e09e59f2 901 char bytestosend[100];
pathfindr 40:4270e09e59f2 902 memset(bytestosend,0x00,sizeof(bytestosend));
pathfindr 52:bd7678eade77 903 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s.%s,s:0)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,timetaken,error,GLOBAL_exceptionString);
pathfindr 43:7b232f03628f 904 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 905 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 906 modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi);
pathfindr 43:7b232f03628f 907 }
pathfindr 43:7b232f03628f 908 }
pathfindr 40:4270e09e59f2 909 ThisThread::sleep_for(250);
pathfindr 40:4270e09e59f2 910 }
pathfindr 40:4270e09e59f2 911
pathfindr 40:4270e09e59f2 912 //------------------------------------------------------------------------------
pathfindr 43:7b232f03628f 913 // EVENTS
pathfindr 13:29f67f256709 914 //------------------------------------------------------------------------------
pathfindr 51:9078e6928412 915 void failed_broadcast_log(char* eventstring) {
pathfindr 51:9078e6928412 916 //First available free slot
pathfindr 51:9078e6928412 917 bool saved = false;
pathfindr 51:9078e6928412 918 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 919 if (GLOBAL_failed_broadcast_slots[i] == false) {
pathfindr 51:9078e6928412 920 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i])); //clear row
pathfindr 51:9078e6928412 921 snprintf(GLOBAL_failed_broadcasts[i],sizeof(GLOBAL_failed_broadcasts[i]),"%s",eventstring); //save new row
pathfindr 51:9078e6928412 922 GLOBAL_failed_broadcast_slots[i] = true;
pathfindr 51:9078e6928412 923 saved = true;
pathfindr 51:9078e6928412 924 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast saved: %s",eventstring);debug_exe();}
pathfindr 51:9078e6928412 925 break;
pathfindr 51:9078e6928412 926 }
pathfindr 51:9078e6928412 927 }
pathfindr 51:9078e6928412 928 if (!saved) {
pathfindr 51:9078e6928412 929 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast save fail");debug_exe();}
pathfindr 51:9078e6928412 930 }
pathfindr 51:9078e6928412 931 return;
pathfindr 51:9078e6928412 932 }
pathfindr 51:9078e6928412 933 void failed_broadcasts_tx() {
pathfindr 52:bd7678eade77 934 #if BLE_ENABLED
pathfindr 52:bd7678eade77 935 bleStopAdvertising();
pathfindr 52:bd7678eade77 936 #endif
pathfindr 51:9078e6928412 937 //check we have something to send...
pathfindr 51:9078e6928412 938 int numbertosend = 0;
pathfindr 51:9078e6928412 939 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 940 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 941 numbertosend ++;
pathfindr 51:9078e6928412 942 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Retry:%s",GLOBAL_failed_broadcasts[i]);debug_exe();}
pathfindr 51:9078e6928412 943 }
pathfindr 51:9078e6928412 944 }
pathfindr 51:9078e6928412 945 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcasts to send:%d",numbertosend);debug_exe();}
pathfindr 51:9078e6928412 946 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 947 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 948 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 949 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 950 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 951 char result[100];
pathfindr 51:9078e6928412 952 bool getResponse = false;
pathfindr 51:9078e6928412 953 snprintf(result,sizeof(result),"%s",modem.USSDmessage(GLOBAL_failed_broadcasts[i], getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 954 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 955 //clear row
pathfindr 51:9078e6928412 956 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i]));
pathfindr 51:9078e6928412 957 GLOBAL_failed_broadcast_slots[i] = false;
pathfindr 51:9078e6928412 958 }
pathfindr 51:9078e6928412 959 }
pathfindr 51:9078e6928412 960 }
pathfindr 51:9078e6928412 961 }
pathfindr 51:9078e6928412 962 }
pathfindr 51:9078e6928412 963 }
pathfindr 51:9078e6928412 964 }
pathfindr 43:7b232f03628f 965 bool event_setup(bool manualrun) {
pathfindr 52:bd7678eade77 966 #if BLE_ENABLED
pathfindr 52:bd7678eade77 967 bleStopAdvertising();
pathfindr 52:bd7678eade77 968 #endif
pathfindr 51:9078e6928412 969 bool pass = false;
pathfindr 49:15ddd84ec9fa 970 LED1on(0);
pathfindr 48:64b1613941d5 971 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 48:64b1613941d5 972 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 41:07e41dbd5304 973 RET_setupInProgress = true;
pathfindr 48:64b1613941d5 974 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 975 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 976 RET_setting_connection_timeout = 180; //reset to longer value to setup run to help with connection
pathfindr 25:7adeb1a53360 977 bool selftestresult = selfTest();
pathfindr 45:6c7f5b556d74 978 int connectionStartTime = RET_RTCunixtime;
pathfindr 38:476a9b5629a1 979 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 980 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 981 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
pathfindr 45:6c7f5b556d74 982 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 43:7b232f03628f 983 if (manualrun) addToExceptionString("MAN");
pathfindr 17:ba55c026b1d6 984 char bytestosend[160];
pathfindr 52:bd7678eade77 985 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,z:SETUP-%s,k:%s,m:%s,s:1)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,RET_temperature,timetaken_total,timetaken_connection,GLOBAL_exceptionString,SKU,HW_MAJORREVISION);
pathfindr 34:4493c9f6d707 986 char result[200];
pathfindr 51:9078e6928412 987 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, GLOBAL_defaultApi));
pathfindr 49:15ddd84ec9fa 988 RET_setupInProgress = false; //this turns off the flashing led
pathfindr 51:9078e6928412 989 if (strcmp(result, "sendokrecfail") == 0) {
pathfindr 51:9078e6928412 990 //send ok but receive fail
pathfindr 51:9078e6928412 991 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 51:9078e6928412 992 sendErrorLog("ERR-SU-NR");
pathfindr 51:9078e6928412 993 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 51:9078e6928412 994 //do nothing
pathfindr 51:9078e6928412 995 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 51:9078e6928412 996 } else {
pathfindr 51:9078e6928412 997 if (saveSettings(result) == true) {
pathfindr 51:9078e6928412 998 pass = true;
pathfindr 51:9078e6928412 999 } else {
pathfindr 19:22261767c87a 1000 //something went critically wrong getting settings
pathfindr 48:64b1613941d5 1001 sendErrorLog("ERR-SU-IR");
pathfindr 51:9078e6928412 1002 }
pathfindr 16:3bf5f1a5f869 1003 }
pathfindr 13:29f67f256709 1004 }
pathfindr 13:29f67f256709 1005 } else {
pathfindr 19:22261767c87a 1006 //FAILUREMODE Modem failed to turn on
pathfindr 51:9078e6928412 1007 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Modem Fail");debug_exe();}
pathfindr 13:29f67f256709 1008 }
pathfindr 49:15ddd84ec9fa 1009
pathfindr 49:15ddd84ec9fa 1010 //IF WE PASSED THEN LETS DO OTHER POSTS
pathfindr 49:15ddd84ec9fa 1011 if (pass == true) {
pathfindr 49:15ddd84ec9fa 1012 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 1013 //GET LOC DATA
pathfindr 52:bd7678eade77 1014 //uint8_t previous_location_accuracy = RET_setting_location_accuracy;
pathfindr 52:bd7678eade77 1015 //RET_setting_location_accuracy = 1; //set location mode to CL only
pathfindr 51:9078e6928412 1016 event_location_tx(false);
pathfindr 52:bd7678eade77 1017 //RET_setting_location_accuracy = previous_location_accuracy; //set location mode back to previous
pathfindr 49:15ddd84ec9fa 1018 //SEND ANY ACTIVITY DATA
pathfindr 49:15ddd84ec9fa 1019 event_activity_tx();
pathfindr 49:15ddd84ec9fa 1020 LED1blink(4,500); //PASS AND END
pathfindr 49:15ddd84ec9fa 1021 }
pathfindr 49:15ddd84ec9fa 1022
pathfindr 49:15ddd84ec9fa 1023 //TIDY UP = LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 1024 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 49:15ddd84ec9fa 1025 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 1026 RET_NetworkFailCount = 0;
pathfindr 52:bd7678eade77 1027 setEventTimes();
pathfindr 13:29f67f256709 1028 //RESULT
pathfindr 19:22261767c87a 1029 return pass;
pathfindr 13:29f67f256709 1030 }
pathfindr 39:f767b8037475 1031 void event_turnonofflog_tx(bool turnon) {
pathfindr 52:bd7678eade77 1032 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1033 bleStopAdvertising();
pathfindr 52:bd7678eade77 1034 #endif
pathfindr 48:64b1613941d5 1035 RET_NetworkFailCount = 0; //reset network blocker
pathfindr 48:64b1613941d5 1036 RET_GPSFailCount = 0;
pathfindr 51:9078e6928412 1037 updateBatteryV();
pathfindr 45:6c7f5b556d74 1038 int connectionStartTime = RET_RTCunixtime;
pathfindr 39:f767b8037475 1039 if (modem.on(RET_force2G)) {
pathfindr 39:f767b8037475 1040 char locString[70];
pathfindr 45:6c7f5b556d74 1041 int gpsStartTime = RET_RTCunixtime;
pathfindr 48:64b1613941d5 1042 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 45:6c7f5b556d74 1043 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 48:64b1613941d5 1044 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 1045 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 45:6c7f5b556d74 1046 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 39:f767b8037475 1047 char bytestosend[160];
pathfindr 39:f767b8037475 1048 if (turnon) {
pathfindr 52:bd7678eade77 1049 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNON,e:%d,y:%d,x:%d,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 1050 } else {
pathfindr 52:bd7678eade77 1051 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,y:%d,x:%d,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 1052 }
pathfindr 39:f767b8037475 1053 char result[180];
pathfindr 51:9078e6928412 1054 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi));
pathfindr 39:f767b8037475 1055 }
pathfindr 39:f767b8037475 1056 }
pathfindr 39:f767b8037475 1057 }
pathfindr 51:9078e6928412 1058 void event_location_tx(bool failsafe) {
pathfindr 52:bd7678eade77 1059 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1060 bleStopAdvertising();
pathfindr 52:bd7678eade77 1061 #endif
pathfindr 51:9078e6928412 1062 //check if we have any outstanding to send
pathfindr 51:9078e6928412 1063 failed_broadcasts_tx();
pathfindr 49:15ddd84ec9fa 1064 bool selfTestResult = selfTest();
pathfindr 52:bd7678eade77 1065 bool skipTX = false;
pathfindr 51:9078e6928412 1066 char bytesToSend[160];
pathfindr 52:bd7678eade77 1067
pathfindr 52:bd7678eade77 1068 //Check for Nearby Location Beacons
pathfindr 53:c6942af186d7 1069 memset(RET_closestLocationTag_id, 0x00, sizeof(RET_closestLocationTag_id));
pathfindr 53:c6942af186d7 1070 RET_closestLocationTag_rssi = 999;
pathfindr 53:c6942af186d7 1071 RET_locationTag_present = false;
pathfindr 53:c6942af186d7 1072 RET_detector_present = false;
pathfindr 52:bd7678eade77 1073 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1074 if (RET_setting_beacon_scan == 1 && RET_setting_location_mode != LOCATION_MODE_REALTIME) {
pathfindr 52:bd7678eade77 1075 LowPowerTimer bleScan_t;
pathfindr 52:bd7678eade77 1076 bleScan_t.start();
pathfindr 52:bd7678eade77 1077 uint32_t bleScan_startmillis = bleScan_t.read_ms(); uint32_t bleScan_runtime = 0;
pathfindr 52:bd7678eade77 1078 if (!myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1079 myble.init();
pathfindr 52:bd7678eade77 1080 while (!myble.hasInitialized() && bleScan_runtime < 1000) {
pathfindr 52:bd7678eade77 1081 bleScan_runtime = (bleScan_t.read_ms() - bleScan_startmillis);
pathfindr 52:bd7678eade77 1082 }
pathfindr 52:bd7678eade77 1083 }
pathfindr 52:bd7678eade77 1084 if (myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1085 //Scan for 10 seconds
pathfindr 52:bd7678eade77 1086 bleConfigureScan();
pathfindr 52:bd7678eade77 1087 bleScan_startmillis = bleScan_t.read_ms();
pathfindr 52:bd7678eade77 1088 while (bleScan_runtime < 10000 && RET_detector_present == false) {
pathfindr 52:bd7678eade77 1089 bleScan_runtime = (bleScan_t.read_ms() - bleScan_startmillis);
pathfindr 52:bd7678eade77 1090 myble.waitForEvent(); // low power wait for event
pathfindr 52:bd7678eade77 1091 }
pathfindr 53:c6942af186d7 1092 if (RET_locationTag_present && RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "LocationTag: %s, rssi: %d",RET_closestLocationTag_id, RET_closestLocationTag_rssi);debug_exe();}
pathfindr 52:bd7678eade77 1093 }
pathfindr 52:bd7678eade77 1094 bleScan_t.stop();
pathfindr 52:bd7678eade77 1095 myble.gap().stopScan();
pathfindr 52:bd7678eade77 1096 }
pathfindr 52:bd7678eade77 1097 //see if we are in range of ble detector, if so, no need to get gps and broadcast
pathfindr 52:bd7678eade77 1098 if (RET_detector_present == true && RET_setting_beacon_interval_seconds > 0) {
pathfindr 52:bd7678eade77 1099 skipTX = true;
pathfindr 52:bd7678eade77 1100 }
pathfindr 52:bd7678eade77 1101 #endif
pathfindr 52:bd7678eade77 1102
pathfindr 52:bd7678eade77 1103
pathfindr 52:bd7678eade77 1104 if (skipTX == false || failsafe == true) {
pathfindr 52:bd7678eade77 1105 //Set any network or GPS fail flags
pathfindr 52:bd7678eade77 1106 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 52:bd7678eade77 1107 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 52:bd7678eade77 1108 //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 52:bd7678eade77 1109 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 52:bd7678eade77 1110 if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); }
pathfindr 52:bd7678eade77 1111 if (modem.on(RET_force2G)) {
pathfindr 52:bd7678eade77 1112 char locString[70];
pathfindr 52:bd7678eade77 1113 int gpsStartTime = RET_RTCunixtime;
pathfindr 52:bd7678eade77 1114
pathfindr 53:c6942af186d7 1115 if (RET_locationTag_present) {
pathfindr 53:c6942af186d7 1116 snprintf(locString, sizeof(locString), ",l:%s.%d\0",RET_closestLocationTag_id,RET_closestLocationTag_rssi);
pathfindr 53:c6942af186d7 1117 addToExceptionString("BL");
pathfindr 51:9078e6928412 1118 } else {
pathfindr 53:c6942af186d7 1119 //check if we have moved since last post, if not use same valid gps fix if exists
pathfindr 53:c6942af186d7 1120 if (RET_motionStateOnInLocTXInterval == false && GLOBAL_have_GPSlocString_prev == true) {
pathfindr 53:c6942af186d7 1121 memcpy(locString, GLOBAL_GPSlocString_prev, sizeof(locString));
pathfindr 53:c6942af186d7 1122 addToExceptionString("P");
pathfindr 53:c6942af186d7 1123 } else {
pathfindr 53:c6942af186d7 1124 memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 53:c6942af186d7 1125 }
pathfindr 52:bd7678eade77 1126 }
pathfindr 52:bd7678eade77 1127
pathfindr 52:bd7678eade77 1128 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 52:bd7678eade77 1129
pathfindr 52:bd7678eade77 1130 //SEND DATA
pathfindr 52:bd7678eade77 1131 int connectionStartTime = RET_RTCunixtime;
pathfindr 52:bd7678eade77 1132 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 52:bd7678eade77 1133 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time.
pathfindr 52:bd7678eade77 1134 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 52:bd7678eade77 1135 //Check if we should wait for settings back
pathfindr 52:bd7678eade77 1136 bool getSettings = true;
pathfindr 52:bd7678eade77 1137 //work out if we need to get settings back
pathfindr 52:bd7678eade77 1138 if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times
pathfindr 53:c6942af186d7 1139 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.0f,n:%.0f,q:%.0f,w:%.0f,v:%.1f,z:L%s,e:%d,y:%d,x:%d,j:%d,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_temperature_min,RET_temperature_max,RET_humidity,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionStateOnInLocTXInterval,getSettings,locString);
pathfindr 52:bd7678eade77 1140 //snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.0f,v:%.1f,z:L-%s,e:%d,y:%d,x:%d,j:%d,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionStateOnInLocTXInterval,getSettings,locString);
pathfindr 52:bd7678eade77 1141 char result[180];
pathfindr 52:bd7678eade77 1142 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, GLOBAL_defaultApi));
pathfindr 52:bd7678eade77 1143 if (getSettings == true && strcmp(result, "sendokrecfail") == 0) {
pathfindr 52:bd7678eade77 1144 //send ok but receive fail
pathfindr 52:bd7678eade77 1145 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 52:bd7678eade77 1146 //sendErrorLog("ERR-LOC-NR");
pathfindr 52:bd7678eade77 1147 } else if (strcmp(result, "sendok") == 0) {
pathfindr 52:bd7678eade77 1148 //do nothing
pathfindr 52:bd7678eade77 1149 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 52:bd7678eade77 1150 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 52:bd7678eade77 1151 //need to log for retry
pathfindr 52:bd7678eade77 1152 } else {
pathfindr 52:bd7678eade77 1153 if (getSettings) {
pathfindr 52:bd7678eade77 1154 if (saveSettings(result) == false){sendErrorLog("ERR-LOC-IR");}
pathfindr 52:bd7678eade77 1155 }
pathfindr 48:64b1613941d5 1156 }
pathfindr 22:810425eb76e1 1157 }
pathfindr 22:810425eb76e1 1158 }
pathfindr 22:810425eb76e1 1159 }
pathfindr 20:5404841fdd2b 1160 }
pathfindr 20:5404841fdd2b 1161 //RESETS
pathfindr 52:bd7678eade77 1162 RET_motionStateOnInLocTXInterval = false;
pathfindr 41:07e41dbd5304 1163 setEventTime_Location();
pathfindr 19:22261767c87a 1164 }
pathfindr 51:9078e6928412 1165 void event_activity_tx() {
pathfindr 52:bd7678eade77 1166 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1167 bleStopAdvertising();
pathfindr 52:bd7678eade77 1168 #endif
pathfindr 51:9078e6928412 1169 //check if we have any outstanding to send
pathfindr 51:9078e6928412 1170 failed_broadcasts_tx();
pathfindr 51:9078e6928412 1171 //check we have something to send...
pathfindr 51:9078e6928412 1172 bool sendok = false;
pathfindr 51:9078e6928412 1173 if (RET_motionTotalActivityHoursSincePost > 0.0f && RET_setting_activity_mode > 0) {
pathfindr 51:9078e6928412 1174 //Build data to send
pathfindr 51:9078e6928412 1175 char bytesToSend[160];
pathfindr 51:9078e6928412 1176 if (RET_setting_activity_mode == 1) {
pathfindr 52:bd7678eade77 1177 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours);
pathfindr 51:9078e6928412 1178 } else if (RET_setting_activity_mode == 2) {
pathfindr 52:bd7678eade77 1179 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost);
pathfindr 51:9078e6928412 1180 }
pathfindr 51:9078e6928412 1181 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 1182 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 1183 //SEND DATA
pathfindr 51:9078e6928412 1184 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 1185 char result[100];
pathfindr 51:9078e6928412 1186 bool getResponse = false;
pathfindr 51:9078e6928412 1187 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 1188 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 1189 sendok = true;
pathfindr 51:9078e6928412 1190 //if(!RET_debug) {sendok = true;} //FOR TESTING
pathfindr 51:9078e6928412 1191 }
pathfindr 51:9078e6928412 1192 }
pathfindr 51:9078e6928412 1193 }
pathfindr 51:9078e6928412 1194 }
pathfindr 51:9078e6928412 1195 //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 1196 if (sendok == true) {
pathfindr 51:9078e6928412 1197 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 1198 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1199 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1200 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 1201 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1202 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1203 }
pathfindr 51:9078e6928412 1204 } else if (sendok == false) {
pathfindr 51:9078e6928412 1205 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 1206 //do nothing
pathfindr 51:9078e6928412 1207 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 1208 failed_broadcast_log(bytesToSend);
pathfindr 51:9078e6928412 1209 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1210 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1211 }
pathfindr 51:9078e6928412 1212 }
pathfindr 51:9078e6928412 1213 }
pathfindr 51:9078e6928412 1214 //RESETS
pathfindr 51:9078e6928412 1215 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 51:9078e6928412 1216 setEventTime_Activity();
pathfindr 48:64b1613941d5 1217 }
pathfindr 24:c161db07557f 1218
pathfindr 7:e9a19750700d 1219 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 1220 // STATE ENGINE
pathfindr 7:e9a19750700d 1221 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 1222 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 1223 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 1224 case STATE_SETUP :
pathfindr 34:4493c9f6d707 1225 { //need the curlies to avoid "transfer of control bypass init error warning"
pathfindr 48:64b1613941d5 1226 RET_busy = true;
pathfindr 41:07e41dbd5304 1227 //check that we havent run setup too recently
pathfindr 41:07e41dbd5304 1228 time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt);
pathfindr 49:15ddd84ec9fa 1229 if (RET_SetupRunAt == 0 || setupRunAt_delta >= ONEDAYINSECONDS) {
pathfindr 51:9078e6928412 1230 updateBatteryV();
pathfindr 43:7b232f03628f 1231 if (event_setup(false)) {
pathfindr 41:07e41dbd5304 1232 // All good
pathfindr 41:07e41dbd5304 1233 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 1234 } else {
pathfindr 41:07e41dbd5304 1235 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + ONEDAYINSECONDS);
pathfindr 41:07e41dbd5304 1236 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1237 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 1238 }
pathfindr 19:22261767c87a 1239 } else {
pathfindr 41:07e41dbd5304 1240 time_t setupCanRunAt = (RET_RTCunixtime + (ONEDAYINSECONDS - setupRunAt_delta));
pathfindr 51:9078e6928412 1241 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 1242 }
pathfindr 48:64b1613941d5 1243 RET_busy = false;
pathfindr 6:388d3c7efdd9 1244 break;
pathfindr 34:4493c9f6d707 1245 }
pathfindr 16:3bf5f1a5f869 1246 case STATE_NORMAL :
pathfindr 48:64b1613941d5 1247 {
pathfindr 48:64b1613941d5 1248 RET_busy = true;
pathfindr 24:c161db07557f 1249 //LOCATION EVENT
pathfindr 19:22261767c87a 1250 bool run_location_tx = false;
pathfindr 27:fa76f5a08195 1251 switch (RET_setting_location_mode) {
pathfindr 27:fa76f5a08195 1252 case 1: //INTERVAL POST
pathfindr 27:fa76f5a08195 1253 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 1254 break;
pathfindr 27:fa76f5a08195 1255 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 40:4270e09e59f2 1256 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 52:bd7678eade77 1257 if (RET_motionStateOnInLocTXInterval == true || RET_motionState == true) {
pathfindr 40:4270e09e59f2 1258 run_location_tx = true;
pathfindr 40:4270e09e59f2 1259 } else {
pathfindr 52:bd7678eade77 1260 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX no mot\n");debug_exe();}
pathfindr 40:4270e09e59f2 1261 }
pathfindr 40:4270e09e59f2 1262 }
pathfindr 27:fa76f5a08195 1263 break;
pathfindr 27:fa76f5a08195 1264 case 3: //POST ON STOP MOTION
pathfindr 52:bd7678eade77 1265 if (GLOBAL_motionStopFlagTriggered) { GLOBAL_motionStopFlagTriggered = false; run_location_tx = true; }
pathfindr 27:fa76f5a08195 1266 break;
pathfindr 27:fa76f5a08195 1267 }
pathfindr 48:64b1613941d5 1268 if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) {
pathfindr 48:64b1613941d5 1269 RET_NetworkFailCount = 0; //reset to ensure connection
pathfindr 48:64b1613941d5 1270 RET_GPSFailCount = 0; // reset to ensure gps try
pathfindr 52:bd7678eade77 1271 addToExceptionString("FS");
pathfindr 51:9078e6928412 1272 updateBatteryV();
pathfindr 51:9078e6928412 1273 event_location_tx(true);
pathfindr 51:9078e6928412 1274 } else {
pathfindr 51:9078e6928412 1275 if (run_location_tx) { updateBatteryV(); event_location_tx(false); }
pathfindr 48:64b1613941d5 1276 }
pathfindr 19:22261767c87a 1277
pathfindr 24:c161db07557f 1278 //ACTIVITY EVENT
pathfindr 24:c161db07557f 1279 bool run_activity_tx = false;
pathfindr 45:6c7f5b556d74 1280 if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
pathfindr 48:64b1613941d5 1281 if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0f) {
pathfindr 45:6c7f5b556d74 1282 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1283 }
pathfindr 45:6c7f5b556d74 1284 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > 1) {
pathfindr 45:6c7f5b556d74 1285 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1286 }
pathfindr 9:b0a1535b8ef2 1287 }
pathfindr 45:6c7f5b556d74 1288 //If in sendevent mode, check if buffer is close to full, if so send
pathfindr 45:6c7f5b556d74 1289 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > (ACTIVITY_BUFFERSIZE-20)) { run_activity_tx = true; }
pathfindr 48:64b1613941d5 1290 if (run_activity_tx) { event_activity_tx(); }
pathfindr 48:64b1613941d5 1291
pathfindr 48:64b1613941d5 1292 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1293 break;
pathfindr 34:4493c9f6d707 1294 }
pathfindr 16:3bf5f1a5f869 1295 case STATE_DORMANT :
pathfindr 34:4493c9f6d707 1296 {
pathfindr 50:a94e70c00fff 1297 RET_busy = true;
pathfindr 22:810425eb76e1 1298 if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
pathfindr 22:810425eb76e1 1299 if (RET_haveSettings) {
pathfindr 22:810425eb76e1 1300 setState(STATE_NORMAL);
pathfindr 22:810425eb76e1 1301 } else {
pathfindr 22:810425eb76e1 1302 setState(STATE_SETUP);
pathfindr 22:810425eb76e1 1303 }
pathfindr 7:e9a19750700d 1304 }
pathfindr 51:9078e6928412 1305 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 1306 RET_busy = false;
pathfindr 12:8345612bf867 1307 break;
pathfindr 34:4493c9f6d707 1308 }
pathfindr 16:3bf5f1a5f869 1309 case STATE_BUTTONPRESS1 :
pathfindr 34:4493c9f6d707 1310 {
pathfindr 50:a94e70c00fff 1311 RET_busy = true;
pathfindr 51:9078e6928412 1312 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
pathfindr 51:9078e6928412 1313 updateBatteryV();
pathfindr 51:9078e6928412 1314 if (RET_voltage < 2.5f) {
pathfindr 48:64b1613941d5 1315 LED1blink(3,500);
pathfindr 48:64b1613941d5 1316 } else if (selfTest() == false){
pathfindr 48:64b1613941d5 1317 LED1blink(4,500);
pathfindr 48:64b1613941d5 1318 } else {
pathfindr 48:64b1613941d5 1319 if(RET_state_prev == STATE_DORMANT) {
pathfindr 48:64b1613941d5 1320 LED1on(500);
pathfindr 48:64b1613941d5 1321 } else if (RET_state_prev == STATE_NORMAL) {
pathfindr 48:64b1613941d5 1322 LED1blink(2,500);
pathfindr 48:64b1613941d5 1323 }
pathfindr 48:64b1613941d5 1324 }
pathfindr 50:a94e70c00fff 1325 if (RET_state_prev != STATE_NORMAL || RET_state_prev != STATE_DORMANT) {
pathfindr 50:a94e70c00fff 1326 setState(STATE_NORMAL);
pathfindr 50:a94e70c00fff 1327 } else {
pathfindr 50:a94e70c00fff 1328 setState(RET_state_prev);
pathfindr 50:a94e70c00fff 1329 }
pathfindr 51:9078e6928412 1330 dumpSettings();
pathfindr 50:a94e70c00fff 1331 RET_busy = false;
pathfindr 28:24b02608fa5f 1332 break;
pathfindr 34:4493c9f6d707 1333 }
pathfindr 28:24b02608fa5f 1334 case STATE_BUTTONPRESS3 :
pathfindr 34:4493c9f6d707 1335 {
pathfindr 50:a94e70c00fff 1336 RET_busy = true;
pathfindr 48:64b1613941d5 1337 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1338 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
pathfindr 48:64b1613941d5 1339 LED1blink(3,500);
pathfindr 48:64b1613941d5 1340 LED1on(0);
pathfindr 51:9078e6928412 1341 updateBatteryV();
pathfindr 48:64b1613941d5 1342 event_setup(true);
pathfindr 48:64b1613941d5 1343 LED1off();
pathfindr 48:64b1613941d5 1344 }
pathfindr 48:64b1613941d5 1345 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1346 RET_busy = false;
pathfindr 48:64b1613941d5 1347 break;
pathfindr 48:64b1613941d5 1348 }
pathfindr 48:64b1613941d5 1349 case STATE_BUTTONPRESS5 :
pathfindr 48:64b1613941d5 1350 {
pathfindr 50:a94e70c00fff 1351 RET_busy = true;
pathfindr 48:64b1613941d5 1352 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1353 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();}
pathfindr 48:64b1613941d5 1354 LED1blink(5,500);
pathfindr 48:64b1613941d5 1355 LED1on(0);
pathfindr 51:9078e6928412 1356 updateBatteryV();
pathfindr 51:9078e6928412 1357 event_location_tx(false);
pathfindr 48:64b1613941d5 1358 LED1off();
pathfindr 48:64b1613941d5 1359 }
pathfindr 48:64b1613941d5 1360 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1361 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1362 break;
pathfindr 34:4493c9f6d707 1363 }
pathfindr 51:9078e6928412 1364 case STATE_TURNOFF :
pathfindr 34:4493c9f6d707 1365 {
pathfindr 50:a94e70c00fff 1366 RET_busy = true;
pathfindr 51:9078e6928412 1367 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1368 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
pathfindr 51:9078e6928412 1369 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1370 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING OFF\n");debug_exe();}
pathfindr 51:9078e6928412 1371 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 1372 LED1on(5000);
pathfindr 51:9078e6928412 1373 //event_turnonofflog_tx(false);
pathfindr 51:9078e6928412 1374 RET_busy = false;
pathfindr 51:9078e6928412 1375 break;
pathfindr 51:9078e6928412 1376 }
pathfindr 51:9078e6928412 1377 case STATE_TURNON :
pathfindr 51:9078e6928412 1378 {
pathfindr 51:9078e6928412 1379 RET_busy = true;
pathfindr 51:9078e6928412 1380 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1381 if (RET_haveSettings) {
pathfindr 51:9078e6928412 1382 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 1383 } else {
pathfindr 51:9078e6928412 1384 setState(STATE_SETUP);
pathfindr 19:22261767c87a 1385 }
pathfindr 51:9078e6928412 1386 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
pathfindr 51:9078e6928412 1387 LED1blink(20,100);
pathfindr 50:a94e70c00fff 1388 RET_busy = false;
pathfindr 6:388d3c7efdd9 1389 break;
pathfindr 34:4493c9f6d707 1390 }
pathfindr 39:f767b8037475 1391 case STATE_SCORCHEDEARTH :
pathfindr 39:f767b8037475 1392 {
pathfindr 50:a94e70c00fff 1393 RET_busy = true;
pathfindr 51:9078e6928412 1394 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
pathfindr 39:f767b8037475 1395 setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
pathfindr 39:f767b8037475 1396 read_app_data_from_flash(&app_data);
pathfindr 39:f767b8037475 1397 app_data.current_firmware_version = 0;
pathfindr 39:f767b8037475 1398 app_data.target_firmware_version = 0;
pathfindr 39:f767b8037475 1399 write_app_data_to_flash(&app_data);
pathfindr 39:f767b8037475 1400 system_reset();
pathfindr 50:a94e70c00fff 1401 RET_busy = false;
pathfindr 39:f767b8037475 1402 break;
pathfindr 39:f767b8037475 1403 }
pathfindr 6:388d3c7efdd9 1404 default :
pathfindr 34:4493c9f6d707 1405 {
pathfindr 19:22261767c87a 1406 setState(STATE_SETUP);
pathfindr 34:4493c9f6d707 1407 }
pathfindr 6:388d3c7efdd9 1408 }
pathfindr 6:388d3c7efdd9 1409 }
pathfindr 6:388d3c7efdd9 1410
pathfindr 7:e9a19750700d 1411 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 1412 // MAIN
pathfindr 7:e9a19750700d 1413 //------------------------------------------------------------------------------
pathfindr 50:a94e70c00fff 1414 int main() {
pathfindr 24:c161db07557f 1415 //INIT
pathfindr 51:9078e6928412 1416 LED1off();
pathfindr 51:9078e6928412 1417 ThisThread::sleep_for(2000); //Initial pause, this is needed for softdevice to init, dont remove!! If we dont have this we get crashes
pathfindr 52:bd7678eade77 1418 watchdog.configure(300.0); //5 mins //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 43:7b232f03628f 1419 modem.off(false);
pathfindr 24:c161db07557f 1420 RTCticker.attach(&RTCtick, 1.0);
pathfindr 51:9078e6928412 1421 LPtimer.start();
pathfindr 34:4493c9f6d707 1422 button.fall(&buttonPress);
pathfindr 24:c161db07557f 1423 button.rise(&buttonRelease);
pathfindr 43:7b232f03628f 1424 read_app_data_from_flash(&app_data);
pathfindr 34:4493c9f6d707 1425 dumpSettings();
pathfindr 4:8d8e9bfa82e4 1426
pathfindr 4:8d8e9bfa82e4 1427 //CHECK IF THIS IS RESET
pathfindr 43:7b232f03628f 1428 //0x00000008 == lock up //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 1429 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 43:7b232f03628f 1430 RET_coldBoot = 1;
pathfindr 4:8d8e9bfa82e4 1431 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 1432 case 0x00000001 :
pathfindr 52:bd7678eade77 1433 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:HW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1434 break;
pathfindr 4:8d8e9bfa82e4 1435 case 0x00000002 :
pathfindr 52:bd7678eade77 1436 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:WD\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1437 break;
pathfindr 4:8d8e9bfa82e4 1438 case 0x00000004 :
pathfindr 52:bd7678eade77 1439 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:SW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1440 break;
pathfindr 43:7b232f03628f 1441 case 0x00000008 :
pathfindr 52:bd7678eade77 1442 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:LU\n");debug_exe();}
pathfindr 43:7b232f03628f 1443 break;
pathfindr 4:8d8e9bfa82e4 1444 }
pathfindr 36:8e359069192b 1445 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 4:8d8e9bfa82e4 1446 }
pathfindr 24:c161db07557f 1447
pathfindr 9:b0a1535b8ef2 1448 //CHECK FOR FIRST BOOT
pathfindr 52:bd7678eade77 1449 if (RET_coldBoot == 1) {
pathfindr 52:bd7678eade77 1450 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 52:bd7678eade77 1451 lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
pathfindr 51:9078e6928412 1452 updateBatteryV();
pathfindr 52:bd7678eade77 1453 if (RET_voltage < 2.5f) { LED1errorCode(10,2);}
pathfindr 43:7b232f03628f 1454 addToExceptionString("FR");
pathfindr 24:c161db07557f 1455 }
pathfindr 13:29f67f256709 1456
pathfindr 34:4493c9f6d707 1457 //MAIN LOOP
pathfindr 52:bd7678eade77 1458 while(true) {
pathfindr 39:f767b8037475 1459 RET_asleep = false;
pathfindr 52:bd7678eade77 1460 watchdogKick();
pathfindr 51:9078e6928412 1461 updateTemperatures();
pathfindr 52:bd7678eade77 1462 resetSessionVars();
pathfindr 39:f767b8037475 1463 healthCheck(); //this must be after resetGlobals
pathfindr 13:29f67f256709 1464 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 1465
pathfindr 40:4270e09e59f2 1466 //check and log motion
pathfindr 48:64b1613941d5 1467 if (RET_state == STATE_NORMAL || RET_state == STATE_SETUP) {
pathfindr 52:bd7678eade77 1468 if (checkMotion() == true) { //if motionstate is true, then reset network and gps fail counts, so that they are not skipped
pathfindr 48:64b1613941d5 1469 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 1470 RET_GPSFailCount = 0;
pathfindr 48:64b1613941d5 1471 }
pathfindr 48:64b1613941d5 1472 }
pathfindr 40:4270e09e59f2 1473
pathfindr 9:b0a1535b8ef2 1474 //MAIN LOGIC
pathfindr 51:9078e6928412 1475 if(RET_debug){
pathfindr 39:f767b8037475 1476 if (RET_state != 99) {
pathfindr 52:bd7678eade77 1477 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_motionStateOnInLocTXInterval, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
pathfindr 51:9078e6928412 1478 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 1479 } else {
pathfindr 43:7b232f03628f 1480 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 1481 }
pathfindr 51:9078e6928412 1482 if (RET_coldBoot == false && RET_RTCunixtime > RET_debug_offat) {
pathfindr 51:9078e6928412 1483 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"DEBUGGING OFF\n");debug_exe();
pathfindr 51:9078e6928412 1484 RET_debug = false;
pathfindr 51:9078e6928412 1485 }
pathfindr 38:476a9b5629a1 1486 }
pathfindr 37:505ef618f06c 1487
pathfindr 42:f482f4cdb319 1488 //STATE ENGINE
pathfindr 9:b0a1535b8ef2 1489 mainStateEngine();
pathfindr 51:9078e6928412 1490
pathfindr 34:4493c9f6d707 1491 //LOG FIRST RUN - BOOTLOADER COMMS
pathfindr 34:4493c9f6d707 1492 if (RET_coldBoot) {
pathfindr 45:6c7f5b556d74 1493 recordFirmwareAsValid();
pathfindr 34:4493c9f6d707 1494 RET_coldBoot = 0;
pathfindr 34:4493c9f6d707 1495 }
pathfindr 51:9078e6928412 1496
pathfindr 52:bd7678eade77 1497 //CONFIGURE BLE BEACON BROADCAST
pathfindr 52:bd7678eade77 1498 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1499 //see if we are in range of ble detector, if so, no need to get gps and broadcast
pathfindr 52:bd7678eade77 1500 if (RET_setting_beacon_interval_seconds > 0 && RET_pf_identifier[0] != 0x00 && RET_pf_identifier[1] != 0x00 && RET_pf_identifier[2] != 0x00) {
pathfindr 52:bd7678eade77 1501 if (RET_bleBroadcasting == false) {
pathfindr 52:bd7678eade77 1502 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE init...");debug_exe();}
pathfindr 52:bd7678eade77 1503 myble.init(bleSetupAdvertising);
pathfindr 52:bd7678eade77 1504 if (myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1505 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OK");debug_exe();}
pathfindr 52:bd7678eade77 1506 bleUpdateAndAdvertise();
pathfindr 52:bd7678eade77 1507 } else {
pathfindr 52:bd7678eade77 1508 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FAIL");debug_exe();}
pathfindr 52:bd7678eade77 1509 }
pathfindr 52:bd7678eade77 1510 } else {
pathfindr 52:bd7678eade77 1511 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE update");debug_exe();}
pathfindr 52:bd7678eade77 1512 bleUpdateAndAdvertise();
pathfindr 52:bd7678eade77 1513 }
pathfindr 52:bd7678eade77 1514 } else {
pathfindr 52:bd7678eade77 1515 if (RET_bleBroadcasting == true) bleStopAdvertising();
pathfindr 52:bd7678eade77 1516 }
pathfindr 52:bd7678eade77 1517 #endif
pathfindr 52:bd7678eade77 1518
pathfindr 9:b0a1535b8ef2 1519 //PRE-SLEEP ACTIONS
pathfindr 45:6c7f5b556d74 1520 LED1off();
pathfindr 52:bd7678eade77 1521 modem.off(GLOBAL_modemOn); //HARD OR SOFT SHUT DOWN - DEPENDING ON modemOn state
pathfindr 34:4493c9f6d707 1522 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
pathfindr 50:a94e70c00fff 1523 nrf_configureForSleep();
pathfindr 34:4493c9f6d707 1524 NRFuart_uninit();
pathfindr 34:4493c9f6d707 1525 watchdogKick();
pathfindr 34:4493c9f6d707 1526 //SLEEP
pathfindr 39:f767b8037475 1527 RET_asleep = true;
pathfindr 26:fa3579737329 1528 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 34:4493c9f6d707 1529 }
pathfindr 7:e9a19750700d 1530 }