init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Jan 03 10:09:38 2019 +0000
Revision:
33:760005331b4c
Parent:
32:dff4858bdf37
Child:
34:4493c9f6d707
12

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