SenseAir LP8 connected to BLE Nano

Dependencies:   BLE_API mbed nRF51822

Fork of SenseAirLP8 by Private Private

main.cpp

Committer:
jony1401
Date:
2017-04-21
Revision:
0:ee3787c8e209
Child:
1:b512a405b584

File content as of revision 0:ee3787c8e209:

#include "mbed.h"
#include "BLE.h"
#include "LP8_Service.h"
#include "LP8.h"

//setup ble stack
BLE             ble;                                                                    //BLE object

// Pins and timers needed for lp8 communication
DigitalIn       RDY(P0_8);                                                              //
DigitalOut      VBB(P0_5);
DigitalOut      VBB_EN(P0_4);                                                        //
Serial          Device(P0_9, P0_11);                                                    //tx, rx
DigitalOut      led1(P0_19, 1);                                                         //board led
Timer           lp8Wait;                                                                //timer for sensor communication



//Sensor and ble Configuration parameters
#define     SENSOR_TIMER                        10.0                                    //lp8 polling interval (seconds)
#define     BLE_ADV_INTERVAL                    500                                     //advertisment interval (milliseconds)



//device name and uuid list setup
const static char       DEVICE_NAME[] = "SenseAir LP8";
static const uint16_t   uuid16_list[] = {LP8_Service::LP8_SERVICE_UUID};


//check for sensor triggering and uppdating Gatt Server
bool                    triggerSensor = false;                                          //check for sensor polling
LP8_Service             *lp8ServicePtr;                                                 //pointer to lp8 service object



//****************************   ble functions      *******************************
//****************************                      *******************************
// on Disconnect, Restart BroadCasting
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    //reset I/O pin, necessary?
//    VBB_EN.write ( 1 );
    
    //restart broadcast
    ble.gap().startAdvertising();
}
//timer function callback function
void triggerSensorPollingInterupt()
{
    triggerSensor = true;
};



//main 
int main(void)
{
    led1 = 1;      //turn off led                                                                
    
    Ticker lp8Timer;                                                                    //timer object for sensor polling interupts
    lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER);                       //trigger sensor reading every X.Y sec
    
    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);                                   //do callback if disconnection occurs


    int                     co2Value = 400;                                             //initial CO2 value
    bool                    initCheck = true;                                           //check for init sensor state or lost state
    bool                    successCheck = false;
    
//setup LP8 peripheral communication 
    LP8               lp8(Device, VBB, VBB_EN, RDY, lp8Wait);
     
     
//setup for GattService
/* Setup Gatt server */
    LP8_Service      lp8Service(ble, co2Value);                                         //pass in ble object and initial co2 value
    lp8ServicePtr     = &lp8Service;                                                    //set pointer to "real" lp8 service object


/* setup advertising parameters */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);            //general bluetooth information(only support for ble
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); //service list
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL);                 /* advertising interval in ms. */
    ble.gap().startAdvertising();                                       //start broadcast
    
        /* SpinWait for initialization to complete. This is necessary because the
     * BLE object is used in the main loop below. */
    while (ble.hasInitialized()  == false) { /* spin loop */ }



//start the loop    
    while ( true ) 
    {
        if(triggerSensor && ble.gap().getState().connected )                            //trigger when timer interupts ticks and there is an established connection
        {
            if ( initCheck ) {
                successCheck = lp8.lp8Init();
                
//                if ( successCheck ) {
                    initCheck = false;
//                    led1 = 0;       //turn on led 
//                }
            }
            else {
                lp8.lp8Talk();
                led1 = 0;   //on when talking
            }
            
            //reset sensor status
            triggerSensor = false;
            //update gattServer with new CO2 value
            lp8ServicePtr->updateCo2Value( lp8.getValue() );
        }
        
        else { ble.waitForEvent(); }                //ble save energy
    }
    
};