init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Sun Dec 23 20:24:34 2018 +0000
Revision:
25:7adeb1a53360
Parent:
24:c161db07557f
Child:
26:fa3579737329
gfg

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