init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
main.cpp@16:3bf5f1a5f869, 2018-12-17 (annotated)
- Committer:
- pathfindr
- Date:
- Mon Dec 17 22:47:13 2018 +0000
- Revision:
- 16:3bf5f1a5f869
- Parent:
- 15:7aad9a7f970c
- Child:
- 17:ba55c026b1d6
7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pathfindr | 0:94b743e06998 | 1 | #include "main.h" |
pathfindr | 5:8f8951127724 | 2 | //include "nrf_soc.h" |
pathfindr | 4:8d8e9bfa82e4 | 3 | |
pathfindr | 0:94b743e06998 | 4 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 5 | //FUNCTION PROTOTYPES |
pathfindr | 0:94b743e06998 | 6 | //------------------------------------------------------------------------------ |
pathfindr | 4:8d8e9bfa82e4 | 7 | static void mainStateEngine(void); |
pathfindr | 4:8d8e9bfa82e4 | 8 | static void selftest(void); |
pathfindr | 7:e9a19750700d | 9 | static void buttonPress(void); |
pathfindr | 7:e9a19750700d | 10 | static void buttonRelease(void); |
pathfindr | 0:94b743e06998 | 11 | |
pathfindr | 0:94b743e06998 | 12 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 13 | //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK) |
pathfindr | 0:94b743e06998 | 14 | //------------------------------------------------------------------------------ |
pathfindr | 15:7aad9a7f970c | 15 | char* GLOBAL_defaultApi = "b:gps2,d:3"; |
pathfindr | 13:29f67f256709 | 16 | bool GLOBAL_accel_healthy = false; |
pathfindr | 13:29f67f256709 | 17 | bool GLOBAL_requireSoftReset = false; |
pathfindr | 13:29f67f256709 | 18 | bool GLOBAL_motionFlagTriggered = false; |
pathfindr | 13:29f67f256709 | 19 | bool GLOBAL_debugLED = false; |
pathfindr | 13:29f67f256709 | 20 | bool GLOBAL_needToConfigureLis3dh = false; |
pathfindr | 13:29f67f256709 | 21 | bool GLOBAL_registeredOnNetwork = false; |
pathfindr | 13:29f67f256709 | 22 | long long GLOBAL_imei = 0; |
pathfindr | 13:29f67f256709 | 23 | float GLOBAL_voltage = 0.0; |
pathfindr | 13:29f67f256709 | 24 | time_t GLOBAL_RTCunixtime = 0; |
pathfindr | 13:29f67f256709 | 25 | time_t GLOBAL_wakeTime = 0; |
pathfindr | 0:94b743e06998 | 26 | |
pathfindr | 0:94b743e06998 | 27 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 28 | //RETAINED NOINIT RAM VARS |
pathfindr | 0:94b743e06998 | 29 | //------------------------------------------------------------------------------ |
pathfindr | 13:29f67f256709 | 30 | #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS |
pathfindr | 13:29f67f256709 | 31 | //IDENTITY |
pathfindr | 16:3bf5f1a5f869 | 32 | static long long RET_imei __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 33 | static char RET_pf_identifier[6] __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 34 | //SETTINGS |
pathfindr | 16:3bf5f1a5f869 | 35 | static int RET_setting_firmware __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 36 | static uint8_t RET_setting_location_accuracy __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 37 | static uint32_t RET_setting_location_tx_interval_mins __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 38 | static uint32_t RET_setting_location_tx_failsafe_hrs __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 39 | static uint16_t RET_setting_location_timeout __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 40 | static uint8_t RET_setting_activity_mode __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 41 | static uint32_t RET_setting_activity_tx_interval_mins __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 42 | static uint32_t RET_setting_environmental_tx_interval_mins __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 43 | static uint16_t RET_setting_motion_g __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 44 | static time_t RET_setting_motion_start_seconds __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 45 | static time_t RET_setting_motion_stop_seconds __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 46 | static uint16_t RET_setting_impact_g __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 47 | static uint8_t RET_setting_impact_alert __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 48 | static uint16_t RET_setting_connection_timeout __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 49 | static uint16_t RET_setting_beacon_interval_seconds __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 50 | static uint16_t RET_setting_beacon_scan __attribute__((section("noinit"),zero_init)); |
pathfindr | 13:29f67f256709 | 51 | //STATE |
pathfindr | 16:3bf5f1a5f869 | 52 | static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 53 | static bool RET_haveSettings __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 54 | static uint8_t RET_state __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 55 | static uint8_t RET_state_prev __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 56 | static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 57 | static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 58 | static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 59 | static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 60 | static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 61 | static long RET_RTCmicros __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 62 | //MOTION STATE |
pathfindr | 16:3bf5f1a5f869 | 63 | static bool RET_motionTriggered __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 64 | static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 65 | static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 66 | static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 67 | static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 68 | static bool RET_motionState __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 69 | static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 70 | static char RET_activityData[100] __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 71 | //IMPACT |
pathfindr | 16:3bf5f1a5f869 | 72 | static bool RET_impactTriggered __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 73 | //EVENT HANDLING |
pathfindr | 16:3bf5f1a5f869 | 74 | static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 75 | static time_t RET_eventTime_location_tx __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 76 | static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 77 | static time_t RET_eventTime_environmental_tx __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 78 | static time_t RET_eventTime_activity_tx __attribute__((section("noinit"),zero_init)); |
pathfindr | 16:3bf5f1a5f869 | 79 | static time_t RET_eventTime_wakeFromDormant __attribute__((section("noinit"),zero_init)); |
pathfindr | 4:8d8e9bfa82e4 | 80 | #elif defined ( __GNUC__ ) |
pathfindr | 4:8d8e9bfa82e4 | 81 | #elif defined ( __ICCARM__ ) |
pathfindr | 4:8d8e9bfa82e4 | 82 | #endif |
pathfindr | 4:8d8e9bfa82e4 | 83 | |
pathfindr | 0:94b743e06998 | 84 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 85 | //GPIO |
pathfindr | 9:b0a1535b8ef2 | 86 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 87 | InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep |
pathfindr | 15:7aad9a7f970c | 88 | DigitalOut vreg_en(PN_VREG_EN); |
pathfindr | 9:b0a1535b8ef2 | 89 | |
pathfindr | 9:b0a1535b8ef2 | 90 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 91 | //PERIPHERALS |
pathfindr | 0:94b743e06998 | 92 | //------------------------------------------------------------------------------ |
pathfindr | 4:8d8e9bfa82e4 | 93 | //BLE myble; |
pathfindr | 12:8345612bf867 | 94 | extern WatchdogTimer watchdog(240.0); //Do not set to less than 4500ms or can cause issues with softdevice |
pathfindr | 11:60eb0ff945f2 | 95 | //SERIAL DEBUG? |
pathfindr | 11:60eb0ff945f2 | 96 | #if CONSOLE_DEBUG |
pathfindr | 11:60eb0ff945f2 | 97 | Serial uart(PN_UART_TX, PN_UART_RX, 115200); |
pathfindr | 4:8d8e9bfa82e4 | 98 | #endif |
pathfindr | 11:60eb0ff945f2 | 99 | |
pathfindr | 6:388d3c7efdd9 | 100 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 101 | //TIMERS |
pathfindr | 9:b0a1535b8ef2 | 102 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 103 | LowPowerTicker RTCticker; //no impact on power consumption |
pathfindr | 9:b0a1535b8ef2 | 104 | |
pathfindr | 9:b0a1535b8ef2 | 105 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 106 | //THREAD SEMAPHORES |
pathfindr | 9:b0a1535b8ef2 | 107 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 108 | Semaphore mainthread; |
pathfindr | 9:b0a1535b8ef2 | 109 | |
pathfindr | 9:b0a1535b8ef2 | 110 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 111 | //SINGLETONS |
pathfindr | 6:388d3c7efdd9 | 112 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 113 | //NVStore &nvstore = NVStore::get_instance(); |
pathfindr | 6:388d3c7efdd9 | 114 | |
pathfindr | 7:e9a19750700d | 115 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 116 | // LOW LEVEL FUNCS |
pathfindr | 7:e9a19750700d | 117 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 118 | void turnOffEverything() { |
pathfindr | 15:7aad9a7f970c | 119 | vreg_en = 0; |
pathfindr | 9:b0a1535b8ef2 | 120 | LED1off(); |
pathfindr | 9:b0a1535b8ef2 | 121 | } |
pathfindr | 4:8d8e9bfa82e4 | 122 | void gotoSleep(long sleep_milliseconds) { |
pathfindr | 9:b0a1535b8ef2 | 123 | turnOffEverything(); |
pathfindr | 9:b0a1535b8ef2 | 124 | watchdog.kick(); |
pathfindr | 13:29f67f256709 | 125 | if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false |
pathfindr | 4:8d8e9bfa82e4 | 126 | system_reset(); |
pathfindr | 4:8d8e9bfa82e4 | 127 | } |
pathfindr | 9:b0a1535b8ef2 | 128 | mainthread.wait(sleep_milliseconds); |
pathfindr | 6:388d3c7efdd9 | 129 | } |
pathfindr | 9:b0a1535b8ef2 | 130 | void RTCtick() { |
pathfindr | 9:b0a1535b8ef2 | 131 | RET_RTCmicros += 100; |
pathfindr | 9:b0a1535b8ef2 | 132 | RET_RTCunixtime = (RET_RTCmicros / 1000); |
pathfindr | 13:29f67f256709 | 133 | GLOBAL_RTCunixtime = RET_RTCunixtime; |
pathfindr | 9:b0a1535b8ef2 | 134 | //button logic - check for hold |
pathfindr | 9:b0a1535b8ef2 | 135 | if (RET_buttonHoldTime > 4000) { |
pathfindr | 9:b0a1535b8ef2 | 136 | RET_buttonHoldTime = 0; |
pathfindr | 9:b0a1535b8ef2 | 137 | RET_buttonPressCount = 0; |
pathfindr | 16:3bf5f1a5f869 | 138 | RET_state_prev = RET_state; |
pathfindr | 16:3bf5f1a5f869 | 139 | RET_state = STATE_BUTTONHOLD; |
pathfindr | 9:b0a1535b8ef2 | 140 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 141 | } else { |
pathfindr | 9:b0a1535b8ef2 | 142 | if((RET_RTCmicros - RET_buttonPressTime) > 500) { |
pathfindr | 9:b0a1535b8ef2 | 143 | switch (RET_buttonPressCount) { |
pathfindr | 9:b0a1535b8ef2 | 144 | case 1 : |
pathfindr | 16:3bf5f1a5f869 | 145 | RET_state_prev = RET_state; |
pathfindr | 16:3bf5f1a5f869 | 146 | RET_state = STATE_BUTTONPRESS1; |
pathfindr | 9:b0a1535b8ef2 | 147 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 148 | break; |
pathfindr | 9:b0a1535b8ef2 | 149 | case 2 : |
pathfindr | 16:3bf5f1a5f869 | 150 | RET_state_prev = RET_state; |
pathfindr | 16:3bf5f1a5f869 | 151 | RET_state = STATE_BUTTONPRESS2; |
pathfindr | 9:b0a1535b8ef2 | 152 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 153 | break; |
pathfindr | 9:b0a1535b8ef2 | 154 | default : |
pathfindr | 9:b0a1535b8ef2 | 155 | //do nothing |
pathfindr | 9:b0a1535b8ef2 | 156 | break; |
pathfindr | 9:b0a1535b8ef2 | 157 | } |
pathfindr | 9:b0a1535b8ef2 | 158 | RET_buttonPressCount = 0; |
pathfindr | 9:b0a1535b8ef2 | 159 | } |
pathfindr | 9:b0a1535b8ef2 | 160 | } |
pathfindr | 9:b0a1535b8ef2 | 161 | } |
pathfindr | 7:e9a19750700d | 162 | void factoryReset() { |
pathfindr | 13:29f67f256709 | 163 | DEBUG("Factory Reset \n"); |
pathfindr | 15:7aad9a7f970c | 164 | Thread::wait(500); //wait to ensure that devices are turned off |
pathfindr | 16:3bf5f1a5f869 | 165 | //IDENTITY |
pathfindr | 13:29f67f256709 | 166 | RET_imei = 0; |
pathfindr | 16:3bf5f1a5f869 | 167 | memset(RET_pf_identifier,0,sizeof(RET_pf_identifier)); |
pathfindr | 16:3bf5f1a5f869 | 168 | //SETTINGS |
pathfindr | 16:3bf5f1a5f869 | 169 | RET_setting_firmware = 0; |
pathfindr | 16:3bf5f1a5f869 | 170 | RET_setting_location_accuracy = 1; |
pathfindr | 16:3bf5f1a5f869 | 171 | RET_setting_location_tx_interval_mins = 1440; |
pathfindr | 16:3bf5f1a5f869 | 172 | RET_setting_location_tx_failsafe_hrs = (7*24); //7 days |
pathfindr | 16:3bf5f1a5f869 | 173 | RET_setting_location_timeout = 180; |
pathfindr | 16:3bf5f1a5f869 | 174 | RET_setting_activity_tx_interval_mins = 0; |
pathfindr | 16:3bf5f1a5f869 | 175 | RET_setting_environmental_tx_interval_mins = 0; |
pathfindr | 16:3bf5f1a5f869 | 176 | RET_setting_motion_g = 11; |
pathfindr | 16:3bf5f1a5f869 | 177 | RET_setting_motion_start_seconds = 120; |
pathfindr | 16:3bf5f1a5f869 | 178 | RET_setting_motion_stop_seconds = 120; |
pathfindr | 16:3bf5f1a5f869 | 179 | RET_setting_impact_g = 0; |
pathfindr | 16:3bf5f1a5f869 | 180 | RET_setting_impact_alert = 0; |
pathfindr | 16:3bf5f1a5f869 | 181 | RET_setting_connection_timeout = 180; |
pathfindr | 16:3bf5f1a5f869 | 182 | RET_setting_beacon_interval_seconds = 10; |
pathfindr | 16:3bf5f1a5f869 | 183 | RET_setting_beacon_scan = 0; |
pathfindr | 16:3bf5f1a5f869 | 184 | //STATE |
pathfindr | 9:b0a1535b8ef2 | 185 | RET_coldBoot = 1; |
pathfindr | 15:7aad9a7f970c | 186 | RET_haveSettings = 0; |
pathfindr | 16:3bf5f1a5f869 | 187 | RET_state = STATE_SETUP; |
pathfindr | 16:3bf5f1a5f869 | 188 | RET_state_prev = RET_state; |
pathfindr | 9:b0a1535b8ef2 | 189 | RET_RTCunixtime = 0; |
pathfindr | 9:b0a1535b8ef2 | 190 | RET_RTCmicros = 0; |
pathfindr | 16:3bf5f1a5f869 | 191 | RET_buttonPressCount = 0; |
pathfindr | 16:3bf5f1a5f869 | 192 | RET_buttonPressTime = 0; |
pathfindr | 16:3bf5f1a5f869 | 193 | RET_buttonReleaseTime = 0; |
pathfindr | 16:3bf5f1a5f869 | 194 | RET_buttonHoldTime = 0; |
pathfindr | 16:3bf5f1a5f869 | 195 | RET_RTCunixtime = 0; |
pathfindr | 16:3bf5f1a5f869 | 196 | RET_RTCmicros = 0; |
pathfindr | 16:3bf5f1a5f869 | 197 | //MOTION STATE |
pathfindr | 10:c8798fd9773b | 198 | RET_motionTriggered = 0; |
pathfindr | 7:e9a19750700d | 199 | RET_motionStartTime = 0; |
pathfindr | 7:e9a19750700d | 200 | RET_motionStopTime = 0; |
pathfindr | 16:3bf5f1a5f869 | 201 | RET_motionPendingOnState = 0; |
pathfindr | 16:3bf5f1a5f869 | 202 | RET_motionPendingOffState = 0; |
pathfindr | 16:3bf5f1a5f869 | 203 | RET_motionState = 0; |
pathfindr | 9:b0a1535b8ef2 | 204 | RET_motionTotalActivityHours = 0.0; |
pathfindr | 16:3bf5f1a5f869 | 205 | memset(RET_activityData,0,sizeof(RET_activityData)); |
pathfindr | 16:3bf5f1a5f869 | 206 | //IMPACT |
pathfindr | 16:3bf5f1a5f869 | 207 | RET_impactTriggered = 0; |
pathfindr | 16:3bf5f1a5f869 | 208 | //EVENT HANDLING |
pathfindr | 16:3bf5f1a5f869 | 209 | RET_eventTime_location_log = 0; |
pathfindr | 16:3bf5f1a5f869 | 210 | RET_eventTime_location_tx = 0; |
pathfindr | 16:3bf5f1a5f869 | 211 | RET_eventTime_environmental_log = 0; |
pathfindr | 16:3bf5f1a5f869 | 212 | RET_eventTime_environmental_tx = 0; |
pathfindr | 16:3bf5f1a5f869 | 213 | RET_eventTime_activity_tx = 0; |
pathfindr | 16:3bf5f1a5f869 | 214 | RET_eventTime_wakeFromDormant = 0; |
pathfindr | 16:3bf5f1a5f869 | 215 | //OTHER LOCAL GLOBALS |
pathfindr | 16:3bf5f1a5f869 | 216 | GLOBAL_requireSoftReset = false; |
pathfindr | 16:3bf5f1a5f869 | 217 | //PERIPHERAL RESET |
pathfindr | 10:c8798fd9773b | 218 | lis3dh_configureForSleep(10,127); |
pathfindr | 13:29f67f256709 | 219 | } |
pathfindr | 13:29f67f256709 | 220 | void copyRETtoGLOBAL() { |
pathfindr | 15:7aad9a7f970c | 221 | GLOBAL_imei = RET_imei; |
pathfindr | 7:e9a19750700d | 222 | } |
pathfindr | 9:b0a1535b8ef2 | 223 | void selfTest() { |
pathfindr | 8:7351f8c4cd60 | 224 | //Accelerometer |
pathfindr | 8:7351f8c4cd60 | 225 | LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK); |
pathfindr | 8:7351f8c4cd60 | 226 | uint8_t lis3dh_id; |
pathfindr | 8:7351f8c4cd60 | 227 | lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id); |
pathfindr | 9:b0a1535b8ef2 | 228 | if (lis3dh_id == 51) { |
pathfindr | 9:b0a1535b8ef2 | 229 | LED1blink(10,50); |
pathfindr | 9:b0a1535b8ef2 | 230 | } |
pathfindr | 9:b0a1535b8ef2 | 231 | } |
pathfindr | 9:b0a1535b8ef2 | 232 | |
pathfindr | 9:b0a1535b8ef2 | 233 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 234 | // USER BUTTON HANDLING |
pathfindr | 9:b0a1535b8ef2 | 235 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 236 | void buttonPress() { |
pathfindr | 9:b0a1535b8ef2 | 237 | RET_buttonPressTime = RET_RTCmicros; |
pathfindr | 9:b0a1535b8ef2 | 238 | } |
pathfindr | 9:b0a1535b8ef2 | 239 | void buttonRelease() { |
pathfindr | 9:b0a1535b8ef2 | 240 | RET_buttonHoldTime = (RET_RTCmicros - RET_buttonPressTime); |
pathfindr | 9:b0a1535b8ef2 | 241 | //debounce catch |
pathfindr | 9:b0a1535b8ef2 | 242 | if((RET_RTCmicros - RET_buttonPressTime) >= 100) { |
pathfindr | 9:b0a1535b8ef2 | 243 | RET_buttonPressCount ++; |
pathfindr | 9:b0a1535b8ef2 | 244 | } |
pathfindr | 9:b0a1535b8ef2 | 245 | } |
pathfindr | 9:b0a1535b8ef2 | 246 | |
pathfindr | 9:b0a1535b8ef2 | 247 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 248 | // MOTION FUNCS |
pathfindr | 9:b0a1535b8ef2 | 249 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 250 | void checkMotion() { |
pathfindr | 9:b0a1535b8ef2 | 251 | if (lis3dh_int2) { |
pathfindr | 13:29f67f256709 | 252 | if (GLOBAL_debugLED) LED1blink(2,100); |
pathfindr | 9:b0a1535b8ef2 | 253 | RET_motionTriggered = true; |
pathfindr | 13:29f67f256709 | 254 | GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it |
pathfindr | 9:b0a1535b8ef2 | 255 | if (!RET_motionPendingOnState) { |
pathfindr | 9:b0a1535b8ef2 | 256 | RET_motionPendingOnState = true; |
pathfindr | 9:b0a1535b8ef2 | 257 | RET_motionPendingOffState = false; |
pathfindr | 9:b0a1535b8ef2 | 258 | // Log start motion time |
pathfindr | 13:29f67f256709 | 259 | RET_motionStartTime = RET_RTCunixtime; |
pathfindr | 9:b0a1535b8ef2 | 260 | } |
pathfindr | 9:b0a1535b8ef2 | 261 | } else { |
pathfindr | 13:29f67f256709 | 262 | if (GLOBAL_debugLED) LED1blink(2,500); |
pathfindr | 9:b0a1535b8ef2 | 263 | RET_motionTriggered = false; |
pathfindr | 9:b0a1535b8ef2 | 264 | RET_motionPendingOnState = false; |
pathfindr | 9:b0a1535b8ef2 | 265 | if (!RET_motionPendingOffState) { |
pathfindr | 9:b0a1535b8ef2 | 266 | RET_motionPendingOffState = true; |
pathfindr | 9:b0a1535b8ef2 | 267 | //log stop motion time |
pathfindr | 13:29f67f256709 | 268 | RET_motionStopTime = RET_RTCunixtime; |
pathfindr | 9:b0a1535b8ef2 | 269 | } |
pathfindr | 9:b0a1535b8ef2 | 270 | } |
pathfindr | 9:b0a1535b8ef2 | 271 | //calculate motion state |
pathfindr | 9:b0a1535b8ef2 | 272 | if (RET_motionPendingOnState) { |
pathfindr | 9:b0a1535b8ef2 | 273 | //check if above threshold |
pathfindr | 13:29f67f256709 | 274 | time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime); |
pathfindr | 16:3bf5f1a5f869 | 275 | if (inMotionForSeconds >= RET_setting_motion_start_seconds) { |
pathfindr | 9:b0a1535b8ef2 | 276 | RET_motionState = true; |
pathfindr | 13:29f67f256709 | 277 | if (GLOBAL_debugLED) LED1blink(10,100); |
pathfindr | 9:b0a1535b8ef2 | 278 | } |
pathfindr | 9:b0a1535b8ef2 | 279 | } |
pathfindr | 9:b0a1535b8ef2 | 280 | if (RET_motionPendingOffState) { |
pathfindr | 13:29f67f256709 | 281 | time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime); |
pathfindr | 16:3bf5f1a5f869 | 282 | if (noMotionForSeconds >= RET_setting_motion_stop_seconds) { |
pathfindr | 9:b0a1535b8ef2 | 283 | RET_motionState = false; |
pathfindr | 9:b0a1535b8ef2 | 284 | RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); |
pathfindr | 13:29f67f256709 | 285 | if (GLOBAL_debugLED) LED1blink(5,500); |
pathfindr | 9:b0a1535b8ef2 | 286 | } |
pathfindr | 9:b0a1535b8ef2 | 287 | } |
pathfindr | 8:7351f8c4cd60 | 288 | } |
pathfindr | 8:7351f8c4cd60 | 289 | |
pathfindr | 14:9a54b1b65bc8 | 290 | //------------------------------------------------------------------------------ |
pathfindr | 14:9a54b1b65bc8 | 291 | // UPDATE OPERATING SETTINGS |
pathfindr | 14:9a54b1b65bc8 | 292 | //------------------------------------------------------------------------------ |
pathfindr | 14:9a54b1b65bc8 | 293 | bool saveSettings(char* settingsBuffer) { |
pathfindr | 14:9a54b1b65bc8 | 294 | //process result |
pathfindr | 14:9a54b1b65bc8 | 295 | int matchCount = 0; |
pathfindr | 15:7aad9a7f970c | 296 | char TEMP_a[6]; |
pathfindr | 14:9a54b1b65bc8 | 297 | int TEMP_b = 0; |
pathfindr | 14:9a54b1b65bc8 | 298 | int TEMP_c = 0; |
pathfindr | 14:9a54b1b65bc8 | 299 | int TEMP_d = 0; |
pathfindr | 14:9a54b1b65bc8 | 300 | int TEMP_e = 0; |
pathfindr | 14:9a54b1b65bc8 | 301 | int TEMP_f = 0; |
pathfindr | 14:9a54b1b65bc8 | 302 | int TEMP_g = 0; |
pathfindr | 14:9a54b1b65bc8 | 303 | int TEMP_h = 0; |
pathfindr | 14:9a54b1b65bc8 | 304 | int TEMP_i = 0; |
pathfindr | 14:9a54b1b65bc8 | 305 | int TEMP_j = 0; |
pathfindr | 14:9a54b1b65bc8 | 306 | int TEMP_k = 0; |
pathfindr | 14:9a54b1b65bc8 | 307 | int TEMP_l = 0; |
pathfindr | 14:9a54b1b65bc8 | 308 | int TEMP_m = 0; |
pathfindr | 14:9a54b1b65bc8 | 309 | int TEMP_n = 0; |
pathfindr | 15:7aad9a7f970c | 310 | time_t TEMP_o = 0; |
pathfindr | 15:7aad9a7f970c | 311 | if ( (matchCount = sscanf(settingsBuffer,"a:%c%c%c%c%c%c,b:%d,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,0:%u", |
pathfindr | 15:7aad9a7f970c | 312 | &TEMP_a[0],&TEMP_a[1],&TEMP_a[2],&TEMP_a[3],&TEMP_a[4],&TEMP_a[5],&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) ) > 0 ) { |
pathfindr | 15:7aad9a7f970c | 313 | DEBUG("VALUES: a:%s,b:%d,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\n",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); |
pathfindr | 15:7aad9a7f970c | 314 | RET_haveSettings = true; |
pathfindr | 14:9a54b1b65bc8 | 315 | return true; |
pathfindr | 14:9a54b1b65bc8 | 316 | } else { |
pathfindr | 15:7aad9a7f970c | 317 | RET_haveSettings = false; |
pathfindr | 15:7aad9a7f970c | 318 | return false; |
pathfindr | 14:9a54b1b65bc8 | 319 | } |
pathfindr | 14:9a54b1b65bc8 | 320 | } |
pathfindr | 14:9a54b1b65bc8 | 321 | |
pathfindr | 13:29f67f256709 | 322 | |
pathfindr | 13:29f67f256709 | 323 | //------------------------------------------------------------------------------ |
pathfindr | 13:29f67f256709 | 324 | // SETUP |
pathfindr | 13:29f67f256709 | 325 | //------------------------------------------------------------------------------ |
pathfindr | 13:29f67f256709 | 326 | bool setup() { |
pathfindr | 15:7aad9a7f970c | 327 | DEBUG("SETUP\n"); |
pathfindr | 13:29f67f256709 | 328 | bool pass = true; |
pathfindr | 13:29f67f256709 | 329 | Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN); |
pathfindr | 13:29f67f256709 | 330 | if (modem.on()) { |
pathfindr | 13:29f67f256709 | 331 | RET_imei = modem.getIMEI(); |
pathfindr | 14:9a54b1b65bc8 | 332 | GLOBAL_imei = RET_imei; |
pathfindr | 13:29f67f256709 | 333 | DEBUG("imei: %lld \n",RET_imei); |
pathfindr | 13:29f67f256709 | 334 | if (modem.registerOnNetwork(3,60000)) { |
pathfindr | 16:3bf5f1a5f869 | 335 | char* bytestosend; |
pathfindr | 16:3bf5f1a5f869 | 336 | sprintf(bytestosend,"(im:%lld,%s,v:%.2f,fr:1,z:SETUP,c:1)\0",GLOBAL_imei,GLOBAL_defaultApi,GLOBAL_voltage); |
pathfindr | 16:3bf5f1a5f869 | 337 | char* result = modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi); |
pathfindr | 16:3bf5f1a5f869 | 338 | if (result != "err") { |
pathfindr | 16:3bf5f1a5f869 | 339 | DEBUG("result: %s \n",result); |
pathfindr | 16:3bf5f1a5f869 | 340 | saveSettings(result); |
pathfindr | 16:3bf5f1a5f869 | 341 | } |
pathfindr | 13:29f67f256709 | 342 | } else { |
pathfindr | 16:3bf5f1a5f869 | 343 | //FAILUREMODE modem failed to register on network |
pathfindr | 13:29f67f256709 | 344 | } |
pathfindr | 13:29f67f256709 | 345 | } else { |
pathfindr | 13:29f67f256709 | 346 | pass = false; |
pathfindr | 16:3bf5f1a5f869 | 347 | //FAILUREMODE Modem failed to turn on |
pathfindr | 13:29f67f256709 | 348 | } |
pathfindr | 13:29f67f256709 | 349 | |
pathfindr | 16:3bf5f1a5f869 | 350 | //while (1) { |
pathfindr | 16:3bf5f1a5f869 | 351 | // Thread::wait(60000); |
pathfindr | 16:3bf5f1a5f869 | 352 | // watchdog.kick(); |
pathfindr | 16:3bf5f1a5f869 | 353 | //} |
pathfindr | 13:29f67f256709 | 354 | |
pathfindr | 13:29f67f256709 | 355 | //RESULT |
pathfindr | 13:29f67f256709 | 356 | if (pass) { |
pathfindr | 13:29f67f256709 | 357 | return true; |
pathfindr | 13:29f67f256709 | 358 | } else { |
pathfindr | 13:29f67f256709 | 359 | return false; |
pathfindr | 13:29f67f256709 | 360 | } |
pathfindr | 13:29f67f256709 | 361 | } |
pathfindr | 13:29f67f256709 | 362 | |
pathfindr | 13:29f67f256709 | 363 | |
pathfindr | 7:e9a19750700d | 364 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 365 | // STATE ENGINE |
pathfindr | 7:e9a19750700d | 366 | //------------------------------------------------------------------------------ |
pathfindr | 6:388d3c7efdd9 | 367 | void mainStateEngine() { |
pathfindr | 16:3bf5f1a5f869 | 368 | DEBUG("StateEngine:%d \n",RET_state); |
pathfindr | 16:3bf5f1a5f869 | 369 | switch(RET_state) { |
pathfindr | 16:3bf5f1a5f869 | 370 | case STATE_SETUP : |
pathfindr | 7:e9a19750700d | 371 | factoryReset(); |
pathfindr | 13:29f67f256709 | 372 | if (setup()) { |
pathfindr | 14:9a54b1b65bc8 | 373 | copyRETtoGLOBAL(); |
pathfindr | 16:3bf5f1a5f869 | 374 | RET_state = STATE_NORMAL; |
pathfindr | 13:29f67f256709 | 375 | } |
pathfindr | 6:388d3c7efdd9 | 376 | break; |
pathfindr | 16:3bf5f1a5f869 | 377 | case STATE_NORMAL : |
pathfindr | 7:e9a19750700d | 378 | //check and log motion |
pathfindr | 9:b0a1535b8ef2 | 379 | checkMotion(); |
pathfindr | 9:b0a1535b8ef2 | 380 | |
pathfindr | 9:b0a1535b8ef2 | 381 | //EVENTS |
pathfindr | 9:b0a1535b8ef2 | 382 | //Location |
pathfindr | 12:8345612bf867 | 383 | if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) { |
pathfindr | 9:b0a1535b8ef2 | 384 | //getdata_environmental(); |
pathfindr | 9:b0a1535b8ef2 | 385 | //event_location_log(); |
pathfindr | 9:b0a1535b8ef2 | 386 | } |
pathfindr | 16:3bf5f1a5f869 | 387 | if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { |
pathfindr | 9:b0a1535b8ef2 | 388 | //getdata_battery(); |
pathfindr | 9:b0a1535b8ef2 | 389 | //event_location_broadcast(); |
pathfindr | 9:b0a1535b8ef2 | 390 | } |
pathfindr | 9:b0a1535b8ef2 | 391 | //Environmental |
pathfindr | 12:8345612bf867 | 392 | if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) { |
pathfindr | 14:9a54b1b65bc8 | 393 | Filesystem filesystem(BD_PAGE_ADDRESS,BD_TOTAL_SIZE); |
pathfindr | 12:8345612bf867 | 394 | SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL); |
pathfindr | 12:8345612bf867 | 395 | float temperature_c = temperature.getTemperature(); |
pathfindr | 12:8345612bf867 | 396 | DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c); |
pathfindr | 12:8345612bf867 | 397 | filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c); |
pathfindr | 9:b0a1535b8ef2 | 398 | } |
pathfindr | 16:3bf5f1a5f869 | 399 | if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) { |
pathfindr | 9:b0a1535b8ef2 | 400 | //event_environmental_bc(); |
pathfindr | 12:8345612bf867 | 401 | //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1); |
pathfindr | 7:e9a19750700d | 402 | } |
pathfindr | 9:b0a1535b8ef2 | 403 | //Activity |
pathfindr | 16:3bf5f1a5f869 | 404 | if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) { |
pathfindr | 9:b0a1535b8ef2 | 405 | //event_activity_bc(); |
pathfindr | 9:b0a1535b8ef2 | 406 | } |
pathfindr | 9:b0a1535b8ef2 | 407 | break; |
pathfindr | 16:3bf5f1a5f869 | 408 | case STATE_DORMANT : |
pathfindr | 12:8345612bf867 | 409 | if (RET_RTCunixtime > RET_eventTime_wakeFromDormant) { |
pathfindr | 16:3bf5f1a5f869 | 410 | RET_state = STATE_NORMAL; |
pathfindr | 7:e9a19750700d | 411 | } |
pathfindr | 12:8345612bf867 | 412 | break; |
pathfindr | 16:3bf5f1a5f869 | 413 | case STATE_BUTTONPRESS1 : |
pathfindr | 12:8345612bf867 | 414 | LED1blink(1,300); |
pathfindr | 16:3bf5f1a5f869 | 415 | RET_state = RET_state_prev; |
pathfindr | 9:b0a1535b8ef2 | 416 | break; |
pathfindr | 16:3bf5f1a5f869 | 417 | case STATE_BUTTONPRESS2 : |
pathfindr | 12:8345612bf867 | 418 | LED1blink(3,300); |
pathfindr | 16:3bf5f1a5f869 | 419 | RET_state = RET_state_prev; |
pathfindr | 9:b0a1535b8ef2 | 420 | break; |
pathfindr | 16:3bf5f1a5f869 | 421 | case STATE_BUTTONHOLD : |
pathfindr | 9:b0a1535b8ef2 | 422 | LED1blink(10,300); |
pathfindr | 12:8345612bf867 | 423 | RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs |
pathfindr | 16:3bf5f1a5f869 | 424 | RET_state = STATE_DORMANT; |
pathfindr | 6:388d3c7efdd9 | 425 | break; |
pathfindr | 6:388d3c7efdd9 | 426 | default : |
pathfindr | 16:3bf5f1a5f869 | 427 | RET_state = STATE_SETUP; |
pathfindr | 6:388d3c7efdd9 | 428 | } |
pathfindr | 6:388d3c7efdd9 | 429 | } |
pathfindr | 6:388d3c7efdd9 | 430 | |
pathfindr | 6:388d3c7efdd9 | 431 | |
pathfindr | 7:e9a19750700d | 432 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 433 | // MAIN |
pathfindr | 7:e9a19750700d | 434 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 435 | int main() { |
pathfindr | 9:b0a1535b8ef2 | 436 | RTCticker.attach(&RTCtick, 0.1); |
pathfindr | 9:b0a1535b8ef2 | 437 | turnOffEverything(); |
pathfindr | 4:8d8e9bfa82e4 | 438 | |
pathfindr | 9:b0a1535b8ef2 | 439 | button.fall(&buttonPress); //does this affect power? |
pathfindr | 9:b0a1535b8ef2 | 440 | button.rise(&buttonRelease); |
pathfindr | 9:b0a1535b8ef2 | 441 | |
pathfindr | 4:8d8e9bfa82e4 | 442 | //CHECK IF THIS IS RESET |
pathfindr | 4:8d8e9bfa82e4 | 443 | //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset |
pathfindr | 4:8d8e9bfa82e4 | 444 | if (NRF_POWER->RESETREAS != 0xffffffff) { |
pathfindr | 4:8d8e9bfa82e4 | 445 | switch(NRF_POWER->RESETREAS) { |
pathfindr | 4:8d8e9bfa82e4 | 446 | case 0x00000001 : |
pathfindr | 4:8d8e9bfa82e4 | 447 | DEBUG("reset_reason: 0x%08x. - Hard Reset\n",NRF_POWER->RESETREAS); |
pathfindr | 13:29f67f256709 | 448 | RET_coldBoot = 1; |
pathfindr | 4:8d8e9bfa82e4 | 449 | break; |
pathfindr | 4:8d8e9bfa82e4 | 450 | case 0x00000002 : |
pathfindr | 4:8d8e9bfa82e4 | 451 | DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS); |
pathfindr | 4:8d8e9bfa82e4 | 452 | break; |
pathfindr | 4:8d8e9bfa82e4 | 453 | case 0x00000004 : |
pathfindr | 4:8d8e9bfa82e4 | 454 | DEBUG("reset_reason: 0x%08x. - Soft reset\n",NRF_POWER->RESETREAS); |
pathfindr | 4:8d8e9bfa82e4 | 455 | break; |
pathfindr | 4:8d8e9bfa82e4 | 456 | } |
pathfindr | 4:8d8e9bfa82e4 | 457 | NRF_POWER->RESETREAS = 0xffffffff; |
pathfindr | 4:8d8e9bfa82e4 | 458 | } |
pathfindr | 9:b0a1535b8ef2 | 459 | //CHECK FOR FIRST BOOT |
pathfindr | 15:7aad9a7f970c | 460 | if (RET_coldBoot == 1) factoryReset(); |
pathfindr | 9:b0a1535b8ef2 | 461 | |
pathfindr | 14:9a54b1b65bc8 | 462 | //COPY ESSENTIAL VALUES FROM RET TO GLOBAL |
pathfindr | 13:29f67f256709 | 463 | copyRETtoGLOBAL(); |
pathfindr | 13:29f67f256709 | 464 | |
pathfindr | 0:94b743e06998 | 465 | while(true) { |
pathfindr | 9:b0a1535b8ef2 | 466 | //SLEEP |
pathfindr | 13:29f67f256709 | 467 | if (RET_coldBoot != 1) gotoSleep(10000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP |
pathfindr | 4:8d8e9bfa82e4 | 468 | watchdog.kick(); |
pathfindr | 9:b0a1535b8ef2 | 469 | |
pathfindr | 13:29f67f256709 | 470 | //LOG START TIME |
pathfindr | 13:29f67f256709 | 471 | GLOBAL_wakeTime = RET_RTCunixtime; |
pathfindr | 11:60eb0ff945f2 | 472 | |
pathfindr | 9:b0a1535b8ef2 | 473 | //MAIN LOGIC |
pathfindr | 16:3bf5f1a5f869 | 474 | DEBUG("State: %i time: %i, %i, %i, %i \n", RET_state, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount); |
pathfindr | 9:b0a1535b8ef2 | 475 | mainStateEngine(); |
pathfindr | 9:b0a1535b8ef2 | 476 | |
pathfindr | 9:b0a1535b8ef2 | 477 | //PRE-SLEEP ACTIONS |
pathfindr | 13:29f67f256709 | 478 | if (GLOBAL_needToConfigureLis3dh) { //ONLY RESET ACCELL IF WE NEED TO |
pathfindr | 10:c8798fd9773b | 479 | lis3dh_configureForSleep(10,127); |
pathfindr | 10:c8798fd9773b | 480 | } |
pathfindr | 9:b0a1535b8ef2 | 481 | RET_coldBoot = 0; |
pathfindr | 0:94b743e06998 | 482 | } |
pathfindr | 7:e9a19750700d | 483 | } |