init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
main.cpp
- Committer:
- pathfindr
- Date:
- 2019-01-13
- Revision:
- 34:4493c9f6d707
- Parent:
- 33:760005331b4c
- Child:
- 35:d9421d57d116
File content as of revision 34:4493c9f6d707:
#include "main.h" //------------------------------------------------------------------------------ //DEFINES //------------------------------------------------------------------------------ #define FW_VERSION 1 //MODES #define USE_NRF_TEMP_SENSOR 1 //DEFAULT SETTINGS #define DEFAULT_SLEEP_FRAME 60000 #define DEFAULT_LOCATION_MODE 2 #define DEFAULT_LOCATION_ACCURACY 2 // 0 = no location, 1 = cl only, 2 = gps then cl #define DEFAULT_LOCATION_TX_INTERVAL_MINS 1440 #define DEFAULT_LOCATION_TX_FAILSAFE_HRS 168 #define DEFAULT_LOCATION_TIMEOUT 180 #define DEFAULT_MOTION_G 11 #define DEFAULT_MOTION_START_SECONDS 120 #define DEFAULT_MOTION_STOP_SECONDS 120 #define DEFAULT_IMPACT_G 127 #define DEFAULT_CONNECTION_ATTEMPTS 1 #define DEFAULT_CONNECTION_TIMEOUT 180 #define DEFAULT_BEACON_INTERVAL_SECONDS 10 //------------------------------------------------------------------------------ //FUNCTION PROTOTYPES //------------------------------------------------------------------------------ static void mainStateEngine(void); static void selftest(void); static void buttonPress(void); static void buttonRelease(void); static void dumpSettings(void); static void saveEventTimes(void); static void event_location_tx(void); //------------------------------------------------------------------------------ //GLOBAL VARS //------------------------------------------------------------------------------ char* GLOBAL_defaultApi = "b:gps2"; bool GLOBAL_accel_healthy = false; bool GLOBAL_motionStopFlagTriggered = false; bool GLOBAL_debugLED = true; bool GLOBAL_needToConfigureLis3dh = true; bool GLOBAL_registeredOnNetwork = false; bool GLOBAL_modemOn = false; bool GLOBAL_LEDSequenceinProgress = false; time_t GLOBAL_wakeTime = 0; char GLOBAL_exceptionString[15]; bool NRFuart_enabled = false; char GLOBAL_debug_buffer[200]; static int RET_setting_firmware; static uint32_t RET_setting_minimumupdate_hrs; static uint8_t RET_setting_location_mode; static uint8_t RET_setting_location_accuracy; static uint32_t RET_setting_location_tx_interval_mins; static uint32_t RET_setting_location_tx_failsafe_hrs; static uint16_t RET_setting_location_timeout; static uint32_t RET_setting_activity_tx_interval_hrs; static uint32_t RET_setting_environmental_tx_interval_mins; static uint16_t RET_setting_motion_g; static time_t RET_setting_motion_start_seconds; static time_t RET_setting_motion_stop_seconds; static uint16_t RET_setting_impact_g; static uint8_t RET_setting_impact_alert; static uint16_t RET_setting_connection_timeout; static uint16_t RET_setting_beacon_interval_seconds; static uint16_t RET_setting_beacon_scan; //STATE static uint8_t RET_coldBoot = 1; static bool RET_haveSettings; static uint8_t RET_state; static uint8_t RET_state_prev; static uint8_t RET_buttonPressCount; static time_t RET_buttonPressTime; static time_t RET_buttonHoldTime; static time_t RET_RTCunixtime; static time_t RET_SetupRunAt; static time_t RET_SettingsGotAt; //MOTION STATE static bool RET_motionTriggered; static bool RET_motionTriggeredInTXInterval; static time_t RET_motionStartTime; static time_t RET_motionStopTime; static bool RET_motionPendingOnState; static bool RET_motionPendingOffState; static bool RET_motionState; static float RET_motionTotalActivityHours; static time_t RET_motionFrameStart; static char RET_activityData[140]; //IMPACT static bool RET_impactTriggered; //EVENTS LOGGING static time_t RET_eventTime_location_log; static time_t RET_eventTime_environmental_log; //EVENTS TX static time_t RET_eventTime_location_tx; static time_t RET_eventTime_location_failsafe_tx; static time_t RET_eventTime_environmental_tx; static time_t RET_eventTime_activity_tx; static time_t RET_eventTime_wakeFromDormant; //------------------------------------------------------------------------------ //GPIO //------------------------------------------------------------------------------ InterruptIn button(PN_IN_BUTTON); //------------------------------------------------------------------------------ //PERIPHERALS //------------------------------------------------------------------------------ //BLE myble; WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice void watchdogKick() {watchdog.kick();} SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL); LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK); Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE); //------------------------------------------------------------------------------ //TIMERS //-----------------------------------------------mi------------------------------- LowPowerTicker RTCticker; //no impact on power consumption //------------------------------------------------------------------------------ //THREAD SEMAPHORES //------------------------------------------------------------------------------ Semaphore mainthread; //------------------------------------------------------------------------------ // LOW LEVEL FUNCS //------------------------------------------------------------------------------ void NRFuart_init_nohwfc() { if(NRFuart_enabled == false) { //Configure UART0 pins. nrf_gpio_cfg_output(PN_UART_TX); nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL); NRF_UART0->PSELTXD = PN_UART_TX; NRF_UART0->PSELRXD = PN_UART_RX; NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled; NRF_UART0->BAUDRATE = NRF_UART_BAUDRATE_115200; NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled; NRF_UART0->EVENTS_RXDRDY = 0; NRF_UART0->EVENTS_TXDRDY = 0; NRF_UART0->EVENTS_ERROR = 0; NRF_UART0->EVENTS_TXDRDY = 0; NRF_UART0->TASKS_STARTRX = 1; NRF_UART0->TASKS_STARTTX = 1; //NRF_UART0->INTENCLR = 0xffffffffUL; //NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Msk; /*NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) | (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) | (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos);*/ //NVIC_ClearPendingIRQ(UART0_IRQn); //NVIC_SetPriority(UART0_IRQn, 1); //3 //NVIC_EnableIRQ(UART0_IRQn); //NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler); } NRFuart_enabled = true; }; void NRFuart_uninit() { if (NRFuart_enabled) { NVIC_DisableIRQ(UART0_IRQn); NRF_UART0->INTENCLR = 0xffffffffUL; NRF_UART0->TASKS_STOPRX = 1; NRF_UART0->TASKS_STOPTX = 1; NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled; NRF_UART0->PSELTXD = 0xFFFFFFFF; NRF_UART0->PSELRXD = 0xFFFFFFFF; NRF_UART0->PSELRTS = 0xFFFFFFFF; NRF_UART0->PSELCTS = 0xFFFFFFFF; NRFuart_enabled = false; } }; void NRFuart_putc(char byte) { if (!NRFuart_enabled) NRFuart_init_nohwfc(); NRF_UART0->TXD = byte; uint32_t safetycounter = 0; while(NRF_UART0->EVENTS_TXDRDY != 1 && safetycounter < 10000) { safetycounter ++; // Wait for the current TXD data to be sent. } NRF_UART0->EVENTS_TXDRDY = 0; }; void NRFuart_puts(char* bytes) { if (!NRFuart_enabled) NRFuart_init_nohwfc(); for(int i = 0; bytes[i] != '\0'; i++) { NRFuart_putc(bytes[i]); } }; void NRFuart_puts_debug(char* bytes) { if (!NRFuart_enabled) NRFuart_init_nohwfc(); for(int i = 0; bytes[i] != '\0'; i++) { NRFuart_putc(bytes[i]); } NRFuart_putc('\n'); }; void debug_prep(){ memset(GLOBAL_debug_buffer, '\0', sizeof(GLOBAL_debug_buffer)); } void debug_exe(){ NRFuart_puts_debug(GLOBAL_debug_buffer); } char NRFuart_getc() { if (!NRFuart_enabled) NRFuart_init_nohwfc(); uint32_t safetycounter = 0; while(NRF_UART0->EVENTS_RXDRDY != 1 && safetycounter < 10000){ safetycounter ++; } NRF_UART0->EVENTS_RXDRDY = 0; return (uint8_t)NRF_UART0->RXD; }; char* NRFuart_gets(char terminator) { if (!NRFuart_enabled) NRFuart_init_nohwfc(); static char buffer[200]; int charindex = 0; memset(buffer,'\0',sizeof(buffer)); while(1) { if (NRF_UART0->EVENTS_RXDRDY == 0) { //Nothing available from the UART. continue; } else { char inbyte = NRFuart_getc(); if (inbyte == terminator) { break; } else { buffer[charindex] = inbyte; charindex++; } } } buffer[charindex] = '\n'; //make sure we end with whitespace lf return buffer; }; void NRFuart_flush() { if (!NRFuart_enabled) NRFuart_init_nohwfc(); //THIS HASNT BEEN TESTED char char1 = 0; while (NRFuart_readable()) { char1 = NRFuart_getc(); } }; bool NRFuart_readable() { if (!NRFuart_enabled) NRFuart_init_nohwfc(); return (NRF_UART0->EVENTS_RXDRDY == 1); }; #define CONSOLE_DEBUG 0 #if CONSOLE_DEBUG #define DEBUG(str, ... ) do{\ /*int n;\ char buffer[200];\ memset(buffer, '\0', sizeof(buffer));\ n = snprintf(buffer, sizeof(buffer), str, __VA_ARGS__);\ buffer[sizeof(buffer)-1] = 0x00;\ if(n < 0) { }\ else if(n >= sizeof(buffer)) { }\ else\ {\ NRFuart_puts_debug(buffer);\ }\ }while(0)*/ #else #define DEBUG(...) #endif void nrf_configureGPIOForSleep(){ nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL); nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL); } void setState(uint8_t state) { RET_state_prev = RET_state; RET_state = state; } void dumpSettings() { if(DEBUG_ON){ debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_RTCunixtime:%u", RET_RTCunixtime);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_firmware:%d", RET_setting_firmware);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_minimumupdate_hrs:%d", RET_setting_minimumupdate_hrs);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_state:%d", RET_state);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_mode:%d", RET_setting_location_mode);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_accuracy:%d", RET_setting_location_accuracy);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_interval_mins:%d", RET_setting_location_tx_interval_mins);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_failsafe_hrs:%d", RET_setting_location_tx_failsafe_hrs);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_timeout:%d", RET_setting_location_timeout);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_tx_interval_hrs:%d", RET_setting_activity_tx_interval_hrs);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_environmental_tx_interval_mins:%d", RET_setting_environmental_tx_interval_mins);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_g:%d", RET_setting_motion_g);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_start_seconds:%d", RET_setting_motion_start_seconds);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_stop_seconds:%d", RET_setting_motion_stop_seconds);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_g:%d", RET_setting_impact_g);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_alert:%d", RET_setting_impact_alert);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_connection_timeout:%d", RET_setting_connection_timeout);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_interval_seconds:%d", RET_setting_beacon_interval_seconds);debug_exe(); debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe(); } DEBUG("RET_RTCunixtime:%u",RET_RTCunixtime); DEBUG("RET_setting_firmware:%d",RET_setting_firmware); DEBUG("RET_setting_minimumupdate_hrs:%d",RET_setting_minimumupdate_hrs); DEBUG("RET_state:%d",RET_state); DEBUG("RET_setting_location_mode:%d",RET_setting_location_mode); DEBUG("RET_setting_location_accuracy:%d",RET_setting_location_accuracy); DEBUG("RET_setting_location_tx_interval_mins:%d",RET_setting_location_tx_interval_mins); DEBUG("RET_setting_location_tx_failsafe_hrs:%d",RET_setting_location_tx_failsafe_hrs); DEBUG("RET_setting_location_timeout:%d",RET_setting_location_timeout); DEBUG("RET_setting_activity_tx_interval_hrs:%d",RET_setting_activity_tx_interval_hrs); DEBUG("RET_setting_environmental_tx_interval_mins:%d",RET_setting_environmental_tx_interval_mins); DEBUG("RET_setting_motion_g:%d",RET_setting_motion_g); DEBUG("RET_setting_motion_start_seconds:%d",RET_setting_motion_start_seconds); DEBUG("RET_setting_motion_stop_seconds:%d",RET_setting_motion_stop_seconds); DEBUG("RET_setting_impact_g:%d",RET_setting_impact_g); DEBUG("RET_setting_impact_alert:%d",RET_setting_impact_alert); DEBUG("RET_setting_connection_timeout:%d",RET_setting_connection_timeout); DEBUG("RET_setting_beacon_interval_seconds:%d",RET_setting_beacon_interval_seconds); DEBUG("RET_setting_beacon_scan:%d",RET_setting_beacon_scan); } float getBatteryV() { NRF52_SAADC batteryIn; batteryIn.addChannel(9); // vdd for battery batteryIn.calibrate(); float voltage = 0.0; 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 batteryIn.updateData(); voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65; } return voltage; } float getTemperature() { float temperature; if (USE_NRF_TEMP_SENSOR) { //INTERNAL NRF52 TEMP SENSOR uint32_t safetycounter = 0; NRF_TEMP->TASKS_START=1; while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000); NRF_TEMP->EVENTS_DATARDY=0; temperature = nrf_temp_read()/4.0; NRF_TEMP->TASKS_STOP=1; } else { temperature = si7060.getTemperature(); //currently disabled because its causing a high current 450ua sleep, most likely due to sensor not sleeping correctly, or i2c sleep issue } return temperature; } void addToExceptionString(char* value) { snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),"%s.",value); } //------------------------------------------------------------------------------ // USER BUTTON HANDLING //------------------------------------------------------------------------------ void buttonPress() { RET_buttonPressTime = RET_RTCunixtime; } void buttonRelease() { RET_buttonHoldTime = (RET_RTCunixtime - RET_buttonPressTime); RET_buttonPressCount ++; } //------------------------------------------------------------------------------ // RTC TICKER //------------------------------------------------------------------------------ void RTCtick() { //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars RET_RTCunixtime += 1; //button logic - check for hold if (RET_buttonHoldTime > 4) { RET_buttonHoldTime = 0; RET_buttonPressCount = 0; RET_state_prev = RET_state; RET_state = STATE_BUTTONHOLD; mainthread.release(); } else { if((RET_RTCunixtime - RET_buttonPressTime) > 1) { switch (RET_buttonPressCount) { case 1 : if (RET_state == STATE_NORMAL) { RET_state = STATE_BUTTONPRESS1; mainthread.release(); } break; case 3 : if (RET_state == STATE_NORMAL) { RET_state = STATE_BUTTONPRESS2; mainthread.release(); } break; case 5 : if (RET_state == STATE_NORMAL) { RET_state = STATE_BUTTONPRESS3; mainthread.release(); } break; default : //do nothing break; } RET_buttonPressCount = 0; } } //SETUP STATE VISUALISE if (!GLOBAL_LEDSequenceinProgress && RET_state == STATE_SETUP) { led1 = !led1; } } void resetGlobals() { GLOBAL_accel_healthy = false; GLOBAL_motionStopFlagTriggered = false; memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString)); if (DEBUG_ON) {memset(GLOBAL_debug_buffer,0x00,sizeof(GLOBAL_debug_buffer));} } bool selfTest() { int test_count = 0; int test_pass = 0; //Accelerometer test_count ++; uint8_t lis3dh_id; lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id); if (lis3dh_id == 51) { test_pass ++; } else { addToExceptionString("A."); } //Temperature test_count ++; float temperature; if (USE_NRF_TEMP_SENSOR) { //INTERNAL NRF52 TEMP SENSOR uint32_t safetycounter = 0; NRF_TEMP->TASKS_START=1; while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000); NRF_TEMP->EVENTS_DATARDY=0; temperature = nrf_temp_read()/4.0; NRF_TEMP->TASKS_STOP=1; } else { temperature = si7060.getTemperature(); } if (temperature > -40 && temperature < 60) { test_pass ++; } else { addToExceptionString("T."); } //Result if (test_count == test_pass) { addToExceptionString("OK."); return true; } else { addToExceptionString("FAIL."); return false; } } //------------------------------------------------------------------------------ // MOTION FUNCS //------------------------------------------------------------------------------ void checkMotion() { if (lis3dh_int2) { //if (GLOBAL_debugLED) LED1blink(1,50); RET_motionTriggered = true; GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it if (!RET_motionPendingOnState) { RET_motionPendingOnState = true; RET_motionPendingOffState = false; // Log start motion time RET_motionStartTime = RET_RTCunixtime; } } else { RET_motionTriggered = false; RET_motionPendingOnState = false; if (!RET_motionPendingOffState) { RET_motionPendingOffState = true; //log stop motion time RET_motionStopTime = RET_RTCunixtime; } } //calculate motion state if (RET_motionPendingOnState) { //check if above threshold time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime) + 2; //Plus 2 to account for rounding and non exact clocks if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) { RET_motionState = true; RET_motionTriggeredInTXInterval = true; if (GLOBAL_debugLED) LED1blink(1,50); if (RET_setting_location_tx_failsafe_hrs > 0) { time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60); sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins); } } } if (RET_motionPendingOffState) { time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime) + 2; //Plus 2 to account for rounding and non exact clocks if (noMotionForSeconds >= RET_setting_motion_stop_seconds && RET_motionState == true) { RET_motionPendingOffState = false; RET_motionState = false; GLOBAL_motionStopFlagTriggered = true; RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); if (GLOBAL_debugLED) LED1blink(2,50); if (RET_setting_location_tx_failsafe_hrs > 0) { time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60); sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins); } } } } //------------------------------------------------------------------------------ // UPDATE OPERATING SETTINGS //------------------------------------------------------------------------------ bool saveSettings(char* settingsBuffer) { int matchCount = 0; int critical_fail_count = 0; int TEMP_a = -1; time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 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; if ( (matchCount = sscanf(settingsBuffer,"a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d", &TEMP_a,&TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) { DEBUG("FROMSERVER: a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n", 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); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FROMSERVER: a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n", 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);debug_exe();} //FAILUREMODE need to be checking these against checksums char changed; if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_minimumupdate_hrs:%u..%c\n",RET_setting_minimumupdate_hrs,changed); 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); 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); if(TEMP_d != -1) { setState(TEMP_d); changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed); 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); if(RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;} 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); if(RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;} 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); 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); if(RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;} 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); if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;} 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); 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); 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); if(RET_setting_motion_g < 6 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;} 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); 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); 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); 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); 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); if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; } 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); 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); if (critical_fail_count == 0) { if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "GOT SETTINGS OK");debug_exe();} DEBUG("GOT SETTINGS OK",false); dumpSettings(); saveEventTimes(); RET_haveSettings = true; GLOBAL_needToConfigureLis3dh = true; RET_SettingsGotAt = RET_RTCunixtime; return true; } else { if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();} DEBUG("CRITICAL FAILS:%d",critical_fail_count); dumpSettings(); RET_haveSettings = false; return false; } } else { RET_haveSettings = false; return false; } } void saveEventTimes() { //SET EVENT TIMES if(RET_setting_location_tx_interval_mins > 0) { RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);debug_exe();} DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode); } if(RET_setting_location_tx_failsafe_hrs > 0) { RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);debug_exe();} DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx); } if(RET_setting_activity_tx_interval_hrs > 0) { RET_motionFrameStart = RET_RTCunixtime; //SET START FRAME INITAL RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);debug_exe();} DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx); } if(RET_eventTime_environmental_tx > 0) { RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);debug_exe();} DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx); } } //------------------------------------------------------------------------------ // SETUP //------------------------------------------------------------------------------ bool setup() { bool pass = true; float temperature = getTemperature(); float voltage = getBatteryV(); bool selftestresult = selfTest(); if (selftestresult == false) { LED1errorCode(4,20); } //ERROR 4 if (modem.on()) { //RET_imei = modem.getIMEI(); //DEBUG("imei: %lld \n",RET_imei); //char* modemModel = modem.getModemModel(); char locString[70]; memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70); if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time. char bytestosend[160]; snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString); char result[200]; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi)); if (strcmp(result, "err") != 0) { //DEBUG("\nfromserver: %s \n",result); if (!saveSettings(result)) { //something went critically wrong getting settings pass = false; modem.off(false); if(GLOBAL_debugLED) LED1errorCode(5,10); //ERROR 5 } } else { //Response error } } else { //FAILUREMODE modem failed to register on network DEBUG("NET REGISTER FAIL",false); modem.off(false); if(GLOBAL_debugLED) LED1errorCode(3,10); //ERROR 3 pass = false; } } else { //FAILUREMODE Modem failed to turn on DEBUG("MODEM ON FAIL",false); modem.off(false); if(GLOBAL_debugLED) LED1errorCode(2,10); //ERROR 2 pass = false; } modem.off(false); //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP RET_SetupRunAt = RET_RTCunixtime; //RESULT return pass; } //------------------------------------------------------------------------------ // EVENTS //------------------------------------------------------------------------------ void event_connectiontest_tx(int location_accuracy) { DEBUG("TEST EVENT\n",false); float temperature = getTemperature(); float voltage = getBatteryV(); int selftestresult = selfTest(); if (modem.on()) { char locString[70]; memcpy(locString, modem.getLocation(location_accuracy, RET_setting_location_timeout), 70); if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.; char bytestosend[160]; snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString); char result[180]; bool getSettings = true; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi)); if (result != "err") { //DEBUG("\nfromserver: %s \n",result); if (getSettings) { saveSettings(result); if(GLOBAL_debugLED) LED1blink(5,250); } } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 2 } modem.off(false); //RESETS RET_motionTriggeredInTXInterval = 0; saveEventTimes(); } void event_location_tx() { DEBUG("LOCATION TX\n",false); float temperature = getTemperature(); float voltage = getBatteryV(); int selfTestResult = selfTest(); if (modem.on()) { char locString[70]; memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70); //DEBUG("locString:%s-\n",locString); //SEND DATA if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time //Check if we should wait for settings back bool getSettings = true; if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken -= 10;} //remove the extra 10 seconds from times char bytesToSend[160]; snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,getSettings,locString); char result[180]; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi)); if (result != "err") { //DEBUG("\nfromserver: %s \n",result); if (getSettings) { saveSettings(result); } } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 2 } modem.off(false); //RESETS RET_motionTriggeredInTXInterval = 0; saveEventTimes(); } void event_activity_tx() { DEBUG("ACTIVITY TX\n",false); float temperature = getTemperature(); if (modem.on()) { //SEND DATA if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { char bytesToSend[160]; snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHours); char result[180]; bool getResponse = false; snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 2, GLOBAL_defaultApi)); if (result != "err") { //RESET ACTIVITY FRAME memset(RET_activityData,0,sizeof(RET_activityData)); if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; } } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(3,20); //ERROR 3 } } else { modem.off(false); if(GLOBAL_debugLED) LED1errorCode(2,20); //ERROR 3 } modem.off(false); //RESETS RET_motionTriggeredInTXInterval = 0; saveEventTimes(); } //------------------------------------------------------------------------------ // STATE ENGINE //------------------------------------------------------------------------------ void mainStateEngine() { switch(RET_state) { case STATE_SETUP : { //need the curlies to avoid "transfer of control bypass init error warning" DEBUG("STATE:SETUP\n",false); if (setup()) { // All good } else { RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs setState(STATE_DORMANT); DEBUG("SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant); } break; } case STATE_NORMAL : { DEBUG("STATE:NORMAL\n",false); //check and log motion checkMotion(); //LOCATION EVENT bool run_location_tx = false; switch (RET_setting_location_mode) { case 1: //INTERVAL POST if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; } break; case 2: //INTERVAL POST WITH MOTION CHECK if(RET_motionTriggeredInTXInterval && RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; } break; case 3: //POST ON STOP MOTION if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; } break; } if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; } if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n",false); event_location_tx(); } //if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; } //Location Failsafe timer catchall //if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; } //if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); } //ACTIVITY EVENT bool run_activity_tx = false; if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0 && strlen(RET_activityData) > 1) { run_activity_tx = true; } if (strlen(RET_activityData) > 130) { run_activity_tx = true; } if (run_activity_tx) { DEBUG("ACTIVITY TX...\n",false); event_activity_tx();} break; } case STATE_DORMANT : { if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) { if (RET_haveSettings) { setState(STATE_NORMAL); } else { setState(STATE_SETUP); } DEBUG("WAKING UP FROM DORMANT\n",false); } DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant); break; } case STATE_BUTTONPRESS1 : { setState(STATE_NORMAL); DEBUG("STATE:BUTTONPRESS1\n",false); LED1blink(1,300); break; } case STATE_BUTTONPRESS2 : { setState(STATE_NORMAL); DEBUG("STATE:BUTTONPRESS2\n",false); LED1blink(2,300); LED1on(0); event_connectiontest_tx(1); LED1off(); break; } case STATE_BUTTONPRESS3 : { setState(STATE_NORMAL); DEBUG("STATE:BUTTONPRESS3\n",false); LED1blink(3,300); LED1on(0); event_connectiontest_tx(2); LED1off(); break; } case STATE_BUTTONHOLD : { DEBUG("STATE:BUTTONHOLD\n",false); if (RET_state_prev == STATE_NORMAL) { setState(STATE_DORMANT); RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs DEBUG("TURNING OFF\n",false); DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant); LED1on(5000); } else { if (RET_haveSettings) { setState(STATE_NORMAL); } else { setState(STATE_SETUP); } DEBUG("TURNING ON\n",false); LED1blink(20,100); } break; } default : { setState(STATE_SETUP); DEBUG("DEFAULT STATE\n",false); } } } //------------------------------------------------------------------------------ // MAIN //------------------------------------------------------------------------------ int main() { LED1on(500); //INIT LED1off(); RTCticker.attach(&RTCtick, 1.0); button.fall(&buttonPress); button.rise(&buttonRelease); dumpSettings(); //CHECK IF THIS IS RESET //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset if (NRF_POWER->RESETREAS != 0xffffffff) { switch(NRF_POWER->RESETREAS) { case 0x00000001 : DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state); RET_coldBoot = 1; dumpSettings(); NRF_POWER->RESETREAS = 0xffffffff; break; case 0x00000002 : DEBUG("Watchdog ST:%d\n",RET_state); NRF_POWER->RESETREAS = 0xffffffff; break; case 0x00000004 : DEBUG("Soft ST:%d\n",RET_state); break; } } //CHECK FOR FIRST BOOT if (RET_coldBoot == 1) { LED1errorCode(6,2); } //MAIN LOOP while(true) { //WATCHDOG watchdogKick(); //only need this if we're in while loop //INIT resetGlobals(); // could move this into state engine switches to save actions on each loop GLOBAL_wakeTime = RET_RTCunixtime; //MAIN LOGIC 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); DEBUG("ACTIVITY:%s\n",RET_activityData); mainStateEngine(); //LOG FIRST RUN - BOOTLOADER COMMS if (RET_coldBoot) { read_app_data_from_flash(&app_data); bool write_app_data_to_flash_execute = false; if(get_flag(&app_data, app_execution_flag) == true) { clr_flag(&app_data, app_execution_flag); write_app_data_to_flash_execute = true; } if(app_data.current_firmware_version != FW_VERSION) { app_data.current_firmware_version = FW_VERSION; app_data.target_firmware_version = FW_VERSION; write_app_data_to_flash_execute = true; } if (write_app_data_to_flash_execute) { write_app_data_to_flash(&app_data); } RET_coldBoot = 0; } //PRE-SLEEP ACTIONS LED1off(); if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); } modem.off(false); NRFuart_uninit(); nrf_configureGPIOForSleep(); watchdogKick(); //SLEEP mainthread.wait(DEFAULT_SLEEP_FRAME); } }