init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

main.cpp

Committer:
pathfindr
Date:
2018-12-11
Revision:
8:7351f8c4cd60
Parent:
7:e9a19750700d
Child:
9:b0a1535b8ef2

File content as of revision 8:7351f8c4cd60:

#include "main.h"
//include "nrf_soc.h"

//------------------------------------------------------------------------------
//FUNCTION PROTOTYPES
//------------------------------------------------------------------------------ 
static void mainStateEngine(void);
static void selftest(void);
static void buttonPress(void);
static void buttonRelease(void);

//------------------------------------------------------------------------------
//GLOBAL VARS
//------------------------------------------------------------------------------ 
bool accel_healthy = false;
bool firstBoot = false;
bool requireSoftReset = false;
bool motionFlagTriggered = false;

//------------------------------------------------------------------------------
//RETAINED NOINIT RAM VARS
//------------------------------------------------------------------------------ 
#if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/
static uint8_t          RET_mode                    __attribute__((section("noinit"),zero_init));

static uint8_t          RET_buttonReleaseCount      __attribute__((section("noinit"),zero_init));
static uint32_t         RET_buttonPressTime         __attribute__((section("noinit"),zero_init));
static uint32_t         RET_buttonReleaseTime       __attribute__((section("noinit"),zero_init));
static uint32_t         RET_unixtime_backup         __attribute__((section("noinit"),zero_init));
static bool             RET_requireImpactFlag       __attribute__((section("noinit"),zero_init));
static char             RET_buffer[64]              __attribute__((section("noinit"),zero_init));

//MOTION SETTINGS
static bool             RET_motionTriggered              __attribute__((section("noinit"),zero_init));
static time_t           RET_motionStartTime              __attribute__((section("noinit"),zero_init));
static time_t           RET_motionStopTime              __attribute__((section("noinit"),zero_init));
static uint32_t         RET_motionStartThreshold_seconds    __attribute__((section("noinit"),zero_init));
static uint32_t         RET_motionStopThreshold_seconds     __attribute__((section("noinit"),zero_init));
static bool             RET_motionPendingOnState              __attribute__((section("noinit"),zero_init));
static bool             RET_motionPendingOffState              __attribute__((section("noinit"),zero_init));
static bool             RET_motionState              __attribute__((section("noinit"),zero_init));

//EVENT HANDLING
static uint32_t         RET_eventTime_location      __attribute__((section("noinit"),zero_init));
static uint32_t         RET_eventTime_environmental __attribute__((section("noinit"),zero_init));
static uint32_t         RET_eventTime_activity      __attribute__((section("noinit"),zero_init));
#elif defined ( __GNUC__ )
#elif defined ( __ICCARM__ )
#endif

//------------------------------------------------------------------------------
//PERIPHERALS
//------------------------------------------------------------------------------ 
//BLE myble;
WatchdogTimer watchdog(65.0); //Do not set to less than 4500ms or can cause issues with softdevice
InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep
#if NEED_CONSOLE_OUTPUT
Serial uart(PN_UART_TX, PN_UART_RX, 115200);
#endif

//------------------------------------------------------------------------------
//SINGLETONS
//------------------------------------------------------------------------------
//NVStore &nvstore = NVStore::get_instance();


//------------------------------------------------------------------------------
// LOW LEVEL CRITICAL FUNCS
//------------------------------------------------------------------------------ 
void gotoSleep(long sleep_milliseconds) {
    
    if (requireSoftReset) { //dont need to clear this var as reset changes it back to false
        RET_unixtime_backup = time(NULL); //save unixtime for reset
        system_reset();
    }
    
    //button.fall(&buttonPress);
    //button.rise(&buttonRelease);
    
    ThisThread::sleep_for(sleep_milliseconds);   
}

void factoryReset() {
    firstBoot = true;
    
    //RESET VARS
    RET_mode = 0;
    RET_unixtime_backup = 0;
    RET_buttonReleaseCount = 0;
    RET_eventTime_location = 0;
    RET_eventTime_environmental = 0;
    RET_eventTime_activity = 0;
    RET_motionPendingOffState = 0;
    RET_motionPendingOnState = 0;
    RET_motionState = 0;
    RET_motionStartTime = 0;
    RET_motionStopTime = 0;
    RET_motionStartThreshold_seconds = 60;
    RET_motionStopThreshold_seconds = 60;
    set_time(RET_unixtime_backup);
    
    //SET IDENTIFIER
    //uint32_t nv_value = 12345678;
    //int rc = nvstore.set(NV_IDENTIFIER, sizeof(nv_value), &nv_value);
}

