Private Private / Mbed 2 deprecated SenseAirLP8

Dependencies:   BLE_API mbed nRF51822

Fork of SenseAirLP8 by Jonas Skalman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLE.h"
00003 #include "LP8_Service.h"
00004 #include "LP8.h"
00005 
00006 //Sensor and ble Configuration parameters
00007 #define     SENSOR_TIMER                        16.0                                    //lp8 polling interval (seconds)
00008 #define     BLE_ADV_INTERVAL                    500                                     //advertisment interval (milliseconds)
00009 
00010 
00011 //setup ble stack
00012 BLE             ble;                                                                    //BLE object
00013 
00014 // setup Pins 
00015 DigitalOut      Res(P0_0, 1);                                                           //reset, pull Low for reset
00016 DigitalIn       RDY(P0_10);                                                             //rdy
00017 DigitalOut      VBB_EN(P0_5, 0);                                                        //set to low at startup
00018 Serial          Device(P0_9, P0_11);                                                    //tx, rx
00019 
00020 // Timers
00021 Timer           lp8Wait;                                                                //timer for sensor communication
00022 Ticker          lp8Timer;                                                               //timer object for sensor polling 
00023 
00024 //BLE device name and uuid list setup
00025 const static char       DEVICE_NAME[] = "SenseAir LP8";
00026 static const uint16_t   uuid16_list[] = {LP8_Service::LP8_SERVICE_UUID};
00027 
00028 //check for sensor triggering
00029 bool                    triggerSensor = false;                                          //sensor polling flag
00030 bool                    doCalibration = false;                                          //background calibration flag
00031 bool                    initCheck     = true;                                           //check for init sensor state
00032 
00033 LP8_Service             *lp8ServicePtr;                                                 //
00034 
00035 uint8_t                 ccByte = 0x20;                                                  //calculation control byte to LP8
00036 
00037 
00038 
00039 
00040 //****************************   ble callback functions      ***************************
00041 // on Disconnect interupt
00042 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00043 {
00044     initCheck = true;
00045     //turn of sensor at ble disconnect
00046     VBB_EN.write( 0 );
00047     //cancel sensor timer
00048     lp8Timer.detach();
00049     
00050     //restart broadcast
00051     ble.gap().startAdvertising();
00052 }
00053 //sensor polling 
00054 void triggerSensorPollingInterupt()
00055 {
00056     triggerSensor = true;
00057       
00058     //reset callback timer
00059     lp8Timer.detach();
00060     lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER);
00061     
00062 };
00063 
00064 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
00065 {
00066     lp8Timer.attach( triggerSensorPollingInterupt, 4 );
00067 }
00068 
00069 //on BLE data written
00070 void calibrationCallback(const GattWriteCallbackParams *eventData)
00071 {
00072     doCalibration = true;
00073     ccByte = eventData-> data[0];
00074 }
00075 
00076 
00077 
00078 //****************************     main      **************************************
00079 int main()
00080 {
00081     wait(1); 
00082 
00083     ble.init();
00084     
00085     //callbacks
00086     ble.gap().onConnection( connectionCallback );
00087     ble.gap().onDisconnection( disconnectionCallback );                                  //
00088     ble.gattServer().onDataWritten( calibrationCallback );                               //
00089 
00090 //************************     LP8 variables      *********************************
00091     int                     co2Value    = 99;                                            //initial CO2 value
00092     float                   tempValue   = 99;                                            //init temp value
00093     int                     Vcap        = 99;                                            //mV    
00094     uint32_t                errorFlag   = 0;                                             //error bits from lp8
00095     uint8_t                 sentCCbyte  = 0x99;
00096     
00097 
00098     //setup LP8 object 
00099     LP8                  *lp8 = new LP8(Device, VBB_EN, RDY, Res, lp8Wait);    
00100 
00101      
00102 //Setup GattService
00103 //    LP8_Service      lp8Service(ble, co2Value, tempValue, 
00104  //                    Vcap, errorFlag, ccByte, sentCCbyte);                              
00105     lp8ServicePtr     = new LP8_Service(ble, co2Value, tempValue, 
00106                      Vcap, errorFlag, ccByte, sentCCbyte);                              // lp8Service;                                                    
00107 
00108 
00109     // setup ble advertising parameters 
00110     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);            //general bluetooth information(only support for ble
00111     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); //service list
00112     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00113     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00114     ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL);                                 /* advertising interval in ms. */
00115     ble.gap().startAdvertising();                                                       
00116     
00117     // Wait for initialization to complete.
00118     while (ble.hasInitialized()  == false) { 
00119     /* spin loop */ 
00120     }
00121     /* Waiting while super-capacitor will charged for a first measurement */
00122     wait(SENSOR_TIMER); 
00123 
00124 
00125 //***************************     start the main loop      ***********************************
00126     while ( true ) 
00127     {
00128 
00129         if(triggerSensor && ble.gap().getState().connected )                            //trigger when timer interupts and there is an established ble connection
00130         {
00131             if ( initCheck ) {
00132                 lp8->responsePurge(50);                                                  //purge buffer
00133                 if ( lp8->lp8Init() != true ) {
00134                     //initCheck = true;
00135                 }
00136                 else {
00137                     initCheck = false;                   
00138                     
00139                 }                                                          
00140            }
00141           
00142             else if ( lp8->lp8Talk( ccByte ) != true ) {
00143                 //restart with init sequence if sensor state is lost or communication fails
00144                 initCheck = true;
00145                 ccByte = 0xff;                                                          //app error flag
00146             }
00147                         
00148             //reset polling check
00149             triggerSensor = false;
00150             
00151             //update the gattServer characteristic values
00152             lp8ServicePtr->updateCo2Value( lp8->getValue() );                            //get CO2 value
00153             lp8ServicePtr->updateTempValue( lp8->getTempValue() );                       //
00154             lp8ServicePtr->updateVcapValue( lp8->getVcapValue() );                       //
00155             lp8ServicePtr->updateError( lp8->getErrorStatus() );                         //
00156             lp8ServicePtr->updateDataWritten( lp8->getCCbyte() );                        //send back the calculation control byte that was used in LP8 measurement
00157             lp8ServicePtr->updateReWrite( ccByte ); 
00158             
00159             //reset calibration control
00160             ccByte = 0x20;      /* resets to 0x20 (subs. talk) after one sample.
00161             Check lp8Talk() function. */                                                         
00162         }
00163         
00164         else { ble.waitForEvent(); }                                                    //ble save energy
00165     }
00166     
00167 };
00168