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