Fork for https://developer.mbed.org/users/jony1401/code/SenseAirLP8/

Dependencies:   BLE_API mbed nRF51822

Fork of SenseAirLP8 by Jonas Skalman

Revision:
2:d02255d8c36f
Parent:
1:b512a405b584
Child:
3:933dd59ad44d
--- a/main.cpp	Mon Jun 05 11:10:28 2017 +0000
+++ b/main.cpp	Mon Aug 14 20:52:48 2017 +0000
@@ -3,31 +3,38 @@
 #include "LP8_Service.h"
 #include "LP8.h"
 
+//Sensor and ble Configuration parameters
+#define     SENSOR_TIMER                        20.0                                    //lp8 polling interval (seconds)
+#define     BLE_ADV_INTERVAL                    500                                     //advertisment interval (milliseconds)
+#define     BACKGROUND_CALIBRATION_TIME         180                                     //timer to do background calibration (seconds)
+
+
+
 //setup ble stack
 BLE             ble;                                                                    //BLE object
 
-// Pins and timers needed for lp8 communication
-DigitalIn       RDY(P0_5);                                                              //
-DigitalOut      VBB_EN(P0_29, 0);                                                       //set to low at startup
+// setup Pins 
+DigitalOut      Res(P0_0, 1);                                                           //reset, pull Low for reset
+DigitalIn       RDY(P0_10);                                                             //rdy
+DigitalOut      VBB_EN(P0_5, 0);                                                        //set to low at startup
 Serial          Device(P0_9, P0_11);                                                    //tx, rx
+
+// Timers
 Timer           lp8Wait;                                                                //timer for sensor communication
-
+Timer           bCalibration;                                                           //timer for background calibration
 
 
-//Sensor and ble Configuration parameters
-#define     SENSOR_TIMER                        20.0                                    //lp8 polling interval (seconds)
-#define     BLE_ADV_INTERVAL                    1000                                    //advertisment interval (milliseconds)
-
-
-
-//device name and uuid list setup
+//BLE 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, used with interupt
-LP8_Service             *lp8ServicePtr;                                                 //pointer to lp8 service object
+//check for sensor triggering and uppdating the GattServer
+bool                    triggerSensor = false;                                          //sensor polling flag
+bool                    doCalibration = false;                                          //background calibration flag
+
+LP8_Service             *lp8ServicePtr;                                                 //pointer to lp8  BLE service object
+uint8_t                 ccByte = 0x20;                                                  //calculation control byte to LP8
 
 
 
@@ -40,46 +47,63 @@
     //restart broadcast
     ble.gap().startAdvertising();
 }
-//timer function callback
+//sensor polling interupt
 void triggerSensorPollingInterupt()
 {
     triggerSensor = true;
 };
+//app interupt for calibration...
+void calibrationCallback(const GattWriteCallbackParams *eventData)
+{
+    ccByte = eventData-> data[0];
+    doCalibration = true;
+}
 
 
 
-//main 
-int main(void)
+//****************************     main      **************************************
+int main()
 {
     wait(1); 
+
     
     Ticker lp8Timer;                                                                    //timer object for sensor polling interupts
-    lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER);                       //trigger sensor reading every X.Y sec
+    
+    //timer callback
+    lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER);                       //trigger sensor reading every X sec
+ 
+    ble.init();
+ 
+    //disconnect callback
+    ble.gap().onDisconnection( disconnectionCallback );                                 //do callback if ble disconnect
+    //on data recevied callback
+    ble.onDataWritten( calibrationCallback );                                           //trigger for calibration control
+
+//************************     LP8 variables      *********************************
+    int                     co2Value    = 400;                                          //initial CO2 value
+    double                  tempValue   = 20.0;                                         //init temp value
+    int                     Vcap        = 3000;                                         //mV    
+    uint32_t                errorFlag   = 0;                                            //error bits from lp8
     
