init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sun Dec 23 21:13:19 2018 +0000
Revision:
26:fa3579737329
Parent:
25:7adeb1a53360
Child:
27:fa76f5a08195
ggh

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