init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Dec 20 22:04:45 2018 +0000
Revision:
21:e0b866630c27
Parent:
20:5404841fdd2b
Child:
22:810425eb76e1
rr

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 21:e0b866630c27 4 //FUNCTION PROTOTYPES
pathfindr 21:e0b866630c27 5 //------------------------------------------------------------------------------
pathfindr 21:e0b866630c27 6 #define FW_VERSION 1
pathfindr 4:8d8e9bfa82e4 7
pathfindr 0:94b743e06998 8 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 9 //FUNCTION PROTOTYPES
pathfindr 0:94b743e06998 10 //------------------------------------------------------------------------------
pathfindr 4:8d8e9bfa82e4 11 static void mainStateEngine(void);
pathfindr 4:8d8e9bfa82e4 12 static void selftest(void);
pathfindr 7:e9a19750700d 13 static void buttonPress(void);
pathfindr 7:e9a19750700d 14 static void buttonRelease(void);
pathfindr 20:5404841fdd2b 15 static void saveEventTimes(void);
pathfindr 0:94b743e06998 16
pathfindr 0:94b743e06998 17 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 18 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
pathfindr 0:94b743e06998 19 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 20 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 13:29f67f256709 21 bool GLOBAL_accel_healthy = false;
pathfindr 13:29f67f256709 22 bool GLOBAL_requireSoftReset = false;
pathfindr 20:5404841fdd2b 23 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 13:29f67f256709 24 bool GLOBAL_debugLED = false;
pathfindr 13:29f67f256709 25 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 13:29f67f256709 26 bool GLOBAL_registeredOnNetwork = false;
pathfindr 13:29f67f256709 27 long long GLOBAL_imei = 0;
pathfindr 13:29f67f256709 28 float GLOBAL_voltage = 0.0;
pathfindr 13:29f67f256709 29 time_t GLOBAL_RTCunixtime = 0;
pathfindr 13:29f67f256709 30 time_t GLOBAL_wakeTime = 0;
pathfindr 0:94b743e06998 31
pathfindr 0:94b743e06998 32 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 33 //RETAINED NOINIT RAM VARS
pathfindr 0:94b743e06998 34 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 35 #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
pathfindr 13:29f67f256709 36 //IDENTITY
pathfindr 16:3bf5f1a5f869 37 static long long RET_imei __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 38 static char RET_pf_identifier[7] __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 39 //SETTINGS
pathfindr 16:3bf5f1a5f869 40 static int RET_setting_firmware __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 41 static uint8_t RET_setting_location_mode __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 42 static uint8_t RET_setting_location_accuracy __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 43 static uint32_t RET_setting_location_tx_interval_mins __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 44 static uint32_t RET_setting_location_tx_failsafe_hrs __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 45 static uint16_t RET_setting_location_timeout __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 46 static uint8_t RET_setting_activity_mode __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 47 static uint32_t RET_setting_activity_tx_interval_mins __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 16:3bf5f1a5f869 58 static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 59 static bool RET_haveSettings __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 60 static uint8_t RET_state __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 61 static uint8_t RET_state_prev __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 62 static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 63 static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 64 static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 65 static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 66 static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init));
pathfindr 21:e0b866630c27 67 static time_t RET_SetupRunAt __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 68 //MOTION STATE
pathfindr 16:3bf5f1a5f869 69 static bool RET_motionTriggered __attribute__((section("noinit"),zero_init));
pathfindr 20:5404841fdd2b 70 static bool RET_motionTriggeredInTXInterval __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 71 static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 72 static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 73 static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 74 static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 75 static bool RET_motionState __attribute__((section("noinit"),zero_init));
pathfindr 16:3bf5f1a5f869 76 static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 77 static char RET_activityData[130] __attribute__((section("noinit"),zero_init));
pathfindr 19:22261767c87a 78 //TO MAKE THE ABOVE BIGGER YOU NEED TO ALLOCATE MORE NOINIT MEMORY!!!!!
pathfindr 19:22261767c87a 79
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 4:8d8e9bfa82e4 91 #elif defined ( __GNUC__ )
pathfindr 4:8d8e9bfa82e4 92 #elif defined ( __ICCARM__ )
pathfindr 4:8d8e9bfa82e4 93 #endif
pathfindr 4:8d8e9bfa82e4 94
pathfindr 0:94b743e06998 95 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 96 //GPIO
pathfindr 9:b0a1535b8ef2 97 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 98 InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep
pathfindr 15:7aad9a7f970c 99 DigitalOut vreg_en(PN_VREG_EN);
pathfindr 9:b0a1535b8ef2 100
pathfindr 9:b0a1535b8ef2 101 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 102 //PERIPHERALS
pathfindr 0:94b743e06998 103 //------------------------------------------------------------------------------
pathfindr 4:8d8e9bfa82e4 104 //BLE myble;
pathfindr 20:5404841fdd2b 105 WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 20:5404841fdd2b 106 void watchdogKick() {watchdog.kick();}
pathfindr 11:60eb0ff945f2 107 //SERIAL DEBUG?
pathfindr 11:60eb0ff945f2 108 #if CONSOLE_DEBUG
pathfindr 11:60eb0ff945f2 109 Serial uart(PN_UART_TX, PN_UART_RX, 115200);
pathfindr 4:8d8e9bfa82e4 110 #endif
pathfindr 11:60eb0ff945f2 111
pathfindr 6:388d3c7efdd9 112 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 113 //TIMERS
pathfindr 18:22edaa7e74b1 114 //-----------------------------------------------mi-------------------------------
pathfindr 9:b0a1535b8ef2 115 LowPowerTicker RTCticker; //no impact on power consumption
pathfindr 9:b0a1535b8ef2 116
pathfindr 9:b0a1535b8ef2 117 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 118 //THREAD SEMAPHORES
pathfindr 9:b0a1535b8ef2 119 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 120 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 121
pathfindr 9:b0a1535b8ef2 122 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 123 //SINGLETONS
pathfindr 6:388d3c7efdd9 124 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 125 //NVStore &nvstore = NVStore::get_instance();
pathfindr 6:388d3c7efdd9 126
pathfindr 7:e9a19750700d 127 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 128 // LOW LEVEL FUNCS
pathfindr 7:e9a19750700d 129 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 130 void turnOffEverything() {
pathfindr 15:7aad9a7f970c 131 vreg_en = 0;
pathfindr 9:b0a1535b8ef2 132 LED1off();
pathfindr 9:b0a1535b8ef2 133 }
pathfindr 4:8d8e9bfa82e4 134 void gotoSleep(long sleep_milliseconds) {
pathfindr 9:b0a1535b8ef2 135 turnOffEverything();
pathfindr 20:5404841fdd2b 136 watchdogKick();
pathfindr 13:29f67f256709 137 if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false
pathfindr 4:8d8e9bfa82e4 138 system_reset();
pathfindr 4:8d8e9bfa82e4 139 }
pathfindr 9:b0a1535b8ef2 140 mainthread.wait(sleep_milliseconds);
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 19:22261767c87a 147 void getBatteryV() {
pathfindr 19:22261767c87a 148 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 149 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 150 batteryIn.calibrate();
pathfindr 19:22261767c87a 151 for (uint8_t i = 1; i < 3; i++) { // need to get it 3 times to get accurate data, first one is always low for some reason
pathfindr 19:22261767c87a 152 batteryIn.updateData();
pathfindr 19:22261767c87a 153 GLOBAL_voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
pathfindr 19:22261767c87a 154 }
pathfindr 19:22261767c87a 155 }
pathfindr 19:22261767c87a 156 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 157 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 158 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 159 void buttonPress() {
pathfindr 19:22261767c87a 160 RET_buttonPressTime = RET_RTCunixtime;
pathfindr 19:22261767c87a 161 }
pathfindr 19:22261767c87a 162 void buttonRelease() {
pathfindr 19:22261767c87a 163 RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime);
pathfindr 19:22261767c87a 164 RET_buttonPressCount ++;
pathfindr 19:22261767c87a 165 }
pathfindr 19:22261767c87a 166 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 167 // RTC TICKER
pathfindr 19:22261767c87a 168 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 169 void RTCtick() {
pathfindr 19:22261767c87a 170 //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
pathfindr 19:22261767c87a 171 RET_RTCunixtime += 1;
pathfindr 13:29f67f256709 172 GLOBAL_RTCunixtime = RET_RTCunixtime;
pathfindr 19:22261767c87a 173
pathfindr 9:b0a1535b8ef2 174 //button logic - check for hold
pathfindr 19:22261767c87a 175 if (RET_buttonHoldTime > 4) {
pathfindr 9:b0a1535b8ef2 176 RET_buttonHoldTime = 0;
pathfindr 9:b0a1535b8ef2 177 RET_buttonPressCount = 0;
pathfindr 16:3bf5f1a5f869 178 RET_state_prev = RET_state;
pathfindr 16:3bf5f1a5f869 179 RET_state = STATE_BUTTONHOLD;
pathfindr 9:b0a1535b8ef2 180 mainthread.release();
pathfindr 9:b0a1535b8ef2 181 } else {
pathfindr 19:22261767c87a 182 if((RET_RTCunixtime - RET_buttonPressTime) > 1) {
pathfindr 9:b0a1535b8ef2 183 switch (RET_buttonPressCount) {
pathfindr 9:b0a1535b8ef2 184 case 1 :
pathfindr 19:22261767c87a 185 if (RET_state == STATE_NORMAL) {
pathfindr 19:22261767c87a 186 RET_state = STATE_BUTTONPRESS1;
pathfindr 19:22261767c87a 187 mainthread.release();
pathfindr 19:22261767c87a 188 }
pathfindr 9:b0a1535b8ef2 189 break;
pathfindr 9:b0a1535b8ef2 190 case 2 :
pathfindr 19:22261767c87a 191 if (RET_state == STATE_NORMAL) {
pathfindr 19:22261767c87a 192 RET_state = STATE_BUTTONPRESS2;
pathfindr 19:22261767c87a 193 mainthread.release();
pathfindr 19:22261767c87a 194 }
pathfindr 9:b0a1535b8ef2 195 break;
pathfindr 9:b0a1535b8ef2 196 default :
pathfindr 9:b0a1535b8ef2 197 //do nothing
pathfindr 9:b0a1535b8ef2 198 break;
pathfindr 9:b0a1535b8ef2 199 }
pathfindr 9:b0a1535b8ef2 200 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 201 }
pathfindr 9:b0a1535b8ef2 202 }
pathfindr 19:22261767c87a 203 //SETUP STATE VISUALISE
pathfindr 19:22261767c87a 204 if (RET_state == STATE_SETUP) {
pathfindr 19:22261767c87a 205 led1 = !led1;
pathfindr 19:22261767c87a 206 }
pathfindr 9:b0a1535b8ef2 207 }
pathfindr 7:e9a19750700d 208 void factoryReset() {
pathfindr 13:29f67f256709 209 DEBUG("Factory Reset \n");
pathfindr 21:e0b866630c27 210 Thread::wait(200); //wait to ensure that devices are turned off
pathfindr 16:3bf5f1a5f869 211 //IDENTITY
pathfindr 13:29f67f256709 212 RET_imei = 0;
pathfindr 16:3bf5f1a5f869 213 memset(RET_pf_identifier,0,sizeof(RET_pf_identifier));
pathfindr 19:22261767c87a 214 //STATE
pathfindr 19:22261767c87a 215 RET_coldBoot = 1;
pathfindr 19:22261767c87a 216 RET_haveSettings = 0;
pathfindr 19:22261767c87a 217 RET_state = STATE_SETUP;
pathfindr 19:22261767c87a 218 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 219 RET_RTCunixtime = 0;
pathfindr 21:e0b866630c27 220 RET_SetupRunAt = 0;
pathfindr 16:3bf5f1a5f869 221 //SETTINGS
pathfindr 16:3bf5f1a5f869 222 RET_setting_firmware = 0;
pathfindr 19:22261767c87a 223 RET_setting_location_mode = 2;
pathfindr 16:3bf5f1a5f869 224 RET_setting_location_accuracy = 1;
pathfindr 16:3bf5f1a5f869 225 RET_setting_location_tx_interval_mins = 1440;
pathfindr 16:3bf5f1a5f869 226 RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
pathfindr 21:e0b866630c27 227 RET_setting_location_timeout = 240;
pathfindr 16:3bf5f1a5f869 228 RET_setting_activity_tx_interval_mins = 0;
pathfindr 16:3bf5f1a5f869 229 RET_setting_environmental_tx_interval_mins = 0;
pathfindr 16:3bf5f1a5f869 230 RET_setting_motion_g = 11;
pathfindr 16:3bf5f1a5f869 231 RET_setting_motion_start_seconds = 120;
pathfindr 16:3bf5f1a5f869 232 RET_setting_motion_stop_seconds = 120;
pathfindr 16:3bf5f1a5f869 233 RET_setting_impact_g = 0;
pathfindr 16:3bf5f1a5f869 234 RET_setting_impact_alert = 0;
pathfindr 16:3bf5f1a5f869 235 RET_setting_connection_timeout = 180;
pathfindr 16:3bf5f1a5f869 236 RET_setting_beacon_interval_seconds = 10;
pathfindr 16:3bf5f1a5f869 237 RET_setting_beacon_scan = 0;
pathfindr 19:22261767c87a 238 //RET_RTCmillis = 0;
pathfindr 16:3bf5f1a5f869 239 RET_buttonPressCount = 0;
pathfindr 16:3bf5f1a5f869 240 RET_buttonPressTime = 0;
pathfindr 16:3bf5f1a5f869 241 RET_buttonReleaseTime = 0;
pathfindr 16:3bf5f1a5f869 242 RET_buttonHoldTime = 0;
pathfindr 16:3bf5f1a5f869 243 //MOTION STATE
pathfindr 10:c8798fd9773b 244 RET_motionTriggered = 0;
pathfindr 20:5404841fdd2b 245 RET_motionTriggeredInTXInterval = 0;
pathfindr 7:e9a19750700d 246 RET_motionStartTime = 0;
pathfindr 7:e9a19750700d 247 RET_motionStopTime = 0;
pathfindr 16:3bf5f1a5f869 248 RET_motionPendingOnState = 0;
pathfindr 16:3bf5f1a5f869 249 RET_motionPendingOffState = 0;
pathfindr 16:3bf5f1a5f869 250 RET_motionState = 0;
pathfindr 9:b0a1535b8ef2 251 RET_motionTotalActivityHours = 0.0;
pathfindr 16:3bf5f1a5f869 252 memset(RET_activityData,0,sizeof(RET_activityData));
pathfindr 16:3bf5f1a5f869 253 //IMPACT
pathfindr 16:3bf5f1a5f869 254 RET_impactTriggered = 0;
pathfindr 16:3bf5f1a5f869 255 //EVENT HANDLING
pathfindr 16:3bf5f1a5f869 256 RET_eventTime_location_log = 0;
pathfindr 16:3bf5f1a5f869 257 RET_eventTime_location_tx = 0;
pathfindr 16:3bf5f1a5f869 258 RET_eventTime_environmental_log = 0;
pathfindr 16:3bf5f1a5f869 259 RET_eventTime_environmental_tx = 0;
pathfindr 16:3bf5f1a5f869 260 RET_eventTime_activity_tx = 0;
pathfindr 16:3bf5f1a5f869 261 RET_eventTime_wakeFromDormant = 0;
pathfindr 16:3bf5f1a5f869 262 //OTHER LOCAL GLOBALS
pathfindr 16:3bf5f1a5f869 263 GLOBAL_requireSoftReset = false;
pathfindr 16:3bf5f1a5f869 264 //PERIPHERAL RESET
pathfindr 10:c8798fd9773b 265 lis3dh_configureForSleep(10,127);
pathfindr 13:29f67f256709 266 }
pathfindr 13:29f67f256709 267 void copyRETtoGLOBAL() {
pathfindr 15:7aad9a7f970c 268 GLOBAL_imei = RET_imei;
pathfindr 7:e9a19750700d 269 }
pathfindr 9:b0a1535b8ef2 270 void selfTest() {
pathfindr 8:7351f8c4cd60 271 //Accelerometer
pathfindr 8:7351f8c4cd60 272 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 8:7351f8c4cd60 273 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 274 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 9:b0a1535b8ef2 275 if (lis3dh_id == 51) {
pathfindr 9:b0a1535b8ef2 276 LED1blink(10,50);
pathfindr 9:b0a1535b8ef2 277 }
pathfindr 9:b0a1535b8ef2 278 }
pathfindr 9:b0a1535b8ef2 279
pathfindr 9:b0a1535b8ef2 280 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 281 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 282 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 283 void checkMotion() {
pathfindr 9:b0a1535b8ef2 284 if (lis3dh_int2) {
pathfindr 13:29f67f256709 285 if (GLOBAL_debugLED) LED1blink(2,100);
pathfindr 9:b0a1535b8ef2 286 RET_motionTriggered = true;
pathfindr 13:29f67f256709 287 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 9:b0a1535b8ef2 288 if (!RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 289 RET_motionPendingOnState = true;
pathfindr 9:b0a1535b8ef2 290 RET_motionPendingOffState = false;
pathfindr 9:b0a1535b8ef2 291 // Log start motion time
pathfindr 13:29f67f256709 292 RET_motionStartTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 293 }
pathfindr 9:b0a1535b8ef2 294 } else {
pathfindr 13:29f67f256709 295 if (GLOBAL_debugLED) LED1blink(2,500);
pathfindr 9:b0a1535b8ef2 296 RET_motionTriggered = false;
pathfindr 9:b0a1535b8ef2 297 RET_motionPendingOnState = false;
pathfindr 9:b0a1535b8ef2 298 if (!RET_motionPendingOffState) {
pathfindr 9:b0a1535b8ef2 299 RET_motionPendingOffState = true;
pathfindr 9:b0a1535b8ef2 300 //log stop motion time
pathfindr 13:29f67f256709 301 RET_motionStopTime = RET_RTCunixtime;
pathfindr 9:b0a1535b8ef2 302 }
pathfindr 9:b0a1535b8ef2 303 }
pathfindr 9:b0a1535b8ef2 304 //calculate motion state
pathfindr 9:b0a1535b8ef2 305 if (RET_motionPendingOnState) {
pathfindr 9:b0a1535b8ef2 306 //check if above threshold
pathfindr 13:29f67f256709 307 time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime);
pathfindr 16:3bf5f1a5f869 308 if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
pathfindr 9:b0a1535b8ef2 309 RET_motionState = true;
pathfindr 20:5404841fdd2b 310 RET_motionTriggeredInTXInterval = true;
pathfindr 13:29f67f256709 311 if (GLOBAL_debugLED) LED1blink(10,100);
pathfindr 9:b0a1535b8ef2 312 }
pathfindr 9:b0a1535b8ef2 313 }
pathfindr 9:b0a1535b8ef2 314 if (RET_motionPendingOffState) {
pathfindr 13:29f67f256709 315 time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime);
pathfindr 16:3bf5f1a5f869 316 if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
pathfindr 9:b0a1535b8ef2 317 RET_motionState = false;
pathfindr 20:5404841fdd2b 318 GLOBAL_motionStopFlagTriggered = true;
pathfindr 9:b0a1535b8ef2 319 RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0);
pathfindr 13:29f67f256709 320 if (GLOBAL_debugLED) LED1blink(5,500);
pathfindr 9:b0a1535b8ef2 321 }
pathfindr 9:b0a1535b8ef2 322 }
pathfindr 8:7351f8c4cd60 323 }
pathfindr 8:7351f8c4cd60 324
pathfindr 14:9a54b1b65bc8 325 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 326 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 327 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 328 bool saveSettings(char* settingsBuffer) {
pathfindr 14:9a54b1b65bc8 329 //process result
pathfindr 18:22edaa7e74b1 330 int matchCount = 0;
pathfindr 19:22261767c87a 331 int critical_fail_count = 0;
pathfindr 19:22261767c87a 332 char TEMP_a[7]; 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 333 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 17:ba55c026b1d6 334 if ( (matchCount = sscanf(settingsBuffer,"a:%c%c%c%c%c%c,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 17:ba55c026b1d6 335 &TEMP_a[0],&TEMP_a[1],&TEMP_a[2],&TEMP_a[3],&TEMP_a[4],&TEMP_a[5],&TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) {
pathfindr 17:ba55c026b1d6 336 DEBUG("VALUES: a:%s,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 17:ba55c026b1d6 337 TEMP_a,TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s);
pathfindr 18:22edaa7e74b1 338
pathfindr 18:22edaa7e74b1 339 //FAILUREMODE need to be checking thse against checksums
pathfindr 18:22edaa7e74b1 340 char changed;
pathfindr 18:22edaa7e74b1 341 //FAILUREMODE need to verify the identifier against a reg exp or similar
pathfindr 19:22261767c87a 342 if(1==1) { memcpy(RET_pf_identifier, TEMP_a, sizeof(TEMP_a)); changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_pf_identifier:%s..%c\n",RET_pf_identifier,changed);
pathfindr 19:22261767c87a 343 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 344 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 345 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 19:22261767c87a 346 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
pathfindr 19:22261767c87a 347 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 348 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 349 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 350 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 21:e0b866630c27 351 if(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,changed);
pathfindr 21:e0b866630c27 352 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 353 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 354 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 355 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 356 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 357 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 358 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 359 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 360 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 361
pathfindr 19:22261767c87a 362 if (critical_fail_count == 0) {
pathfindr 19:22261767c87a 363 DEBUG("GOT SETTINGS OK\n");
pathfindr 20:5404841fdd2b 364 saveEventTimes();
pathfindr 19:22261767c87a 365 RET_haveSettings = true;
pathfindr 19:22261767c87a 366 return true;
pathfindr 19:22261767c87a 367 } else {
pathfindr 19:22261767c87a 368 DEBUG("CRITICAL FAILS:%d\n",critical_fail_count);
pathfindr 19:22261767c87a 369 RET_haveSettings = false;
pathfindr 19:22261767c87a 370 return false;
pathfindr 19:22261767c87a 371 }
pathfindr 14:9a54b1b65bc8 372 } else {
pathfindr 15:7aad9a7f970c 373 RET_haveSettings = false;
pathfindr 15:7aad9a7f970c 374 return false;
pathfindr 14:9a54b1b65bc8 375 }
pathfindr 14:9a54b1b65bc8 376 }
pathfindr 21:e0b866630c27 377 bool dumpSettings() {
pathfindr 21:e0b866630c27 378 DEBUG("RET_pf_identifier:%s \n",RET_pf_identifier);
pathfindr 21:e0b866630c27 379 DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
pathfindr 21:e0b866630c27 380 DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
pathfindr 21:e0b866630c27 381 DEBUG("RET_state:%d \n",RET_state);
pathfindr 21:e0b866630c27 382 DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
pathfindr 21:e0b866630c27 383 DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
pathfindr 21:e0b866630c27 384 DEBUG("RET_setting_location_tx_interval_mins:%d \n",RET_setting_location_tx_interval_mins);
pathfindr 21:e0b866630c27 385 DEBUG("RET_setting_location_tx_failsafe_hrs:%d \n",RET_setting_location_tx_failsafe_hrs);
pathfindr 21:e0b866630c27 386 DEBUG("RET_setting_location_timeout:%d \n",RET_setting_location_timeout);
pathfindr 21:e0b866630c27 387 DEBUG("RET_setting_activity_tx_interval_mins:%d \n",RET_setting_activity_tx_interval_mins);
pathfindr 21:e0b866630c27 388 DEBUG("RET_setting_environmental_tx_interval_mins:%d \n",RET_setting_environmental_tx_interval_mins);
pathfindr 21:e0b866630c27 389 DEBUG("RET_setting_motion_g:%d \n",RET_setting_motion_g);
pathfindr 21:e0b866630c27 390 DEBUG("RET_setting_motion_start_seconds:%d \n",RET_setting_motion_start_seconds);
pathfindr 21:e0b866630c27 391 DEBUG("RET_setting_motion_stop_seconds:%d \n",RET_setting_motion_stop_seconds);
pathfindr 21:e0b866630c27 392 DEBUG("RET_setting_impact_g:%d \n",RET_setting_impact_g);
pathfindr 21:e0b866630c27 393 DEBUG("RET_setting_impact_alert:%d \n",RET_setting_impact_alert);
pathfindr 21:e0b866630c27 394 DEBUG("RET_setting_connection_timeout:%d \n",RET_setting_connection_timeout);
pathfindr 21:e0b866630c27 395 DEBUG("RET_setting_beacon_interval_seconds:%d \n",RET_setting_beacon_interval_seconds);
pathfindr 21:e0b866630c27 396 DEBUG("RET_setting_beacon_scan:%d \n",RET_setting_beacon_scan);
pathfindr 21:e0b866630c27 397 }
pathfindr 20:5404841fdd2b 398 void saveEventTimes() {
pathfindr 20:5404841fdd2b 399 //SET EVENT TIMES
pathfindr 20:5404841fdd2b 400 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 401 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 20:5404841fdd2b 402 DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
pathfindr 20:5404841fdd2b 403 }
pathfindr 20:5404841fdd2b 404 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 405 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 20:5404841fdd2b 406 DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
pathfindr 20:5404841fdd2b 407 }
pathfindr 20:5404841fdd2b 408 if(RET_setting_activity_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 409 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_mins * 60));
pathfindr 20:5404841fdd2b 410 DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
pathfindr 20:5404841fdd2b 411 }
pathfindr 20:5404841fdd2b 412 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 413 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 20:5404841fdd2b 414 DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
pathfindr 20:5404841fdd2b 415 }
pathfindr 20:5404841fdd2b 416 }
pathfindr 13:29f67f256709 417
pathfindr 13:29f67f256709 418 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 419 // SETUP
pathfindr 13:29f67f256709 420 //------------------------------------------------------------------------------
pathfindr 13:29f67f256709 421 bool setup() {
pathfindr 13:29f67f256709 422 bool pass = true;
pathfindr 21:e0b866630c27 423 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 424 if (modem.on()) {
pathfindr 13:29f67f256709 425 RET_imei = modem.getIMEI();
pathfindr 14:9a54b1b65bc8 426 GLOBAL_imei = RET_imei;
pathfindr 13:29f67f256709 427 DEBUG("imei: %lld \n",RET_imei);
pathfindr 20:5404841fdd2b 428 if (modem.registerOnNetwork(2,180000)) {
pathfindr 17:ba55c026b1d6 429 char bytestosend[160];
pathfindr 17:ba55c026b1d6 430 snprintf(bytestosend,sizeof(bytestosend),"(%s,im:%lld,v:%.2f,fr:1,z:SETUP,c:1)\0",GLOBAL_defaultApi,GLOBAL_imei,GLOBAL_voltage);
pathfindr 17:ba55c026b1d6 431 char result[180];
pathfindr 17:ba55c026b1d6 432 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
pathfindr 16:3bf5f1a5f869 433 if (result != "err") {
pathfindr 21:e0b866630c27 434 DEBUG("\nfromserver: %s \n",result);
pathfindr 19:22261767c87a 435 if (!saveSettings(result)) {
pathfindr 19:22261767c87a 436 //something went critically wrong getting settings
pathfindr 19:22261767c87a 437 pass = false;
pathfindr 19:22261767c87a 438 }
pathfindr 16:3bf5f1a5f869 439 }
pathfindr 13:29f67f256709 440 } else {
pathfindr 16:3bf5f1a5f869 441 //FAILUREMODE modem failed to register on network
pathfindr 19:22261767c87a 442 pass = false;
pathfindr 13:29f67f256709 443 }
pathfindr 13:29f67f256709 444 } else {
pathfindr 19:22261767c87a 445 //FAILUREMODE Modem failed to turn on
pathfindr 13:29f67f256709 446 pass = false;
pathfindr 13:29f67f256709 447 }
pathfindr 13:29f67f256709 448
pathfindr 21:e0b866630c27 449 //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 450 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 21:e0b866630c27 451
pathfindr 13:29f67f256709 452 //RESULT
pathfindr 19:22261767c87a 453 return pass;
pathfindr 13:29f67f256709 454 }
pathfindr 13:29f67f256709 455
pathfindr 19:22261767c87a 456 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 457 // EVENTS
pathfindr 19:22261767c87a 458 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 459 void event_location_tx() {
pathfindr 20:5404841fdd2b 460 DEBUG("PERFORM LOCATION TX\n");
pathfindr 20:5404841fdd2b 461 getBatteryV();
pathfindr 21:e0b866630c27 462 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 463 if (modem.on()) {
pathfindr 20:5404841fdd2b 464 //RET_setting_location_accuracy
pathfindr 20:5404841fdd2b 465 modem.getLocation(true, 3, true, RET_setting_location_timeout);
pathfindr 20:5404841fdd2b 466 }
pathfindr 20:5404841fdd2b 467 //RESETS
pathfindr 20:5404841fdd2b 468 RET_motionTriggeredInTXInterval = 0;
pathfindr 20:5404841fdd2b 469 saveEventTimes();
pathfindr 19:22261767c87a 470 }
pathfindr 7:e9a19750700d 471 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 472 // STATE ENGINE
pathfindr 7:e9a19750700d 473 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 474 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 475 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 476 case STATE_SETUP :
pathfindr 19:22261767c87a 477 DEBUG("STATE:SETUP\n");
pathfindr 19:22261767c87a 478 getBatteryV();
pathfindr 13:29f67f256709 479 if (setup()) {
pathfindr 14:9a54b1b65bc8 480 copyRETtoGLOBAL();
pathfindr 19:22261767c87a 481 } else {
pathfindr 19:22261767c87a 482 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (72*3600)); //72hrs
pathfindr 19:22261767c87a 483 setState(STATE_DORMANT);
pathfindr 19:22261767c87a 484 DEBUG("SETSTATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 13:29f67f256709 485 }
pathfindr 6:388d3c7efdd9 486 break;
pathfindr 16:3bf5f1a5f869 487 case STATE_NORMAL :
pathfindr 19:22261767c87a 488 DEBUG("STATE:NORMAL\n");
pathfindr 7:e9a19750700d 489 //check and log motion
pathfindr 9:b0a1535b8ef2 490 checkMotion();
pathfindr 9:b0a1535b8ef2 491
pathfindr 9:b0a1535b8ef2 492 //EVENTS
pathfindr 19:22261767c87a 493
pathfindr 19:22261767c87a 494 //LOCATION
pathfindr 19:22261767c87a 495 //Location Log
pathfindr 21:e0b866630c27 496 /*
pathfindr 21:e0b866630c27 497 if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) {
pathfindr 21:e0b866630c27 498 }
pathfindr 21:e0b866630c27 499 */
pathfindr 19:22261767c87a 500 bool run_location_tx = false;
pathfindr 19:22261767c87a 501 switch (RET_setting_location_mode) {
pathfindr 19:22261767c87a 502 case 1: //INTERVAL POST
pathfindr 20:5404841fdd2b 503 if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 20:5404841fdd2b 504 DEBUG("INTERVAL LOC TX...\n");
pathfindr 20:5404841fdd2b 505 run_location_tx = true;
pathfindr 20:5404841fdd2b 506 }
pathfindr 19:22261767c87a 507 break;
pathfindr 19:22261767c87a 508 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 20:5404841fdd2b 509 if(RET_motionTriggeredInTXInterval && RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 20:5404841fdd2b 510 DEBUG("INTERVAL LOC TX WMC...\n");
pathfindr 20:5404841fdd2b 511 run_location_tx = true;
pathfindr 20:5404841fdd2b 512 }
pathfindr 19:22261767c87a 513 break;
pathfindr 19:22261767c87a 514 case 3: //POST ON STOP MOTION
pathfindr 20:5404841fdd2b 515 if (GLOBAL_motionStopFlagTriggered) {
pathfindr 20:5404841fdd2b 516 DEBUG("MOTION STOP LOC TX...\n");
pathfindr 20:5404841fdd2b 517 run_location_tx = true;
pathfindr 20:5404841fdd2b 518 GLOBAL_motionStopFlagTriggered = false;
pathfindr 20:5404841fdd2b 519 }
pathfindr 19:22261767c87a 520 break;
pathfindr 9:b0a1535b8ef2 521 }
pathfindr 20:5404841fdd2b 522 //Failsafe timer catchall
pathfindr 21:e0b866630c27 523 if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
pathfindr 21:e0b866630c27 524 if (run_location_tx) {event_location_tx();}
pathfindr 19:22261767c87a 525
pathfindr 19:22261767c87a 526 //Environmental Log
pathfindr 12:8345612bf867 527 if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
pathfindr 19:22261767c87a 528 /*
pathfindr 14:9a54b1b65bc8 529 Filesystem filesystem(BD_PAGE_ADDRESS,BD_TOTAL_SIZE);
pathfindr 12:8345612bf867 530 SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
pathfindr 12:8345612bf867 531 float temperature_c = temperature.getTemperature();
pathfindr 19:22261767c87a 532 DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c);
pathfindr 12:8345612bf867 533 filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c);
pathfindr 19:22261767c87a 534 */
pathfindr 9:b0a1535b8ef2 535 }
pathfindr 19:22261767c87a 536 //Environmental TX
pathfindr 16:3bf5f1a5f869 537 if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 538 //getBatteryV();
pathfindr 9:b0a1535b8ef2 539 //event_environmental_bc();
pathfindr 12:8345612bf867 540 //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
pathfindr 7:e9a19750700d 541 }
pathfindr 19:22261767c87a 542 //Activity TX
pathfindr 16:3bf5f1a5f869 543 if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
pathfindr 20:5404841fdd2b 544 //event_activity_tx();
pathfindr 9:b0a1535b8ef2 545 }
pathfindr 9:b0a1535b8ef2 546 break;
pathfindr 16:3bf5f1a5f869 547 case STATE_DORMANT :
pathfindr 12:8345612bf867 548 if (RET_RTCunixtime > RET_eventTime_wakeFromDormant) {
pathfindr 19:22261767c87a 549 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 550 DEBUG("WAKING UP FROM DORMANT\n");
pathfindr 7:e9a19750700d 551 }
pathfindr 19:22261767c87a 552 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 12:8345612bf867 553 break;
pathfindr 16:3bf5f1a5f869 554 case STATE_BUTTONPRESS1 :
pathfindr 19:22261767c87a 555 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 556 DEBUG("STATE:BUTTONPRESS1\n");
pathfindr 12:8345612bf867 557 LED1blink(1,300);
pathfindr 9:b0a1535b8ef2 558 break;
pathfindr 16:3bf5f1a5f869 559 case STATE_BUTTONPRESS2 :
pathfindr 19:22261767c87a 560 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 561 DEBUG("STATE:BUTTONPRESS2\n");
pathfindr 19:22261767c87a 562 LED1blink(2,300);
pathfindr 9:b0a1535b8ef2 563 break;
pathfindr 16:3bf5f1a5f869 564 case STATE_BUTTONHOLD :
pathfindr 19:22261767c87a 565 DEBUG("STATE:BUTTONHOLD\n");
pathfindr 19:22261767c87a 566 if (RET_state_prev == STATE_NORMAL) {
pathfindr 19:22261767c87a 567 setState(STATE_DORMANT);
pathfindr 19:22261767c87a 568 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
pathfindr 19:22261767c87a 569 DEBUG("TURNING OFF\n");
pathfindr 19:22261767c87a 570 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
pathfindr 19:22261767c87a 571 LED1on(5000);
pathfindr 19:22261767c87a 572 } else {
pathfindr 19:22261767c87a 573 if (RET_haveSettings) {
pathfindr 19:22261767c87a 574 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 575 } else {
pathfindr 19:22261767c87a 576 setState(STATE_SETUP);
pathfindr 19:22261767c87a 577 }
pathfindr 19:22261767c87a 578 DEBUG("TURNING ON\n");
pathfindr 19:22261767c87a 579 LED1blink(20,100);
pathfindr 19:22261767c87a 580 }
pathfindr 6:388d3c7efdd9 581 break;
pathfindr 6:388d3c7efdd9 582 default :
pathfindr 19:22261767c87a 583 setState(STATE_SETUP);
pathfindr 19:22261767c87a 584 DEBUG("DEFAULT STATE\n");
pathfindr 6:388d3c7efdd9 585 }
pathfindr 6:388d3c7efdd9 586 }
pathfindr 6:388d3c7efdd9 587
pathfindr 6:388d3c7efdd9 588
pathfindr 7:e9a19750700d 589 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 590 // MAIN
pathfindr 7:e9a19750700d 591 //------------------------------------------------------------------------------
pathfindr 0:94b743e06998 592 int main() {
pathfindr 9:b0a1535b8ef2 593 turnOffEverything();
pathfindr 4:8d8e9bfa82e4 594
pathfindr 4:8d8e9bfa82e4 595 //CHECK IF THIS IS RESET
pathfindr 4:8d8e9bfa82e4 596 //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 597 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 4:8d8e9bfa82e4 598 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 599 case 0x00000001 :
pathfindr 19:22261767c87a 600 DEBUG("reset_reason: 0x%08x. - Hard Reset - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
pathfindr 13:29f67f256709 601 RET_coldBoot = 1;
pathfindr 4:8d8e9bfa82e4 602 break;
pathfindr 4:8d8e9bfa82e4 603 case 0x00000002 :
pathfindr 19:22261767c87a 604 DEBUG("reset_reason: 0x%08x. - Watchdog - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
pathfindr 4:8d8e9bfa82e4 605 break;
pathfindr 4:8d8e9bfa82e4 606 case 0x00000004 :
pathfindr 19:22261767c87a 607 DEBUG("reset_reason: 0x%08x. - Soft reset - CSTATE:%d\n",NRF_POWER->RESETREAS, RET_state);
pathfindr 4:8d8e9bfa82e4 608 break;
pathfindr 4:8d8e9bfa82e4 609 }
pathfindr 4:8d8e9bfa82e4 610 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 4:8d8e9bfa82e4 611 }
pathfindr 21:e0b866630c27 612 //DEBUG
pathfindr 21:e0b866630c27 613 dumpSettings();
pathfindr 21:e0b866630c27 614
pathfindr 9:b0a1535b8ef2 615 //CHECK FOR FIRST BOOT
pathfindr 20:5404841fdd2b 616 if (RET_coldBoot == 1) { factoryReset(); }
pathfindr 9:b0a1535b8ef2 617
pathfindr 20:5404841fdd2b 618 //INIT
pathfindr 20:5404841fdd2b 619 RTCticker.attach(&RTCtick, 1.0);
pathfindr 20:5404841fdd2b 620 button.fall(&buttonPress); //does this affect power?
pathfindr 20:5404841fdd2b 621 button.rise(&buttonRelease);
pathfindr 13:29f67f256709 622 copyRETtoGLOBAL();
pathfindr 13:29f67f256709 623
pathfindr 0:94b743e06998 624 while(true) {
pathfindr 9:b0a1535b8ef2 625 //SLEEP
pathfindr 13:29f67f256709 626 if (RET_coldBoot != 1) gotoSleep(10000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
pathfindr 20:5404841fdd2b 627 watchdogKick();
pathfindr 9:b0a1535b8ef2 628
pathfindr 13:29f67f256709 629 //LOG START TIME
pathfindr 13:29f67f256709 630 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 631
pathfindr 9:b0a1535b8ef2 632 //MAIN LOGIC
pathfindr 21:e0b866630c27 633 DEBUG("STATE:%d, RTC_SECONDS:%u, SETUPAT:%u\n", RET_state, RET_RTCunixtime,RET_SetupRunAt);
pathfindr 20:5404841fdd2b 634
pathfindr 20:5404841fdd2b 635 event_location_tx();
pathfindr 20:5404841fdd2b 636 while(1) {
pathfindr 20:5404841fdd2b 637 }
pathfindr 20:5404841fdd2b 638
pathfindr 9:b0a1535b8ef2 639 mainStateEngine();
pathfindr 9:b0a1535b8ef2 640
pathfindr 9:b0a1535b8ef2 641 //PRE-SLEEP ACTIONS
pathfindr 18:22edaa7e74b1 642 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(10,127); }
pathfindr 9:b0a1535b8ef2 643 RET_coldBoot = 0;
pathfindr 0:94b743e06998 644 }
pathfindr 7:e9a19750700d 645 }