-    ble.init();
-    ble.gap().onDisconnection(disconnectionCallback);                                   //do callback if disconnection occurs
+//************************      Checks            *********************************
+    bool                    initCheck       = true;                                     //check for init sensor state
+    bool                    successCheck    = false;                                    //check for sensor communication
+
+//************************      Calculation control bytes     ********************
+//    uint8_t                 sM = 0x20;                                                  //lp8 calculation control byte using Subesquent Measurments mode
+//    uint8_t                 bC = 0x52;                                                  //Background Calibration using unfilterd data + reset filters
+
+    
+//setup LP8 object 
+    LP8               lp8(Device, VBB_EN, RDY, Res, lp8Wait);                           //pass pins: serial, vbb, rdy, res, timer                           
+     
+//Setup GattService
+    LP8_Service      lp8Service(ble, co2Value, tempValue, 
+                     Vcap, errorFlag, ccByte);                                        //
+    lp8ServicePtr     = &lp8Service;                                                    //
 
 
-    int                     co2Value = 400;                                             //initial CO2 value to display
-    bool                    initCheck = true;                                           //check for init sensor state (or lost state)
-    bool                    successCheck = false;                                       //check for sensor communication
-    //calibration control bytes for the lp 8
-    uint8_t                 subsequentMeasurement       = 0x20;                         //lp8 calculation control byte for subs. measurements
-    uint8_t                 noResetABC                  = 0x70;                         //abc calibration byte with no reset on filters
-    uint8_t                 resetABC                    = 0x72;                         //abc calibration AND resets filters
-    uint8_t                 backgroundCalibration       = 0x52;                         //background calibration using unfilterd data + resets filters
-    
-//setup LP8 object 
-    LP8               lp8(Device, VBB_EN, RDY, lp8Wait);                                //constructor needs 4 arguments
-     
-     
-//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 ble advertising parameters */
+// setup ble 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));
@@ -87,31 +111,51 @@
     ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL);                                 /* advertising interval in ms. */
     ble.gap().startAdvertising();                                                       //start broadcast
     
-        /* SpinWait for initialization to complete. */
-    while (ble.hasInitialized()  == false) { /* spin loop */ }
+// Wait for initialization to complete.
+    while (ble.hasInitialized()  == false) { 
+    /* spin loop */ 
+    }
 
 
 
-//start the main loop    
+//***************************     start the main loop      ***********************************
     while ( true ) 
     {
         if(triggerSensor && ble.gap().getState().connected )                            //trigger when timer interupts and there is an established ble connection
         {
             if ( initCheck ) {
+                
                 successCheck = lp8.lp8Init();                                           //initialize talking with the lp8 (first call on startup)
                 
                 if ( successCheck ) {
                     initCheck = false;
                 }
             }
+/*           
+            else if ( doCalibration && ccByte == bC ){
+                lp8.lp8Talk( bC );                                                      //do background calibration
+                bCalibration.start();
+                  if( bCalibration.read() >= BACKGROUND_CALIBRATION_TIME ){
+                      bCalibration.stop();
+                      bCalibration.reset();
+                      doCalibration = false;
+                      ccByte = 0xff;
+                    }
+            }
+*/            
             else {
-                lp8.lp8Talk( subsequentMeasurement );                                   //Communication with the lp8, sends calculation control: 0x20 == subs. talks
+                lp8.lp8Talk( ccByte );                                                  //Communication with lp8, pass Calculation control byte
             }
+            
             //reset polling check
             triggerSensor = false;
             
-            //update the gattServer with new CO2 value
-            lp8ServicePtr->updateCo2Value( lp8.getValue() );
+            //update the gattServer
+            lp8ServicePtr->updateCo2Value( lp8.getValue() );                            //get CO2 value
+            lp8ServicePtr->updateTempValue( lp8.getTempValue() );                       //
+            lp8ServicePtr->updateVcapValue( lp8.getVcapValue() );                       //
+            lp8ServicePtr->updateError( lp8.getErrorStatus() );                         //
+            lp8ServicePtr->updateDataWritten( lp8.getCCbyte() );                        //send back the calculation control byte that was used in LP8 measurement 
         }
         
         else { ble.waitForEvent(); }                                                    //ble save energy