init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
main.cpp@11:60eb0ff945f2, 2018-12-15 (annotated)
- Committer:
- pathfindr
- Date:
- Sat Dec 15 20:09:19 2018 +0000
- Revision:
- 11:60eb0ff945f2
- Parent:
- 10:c8798fd9773b
- Child:
- 12:8345612bf867
3
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 | 0:94b743e06998 | 15 | bool accel_healthy = false; |
pathfindr | 4:8d8e9bfa82e4 | 16 | bool requireSoftReset = false; |
pathfindr | 7:e9a19750700d | 17 | bool motionFlagTriggered = false; |
pathfindr | 9:b0a1535b8ef2 | 18 | bool debugLED = false; |
pathfindr | 10:c8798fd9773b | 19 | bool needToConfigureLis3dh = false; |
pathfindr | 0:94b743e06998 | 20 | |
pathfindr | 0:94b743e06998 | 21 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 22 | //RETAINED NOINIT RAM VARS |
pathfindr | 0:94b743e06998 | 23 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 24 | #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ |
pathfindr | 9:b0a1535b8ef2 | 25 | static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 26 | static uint8_t RET_mode __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 27 | static uint8_t RET_mode_prev __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 28 | static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 29 | static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 30 | static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 31 | static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 32 | static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 33 | static long RET_RTCmicros __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 34 | static bool RET_requireImpactFlag __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 35 | static char RET_buffer[64] __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 36 | |
pathfindr | 7:e9a19750700d | 37 | //MOTION SETTINGS |
pathfindr | 10:c8798fd9773b | 38 | static bool RET_impactTriggered __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 39 | static bool RET_motionTriggered __attribute__((section("noinit"),zero_init)); |
pathfindr | 8:7351f8c4cd60 | 40 | static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 8:7351f8c4cd60 | 41 | static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 42 | static time_t RET_motionStartThreshold_seconds __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 43 | static time_t RET_motionStopThreshold_seconds __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 44 | static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 45 | static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 46 | static bool RET_motionState __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 47 | static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 48 | static char RET_motionData[100] __attribute__((section("noinit"),zero_init)); |
pathfindr | 7:e9a19750700d | 49 | |
pathfindr | 7:e9a19750700d | 50 | //EVENT HANDLING |
pathfindr | 9:b0a1535b8ef2 | 51 | static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 52 | static time_t RET_eventTime_location_bc __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 53 | static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 54 | static time_t RET_eventTime_environmental_bc __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 55 | static time_t RET_eventTime_activity_bc __attribute__((section("noinit"),zero_init)); |
pathfindr | 9:b0a1535b8ef2 | 56 | static time_t RET_eventTime_wakeFromDormant __attribute__((section("noinit"),zero_init)); |
pathfindr | 4:8d8e9bfa82e4 | 57 | #elif defined ( __GNUC__ ) |
pathfindr | 4:8d8e9bfa82e4 | 58 | #elif defined ( __ICCARM__ ) |
pathfindr | 4:8d8e9bfa82e4 | 59 | #endif |
pathfindr | 4:8d8e9bfa82e4 | 60 | |
pathfindr | 0:94b743e06998 | 61 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 62 | //GPIO |
pathfindr | 9:b0a1535b8ef2 | 63 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 64 | InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep |
pathfindr | 9:b0a1535b8ef2 | 65 | |
pathfindr | 9:b0a1535b8ef2 | 66 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 67 | //PERIPHERALS |
pathfindr | 0:94b743e06998 | 68 | //------------------------------------------------------------------------------ |
pathfindr | 4:8d8e9bfa82e4 | 69 | //BLE myble; |
pathfindr | 7:e9a19750700d | 70 | WatchdogTimer watchdog(65.0); //Do not set to less than 4500ms or can cause issues with softdevice |
pathfindr | 11:60eb0ff945f2 | 71 | //SERIAL DEBUG? |
pathfindr | 11:60eb0ff945f2 | 72 | #if CONSOLE_DEBUG |
pathfindr | 11:60eb0ff945f2 | 73 | Serial uart(PN_UART_TX, PN_UART_RX, 115200); |
pathfindr | 4:8d8e9bfa82e4 | 74 | #endif |
pathfindr | 11:60eb0ff945f2 | 75 | |
pathfindr | 6:388d3c7efdd9 | 76 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 77 | //TIMERS |
pathfindr | 9:b0a1535b8ef2 | 78 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 79 | LowPowerTicker RTCticker; //no impact on power consumption |
pathfindr | 9:b0a1535b8ef2 | 80 | |
pathfindr | 9:b0a1535b8ef2 | 81 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 82 | //THREAD SEMAPHORES |
pathfindr | 9:b0a1535b8ef2 | 83 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 84 | Semaphore mainthread; |
pathfindr | 9:b0a1535b8ef2 | 85 | |
pathfindr | 9:b0a1535b8ef2 | 86 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 87 | //SINGLETONS |
pathfindr | 6:388d3c7efdd9 | 88 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 89 | //NVStore &nvstore = NVStore::get_instance(); |
pathfindr | 6:388d3c7efdd9 | 90 | |
pathfindr | 7:e9a19750700d | 91 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 92 | // LOW LEVEL FUNCS |
pathfindr | 7:e9a19750700d | 93 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 94 | void turnOffEverything() { |
pathfindr | 9:b0a1535b8ef2 | 95 | //vreg_en = 0; |
pathfindr | 9:b0a1535b8ef2 | 96 | LED1off(); |
pathfindr | 9:b0a1535b8ef2 | 97 | } |
pathfindr | 4:8d8e9bfa82e4 | 98 | void gotoSleep(long sleep_milliseconds) { |
pathfindr | 9:b0a1535b8ef2 | 99 | turnOffEverything(); |
pathfindr | 9:b0a1535b8ef2 | 100 | watchdog.kick(); |
pathfindr | 4:8d8e9bfa82e4 | 101 | if (requireSoftReset) { //dont need to clear this var as reset changes it back to false |
pathfindr | 4:8d8e9bfa82e4 | 102 | system_reset(); |
pathfindr | 4:8d8e9bfa82e4 | 103 | } |
pathfindr | 9:b0a1535b8ef2 | 104 | mainthread.wait(sleep_milliseconds); |
pathfindr | 6:388d3c7efdd9 | 105 | } |
pathfindr | 9:b0a1535b8ef2 | 106 | void RTCtick() { |
pathfindr | 9:b0a1535b8ef2 | 107 | //RET_unixtime ++; |
pathfindr | 9:b0a1535b8ef2 | 108 | RET_RTCmicros += 100; |
pathfindr | 9:b0a1535b8ef2 | 109 | RET_RTCunixtime = (RET_RTCmicros / 1000); |
pathfindr | 9:b0a1535b8ef2 | 110 | //button logic - check for hold |
pathfindr | 9:b0a1535b8ef2 | 111 | if (RET_buttonHoldTime > 4000) { |
pathfindr | 9:b0a1535b8ef2 | 112 | RET_buttonHoldTime = 0; |
pathfindr | 9:b0a1535b8ef2 | 113 | RET_buttonPressCount = 0; |
pathfindr | 9:b0a1535b8ef2 | 114 | RET_mode_prev = RET_mode; |
pathfindr | 9:b0a1535b8ef2 | 115 | RET_mode = MODE_BUTTONHOLD; |
pathfindr | 9:b0a1535b8ef2 | 116 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 117 | } else { |
pathfindr | 9:b0a1535b8ef2 | 118 | if((RET_RTCmicros - RET_buttonPressTime) > 500) { |
pathfindr | 9:b0a1535b8ef2 | 119 | switch (RET_buttonPressCount) { |
pathfindr | 9:b0a1535b8ef2 | 120 | case 1 : |
pathfindr | 9:b0a1535b8ef2 | 121 | RET_mode_prev = RET_mode; |
pathfindr | 9:b0a1535b8ef2 | 122 | RET_mode = MODE_BUTTONPRESS1; |
pathfindr | 9:b0a1535b8ef2 | 123 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 124 | break; |
pathfindr | 9:b0a1535b8ef2 | 125 | case 2 : |
pathfindr | 9:b0a1535b8ef2 | 126 | RET_mode_prev = RET_mode; |
pathfindr | 9:b0a1535b8ef2 | 127 | RET_mode = MODE_BUTTONPRESS2; |
pathfindr | 9:b0a1535b8ef2 | 128 | mainthread.release(); |
pathfindr | 9:b0a1535b8ef2 | 129 | break; |
pathfindr | 9:b0a1535b8ef2 | 130 | default : |
pathfindr | 9:b0a1535b8ef2 | 131 | //do nothing |
pathfindr | 9:b0a1535b8ef2 | 132 | break; |
pathfindr | 9:b0a1535b8ef2 | 133 | } |
pathfindr | 9:b0a1535b8ef2 | 134 | RET_buttonPressCount = 0; |
pathfindr | 9:b0a1535b8ef2 | 135 | } |
pathfindr | 9:b0a1535b8ef2 | 136 | } |
pathfindr | 9:b0a1535b8ef2 | 137 | } |
pathfindr | 7:e9a19750700d | 138 | void factoryReset() { |
pathfindr | 9:b0a1535b8ef2 | 139 | //firstBoot = true; |
pathfindr | 9:b0a1535b8ef2 | 140 | requireSoftReset = false; |
pathfindr | 6:388d3c7efdd9 | 141 | |
pathfindr | 9:b0a1535b8ef2 | 142 | //RESET RETAINED VARS |
pathfindr | 9:b0a1535b8ef2 | 143 | RET_coldBoot = 1; |
pathfindr | 9:b0a1535b8ef2 | 144 | RET_mode = MODE_NORMAL; |
pathfindr | 9:b0a1535b8ef2 | 145 | RET_mode_prev = RET_mode; |
pathfindr | 9:b0a1535b8ef2 | 146 | RET_RTCunixtime = 0; |
pathfindr | 9:b0a1535b8ef2 | 147 | RET_RTCmicros = 0; |
pathfindr | 9:b0a1535b8ef2 | 148 | RET_eventTime_location_log = 0; |
pathfindr | 9:b0a1535b8ef2 | 149 | RET_eventTime_location_bc = 0; |
pathfindr | 9:b0a1535b8ef2 | 150 | RET_eventTime_environmental_log = 0; |
pathfindr | 9:b0a1535b8ef2 | 151 | RET_eventTime_environmental_bc = 0; |
pathfindr | 9:b0a1535b8ef2 | 152 | RET_eventTime_activity_bc = 0; |
pathfindr | 9:b0a1535b8ef2 | 153 | RET_eventTime_wakeFromDormant = 0; |
pathfindr | 7:e9a19750700d | 154 | RET_motionPendingOffState = 0; |
pathfindr | 7:e9a19750700d | 155 | RET_motionPendingOnState = 0; |
pathfindr | 10:c8798fd9773b | 156 | RET_motionTriggered = 0; |
pathfindr | 10:c8798fd9773b | 157 | RET_impactTriggered = 0; |
pathfindr | 7:e9a19750700d | 158 | RET_motionState = 0; |
pathfindr | 7:e9a19750700d | 159 | RET_motionStartTime = 0; |
pathfindr | 7:e9a19750700d | 160 | RET_motionStopTime = 0; |
pathfindr | 7:e9a19750700d | 161 | RET_motionStartThreshold_seconds = 60; |
pathfindr | 7:e9a19750700d | 162 | RET_motionStopThreshold_seconds = 60; |
pathfindr | 9:b0a1535b8ef2 | 163 | RET_motionTotalActivityHours = 0.0; |
pathfindr | 9:b0a1535b8ef2 | 164 | RET_buttonPressTime = 0; |
pathfindr | 9:b0a1535b8ef2 | 165 | RET_buttonPressCount = 0; |
pathfindr | 9:b0a1535b8ef2 | 166 | RET_buttonHoldTime = 0; |
pathfindr | 6:388d3c7efdd9 | 167 | |
pathfindr | 10:c8798fd9773b | 168 | //CONFIGURE ACCELL DEFAULT |
pathfindr | 10:c8798fd9773b | 169 | lis3dh_configureForSleep(10,127); |
pathfindr | 10:c8798fd9773b | 170 | |
pathfindr | 6:388d3c7efdd9 | 171 | //SET IDENTIFIER |
pathfindr | 7:e9a19750700d | 172 | //uint32_t nv_value = 12345678; |
pathfindr | 7:e9a19750700d | 173 | //int rc = nvstore.set(NV_IDENTIFIER, sizeof(nv_value), &nv_value); |
pathfindr | 7:e9a19750700d | 174 | } |
pathfindr | 9:b0a1535b8ef2 | 175 | void selfTest() { |
pathfindr | 8:7351f8c4cd60 | 176 | //Accelerometer |
pathfindr | 8:7351f8c4cd60 | 177 | LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK); |
pathfindr | 8:7351f8c4cd60 | 178 | uint8_t lis3dh_id; |
pathfindr | 8:7351f8c4cd60 | 179 | lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id); |
pathfindr | 9:b0a1535b8ef2 | 180 | if (lis3dh_id == 51) { |
pathfindr | 9:b0a1535b8ef2 | 181 | LED1blink(10,50); |
pathfindr | 9:b0a1535b8ef2 | 182 | } |
pathfindr | 9:b0a1535b8ef2 | 183 | } |
pathfindr | 9:b0a1535b8ef2 | 184 | |
pathfindr | 9:b0a1535b8ef2 | 185 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 186 | // USER BUTTON HANDLING |
pathfindr | 9:b0a1535b8ef2 | 187 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 188 | void buttonPress() { |
pathfindr | 9:b0a1535b8ef2 | 189 | RET_buttonPressTime = RET_RTCmicros; |
pathfindr | 9:b0a1535b8ef2 | 190 | } |
pathfindr | 9:b0a1535b8ef2 | 191 | void buttonRelease() { |
pathfindr | 9:b0a1535b8ef2 | 192 | RET_buttonHoldTime = (RET_RTCmicros - RET_buttonPressTime); |
pathfindr | 9:b0a1535b8ef2 | 193 | //debounce catch |
pathfindr | 9:b0a1535b8ef2 | 194 | if((RET_RTCmicros - RET_buttonPressTime) >= 100) { |
pathfindr | 9:b0a1535b8ef2 | 195 | RET_buttonPressCount ++; |
pathfindr | 9:b0a1535b8ef2 | 196 | } |
pathfindr | 9:b0a1535b8ef2 | 197 | } |
pathfindr | 9:b0a1535b8ef2 | 198 | |
pathfindr | 9:b0a1535b8ef2 | 199 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 200 | // MOTION FUNCS |
pathfindr | 9:b0a1535b8ef2 | 201 | //------------------------------------------------------------------------------ |
pathfindr | 9:b0a1535b8ef2 | 202 | void checkMotion() { |
pathfindr | 9:b0a1535b8ef2 | 203 | if (lis3dh_int2) { |
pathfindr | 9:b0a1535b8ef2 | 204 | if (debugLED) LED1blink(2,100); |
pathfindr | 9:b0a1535b8ef2 | 205 | RET_motionTriggered = true; |
pathfindr | 10:c8798fd9773b | 206 | needToConfigureLis3dh = true; //interrupt has fire so need to clear it |
pathfindr | 9:b0a1535b8ef2 | 207 | if (!RET_motionPendingOnState) { |
pathfindr | 9:b0a1535b8ef2 | 208 | RET_motionPendingOnState = true; |
pathfindr | 9:b0a1535b8ef2 | 209 | RET_motionPendingOffState = false; |
pathfindr | 9:b0a1535b8ef2 | 210 | // Log start motion time |
pathfindr | 9:b0a1535b8ef2 | 211 | RET_motionStartTime = time(NULL); |
pathfindr | 9:b0a1535b8ef2 | 212 | } |
pathfindr | 9:b0a1535b8ef2 | 213 | } else { |
pathfindr | 9:b0a1535b8ef2 | 214 | if (debugLED) LED1blink(2,500); |
pathfindr | 9:b0a1535b8ef2 | 215 | RET_motionTriggered = false; |
pathfindr | 9:b0a1535b8ef2 | 216 | RET_motionPendingOnState = false; |
pathfindr | 9:b0a1535b8ef2 | 217 | if (!RET_motionPendingOffState) { |
pathfindr | 9:b0a1535b8ef2 | 218 | RET_motionPendingOffState = true; |
pathfindr | 9:b0a1535b8ef2 | 219 | //log stop motion time |
pathfindr | 9:b0a1535b8ef2 | 220 | RET_motionStopTime = time(NULL); |
pathfindr | 9:b0a1535b8ef2 | 221 | } |
pathfindr | 9:b0a1535b8ef2 | 222 | } |
pathfindr | 9:b0a1535b8ef2 | 223 | //calculate motion state |
pathfindr | 9:b0a1535b8ef2 | 224 | if (RET_motionPendingOnState) { |
pathfindr | 9:b0a1535b8ef2 | 225 | //check if above threshold |
pathfindr | 9:b0a1535b8ef2 | 226 | time_t inMotionForSeconds = (time(NULL) - RET_motionStartTime); |
pathfindr | 9:b0a1535b8ef2 | 227 | if (inMotionForSeconds >= RET_motionStartThreshold_seconds) { |
pathfindr | 9:b0a1535b8ef2 | 228 | RET_motionState = true; |
pathfindr | 9:b0a1535b8ef2 | 229 | if (debugLED) LED1blink(10,100); |
pathfindr | 9:b0a1535b8ef2 | 230 | } |
pathfindr | 9:b0a1535b8ef2 | 231 | } |
pathfindr | 9:b0a1535b8ef2 | 232 | if (RET_motionPendingOffState) { |
pathfindr | 9:b0a1535b8ef2 | 233 | time_t noMotionForSeconds = (time(NULL) - RET_motionStopTime); |
pathfindr | 9:b0a1535b8ef2 | 234 | if (noMotionForSeconds >= RET_motionStartThreshold_seconds) { |
pathfindr | 9:b0a1535b8ef2 | 235 | RET_motionState = false; |
pathfindr | 9:b0a1535b8ef2 | 236 | RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); |
pathfindr | 9:b0a1535b8ef2 | 237 | if (debugLED) LED1blink(5,500); |
pathfindr | 9:b0a1535b8ef2 | 238 | } |
pathfindr | 9:b0a1535b8ef2 | 239 | } |
pathfindr | 8:7351f8c4cd60 | 240 | } |
pathfindr | 8:7351f8c4cd60 | 241 | |
pathfindr | 7:e9a19750700d | 242 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 243 | // STATE ENGINE |
pathfindr | 7:e9a19750700d | 244 | //------------------------------------------------------------------------------ |
pathfindr | 6:388d3c7efdd9 | 245 | void mainStateEngine() { |
pathfindr | 6:388d3c7efdd9 | 246 | switch(RET_mode) { |
pathfindr | 6:388d3c7efdd9 | 247 | case MODE_SETUP : |
pathfindr | 7:e9a19750700d | 248 | factoryReset(); |
pathfindr | 6:388d3c7efdd9 | 249 | //selftest(); |
pathfindr | 6:388d3c7efdd9 | 250 | break; |
pathfindr | 6:388d3c7efdd9 | 251 | case MODE_NORMAL : |
pathfindr | 7:e9a19750700d | 252 | //check and log motion |
pathfindr | 9:b0a1535b8ef2 | 253 | checkMotion(); |
pathfindr | 9:b0a1535b8ef2 | 254 | |
pathfindr | 9:b0a1535b8ef2 | 255 | //EVENTS |
pathfindr | 9:b0a1535b8ef2 | 256 | //Location |
pathfindr | 9:b0a1535b8ef2 | 257 | if(time(NULL) > RET_eventTime_location_log && RET_eventTime_location_log > 0) { |
pathfindr | 9:b0a1535b8ef2 | 258 | //getdata_environmental(); |
pathfindr | 9:b0a1535b8ef2 | 259 | //event_location_log(); |
pathfindr | 9:b0a1535b8ef2 | 260 | } |
pathfindr | 9:b0a1535b8ef2 | 261 | if(time(NULL) > RET_eventTime_location_bc && RET_eventTime_location_bc > 0) { |
pathfindr | 9:b0a1535b8ef2 | 262 | //getdata_battery(); |
pathfindr | 9:b0a1535b8ef2 | 263 | //event_location_broadcast(); |
pathfindr | 9:b0a1535b8ef2 | 264 | } |
pathfindr | 9:b0a1535b8ef2 | 265 | //Environmental |
pathfindr | 9:b0a1535b8ef2 | 266 | if(time(NULL) > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) { |
pathfindr | 9:b0a1535b8ef2 | 267 | //event_environmental_log(); |
pathfindr | 9:b0a1535b8ef2 | 268 | } |
pathfindr | 9:b0a1535b8ef2 | 269 | if(time(NULL) > RET_eventTime_environmental_bc && RET_eventTime_environmental_bc > 0) { |
pathfindr | 9:b0a1535b8ef2 | 270 | //event_environmental_bc(); |
pathfindr | 7:e9a19750700d | 271 | } |
pathfindr | 9:b0a1535b8ef2 | 272 | //Activity |
pathfindr | 9:b0a1535b8ef2 | 273 | if(time(NULL) > RET_eventTime_activity_bc && RET_eventTime_activity_bc > 0) { |
pathfindr | 9:b0a1535b8ef2 | 274 | //event_activity_bc(); |
pathfindr | 9:b0a1535b8ef2 | 275 | } |
pathfindr | 9:b0a1535b8ef2 | 276 | break; |
pathfindr | 9:b0a1535b8ef2 | 277 | case MODE_DORMANT : |
pathfindr | 9:b0a1535b8ef2 | 278 | if (time(NULL) > RET_eventTime_wakeFromDormant) { |
pathfindr | 9:b0a1535b8ef2 | 279 | RET_mode = MODE_NORMAL; |
pathfindr | 7:e9a19750700d | 280 | } |
pathfindr | 9:b0a1535b8ef2 | 281 | break; |
pathfindr | 9:b0a1535b8ef2 | 282 | case MODE_BUTTONPRESS1 : |
pathfindr | 9:b0a1535b8ef2 | 283 | LED1blink(2,300); |
pathfindr | 9:b0a1535b8ef2 | 284 | RET_mode = RET_mode_prev; |
pathfindr | 9:b0a1535b8ef2 | 285 | break; |
pathfindr | 9:b0a1535b8ef2 | 286 | case MODE_BUTTONPRESS2 : |
pathfindr | 9:b0a1535b8ef2 | 287 | LED1blink(4,300); |
pathfindr | 9:b0a1535b8ef2 | 288 | RET_mode = RET_mode_prev; |
pathfindr | 9:b0a1535b8ef2 | 289 | break; |
pathfindr | 9:b0a1535b8ef2 | 290 | case MODE_BUTTONHOLD : |
pathfindr | 9:b0a1535b8ef2 | 291 | LED1blink(10,300); |
pathfindr | 9:b0a1535b8ef2 | 292 | RET_mode = RET_mode_prev; |
pathfindr | 6:388d3c7efdd9 | 293 | break; |
pathfindr | 6:388d3c7efdd9 | 294 | default : |
pathfindr | 6:388d3c7efdd9 | 295 | RET_mode = MODE_SETUP; |
pathfindr | 6:388d3c7efdd9 | 296 | } |
pathfindr | 6:388d3c7efdd9 | 297 | } |
pathfindr | 6:388d3c7efdd9 | 298 | |
pathfindr | 6:388d3c7efdd9 | 299 | |
pathfindr | 7:e9a19750700d | 300 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 301 | // MAIN |
pathfindr | 7:e9a19750700d | 302 | //------------------------------------------------------------------------------ |
pathfindr | 7:e9a19750700d | 303 | |
pathfindr | 0:94b743e06998 | 304 | int main() { |
pathfindr | 9:b0a1535b8ef2 | 305 | RTCticker.attach(&RTCtick, 0.1); |
pathfindr | 9:b0a1535b8ef2 | 306 | turnOffEverything(); |
pathfindr | 4:8d8e9bfa82e4 | 307 | |
pathfindr | 9:b0a1535b8ef2 | 308 | button.fall(&buttonPress); //does this affect power? |
pathfindr | 9:b0a1535b8ef2 | 309 | button.rise(&buttonRelease); |
pathfindr | 9:b0a1535b8ef2 | 310 | |
pathfindr | 4:8d8e9bfa82e4 | 311 | //CHECK IF THIS IS RESET |
pathfindr | 4:8d8e9bfa82e4 | 312 | //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset |
pathfindr | 4:8d8e9bfa82e4 | 313 | if (NRF_POWER->RESETREAS != 0xffffffff) { |
pathfindr | 4:8d8e9bfa82e4 | 314 | switch(NRF_POWER->RESETREAS) { |
pathfindr | 4:8d8e9bfa82e4 | 315 | case 0x00000001 : |
pathfindr | 4:8d8e9bfa82e4 | 316 | DEBUG("reset_reason: 0x%08x. - Hard Reset\n",NRF_POWER->RESETREAS); |
pathfindr | 7:e9a19750700d | 317 | factoryReset(); |
pathfindr | 4:8d8e9bfa82e4 | 318 | break; |
pathfindr | 4:8d8e9bfa82e4 | 319 | case 0x00000002 : |
pathfindr | 4:8d8e9bfa82e4 | 320 | DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS); |
pathfindr | 4:8d8e9bfa82e4 | 321 | break; |
pathfindr | 4:8d8e9bfa82e4 | 322 | case 0x00000004 : |
pathfindr | 4:8d8e9bfa82e4 | 323 | DEBUG("reset_reason: 0x%08x. - Soft reset\n",NRF_POWER->RESETREAS); |
pathfindr | 4:8d8e9bfa82e4 | 324 | break; |
pathfindr | 4:8d8e9bfa82e4 | 325 | } |
pathfindr | 4:8d8e9bfa82e4 | 326 | NRF_POWER->RESETREAS = 0xffffffff; |
pathfindr | 4:8d8e9bfa82e4 | 327 | } |
pathfindr | 9:b0a1535b8ef2 | 328 | //CHECK FOR FIRST BOOT |
pathfindr | 9:b0a1535b8ef2 | 329 | if (RET_coldBoot != 0) factoryReset(); |
pathfindr | 9:b0a1535b8ef2 | 330 | |
pathfindr | 0:94b743e06998 | 331 | while(true) { |
pathfindr | 9:b0a1535b8ef2 | 332 | //SLEEP |
pathfindr | 9:b0a1535b8ef2 | 333 | if (RET_coldBoot != 1) gotoSleep(30000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP |
pathfindr | 4:8d8e9bfa82e4 | 334 | watchdog.kick(); |
pathfindr | 9:b0a1535b8ef2 | 335 | |
pathfindr | 11:60eb0ff945f2 | 336 | Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1); |
pathfindr | 11:60eb0ff945f2 | 337 | |
pathfindr | 11:60eb0ff945f2 | 338 | //GET TEMPERATURE |
pathfindr | 11:60eb0ff945f2 | 339 | SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL); |
pathfindr | 11:60eb0ff945f2 | 340 | float temperature_c = temperature.getTemperature(); |
pathfindr | 11:60eb0ff945f2 | 341 | DEBUG("temp: %f \n", temperature_c); |
pathfindr | 11:60eb0ff945f2 | 342 | filesystem.addLogEntry_temperature(RET_RTCunixtime, 10.5); |
pathfindr | 11:60eb0ff945f2 | 343 | |
pathfindr | 9:b0a1535b8ef2 | 344 | //MAIN LOGIC |
pathfindr | 9:b0a1535b8ef2 | 345 | DEBUG("mode: %i time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount); |
pathfindr | 9:b0a1535b8ef2 | 346 | mainStateEngine(); |
pathfindr | 9:b0a1535b8ef2 | 347 | |
pathfindr | 9:b0a1535b8ef2 | 348 | //PRE-SLEEP ACTIONS |
pathfindr | 10:c8798fd9773b | 349 | if (needToConfigureLis3dh) { //ONLY RESET ACCELL IF WE NEED TO |
pathfindr | 10:c8798fd9773b | 350 | lis3dh_configureForSleep(10,127); |
pathfindr | 10:c8798fd9773b | 351 | } |
pathfindr | 9:b0a1535b8ef2 | 352 | RET_coldBoot = 0; |
pathfindr | 9:b0a1535b8ef2 | 353 | |
pathfindr | 9:b0a1535b8ef2 | 354 | |
pathfindr | 9:b0a1535b8ef2 | 355 | /* |
pathfindr | 7:e9a19750700d | 356 | firstBoot = false; //temp |
pathfindr | 7:e9a19750700d | 357 | if (!firstBoot) gotoSleep(10000); |
pathfindr | 7:e9a19750700d | 358 | |
pathfindr | 7:e9a19750700d | 359 | //INIT |
pathfindr | 9:b0a1535b8ef2 | 360 | //Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN); |
pathfindr | 4:8d8e9bfa82e4 | 361 | |
pathfindr | 4:8d8e9bfa82e4 | 362 | //MAIN STATE ENGINE |
pathfindr | 7:e9a19750700d | 363 | |
pathfindr | 7:e9a19750700d | 364 | //Configure for sleep |
pathfindr | 9:b0a1535b8ef2 | 365 | lis3dh_configureForSleep(10,127); |
pathfindr | 9:b0a1535b8ef2 | 366 | */ |
pathfindr | 0:94b743e06998 | 367 | } |
pathfindr | 7:e9a19750700d | 368 | } |