Reading SenseAir LP8 CO2 sensor over bluetooth low energy
Dependencies: BLE_API mbed nRF51822
LP8_Service.h@2:d02255d8c36f, 2017-08-14 (annotated)
- Committer:
- jony1401
- Date:
- Mon Aug 14 20:52:48 2017 +0000
- Revision:
- 2:d02255d8c36f
- Parent:
- 0:ee3787c8e209
- Child:
- 3:933dd59ad44d
v2.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jony1401 | 0:ee3787c8e209 | 1 | #ifndef LP8_SERVICE_H |
jony1401 | 0:ee3787c8e209 | 2 | #define LP8_SERVICE_H |
jony1401 | 0:ee3787c8e209 | 3 | |
jony1401 | 0:ee3787c8e209 | 4 | /* LP8 Gatt Service Class */ |
jony1401 | 0:ee3787c8e209 | 5 | |
jony1401 | 0:ee3787c8e209 | 6 | class LP8_Service |
jony1401 | 0:ee3787c8e209 | 7 | { |
jony1401 | 0:ee3787c8e209 | 8 | public: |
jony1401 | 0:ee3787c8e209 | 9 | |
jony1401 | 2:d02255d8c36f | 10 | //UUID descriptors |
jony1401 | 2:d02255d8c36f | 11 | const static uint16_t LP8_SERVICE_UUID = 0xA000; // |
jony1401 | 2:d02255d8c36f | 12 | const static uint16_t LP8_STATE_CHARACTERISTIC_UUID = 0xA001; // CO2 concentration characteristic |
jony1401 | 2:d02255d8c36f | 13 | const static uint16_t LP8_READ_TEMP_UUID = 0x2A1F; // temp (Celsius) characteristic |
jony1401 | 2:d02255d8c36f | 14 | const static uint16_t LP8_VCAP_UUID = 0xA010; // Vcap characteristic (mV) |
jony1401 | 2:d02255d8c36f | 15 | const static uint16_t LP8_ERROR_UUID = 0xA011; // LP8 Error bits characteristic |
jony1401 | 2:d02255d8c36f | 16 | |
jony1401 | 2:d02255d8c36f | 17 | const static uint16_t LP8_WRITE_UUID = 0xA002; // Write calculation control from app to lp8 |
jony1401 | 2:d02255d8c36f | 18 | const static uint16_t LP8_WRITTEN_UUID = 0xB001; // display what was written into lp8 calculation control |
jony1401 | 2:d02255d8c36f | 19 | |
jony1401 | 0:ee3787c8e209 | 20 | |
jony1401 | 0:ee3787c8e209 | 21 | //constructor setup for Gatt Service |
jony1401 | 2:d02255d8c36f | 22 | LP8_Service(BLE &_ble, int co2ValueInitial, double tempValueInitial, |
jony1401 | 2:d02255d8c36f | 23 | int initialVcap, uint32_t initialError, uint8_t initCC): /* Pass variables to ble stack */ |
jony1401 | 0:ee3787c8e209 | 24 | ble(_ble), |
jony1401 | 2:d02255d8c36f | 25 | lp8State(LP8_STATE_CHARACTERISTIC_UUID, &co2ValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 26 | lp8Temp(LP8_READ_TEMP_UUID, &tempValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 27 | lp8Vcap(LP8_VCAP_UUID, &initialVcap, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 28 | lp8Error(LP8_ERROR_UUID, &initialError, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 29 | lp8WriteCC(LP8_WRITE_UUID, &initCC), |
jony1401 | 2:d02255d8c36f | 30 | lp8Written(LP8_WRITTEN_UUID, &initCC, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
jony1401 | 0:ee3787c8e209 | 31 | { |
jony1401 | 2:d02255d8c36f | 32 | //characterisitics |
jony1401 | 2:d02255d8c36f | 33 | GattCharacteristic *charTable[] = {&lp8State, &lp8Temp, &lp8Vcap, &lp8Error, &lp8WriteCC, &lp8Written }; |
jony1401 | 2:d02255d8c36f | 34 | |
jony1401 | 2:d02255d8c36f | 35 | //Service, Setup for LP8 GattService |
jony1401 | 2:d02255d8c36f | 36 | GattService lp8Service(LP8_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
jony1401 | 2:d02255d8c36f | 37 | |
jony1401 | 2:d02255d8c36f | 38 | //add gatt service to ble stack |
jony1401 | 2:d02255d8c36f | 39 | ble.addService(lp8Service); |
jony1401 | 0:ee3787c8e209 | 40 | }; |
jony1401 | 0:ee3787c8e209 | 41 | |
jony1401 | 2:d02255d8c36f | 42 | |
jony1401 | 2:d02255d8c36f | 43 | //update Gattserver with new values to characteristics |
jony1401 | 0:ee3787c8e209 | 44 | void updateCo2Value(int co2Value) |
jony1401 | 0:ee3787c8e209 | 45 | { |
jony1401 | 2:d02255d8c36f | 46 | ble.gattServer().write(lp8State.getValueAttribute().getHandle(),(uint8_t *) &co2Value, sizeof(co2Value)); |
jony1401 | 2:d02255d8c36f | 47 | }; |
jony1401 | 2:d02255d8c36f | 48 | |
jony1401 | 2:d02255d8c36f | 49 | void updateTempValue(double tempValue) |
jony1401 | 2:d02255d8c36f | 50 | { |
jony1401 | 2:d02255d8c36f | 51 | ble.gattServer().write(lp8Temp.getValueAttribute().getHandle(),(uint8_t *) &tempValue, sizeof(tempValue)); |
jony1401 | 2:d02255d8c36f | 52 | }; |
jony1401 | 2:d02255d8c36f | 53 | |
jony1401 | 2:d02255d8c36f | 54 | void updateVcapValue(int Vcap) |
jony1401 | 2:d02255d8c36f | 55 | { |
jony1401 | 2:d02255d8c36f | 56 | ble.gattServer().write(lp8Vcap.getValueAttribute().getHandle(),(uint8_t *) &Vcap, sizeof(Vcap)); |
jony1401 | 0:ee3787c8e209 | 57 | }; |
jony1401 | 2:d02255d8c36f | 58 | |
jony1401 | 2:d02255d8c36f | 59 | void updateError(uint32_t lp8ErrorValue) |
jony1401 | 2:d02255d8c36f | 60 | { |
jony1401 | 2:d02255d8c36f | 61 | ble.gattServer().write(lp8Error.getValueAttribute().getHandle(),(uint8_t *) &lp8ErrorValue, sizeof(lp8ErrorValue)); |
jony1401 | 2:d02255d8c36f | 62 | }; |
jony1401 | 2:d02255d8c36f | 63 | |
jony1401 | 2:d02255d8c36f | 64 | //reWrite written data |
jony1401 | 2:d02255d8c36f | 65 | void updateDataWritten(uint8_t lp8WrittenValue) |
jony1401 | 2:d02255d8c36f | 66 | { |
jony1401 | 2:d02255d8c36f | 67 | ble.gattServer().write(lp8Written.getValueAttribute().getHandle(),(uint8_t *) &lp8WrittenValue, sizeof(lp8WrittenValue)); |
jony1401 | 2:d02255d8c36f | 68 | } |
jony1401 | 2:d02255d8c36f | 69 | |
jony1401 | 0:ee3787c8e209 | 70 | |
jony1401 | 0:ee3787c8e209 | 71 | private: |
jony1401 | 0:ee3787c8e209 | 72 | |
jony1401 | 2:d02255d8c36f | 73 | BLE &ble; // |
jony1401 | 2:d02255d8c36f | 74 | |
jony1401 | 2:d02255d8c36f | 75 | //Service Characteristics |
jony1401 | 2:d02255d8c36f | 76 | //Read Values |
jony1401 | 2:d02255d8c36f | 77 | ReadOnlyGattCharacteristic<int> lp8State; // |
jony1401 | 2:d02255d8c36f | 78 | ReadOnlyGattCharacteristic<double> lp8Temp; |
jony1401 | 2:d02255d8c36f | 79 | ReadOnlyGattCharacteristic<int> lp8Vcap; |
jony1401 | 2:d02255d8c36f | 80 | ReadOnlyGattCharacteristic<uint32_t> lp8Error; |
jony1401 | 2:d02255d8c36f | 81 | ReadOnlyGattCharacteristic<uint8_t> lp8Written; |
jony1401 | 2:d02255d8c36f | 82 | //Write Values |
jony1401 | 2:d02255d8c36f | 83 | WriteOnlyGattCharacteristic<uint8_t> lp8WriteCC; |
jony1401 | 0:ee3787c8e209 | 84 | |
jony1401 | 0:ee3787c8e209 | 85 | |
jony1401 | 0:ee3787c8e209 | 86 | }; |
jony1401 | 0:ee3787c8e209 | 87 | #endif |