init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
main.cpp
- Committer:
- pathfindr
- Date:
- 2018-10-27
- Revision:
- 0:94b743e06998
- Child:
- 2:fd554f01abdf
File content as of revision 0:94b743e06998:
#include "main.h" #include "board.h" #include "modes.h" //LowPowerTicker ticker; //no impact on power consumption //------------------------------------------------------------------------------ //Function declarations- Local //------------------------------------------------------------------------------ static void setup(void); static void loop(void); static void mode_mtu(void); static int selftest(void); static void LEDon(int milliseconds); static void LEDoff(void); //------------------------------------------------------------------------------ //Var declarations- Local //------------------------------------------------------------------------------ int mode = 0; string exceptions = ""; bool accel_healthy = false; //------------------------------------------------------------------------------ //Var declarations- RETAINED //------------------------------------------------------------------------------ int RET_mode = 0; char RET_pf_identifier[9]; //includes null byte at end int RET_interval_setting = 720; int RET_interval_hours_failsafe = 24; int RET_motioncheck_interval_seconds = 60; int RET_gps_timeout = 180; int RET_gsm_timeout = 120; bool RET_gsmtimedout = false; int RET_beacon_scan = 0; int RET_accel_awake_thr = 11; int RET_accel_awake_triggercount = 0; int RET_accel_alarm_thr = 127; unsigned long RET_unixtime_configrun = 0; unsigned long RET_unixtime_lastpost = 0; unsigned long RET_unixtime_nextpost = 0; unsigned long RET_unixtime_nextpost_failsafe = 0; unsigned long RET_unixtime_framestart = 0; unsigned long RET_motionstarttime = 0; unsigned long RET_motionstoptime = 0; int RET_gsm_failcount = 0; int RET_serverresponse_failcount = 0; int RET_motionconsecutive = 0; int RET_no_motionconsecutive = 0; bool RET_wethinkinmotion = false; bool RET_wethinkhasmoved = false; bool RET_haveservertime = false; float RET_operationTimeHours = 0; float RET_operationFrameTimeHours = 0; int RET_operationCount = 0; char RET_operationTimeString[100]; //------------------------------------------------------------------------------ //Pin states //------------------------------------------------------------------------------ DigitalOut led1(p11); DigitalOut vreg_en(p29); DigitalOut gsm_pwkey(p28); //------------------------------------------------------------------------------ //Peripherals //------------------------------------------------------------------------------ Serial uart(USBTX, USBRX, 115200); LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G); BLE myble; void gotosleep(long milliseconds) { accelerometer.configureForSleep(); Thread::wait(milliseconds); //system_reset(); } void LED1on(int milliseconds = 0) { led1 = 0; if (milliseconds > 0) { Thread::wait(milliseconds); led1 = 1; } } void LED1off() { led1 = 1; } int main() { setup(); while(true) { loop(); } } void setup() { led1 = 1; set_time(1256729737); LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G); accelerometer.configureForSleep(); } void loop() { bool accel_awake; //uart.printf("blah blah blah"); //sleep_manager_lock_deep_sleep(); //uart.printf("Deep sleep allowed: %i\r\n", sleep_manager_can_deep_sleep()); /* if (accelerometer.read_id() == 51){ led1 = 0; Thread::wait(20); led1 = 1; } else { led1 = 1; }; */ mode = MODE_SETUP; switch(mode) { case MODE_SETUP : selftest(); break; case MODE_INTERVAL : break; case MODE_INTELLIGENT : break; case MODE_MTU : mode_mtu(); break; case MODE_DORMANT : break; default : mode = MODE_SETUP; } gotosleep(60000); } void initMotion() { //CHECK FOR ANY MOTION if (!accelerometer.selfTest()) { if (exceptions.find("A.") < 0) { exceptions += "A."; } DEBUG("Couldnt start accelerometer"); } else { accel_healthy = true; } } void mode_mtu() { bool accel_awake = accelerometer.getINT2(); //CHECK FOR MOTION if (accel_awake == true) { LED1on(50); RET_motionconsecutive ++; RET_no_motionconsecutive = 0; //this is needed to ensure accel_awake_triggercount is 1 or higher for mode 2 if (RET_accel_awake_triggercount == 0) { RET_accel_awake_triggercount = 1; } if (RET_motionconsecutive == RET_accel_awake_triggercount && RET_wethinkinmotion == false) { RET_wethinkinmotion = true; RET_motionstarttime = (time(NULL) - ((RET_accel_awake_triggercount+1) * RET_motioncheck_interval_seconds)); long eventEpoch = RET_motionstarttime; long epochoffsetminutes = ((eventEpoch - RET_unixtime_framestart) / 60); char buf[20]; sprintf(buf,"1.%i!", epochoffsetminutes); strcat(RET_operationTimeString, buf); RET_operationCount ++; DEBUG("%s", RET_operationTimeString); LEDon(100); LEDon(100); LEDon(100); LEDon(100); } } else if (accel_awake == false) { RET_no_motionconsecutive ++; RET_motionconsecutive = 0; if (RET_no_motionconsecutive == RET_accel_awake_triggercount && RET_wethinkinmotion == true) { RET_wethinkinmotion = false; // log engine stop RET_motionstoptime = (time(NULL) - ((RET_accel_awake_triggercount+1) * RET_motioncheck_interval_seconds)); long eventEpoch = RET_motionstoptime; long epochoffsetminutes = ((eventEpoch - RET_unixtime_framestart) / 60); char buf[20]; sprintf(buf,"0.%i!", epochoffsetminutes); strcat(RET_operationTimeString, buf); RET_operationFrameTimeHours = (float(RET_motionstoptime - RET_motionstarttime) / 3600.0); RET_operationTimeHours += RET_operationFrameTimeHours; //test by posting on every stop event //doOperationTimePost = true; DEBUG("%i", RET_operationTimeHours); LEDon(500); } } // Check time interval for location run if (time(NULL) > RET_unixtime_nextpost_failsafe) { //setfailsafetime(); //these must be before gpsPost, incase gpsPost fails, then stuck in broadcast loop //gpsPost(); } //Check for operatingTime post time or if data buffer is full for force post if (RET_operationFrameTimeHours > 0.0) { if (time(NULL) > RET_unixtime_nextpost || strlen(RET_operationTimeString) > 60) { //only bother if we actual have data to post //setwaketime(); //these must be before gpsPost, incase gpsPost fails, then stuck in broadcast loop //uploadOperationTimeData(); } } } int selftest() { int result = 0; int tests = 0; int testscore = 0; led1 = 0; string failures; //Accelerometer tests ++; if (accelerometer.read_id() == 51){ testscore ++; } else { //Accelerometer Fail failures += 'Ac,'; }; return result; }