Reading SenseAir LP8 CO2 sensor over bluetooth low energy

Dependencies:   BLE_API mbed nRF51822

LP8_Service.h

Committer:
jony1401
Date:
2017-08-14
Revision:
2:d02255d8c36f
Parent:
0:ee3787c8e209
Child:
3:933dd59ad44d

File content as of revision 2:d02255d8c36f:

#ifndef LP8_SERVICE_H
#define LP8_SERVICE_H

/* LP8 Gatt Service Class */

class LP8_Service 
{
public:

    //UUID descriptors
    const static uint16_t LP8_SERVICE_UUID                      = 0xA000;           // 
    const static uint16_t LP8_STATE_CHARACTERISTIC_UUID         = 0xA001;           // CO2 concentration characteristic
    const static uint16_t LP8_READ_TEMP_UUID                    = 0x2A1F;           // temp (Celsius) characteristic
    const static uint16_t LP8_VCAP_UUID                         = 0xA010;           // Vcap characteristic (mV)
    const static uint16_t LP8_ERROR_UUID                        = 0xA011;           // LP8 Error bits characteristic
    
    const static uint16_t LP8_WRITE_UUID                        = 0xA002;           // Write calculation control from app to lp8
    const static uint16_t LP8_WRITTEN_UUID                      = 0xB001;           // display what was written into lp8 calculation control
    
    
    //constructor setup for Gatt Service
    LP8_Service(BLE &_ble, int co2ValueInitial, double tempValueInitial, 
                int initialVcap, uint32_t initialError, uint8_t initCC):  /* Pass variables to ble stack  */
        ble(_ble), 
        lp8State(LP8_STATE_CHARACTERISTIC_UUID, &co2ValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        lp8Temp(LP8_READ_TEMP_UUID, &tempValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        lp8Vcap(LP8_VCAP_UUID, &initialVcap, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        lp8Error(LP8_ERROR_UUID, &initialError, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        lp8WriteCC(LP8_WRITE_UUID, &initCC),
        lp8Written(LP8_WRITTEN_UUID, &initCC, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
        {
            //characterisitics 
            GattCharacteristic *charTable[] = {&lp8State, &lp8Temp, &lp8Vcap, &lp8Error, &lp8WriteCC, &lp8Written };
             
            //Service, Setup for LP8 GattService
            GattService        lp8Service(LP8_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));       
            
            //add gatt service to ble stack
            ble.addService(lp8Service);                                                                                         
        };


//update Gattserver with new values to characteristics
    void updateCo2Value(int co2Value)
    {
        ble.gattServer().write(lp8State.getValueAttribute().getHandle(),(uint8_t *) &co2Value, sizeof(co2Value));   
    };

    void updateTempValue(double tempValue)
    {
        ble.gattServer().write(lp8Temp.getValueAttribute().getHandle(),(uint8_t *) &tempValue, sizeof(tempValue));              
    };
    
    void updateVcapValue(int Vcap)
    {
        ble.gattServer().write(lp8Vcap.getValueAttribute().getHandle(),(uint8_t *) &Vcap, sizeof(Vcap));                        
    };
    
    void updateError(uint32_t lp8ErrorValue)
    {
        ble.gattServer().write(lp8Error.getValueAttribute().getHandle(),(uint8_t *) &lp8ErrorValue, sizeof(lp8ErrorValue));                        
    };

//reWrite written data     
    void updateDataWritten(uint8_t lp8WrittenValue)
    {
        ble.gattServer().write(lp8Written.getValueAttribute().getHandle(),(uint8_t *) &lp8WrittenValue, sizeof(lp8WrittenValue));
    }


private:

    BLE                                     &ble;       //
    
    //Service Characteristics
    //Read Values
    ReadOnlyGattCharacteristic<int>         lp8State;   //
    ReadOnlyGattCharacteristic<double>      lp8Temp;
    ReadOnlyGattCharacteristic<int>         lp8Vcap;
    ReadOnlyGattCharacteristic<uint32_t>    lp8Error;
    ReadOnlyGattCharacteristic<uint8_t>     lp8Written;
    //Write Values
    WriteOnlyGattCharacteristic<uint8_t>    lp8WriteCC;
    

};
#endif