init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Mon Dec 17 19:17:39 2018 +0000
Revision:
15:7aad9a7f970c
Parent:
14:9a54b1b65bc8
Child:
16:3bf5f1a5f869
6

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