init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sun Dec 16 01:06:14 2018 +0000
Revision:
12:8345612bf867
Parent:
11:60eb0ff945f2
Child:
13:29f67f256709
4

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