init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Tue Jan 01 15:44:24 2019 +0000
Revision:
31:c84fc6d8eaa3
Parent:
29:059fc7324328
Child:
32:dff4858bdf37
10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pathfindr 0:94b743e06998 1 #include "main.h"
pathfindr 21:e0b866630c27 2
pathfindr 21:e0b866630c27 3 //------------------------------------------------------------------------------
pathfindr 24:c161db07557f 4 //DEFINES
pathfindr 21:e0b866630c27 5 //------------------------------------------------------------------------------
pathfindr 21:e0b866630c27 6 #define FW_VERSION 1
pathfindr 26:fa3579737329 7
pathfindr 26:fa3579737329 8 #define DEFAULT_SLEEP_FRAME 30000
pathfindr 26:fa3579737329 9 #define DEFAULT_LOCATION_MODE 2
pathfindr 26:fa3579737329 10 #define DEFAULT_LOCATION_ACCURACY 2 // 0 = no location, 1 = cl only, 2 = gps then cl
pathfindr 26:fa3579737329 11 #define DEFAULT_LOCATION_TX_INTERVAL_MINS 1440
pathfindr 26:fa3579737329 12 #define DEFAULT_LOCATION_TX_FAILSAFE_HRS 168
pathfindr 26:fa3579737329 13 #define DEFAULT_LOCATION_TIMEOUT 180
pathfindr 26:fa3579737329 14 #define DEFAULT_MOTION_G 11
pathfindr 26:fa3579737329 15 #define DEFAULT_MOTION_START_SECONDS 120
pathfindr 26:fa3579737329 16 #define DEFAULT_MOTION_STOP_SECONDS 120
pathfindr 26:fa3579737329 17 #define DEFAULT_IMPACT_G 127
pathfindr 28:24b02608fa5f 18 #define DEFAULT_CONNECTION_ATTEMPTS 1
pathfindr 26:fa3579737329 19 #define DEFAULT_CONNECTION_TIMEOUT 180
pathfindr 26:fa3579737329 20 #define DEFAULT_BEACON_INTERVAL_SECONDS 10
pathfindr 26:fa3579737329 21
pathfindr 4:8d8e9bfa82e4 22
pathfindr 0:94b743e06998 23 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 24 //FUNCTION PROTOTYPES
pathfindr 0:94b743e06998 25 //------------------------------------------------------------------------------
pathfindr 4:8d8e9bfa82e4 26 static void mainStateEngine(void);
pathfindr 4:8d8e9bfa82e4 27 static void selftest(void);
pathfindr 7:e9a19750700d 28 static void buttonPress(void);
pathfindr 7:e9a19750700d 29 static void buttonRelease(void);
pathfindr 24:c161db07557f 30 static void dumpSettings(void);
pathfindr 20:5404841fdd2b 31 static void saveEventTimes(void);
pathfindr 23:a3b0ccf75ca5 32 static void event_location_tx(void);
pathfindr 0:94b743e06998 33
pathfindr 0:94b743e06998 34 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 35 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
pathfindr 0:94b743e06998 36 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 37 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 13:29f67f256709 38 bool GLOBAL_accel_healthy = false;
pathfindr 13:29f67f256709 39 bool GLOBAL_requireSoftReset = false;
pathfindr 20:5404841fdd2b 40 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 24:c161db07557f 41 bool GLOBAL_debugLED = true;
pathfindr 13:29f67f256709 42 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 13:29f67f256709 43 bool GLOBAL_registeredOnNetwork = false;
pathfindr 22:810425eb76e1 44 bool GLOBAL_modemOn = false;
pathfindr 13:29f67f256709 45 time_t GLOBAL_RTCunixtime = 0;
pathfindr 13:29f67f256709 46 time_t GLOBAL_wakeTime = 0;
pathfindr 27:fa76f5a08195 47 char GLOBAL_exceptionString[10];
pathfindr 0:94b743e06998 48
pathfindr 0:94b743e06998 49 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 50 //RETAINED NOINIT RAM VARS
pathfindr 0:94b743e06998 51 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 52 #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
pathfindr 13:29f67f256709 53 //IDENTITY
pathfindr 16:3bf5f1a5f869 54 static long long RET_imei __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 55 //SETTINGS
pathfindr 24:c161db07557f 56 static int RET_setting_firmware __attribute__((section("noinit"),zero_init)); //must be int to support negative value
pathfindr 19:22261767c87a 57 static uint8_t RET_setting_location_mode __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 58 static uint8_t RET_setting_location_accuracy __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 59 static uint32_t RET_setting_location_tx_interval_mins __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 60 static uint32_t RET_setting_location_tx_failsafe_hrs __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 61 static uint16_t RET_setting_location_timeout __attribute__((section("noinit"),zero_init));
pathfindr 24:c161db07557f 62 static uint32_t RET_setting_activity_tx_interval_hrs __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 63 static uint32_t RET_setting_environmental_tx_interval_mins __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 64 static uint16_t RET_setting_motion_g __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 65 static time_t RET_setting_motion_start_seconds __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 66 static time_t RET_setting_motion_stop_seconds __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 67 static uint16_t RET_setting_impact_g __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 68 static uint8_t RET_setting_impact_alert __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 69 static uint16_t RET_setting_connection_timeout __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 70 static uint16_t RET_setting_beacon_interval_seconds __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 71 static uint16_t RET_setting_beacon_scan __attribute__((section("noinit"),zero_init));
pathfindr 13:29f67f256709 72 //STATE
pathfindr 24:c161db07557f 73 static uint16_t RET_sleepToggle __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 74 static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 75 static bool RET_haveSettings __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 76 static uint8_t RET_state __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 77 static uint8_t RET_state_prev __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 78 static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 79 static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 80 static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 81 static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 82 static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init));
pathfindr 21:e0b866630c27 83 static time_t RET_SetupRunAt __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 84 //MOTION STATE
pathfindr 16:3bf5f1a5f869 85 static bool RET_motionTriggered __attribute__((section("noinit"),zero_init));
pathfindr 20:5404841fdd2b 86 static bool RET_motionTriggeredInTXInterval __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 87 static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 88 static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 89 static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 90 static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 91 static bool RET_motionState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 92 static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init));
pathfindr 24:c161db07557f 93 static time_t RET_motionFrameStart __attribute__((section("noinit"),zero_init));
pathfindr 24:c161db07557f 94 static char RET_activityData[140] __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 95 //IMPACT
pathfindr 16:3bf5f1a5f869 96 static bool RET_impactTriggered __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 97 //EVENTS LOGGING
pathfindr 16:3bf5f1a5f869 98 static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 99 static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 100 //EVENTS TX
pathfindr 16:3bf5f1a5f869 101 static time_t RET_eventTime_location_tx __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 102 static time_t RET_eventTime_location_failsafe_tx __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 103 static time_t RET_eventTime_environmental_tx __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 104 static time_t RET_eventTime_activity_tx __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 105 static time_t RET_eventTime_wakeFromDormant __attribute__((section("noinit"),zero_init));
pathfindr 24:c161db07557f 106 //DUPLICATES MEM INIT CHECK
pathfindr 24:c161db07557f 107 static time_t RET_RTCunixtime_bu __attribute__((section("noinit"),zero_init));
pathfindr 24:c161db07557f 108 static uint32_t RET_memTest __attribute__((section("noinit"),zero_init));
pathfindr 4:8d8e9bfa82e4 109 #elif defined ( __GNUC__ )
pathfindr 4:8d8e9bfa82e4 110 #elif defined ( __ICCARM__ )
pathfindr 4:8d8e9bfa82e4 111 #endif
pathfindr 4:8d8e9bfa82e4 112
pathfindr 0:94b743e06998 113 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 114 //GPIO
pathfindr 9:b0a1535b8ef2 115 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 116 InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep
pathfindr 15:7aad9a7f970c 117 DigitalOut vreg_en(PN_VREG_EN);
pathfindr 9:b0a1535b8ef2 118
pathfindr 9:b0a1535b8ef2 119 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 120 //PERIPHERALS
pathfindr 0:94b743e06998 121 //------------------------------------------------------------------------------
pathfindr 4:8d8e9bfa82e4 122 //BLE myble;
pathfindr 20:5404841fdd2b 123 WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 20:5404841fdd2b 124 void watchdogKick() {watchdog.kick();}
pathfindr 11:60eb0ff945f2 125 //SERIAL DEBUG?
pathfindr 11:60eb0ff945f2 126 #if CONSOLE_DEBUG
pathfindr 11:60eb0ff945f2 127 Serial uart(PN_UART_TX, PN_UART_RX, 115200);
pathfindr 4:8d8e9bfa82e4 128 #endif
pathfindr 11:60eb0ff945f2 129
pathfindr 6:388d3c7efdd9 130 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 131 //TIMERS
pathfindr 18:22edaa7e74b1 132 //-----------------------------------------------mi-------------------------------
pathfindr 9:b0a1535b8ef2 133 LowPowerTicker RTCticker; //no impact on power consumption
pathfindr 9:b0a1535b8ef2 134
pathfindr 9:b0a1535b8ef2 135 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 136 //THREAD SEMAPHORES
pathfindr 9:b0a1535b8ef2 137 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 138 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 139
pathfindr 9:b0a1535b8ef2 140 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 141 // LOW LEVEL FUNCS
pathfindr 7:e9a19750700d 142 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 143 void turnOffEverything() {
pathfindr 15:7aad9a7f970c 144 vreg_en = 0;
pathfindr 9:b0a1535b8ef2 145 LED1off();
pathfindr 9:b0a1535b8ef2 146 }
pathfindr 4:8d8e9bfa82e4 147 void gotoSleep(long sleep_milliseconds) {
pathfindr 24:c161db07557f 148 /*
pathfindr 9:b0a1535b8ef2 149 turnOffEverything();
pathfindr 20:5404841fdd2b 150 watchdogKick();
pathfindr 13:29f67f256709 151 if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false
pathfindr 4:8d8e9bfa82e4 152 system_reset();
pathfindr 4:8d8e9bfa82e4 153 }
pathfindr 9:b0a1535b8ef2 154 mainthread.wait(sleep_milliseconds);
pathfindr 24:c161db07557f 155 */
pathfindr 6:388d3c7efdd9 156 }
pathfindr 19:22261767c87a 157 void setState(uint8_t state) {
pathfindr 19:22261767c87a 158 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 159 RET_state = state;
pathfindr 29:059fc7324328 160 //DEBUG("STATEREADBACK %d\n",RET_state)
pathfindr 19:22261767c87a 161 }
pathfindr 22:810425eb76e1 162 bool memoryIntegrityCheck() {
pathfindr 24:c161db07557f 163 bool pass = true;
pathfindr 24:c161db07557f 164 //check memory is init correct
pathfindr 31:c84fc6d8eaa3 165 if (RET_memTest != 12345) { pass = false; DEBUG("Mem fail 1\n"); }
pathfindr 31:c84fc6d8eaa3 166 //Check clocks match
pathfindr 31:c84fc6d8eaa3 167 if (RET_RTCunixtime != RET_RTCunixtime_bu) { pass = false; DEBUG("Mem fail 2\n"); }
pathfindr 31:c84fc6d8eaa3 168 //Check clocks are in range (only if we have settings)
pathfindr 31:c84fc6d8eaa3 169 if (RET_haveSettings && RET_RTCunixtime < 1545412457) { pass = false; DEBUG("Mem fail 3\n"); }
pathfindr 24:c161db07557f 170 return pass;
pathfindr 22:810425eb76e1 171 }
pathfindr 24:c161db07557f 172 void dumpSettings() {
pathfindr 27:fa76f5a08195 173 DEBUG("RET_memTest:%d \n",RET_memTest);
pathfindr 24:c161db07557f 174 DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
pathfindr 31:c84fc6d8eaa3 175 DEBUG("RET_RTCunixtime_bu:%u \n",RET_RTCunixtime_bu);
pathfindr 24:c161db07557f 176 DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
pathfindr 24:c161db07557f 177 DEBUG("RET_state:%d \n",RET_state);
pathfindr 24:c161db07557f 178 DEBUG("RET_setting_location_mode:%d \n",RET_setting_location_mode);
pathfindr 24:c161db07557f 179 DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
pathfindr 24:c161db07557f 180 DEBUG("RET_setting_location_tx_interval_mins:%d \n",RET_setting_location_tx_interval_mins);
pathfindr 24:c161db07557f 181 DEBUG("RET_setting_location_tx_failsafe_hrs:%d \n",RET_setting_location_tx_failsafe_hrs);
pathfindr 24:c161db07557f 182 DEBUG("RET_setting_location_timeout:%d \n",RET_setting_location_timeout);
pathfindr 24:c161db07557f 183 DEBUG("RET_setting_activity_tx_interval_hrs:%d \n",RET_setting_activity_tx_interval_hrs);
pathfindr 24:c161db07557f 184 DEBUG("RET_setting_environmental_tx_interval_mins:%d \n",RET_setting_environmental_tx_interval_mins);
pathfindr 24:c161db07557f 185 DEBUG("RET_setting_motion_g:%d \n",RET_setting_motion_g);
pathfindr 24:c161db07557f 186 DEBUG("RET_setting_motion_start_seconds:%d \n",RET_setting_motion_start_seconds);
pathfindr 24:c161db07557f 187 DEBUG("RET_setting_motion_stop_seconds:%d \n",RET_setting_motion_stop_seconds);
pathfindr 24:c161db07557f 188 DEBUG("RET_setting_impact_g:%d \n",RET_setting_impact_g);
pathfindr 24:c161db07557f 189 DEBUG("RET_setting_impact_alert:%d \n",RET_setting_impact_alert);
pathfindr 24:c161db07557f 190 DEBUG("RET_setting_connection_timeout:%d \n",RET_setting_connection_timeout);
pathfindr 24:c161db07557f 191 DEBUG("RET_setting_beacon_interval_seconds:%d \n",RET_setting_beacon_interval_seconds);
pathfindr 24:c161db07557f 192 DEBUG("RET_setting_beacon_scan:%d \n",RET_setting_beacon_scan);
pathfindr 24:c161db07557f 193 }
pathfindr 25:7adeb1a53360 194 float getBatteryV() { //this creates an extra 400ua of power usage!!!
pathfindr 19:22261767c87a 195 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 196 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 197 batteryIn.calibrate();
pathfindr 25:7adeb1a53360 198 float voltage = 0.0;
pathfindr 25:7adeb1a53360 199 for (uint8_t i = 1; i <= 2; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
pathfindr 19:22261767c87a 200 batteryIn.updateData();
pathfindr 25:7adeb1a53360 201 voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
pathfindr 19:22261767c87a 202 }
pathfindr 25:7adeb1a53360 203 return voltage;
pathfindr 22:810425eb76e1 204 }
pathfindr 22:810425eb76e1 205 float getTemperature() {
pathfindr 22:810425eb76e1 206 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 22:810425eb76e1 207 float temperature = si7060.getTemperature();
pathfindr 26:fa3579737329 208 //DEBUG("Temperature:%.2f\n",temperature);
pathfindr 22:810425eb76e1 209 return temperature;
pathfindr 19:22261767c87a 210 }
pathfindr 27:fa76f5a08195 211 void addToExceptionString(char* value) {
pathfindr 27:fa76f5a08195 212 snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),"%s.",value);
pathfindr 27:fa76f5a08195 213 }
pathfindr 19:22261767c87a 214 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 215 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 216 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 217 void buttonPress() {
pathfindr 19:22261767c87a 218 RET_buttonPressTime = RET_RTCunixtime;
pathfindr 19:22261767c87a 219 }
pathfindr 19:22261767c87a 220 void buttonRelease() {
pathfindr 19:22261767c87a 221 RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime);
pathfindr 19:22261767c87a 222 RET_buttonPressCount ++;
pathfindr 19:22261767c87a 223 }
pathfindr 19:22261767c87a 224 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 225 // RTC TICKER
pathfindr 19:22261767c87a 226 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 227 void RTCtick() {
pathfindr 19:22261767c87a 228 //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
pathfindr 19:22261767c87a 229 RET_RTCunixtime += 1;
pathfindr 31:c84fc6d8eaa3 230 RET_RTCunixtime_bu = RET_RTCunixtime;
pathfindr 13:29f67f256709 231 GLOBAL_RTCunixtime = RET_RTCunixtime;
pathfindr 19:22261767c87a 232
pathfindr 9:b0a1535b8ef2 233 //button logic - check for hold
pathfindr 19:22261767c87a 234 if (RET_buttonHoldTime > 4) {
pathfindr 9:b0a1535b8ef2 235 RET_buttonHoldTime = 0;
pathfindr 9:b0a1535b8ef2 236 RET_buttonPressCount = 0;
pathfindr 16:3bf5f1a5f869 237 RET_state_prev = RET_state;
pathfindr 16:3bf5f1a5f869 238 RET_state = STATE_BUTTONHOLD;
pathfindr 9:b0a1535b8ef2 239 mainthread.release();
pathfindr 9:b0a1535b8ef2 240 } else {
pathfindr 19:22261767c87a 241 if((RET_RTCunixtime - RET_buttonPressTime) > 1) {
pathfindr 9:b0a1535b8ef2 242 switch (RET_buttonPressCount) {
pathfindr 9:b0a1535b8ef2 243 case 1 :
pathfindr 19:22261767c87a 244 if (RET_state == STATE_NORMAL) {
pathfindr 19:22261767c87a 245 RET_state = STATE_BUTTONPRESS1;
pathfindr 19:22261767c87a 246 mainthread.release();
pathfindr 19:22261767c87a 247 }
pathfindr 9:b0a1535b8ef2 248 break;
pathfindr 29:059fc7324328 249 case 3 :
pathfindr 28:24b02608fa5f 250 if (RET_state == STATE_NORMAL) {
pathfindr 28:24b02608fa5f 251 RET_state = STATE_BUTTONPRESS2;
pathfindr 28:24b02608fa5f 252 mainthread.release();
pathfindr 28:24b02608fa5f 253 }
pathfindr 28:24b02608fa5f 254 break;
pathfindr 29:059fc7324328 255 case 5 :
pathfindr 19:22261767c87a 256 if (RET_state == STATE_NORMAL) {
pathfindr 28:24b02608fa5f 257 RET_state = STATE_BUTTONPRESS3;
pathfindr 19:22261767c87a 258 mainthread.release();
pathfindr 19:22261767c87a 259 }
pathfindr 9:b0a1535b8ef2 260 break;
pathfindr 9:b0a1535b8ef2 261 default :
pathfindr 9:b0a1535b8ef2 262 //do nothing
pathfindr 9:b0a1535b8ef2 263 break;
pathfindr 9:b0a1535b8ef2 264 }
pathfindr 9:b0a1535b8ef2 265 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 266 }
pathfindr 9:b0a1535b8ef2 267 }
pathfindr 19:22261767c87a 268 //SETUP STATE VISUALISE
pathfindr 19:22261767c87a 269 if (RET_state == STATE_SETUP) {
pathfindr 19:22261767c87a 270 led1 = !led1;
pathfindr 19:22261767c87a 271 }
pathfindr 9:b0a1535b8ef2 272 }
pathfindr 24:c161db07557f 273
pathfindr 7:e9a19750700d 274 void factoryReset() {
pathfindr 24:c161db07557f 275 //MEM CHECK
pathfindr 24:c161db07557f 276 RET_memTest = 12345;
pathfindr 16:3bf5f1a5f869 277 //IDENTITY
pathfindr 13:29f67f256709 278 RET_imei = 0;
pathfindr 19:22261767c87a 279 //STATE
pathfindr 24:c161db07557f 280 RET_sleepToggle = 0;
pathfindr 19:22261767c87a 281 RET_coldBoot = 1;
pathfindr 19:22261767c87a 282 RET_haveSettings = 0;
pathfindr 19:22261767c87a 283 RET_state = STATE_SETUP;
pathfindr 19:22261767c87a 284 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 285 RET_RTCunixtime = 0;
pathfindr 24:c161db07557f 286 RET_RTCunixtime_bu = 0;
pathfindr 21:e0b866630c27 287 RET_SetupRunAt = 0;
pathfindr 16:3bf5f1a5f869 288 //SETTINGS
pathfindr 16:3bf5f1a5f869 289 RET_setting_firmware = 0;
pathfindr 26:fa3579737329 290 RET_setting_location_mode = DEFAULT_LOCATION_MODE;
pathfindr 26:fa3579737329 291 RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
pathfindr 26:fa3579737329 292 RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
pathfindr 26:fa3579737329 293 RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
pathfindr 26:fa3579737329 294 RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
pathfindr 22:810425eb76e1 295 RET_setting_activity_tx_interval_hrs = 0;
pathfindr 16:3bf5f1a5f869 296 RET_setting_environmental_tx_interval_mins = 0;
pathfindr 26:fa3579737329 297 RET_setting_motion_g = DEFAULT_MOTION_G;
pathfindr 26:fa3579737329 298 RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
pathfindr 26:fa3579737329 299 RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
pathfindr 16:3bf5f1a5f869 300 RET_setting_impact_g = 0;
pathfindr 16:3bf5f1a5f869 301 RET_setting_impact_alert = 0;
pathfindr 26:fa3579737329 302 RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
pathfindr 26:fa3579737329 303 RET_setting_beacon_interval_seconds = DEFAULT_BEACON_INTERVAL_SECONDS;
pathfindr 16:3bf5f1a5f869 304 RET_setting_beacon_scan = 0;
pathfindr 19:22261767c87a 305 //RET_RTCmillis = 0;
pathfindr 16:3bf5f1a5f869 306 RET_buttonPressCount = 0;
pathfindr 16:3bf5f1a5f869 307 RET_buttonPressTime = 0;
pathfindr 16:3bf5f1a5f869 308 RET_buttonReleaseTime = 0;
pathfindr 16:3bf5f1a5f869 309 RET_buttonHoldTime = 0;
pathfindr 16:3bf5f1a5f869 310 //MOTION STATE
pathfindr 10:c8798fd9773b 311 RET_motionTriggered = 0;
pathfindr 20:5404841fdd2b 312 RET_motionTriggeredInTXInterval = 0;
pathfindr 7:e9a19750700d 313 RET_motionStartTime = 0;
pathfindr 7:e9a19750700d 314 RET_motionStopTime = 0;
pathfindr 16:3bf5f1a5f869 315 RET_motionPendingOnState = 0;
pathfindr 16:3bf5f1a5f869 316 RET_motionPendingOffState = 0;
pathfindr 16:3bf5f1a5f869 317 RET_motionState = 0;
pathfindr 9:b0a1535b8ef2 318 RET_motionTotalActivityHours = 0.0;
pathfindr 24:c161db07557f 319 RET_motionFrameStart = 0;
pathfindr 16:3bf5f1a5f869 320 memset(RET_activityData,0,sizeof(RET_activityData));
pathfindr 16:3bf5f1a5f869 321 //IMPACT
pathfindr 16:3bf5f1a5f869 322 RET_impactTriggered = 0;
pathfindr 16:3bf5f1a5f869 323 //EVENT HANDLING
pathfindr 16:3bf5f1a5f869 324 RET_eventTime_location_log = 0;
pathfindr 16:3bf5f1a5f869 325 RET_eventTime_location_tx = 0;
pathfindr 22:810425eb76e1 326 RET_eventTime_location_failsafe_tx = 0;
pathfindr 16:3bf5f1a5f869 327 RET_eventTime_environmental_log = 0;
pathfindr 16:3bf5f1a5f869 328 RET_eventTime_environmental_tx = 0;
pathfindr 16:3bf5f1a5f869 329 RET_eventTime_activity_tx = 0;
pathfindr 16:3bf5f1a5f869 330 RET_eventTime_wakeFromDormant = 0;
pathfindr 16:3bf5f1a5f869 331 //OTHER LOCAL GLOBALS
pathfindr 16:3bf5f1a5f869 332 GLOBAL_requireSoftReset = false;
pathfindr 24:c161db07557f 333
pathfindr 16:3bf5f1a5f869 334 //PERIPHERAL RESET
pathfindr 26:fa3579737329 335 lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
pathfindr 13:29f67f256709 336 }
pathfindr 22:810425eb76e1 337 bool selfTest() {
pathfindr 22:810425eb76e1 338 int test_count = 0;
pathfindr 22:810425eb76e1 339 int test_pass = 0;
pathfindr 22:810425eb76e1 340
pathfindr 8:7351f8c4cd60 341 //Accelerometer
pathfindr 22:810425eb76e1 342 test_count ++;
pathfindr 8:7351f8c4cd60 343 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 8:7351f8c4cd60 344 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 345 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 27:fa76f5a08195 346 if (lis3dh_id == 51) {
pathfindr 27:fa76f5a08195 347 test_pass ++;
pathfindr 27:fa76f5a08195 348 } else {
pathfindr 27:fa76f5a08195 349 addToExceptionString("A.");
pathfindr 27:fa76f5a08195 350 }
pathfindr 22:810425eb76e1 351
pathfindr 22:810425eb76e1 352 //Temperature
pathfindr 22:810425eb76e1 353 test_count ++;
pathfindr 22:810425eb76e1 354 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 22:810425eb76e1 355 float temperature;
pathfindr 22:810425eb76e1 356 temperature = si7060.getTemperature();
pathfindr 27:fa76f5a08195 357 if (temperature > -40 && temperature < 60) {
pathfindr 27:fa76f5a08195 358 test_pass ++;
pathfindr 27:fa76f5a08195 359 } else {
pathfindr 27:fa76f5a08195 360 addToExceptionString("T.");
pathfindr 27:fa76f5a08195 361 }
pathfindr 22:810425eb76e1 362
pathfindr 22:810425eb76e1 363 //Result
pathfindr 22:810425eb76e1 364 if (test_count == test_pass) {
pathfindr 29:059fc7324328 365 addToExceptionString("OK.");
pathfindr 22:810425eb76e1 366 return true;
pathfindr 22:810425eb76e1 367 } else {
pathfindr 29:059fc7324328 368 addToExceptionString("FAIL.");
pathfindr 22:810425eb76e1 369 return false;
pathfindr 9:b0a1535b8ef2 370 }
pathfindr 9:b0a1535b8ef2 371 }
pathfindr 9:b0a1535b8ef2 372
pathfindr 9:b0a1535b8ef2 373 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 374 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 375 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 376 void checkMotion() {
pathfindr 9:b0a1535b8ef2 377 if (lis3dh_int2) {
pathfindr 29:059fc7324328 378 //if (GLOBAL_debugLED) LED1blink(1,50);
pathfindr 9:b0a1535b8ef2 379 RET_motionTriggered = true;
pathfindr 13:29f67f256709 380 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 9:b0a1535b8ef2 381 if (!RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 382 RET_motionPendingOnState = true;
pathfindr 9:b0a1535b8ef2 383 RET_motionPendingOffState = false;
pathfindr 9:b0a1535b8ef2 384 // Log start motion time
pathfindr 13:29f67f256709 385 RET_motionStartTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 386 }
pathfindr 9:b0a1535b8ef2 387 } else {
pathfindr 9:b0a1535b8ef2 388 RET_motionTriggered = false;
pathfindr 9:b0a1535b8ef2 389 RET_motionPendingOnState = false;
pathfindr 9:b0a1535b8ef2 390 if (!RET_motionPendingOffState) {
pathfindr 9:b0a1535b8ef2 391 RET_motionPendingOffState = true;
pathfindr 9:b0a1535b8ef2 392 //log stop motion time
pathfindr 13:29f67f256709 393 RET_motionStopTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 394 }
pathfindr 9:b0a1535b8ef2 395 }
pathfindr 9:b0a1535b8ef2 396 //calculate motion state
pathfindr 9:b0a1535b8ef2 397 if (RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 398 //check if above threshold
pathfindr 24:c161db07557f 399 time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime) + 2; //Plus 2 to account for rounding and non exact clocks
pathfindr 24:c161db07557f 400 if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) {
pathfindr 9:b0a1535b8ef2 401 RET_motionState = true;
pathfindr 20:5404841fdd2b 402 RET_motionTriggeredInTXInterval = true;
pathfindr 29:059fc7324328 403 if (GLOBAL_debugLED) LED1blink(1,50);
pathfindr 24:c161db07557f 404 if (RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 24:c161db07557f 405 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 24:c161db07557f 406 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
pathfindr 23:a3b0ccf75ca5 407 }
pathfindr 9:b0a1535b8ef2 408 }
pathfindr 9:b0a1535b8ef2 409 }
pathfindr 9:b0a1535b8ef2 410 if (RET_motionPendingOffState) {
pathfindr 24:c161db07557f 411 time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime) + 2; //Plus 2 to account for rounding and non exact clocks
pathfindr 24:c161db07557f 412 if (noMotionForSeconds >= RET_setting_motion_stop_seconds && RET_motionState == true) {
pathfindr 24:c161db07557f 413 RET_motionPendingOffState = false;
pathfindr 9:b0a1535b8ef2 414 RET_motionState = false;
pathfindr 20:5404841fdd2b 415 GLOBAL_motionStopFlagTriggered = true;
pathfindr 9:b0a1535b8ef2 416 RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0);
pathfindr 29:059fc7324328 417 if (GLOBAL_debugLED) LED1blink(2,50);
pathfindr 24:c161db07557f 418 if (RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 24:c161db07557f 419 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 24:c161db07557f 420 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
pathfindr 23:a3b0ccf75ca5 421 }
pathfindr 9:b0a1535b8ef2 422 }
pathfindr 9:b0a1535b8ef2 423 }
pathfindr 8:7351f8c4cd60 424 }
pathfindr 8:7351f8c4cd60 425
pathfindr 14:9a54b1b65bc8 426 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 427 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 428 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 429 bool saveSettings(char* settingsBuffer) {
pathfindr 18:22edaa7e74b1 430 int matchCount = 0;
pathfindr 19:22261767c87a 431 int critical_fail_count = 0;
pathfindr 22:810425eb76e1 432 time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1;
pathfindr 19:22261767c87a 433 int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1;
pathfindr 22:810425eb76e1 434 if ( (matchCount = sscanf(settingsBuffer,"b:%u,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,p:%d,q:%d,r:%d,s:%d",
pathfindr 22:810425eb76e1 435 &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,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) {
pathfindr 29:059fc7324328 436 DEBUG("FROMSERVER: b:%u,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,p:%d,q:%d,r:%d,s:%d\n",
pathfindr 22:810425eb76e1 437 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,TEMP_p,TEMP_q,TEMP_r,TEMP_s);
pathfindr 18:22edaa7e74b1 438
pathfindr 27:fa76f5a08195 439 //FAILUREMODE need to be checking these against checksums
pathfindr 18:22edaa7e74b1 440 char changed;
pathfindr 31:c84fc6d8eaa3 441 if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b; RET_RTCunixtime_bu = TEMP_b; changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
pathfindr 21:e0b866630c27 442 if(TEMP_c != -1) { RET_setting_firmware = TEMP_c; changed = 'Y'; } else { changed = 'N'; RET_setting_firmware = -1;}; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
pathfindr 19:22261767c87a 443 if(TEMP_d != -1) { setState(TEMP_d); changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
pathfindr 22:810425eb76e1 444 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_mode:%d..%c\n",RET_setting_location_mode,changed);
pathfindr 27:fa76f5a08195 445 if(RET_setting_location_mode < 0 || RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
pathfindr 19:22261767c87a 446 if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
pathfindr 27:fa76f5a08195 447 if(RET_setting_location_accuracy < 0 || RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
pathfindr 19:22261767c87a 448 if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_interval_mins:%d..%c\n",RET_setting_location_tx_interval_mins,changed);
pathfindr 21:e0b866630c27 449 if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed);
pathfindr 27:fa76f5a08195 450 if(RET_setting_location_tx_failsafe_hrs < 0 || RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
pathfindr 21:e0b866630c27 451 if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed);
pathfindr 27:fa76f5a08195 452 if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
pathfindr 22:810425eb76e1 453 if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_hrs:%d..%c\n",RET_setting_activity_tx_interval_hrs,changed);
pathfindr 21:e0b866630c27 454 if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed);
pathfindr 21:e0b866630c27 455 if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed);
pathfindr 27:fa76f5a08195 456 if(RET_setting_motion_g < 6 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
pathfindr 21:e0b866630c27 457 if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed);
pathfindr 21:e0b866630c27 458 if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed);
pathfindr 21:e0b866630c27 459 if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed);
pathfindr 21:e0b866630c27 460 if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed);
pathfindr 21:e0b866630c27 461 if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed);
pathfindr 27:fa76f5a08195 462 if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
pathfindr 21:e0b866630c27 463 if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed);
pathfindr 21:e0b866630c27 464 if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,changed);
pathfindr 18:22edaa7e74b1 465
pathfindr 19:22261767c87a 466 if (critical_fail_count == 0) {
pathfindr 19:22261767c87a 467 DEBUG("GOT SETTINGS OK\n");
pathfindr 20:5404841fdd2b 468 saveEventTimes();
pathfindr 19:22261767c87a 469 RET_haveSettings = true;
pathfindr 19:22261767c87a 470 return true;
pathfindr 19:22261767c87a 471 } else {
pathfindr 19:22261767c87a 472 DEBUG("CRITICAL FAILS:%d\n",critical_fail_count);
pathfindr 19:22261767c87a 473 RET_haveSettings = false;
pathfindr 19:22261767c87a 474 return false;
pathfindr 19:22261767c87a 475 }
pathfindr 14:9a54b1b65bc8 476 } else {
pathfindr 15:7aad9a7f970c 477 RET_haveSettings = false;
pathfindr 15:7aad9a7f970c 478 return false;
pathfindr 14:9a54b1b65bc8 479 }
pathfindr 14:9a54b1b65bc8 480 }
pathfindr 24:c161db07557f 481
pathfindr 20:5404841fdd2b 482 void saveEventTimes() {
pathfindr 20:5404841fdd2b 483 //SET EVENT TIMES
pathfindr 20:5404841fdd2b 484 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 485 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 20:5404841fdd2b 486 DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
pathfindr 20:5404841fdd2b 487 }
pathfindr 20:5404841fdd2b 488 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 489 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 20:5404841fdd2b 490 DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
pathfindr 20:5404841fdd2b 491 }
pathfindr 22:810425eb76e1 492 if(RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 24:c161db07557f 493 RET_motionFrameStart = RET_RTCunixtime; //SET START FRAME INITAL
pathfindr 22:810425eb76e1 494 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600));
pathfindr 20:5404841fdd2b 495 DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
pathfindr 20:5404841fdd2b 496 }
pathfindr 20:5404841fdd2b 497 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 498 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 20:5404841fdd2b 499 DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
pathfindr 20:5404841fdd2b 500 }
pathfindr 20:5404841fdd2b 501 }
pathfindr 13:29f67f256709 502
pathfindr 13:29f67f256709 503 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 504 // SETUP
pathfindr 13:29f67f256709 505 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 506 bool setup() {
pathfindr 13:29f67f256709 507 bool pass = true;
pathfindr 25:7adeb1a53360 508 float voltage = getBatteryV();
pathfindr 25:7adeb1a53360 509 float temperature = getTemperature();
pathfindr 25:7adeb1a53360 510 bool selftestresult = selfTest();
pathfindr 21:e0b866630c27 511 Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 13:29f67f256709 512 if (modem.on()) {
pathfindr 25:7adeb1a53360 513 //RET_imei = modem.getIMEI();
pathfindr 25:7adeb1a53360 514 //DEBUG("imei: %lld \n",RET_imei);
pathfindr 23:a3b0ccf75ca5 515 //char* modemModel = modem.getModemModel();
pathfindr 28:24b02608fa5f 516 char locString[70];
pathfindr 28:24b02608fa5f 517 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
pathfindr 28:24b02608fa5f 518 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 22:810425eb76e1 519 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 17:ba55c026b1d6 520 char bytestosend[160];
pathfindr 28:24b02608fa5f 521 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
pathfindr 17:ba55c026b1d6 522 char result[180];
pathfindr 17:ba55c026b1d6 523 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
pathfindr 16:3bf5f1a5f869 524 if (result != "err") {
pathfindr 29:059fc7324328 525 //DEBUG("\nfromserver: %s \n",result);
pathfindr 19:22261767c87a 526 if (!saveSettings(result)) {
pathfindr 19:22261767c87a 527 //something went critically wrong getting settings
pathfindr 19:22261767c87a 528 pass = false;
pathfindr 19:22261767c87a 529 }
pathfindr 16:3bf5f1a5f869 530 }
pathfindr 13:29f67f256709 531 } else {
pathfindr 16:3bf5f1a5f869 532 //FAILUREMODE modem failed to register on network
pathfindr 31:c84fc6d8eaa3 533 DEBUG("NET REGISTER FAIL");
pathfindr 19:22261767c87a 534 pass = false;
pathfindr 13:29f67f256709 535 }
pathfindr 13:29f67f256709 536 } else {
pathfindr 19:22261767c87a 537 //FAILUREMODE Modem failed to turn on
pathfindr 31:c84fc6d8eaa3 538 DEBUG("MODEM ON FAIL");
pathfindr 31:c84fc6d8eaa3 539 LED1blink(200,50);
pathfindr 13:29f67f256709 540 pass = false;
pathfindr 13:29f67f256709 541 }
pathfindr 13:29f67f256709 542
pathfindr 21:e0b866630c27 543 //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 544 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 21:e0b866630c27 545
pathfindr 13:29f67f256709 546 //RESULT
pathfindr 19:22261767c87a 547 return pass;
pathfindr 13:29f67f256709 548 }
pathfindr 13:29f67f256709 549
pathfindr 19:22261767c87a 550 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 551 // EVENTS
pathfindr 19:22261767c87a 552 //------------------------------------------------------------------------------
pathfindr 28:24b02608fa5f 553 void event_connectiontest_tx(int location_accuracy) {
pathfindr 28:24b02608fa5f 554 DEBUG("TEST EVENT\n");
pathfindr 25:7adeb1a53360 555 float voltage = getBatteryV();
pathfindr 22:810425eb76e1 556 float temperature = getTemperature();
pathfindr 27:fa76f5a08195 557 int selftestresult = selfTest();
pathfindr 21:e0b866630c27 558 Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 20:5404841fdd2b 559 if (modem.on()) {
pathfindr 28:24b02608fa5f 560 char locString[70];
pathfindr 28:24b02608fa5f 561 memcpy(locString, modem.getLocation(location_accuracy, RET_setting_location_timeout), 70);
pathfindr 28:24b02608fa5f 562 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 22:810425eb76e1 563 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 22:810425eb76e1 564 char bytestosend[160];
pathfindr 29:059fc7324328 565 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
pathfindr 22:810425eb76e1 566 char result[180];
pathfindr 22:810425eb76e1 567 bool getSettings = true;
pathfindr 22:810425eb76e1 568 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
pathfindr 22:810425eb76e1 569 if (result != "err") {
pathfindr 28:24b02608fa5f 570 //DEBUG("\nfromserver: %s \n",result);
pathfindr 22:810425eb76e1 571 if (getSettings) {
pathfindr 22:810425eb76e1 572 saveSettings(result);
pathfindr 27:fa76f5a08195 573 LED1blink(6,250);
pathfindr 22:810425eb76e1 574 }
pathfindr 22:810425eb76e1 575 }
pathfindr 22:810425eb76e1 576 }
pathfindr 31:c84fc6d8eaa3 577 } else {
pathfindr 31:c84fc6d8eaa3 578 LED1blink(200,50);
pathfindr 22:810425eb76e1 579 }
pathfindr 22:810425eb76e1 580 //RESETS
pathfindr 22:810425eb76e1 581 RET_motionTriggeredInTXInterval = 0;
pathfindr 22:810425eb76e1 582 saveEventTimes();
pathfindr 22:810425eb76e1 583 }
pathfindr 22:810425eb76e1 584
pathfindr 22:810425eb76e1 585 void event_location_tx() {
pathfindr 22:810425eb76e1 586 DEBUG("LOCATION TX\n");
pathfindr 25:7adeb1a53360 587 float voltage = getBatteryV();
pathfindr 22:810425eb76e1 588 float temperature = getTemperature();
pathfindr 27:fa76f5a08195 589 int selfTestResult = selfTest();
pathfindr 22:810425eb76e1 590 Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 22:810425eb76e1 591 if (modem.on()) {
pathfindr 23:a3b0ccf75ca5 592 char locString[70];
pathfindr 31:c84fc6d8eaa3 593 memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70);
pathfindr 31:c84fc6d8eaa3 594 //DEBUG("locString:%s-\n",locString);
pathfindr 22:810425eb76e1 595 //SEND DATA
pathfindr 28:24b02608fa5f 596 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 23:a3b0ccf75ca5 597 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 23:a3b0ccf75ca5 598 char bytesToSend[160];
pathfindr 29:059fc7324328 599 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
pathfindr 22:810425eb76e1 600 char result[180];
pathfindr 22:810425eb76e1 601 bool getSettings = true;
pathfindr 23:a3b0ccf75ca5 602 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
pathfindr 22:810425eb76e1 603 if (result != "err") {
pathfindr 23:a3b0ccf75ca5 604 //DEBUG("\nfromserver: %s \n",result);
pathfindr 22:810425eb76e1 605 if (getSettings) {
pathfindr 22:810425eb76e1 606 saveSettings(result);
pathfindr 22:810425eb76e1 607 }
pathfindr 22:810425eb76e1 608 }
pathfindr 22:810425eb76e1 609 }
pathfindr 31:c84fc6d8eaa3 610 } else {
pathfindr 31:c84fc6d8eaa3 611 LED1blink(200,50);
pathfindr 20:5404841fdd2b 612 }
pathfindr 20:5404841fdd2b 613 //RESETS
pathfindr 20:5404841fdd2b 614 RET_motionTriggeredInTXInterval = 0;
pathfindr 20:5404841fdd2b 615 saveEventTimes();
pathfindr 19:22261767c87a 616 }
pathfindr 24:c161db07557f 617
pathfindr 24:c161db07557f 618 void event_activity_tx() {
pathfindr 24:c161db07557f 619 DEBUG("ACTIVITY TX\n");
pathfindr 24:c161db07557f 620 float temperature = getTemperature();
pathfindr 24:c161db07557f 621 Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 24:c161db07557f 622 if (modem.on()) {
pathfindr 24:c161db07557f 623 //SEND DATA
pathfindr 28:24b02608fa5f 624 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
pathfindr 24:c161db07557f 625 char bytesToSend[160];
pathfindr 24:c161db07557f 626 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHours);
pathfindr 24:c161db07557f 627 char result[180];
pathfindr 24:c161db07557f 628 bool getResponse = false;
pathfindr 24:c161db07557f 629 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 2, GLOBAL_defaultApi));
pathfindr 24:c161db07557f 630 if (result != "err") {
pathfindr 24:c161db07557f 631 //RESET ACTIVITY FRAME
pathfindr 24:c161db07557f 632 memset(RET_activityData,0,sizeof(RET_activityData));
pathfindr 29:059fc7324328 633 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 24:c161db07557f 634 }
pathfindr 24:c161db07557f 635 }
pathfindr 31:c84fc6d8eaa3 636 } else {
pathfindr 31:c84fc6d8eaa3 637 LED1blink(200,50);
pathfindr 24:c161db07557f 638 }
pathfindr 24:c161db07557f 639 //RESETS
pathfindr 24:c161db07557f 640 RET_motionTriggeredInTXInterval = 0;
pathfindr 24:c161db07557f 641 saveEventTimes();
pathfindr 24:c161db07557f 642 }
pathfindr 24:c161db07557f 643
pathfindr 7:e9a19750700d 644 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 645 // STATE ENGINE
pathfindr 7:e9a19750700d 646 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 647 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 648 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 649 case STATE_SETUP :
pathfindr 19:22261767c87a 650 DEBUG("STATE:SETUP\n");
pathfindr 22:810425eb76e1 651 factoryReset();
pathfindr 13:29f67f256709 652 if (setup()) {
pathfindr 25:7adeb1a53360 653 // All good
pathfindr 19:22261767c87a 654 } else {
pathfindr 31:c84fc6d8eaa3 655 LED1blink(200,50);
pathfindr 22:810425eb76e1 656 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
pathfindr 19:22261767c87a 657 setState(STATE_DORMANT);
pathfindr 31:c84fc6d8eaa3 658 DEBUG("SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 13:29f67f256709 659 }
pathfindr 6:388d3c7efdd9 660 break;
pathfindr 16:3bf5f1a5f869 661 case STATE_NORMAL :
pathfindr 19:22261767c87a 662 DEBUG("STATE:NORMAL\n");
pathfindr 7:e9a19750700d 663 //check and log motion
pathfindr 9:b0a1535b8ef2 664 checkMotion();
pathfindr 9:b0a1535b8ef2 665
pathfindr 24:c161db07557f 666 //LOCATION EVENT
pathfindr 19:22261767c87a 667 bool run_location_tx = false;
pathfindr 27:fa76f5a08195 668 switch (RET_setting_location_mode) {
pathfindr 27:fa76f5a08195 669 case 1: //INTERVAL POST
pathfindr 27:fa76f5a08195 670 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 671 break;
pathfindr 27:fa76f5a08195 672 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 27:fa76f5a08195 673 if(RET_motionTriggeredInTXInterval && RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 674 break;
pathfindr 27:fa76f5a08195 675 case 3: //POST ON STOP MOTION
pathfindr 27:fa76f5a08195 676 if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
pathfindr 27:fa76f5a08195 677 break;
pathfindr 27:fa76f5a08195 678 }
pathfindr 22:810425eb76e1 679 if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
pathfindr 25:7adeb1a53360 680 if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
pathfindr 19:22261767c87a 681
pathfindr 27:fa76f5a08195 682 //if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 683 //Location Failsafe timer catchall
pathfindr 27:fa76f5a08195 684 //if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 685 //if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
pathfindr 27:fa76f5a08195 686
pathfindr 24:c161db07557f 687 //ACTIVITY EVENT
pathfindr 24:c161db07557f 688 bool run_activity_tx = false;
pathfindr 25:7adeb1a53360 689 if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0 && strlen(RET_activityData) > 1) {
pathfindr 29:059fc7324328 690 run_activity_tx = true;
pathfindr 9:b0a1535b8ef2 691 }
pathfindr 24:c161db07557f 692 if (strlen(RET_activityData) > 130) { run_activity_tx = true; }
pathfindr 25:7adeb1a53360 693 if (run_activity_tx) { DEBUG("ACTIVITY TX...\n"); event_activity_tx();}
pathfindr 9:b0a1535b8ef2 694 break;
pathfindr 16:3bf5f1a5f869 695 case STATE_DORMANT :
pathfindr 22:810425eb76e1 696 if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
pathfindr 22:810425eb76e1 697 if (RET_haveSettings) {
pathfindr 22:810425eb76e1 698 setState(STATE_NORMAL);
pathfindr 22:810425eb76e1 699 } else {
pathfindr 22:810425eb76e1 700 setState(STATE_SETUP);
pathfindr 22:810425eb76e1 701 }
pathfindr 19:22261767c87a 702 DEBUG("WAKING UP FROM DORMANT\n");
pathfindr 7:e9a19750700d 703 }
pathfindr 19:22261767c87a 704 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 12:8345612bf867 705 break;
pathfindr 16:3bf5f1a5f869 706 case STATE_BUTTONPRESS1 :
pathfindr 19:22261767c87a 707 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 708 DEBUG("STATE:BUTTONPRESS1\n");
pathfindr 12:8345612bf867 709 LED1blink(1,300);
pathfindr 9:b0a1535b8ef2 710 break;
pathfindr 16:3bf5f1a5f869 711 case STATE_BUTTONPRESS2 :
pathfindr 19:22261767c87a 712 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 713 DEBUG("STATE:BUTTONPRESS2\n");
pathfindr 19:22261767c87a 714 LED1blink(2,300);
pathfindr 27:fa76f5a08195 715 LED1on(0);
pathfindr 28:24b02608fa5f 716 event_connectiontest_tx(1);
pathfindr 28:24b02608fa5f 717 LED1off();
pathfindr 28:24b02608fa5f 718 break;
pathfindr 28:24b02608fa5f 719 case STATE_BUTTONPRESS3 :
pathfindr 28:24b02608fa5f 720 setState(STATE_NORMAL);
pathfindr 28:24b02608fa5f 721 DEBUG("STATE:BUTTONPRESS3\n");
pathfindr 28:24b02608fa5f 722 LED1blink(3,300);
pathfindr 28:24b02608fa5f 723 LED1on(0);
pathfindr 28:24b02608fa5f 724 event_connectiontest_tx(2);
pathfindr 27:fa76f5a08195 725 LED1off();
pathfindr 9:b0a1535b8ef2 726 break;
pathfindr 16:3bf5f1a5f869 727 case STATE_BUTTONHOLD :
pathfindr 19:22261767c87a 728 DEBUG("STATE:BUTTONHOLD\n");
pathfindr 19:22261767c87a 729 if (RET_state_prev == STATE_NORMAL) {
pathfindr 19:22261767c87a 730 setState(STATE_DORMANT);
pathfindr 19:22261767c87a 731 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
pathfindr 19:22261767c87a 732 DEBUG("TURNING OFF\n");
pathfindr 19:22261767c87a 733 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 19:22261767c87a 734 LED1on(5000);
pathfindr 19:22261767c87a 735 } else {
pathfindr 19:22261767c87a 736 if (RET_haveSettings) {
pathfindr 19:22261767c87a 737 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 738 } else {
pathfindr 19:22261767c87a 739 setState(STATE_SETUP);
pathfindr 19:22261767c87a 740 }
pathfindr 19:22261767c87a 741 DEBUG("TURNING ON\n");
pathfindr 19:22261767c87a 742 LED1blink(20,100);
pathfindr 19:22261767c87a 743 }
pathfindr 6:388d3c7efdd9 744 break;
pathfindr 6:388d3c7efdd9 745 default :
pathfindr 19:22261767c87a 746 setState(STATE_SETUP);
pathfindr 19:22261767c87a 747 DEBUG("DEFAULT STATE\n");
pathfindr 6:388d3c7efdd9 748 }
pathfindr 6:388d3c7efdd9 749 }
pathfindr 6:388d3c7efdd9 750
pathfindr 6:388d3c7efdd9 751
pathfindr 7:e9a19750700d 752 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 753 // MAIN
pathfindr 7:e9a19750700d 754 //------------------------------------------------------------------------------
pathfindr 0:94b743e06998 755 int main() {
pathfindr 24:c161db07557f 756 //INIT
pathfindr 9:b0a1535b8ef2 757 turnOffEverything();
pathfindr 31:c84fc6d8eaa3 758 if (!memoryIntegrityCheck()) { RET_coldBoot = 1;}
pathfindr 24:c161db07557f 759 RTCticker.attach(&RTCtick, 1.0);
pathfindr 24:c161db07557f 760 button.fall(&buttonPress); //does this affect power?
pathfindr 24:c161db07557f 761 button.rise(&buttonRelease);
pathfindr 4:8d8e9bfa82e4 762
pathfindr 4:8d8e9bfa82e4 763 //CHECK IF THIS IS RESET
pathfindr 4:8d8e9bfa82e4 764 //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 765 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 4:8d8e9bfa82e4 766 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 767 case 0x00000001 :
pathfindr 29:059fc7324328 768 DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);
pathfindr 13:29f67f256709 769 RET_coldBoot = 1;
pathfindr 31:c84fc6d8eaa3 770 dumpSettings();
pathfindr 4:8d8e9bfa82e4 771 break;
pathfindr 4:8d8e9bfa82e4 772 case 0x00000002 :
pathfindr 29:059fc7324328 773 DEBUG("Watchdog ST:%d\n",RET_state);
pathfindr 4:8d8e9bfa82e4 774 break;
pathfindr 4:8d8e9bfa82e4 775 case 0x00000004 :
pathfindr 29:059fc7324328 776 DEBUG("Soft ST:%d SLP:%d\n",RET_state, RET_sleepToggle);
pathfindr 4:8d8e9bfa82e4 777 break;
pathfindr 4:8d8e9bfa82e4 778 }
pathfindr 4:8d8e9bfa82e4 779 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 4:8d8e9bfa82e4 780 }
pathfindr 24:c161db07557f 781
pathfindr 21:e0b866630c27 782
pathfindr 9:b0a1535b8ef2 783 //CHECK FOR FIRST BOOT
pathfindr 24:c161db07557f 784 if (RET_coldBoot == 1) { factoryReset(); dumpSettings(); }
pathfindr 9:b0a1535b8ef2 785
pathfindr 24:c161db07557f 786 //SHALL WE SLEEP
pathfindr 24:c161db07557f 787 if (RET_sleepToggle == 1) {
pathfindr 24:c161db07557f 788 RET_sleepToggle = 0;
pathfindr 24:c161db07557f 789 NRF_UART0->TASKS_SUSPEND = 1;
pathfindr 24:c161db07557f 790 NRF_UART0->ENABLE = 0; //NO MORE SERIAL OR DEBUG OUTPUTS AFTER THIS
pathfindr 26:fa3579737329 791 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 24:c161db07557f 792 system_reset();
pathfindr 24:c161db07557f 793 }
pathfindr 13:29f67f256709 794
pathfindr 25:7adeb1a53360 795 //while(true) {
pathfindr 20:5404841fdd2b 796 watchdogKick();
pathfindr 13:29f67f256709 797 //LOG START TIME
pathfindr 13:29f67f256709 798 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 799
pathfindr 9:b0a1535b8ef2 800 //MAIN LOGIC
pathfindr 24:c161db07557f 801 DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
pathfindr 24:c161db07557f 802 DEBUG("ACTIVITY:%s\n",RET_activityData);
pathfindr 9:b0a1535b8ef2 803 mainStateEngine();
pathfindr 9:b0a1535b8ef2 804
pathfindr 9:b0a1535b8ef2 805 //PRE-SLEEP ACTIONS
pathfindr 18:22edaa7e74b1 806 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(10,127); }
pathfindr 25:7adeb1a53360 807 turnOffEverything();
pathfindr 22:810425eb76e1 808
pathfindr 24:c161db07557f 809 //SLEEP DIFFERENT FOR THIS SHAKER TEST VERSION - AS IS WILL GIVE 450ua sleep if in motion
pathfindr 24:c161db07557f 810 /*
pathfindr 22:810425eb76e1 811 NRF_UART0->TASKS_SUSPEND = 1;
pathfindr 22:810425eb76e1 812 NRF_UART0->ENABLE = 0; //NO MORE SERIAL OR DEBUG OUTPUTS AFTER THIS
pathfindr 26:fa3579737329 813 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 24:c161db07557f 814 */
pathfindr 25:7adeb1a53360 815
pathfindr 25:7adeb1a53360 816 RET_coldBoot = 0;
pathfindr 24:c161db07557f 817 RET_sleepToggle = 1;
pathfindr 22:810425eb76e1 818 system_reset();
pathfindr 25:7adeb1a53360 819 //}
pathfindr 7:e9a19750700d 820 }