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