void turnOffEverything() {
    //vreg_en = 0;
    led1 = 1;
}

//------------------------------------------------------------------------------
// USER BUTTON
//------------------------------------------------------------------------------ 
void buttonPress() {
    led1 = 0;
    
    //while(RET_buttonReleaseCount < 1){
        //wait   
    //}
    
    //led1 = 1;
    /*
    //RET_buttonPressTime = time(NULL);
    RET_buttonReleaseCount ++;
    led1 = 0;
    for (int i = 0; i < RET_buttonReleaseCount; i++) {
        led1 = 0;
        wait_ms(100);
        led1 = 1;
        wait_ms(100);
    */
}
void buttonRelease() {
    led1 = 1;
    //RET_buttonReleaseCount ++;
    /*RET_buttonReleaseCount ++;
    led1 = 0;
    for (int i = 0; i < RET_buttonReleaseCount; i++) {
        led1 = 0;
        wait_ms(100);
        led1 = 1;
        wait_ms(100);
    }*/
}

bool selfTest() {
    //Accelerometer
    LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
    uint8_t lis3dh_id; 
    lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
    if (asdf == 51) {
        LED1blink(10,100);
    }   
}

//------------------------------------------------------------------------------
// STATE ENGINE
//------------------------------------------------------------------------------ 

void mainStateEngine() {
    
    RET_mode = MODE_NORMAL;
    
    switch(RET_mode) {
        case MODE_SETUP :
            factoryReset();
            //selftest();
            break;
            
        case MODE_NORMAL :
            //check and log motion
            if (lis3dh_int1) {
                LED1blink(2,100);
                RET_motionTriggered = true;
                if (!RET_motionPendingOnState) {
                    RET_motionPendingOnState = true;
                    RET_motionPendingOffState = false;
                    // Log start motion time
                    RET_motionStartTime = time(NULL);
                    RET_motionStopTime = 0;                
                }
            } else {
                LED1blink(2,500);
                RET_motionTriggered = false;
                RET_motionPendingOnState = false;
                if (!RET_motionPendingOffState) {
                    RET_motionPendingOffState = true;
                    //log stop motion time
                    RET_motionStopTime = time(NULL);
                    RET_motionStartTime = 0;
                }
            }
            //calculate motion state
            if (RET_motionPendingOnState) {
                //check if above threshold
                time_t inMotionForSeconds = (time(NULL) - RET_motionStartTime);
                if (inMotionForSeconds >= RET_motionStartThreshold_seconds) {
                    RET_motionState = true;
                    LED1blink(10,100);
                }
            }
            if (RET_motionPendingOffState) {
                time_t noMotionForSeconds = (time(NULL) - RET_motionStopTime);
                if (noMotionForSeconds >= RET_motionStartThreshold_seconds) {
                    RET_motionState = false;
                    LED1blink(5,500);
                }
            }
            
            break;
            
        case MODE_DORMANT :
            //LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
            break;
            
        case MODE_OFF_48HRS :
           
            break;
      
        default :
            RET_mode = MODE_SETUP;
    }
}


//------------------------------------------------------------------------------
// MAIN
//------------------------------------------------------------------------------ 

int main() {
    
    //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("reset_reason: 0x%08x. - Hard Reset\n",NRF_POWER->RESETREAS);
                factoryReset();
            break;
            case 0x00000002  :
                DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS);
                set_time(RET_unixtime_backup);
            break;
            case 0x00000004  :
                DEBUG("reset_reason: 0x%08x. - Soft reset\n",NRF_POWER->RESETREAS);
                set_time(RET_unixtime_backup);
            break;
        }
        NRF_POWER->RESETREAS = 0xffffffff;
    }
        
    while(true) {
        turnOffEverything();
        watchdog.kick();
        firstBoot = false; //temp
        if (!firstBoot) gotoSleep(10000);
        
        //INIT
        Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
        
        //MAIN STATE ENGINE
        //LED1blink(1,100);
        mainStateEngine();
        
        //Configure for sleep
        lis3dh_configureForSleep(10);
    }
}