init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sun Dec 16 15:21:38 2018 +0000
Revision:
13:29f67f256709
Parent:
12:8345612bf867
Child:
14:9a54b1b65bc8
5

Who changed what in which revision?

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