init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sat Dec 15 13:45:28 2018 +0000
Revision:
9:b0a1535b8ef2
Parent:
8:7351f8c4cd60
Child:
10:c8798fd9773b
sat

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