init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

main.cpp

Committer:
pathfindr
Date:
2018-11-08
Revision:
2:fd554f01abdf
Parent:
0:94b743e06998
Child:
4:8d8e9bfa82e4

File content as of revision 2:fd554f01abdf:

#include "main.h"
#include "board.h"
#include "modes.h"

//#include "ATCommand.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(void);
static void LEDoff(void);

//------------------------------------------------------------------------------
//Var declarations- Local
//------------------------------------------------------------------------------ 
int mode = 0;
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
//------------------------------------------------------------------------------ 

//LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G);
//BLE myble;

//Serial uart(p6, p8, 115200);
//ATSerial atserial(p6,p8,115200);


void gotosleep(long sleep_milliseconds) {
    //accelerometer.configureForSleep();
    ThisThread::sleep_for(sleep_milliseconds);
    //system_reset();
}

void LED1on(long flash_milliseconds = 0) {
    led1 = 0;
    if (flash_milliseconds > 0) {
        ThisThread::sleep_for(flash_milliseconds);
        led1 = 1;
    }
}
void LED1off() {
    led1 = 1;
}

int main() {
    setup();    
    while(true) {
        loop();
    }
}

bool GSMon() {
    //power on GSM
    vreg_en = 1;
    ThisThread::sleep_for(500);
    gsm_pwkey = 0;
    ThisThread::sleep_for(1500);
    gsm_pwkey = 1;
    LED1on(1000);
}

void setup() {
    led1 = 1;
    LED1on(50);
    
    set_time(1256729737);
    //accelerometer.configureForSleep();
    ThisThread::sleep_for(10000);
    
    
    LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G);
    ThisThread::sleep_for(10000);
    system_reset();
}

void loop() {
    LED1on(50);
    
    bool accel_awake;

    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(10000);
}



/*
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;   
}
*/