Reading SenseAir LP8 CO2 sensor over bluetooth low energy
Dependencies: BLE_API mbed nRF51822
LP8_Service.h@3:933dd59ad44d, 2017-08-18 (annotated)
- Committer:
- jony1401
- Date:
- Fri Aug 18 18:20:41 2017 +0000
- Revision:
- 3:933dd59ad44d
- Parent:
- 2:d02255d8c36f
Updated version. Including more functionality.
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 | 3:933dd59ad44d | 11 | const static uint16_t LP8_SERVICE_UUID = 0xA000; // service identifier |
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 | 3:933dd59ad44d | 15 | const static uint16_t LP8_ERROR_UUID = 0xA011; // LP8 Error bits characteristic |
jony1401 | 2:d02255d8c36f | 16 | const static uint16_t LP8_WRITE_UUID = 0xA002; // Write calculation control from app to lp8 |
jony1401 | 2:d02255d8c36f | 17 | const static uint16_t LP8_WRITTEN_UUID = 0xB001; // display what was written into lp8 calculation control |
jony1401 | 2:d02255d8c36f | 18 | |
jony1401 | 3:933dd59ad44d | 19 | const static uint16_t LP8_reWrite_uuid = 0xB111; |
jony1401 | 0:ee3787c8e209 | 20 | |
jony1401 | 3:933dd59ad44d | 21 | |
jony1401 | 3:933dd59ad44d | 22 | //constructor setup for Gatt Service |
jony1401 | 3:933dd59ad44d | 23 | LP8_Service(BLE &_ble, /* Pass variables to characteristics and ble */ |
jony1401 | 3:933dd59ad44d | 24 | int co2ValueInitial, |
jony1401 | 3:933dd59ad44d | 25 | float tempValueInitial, |
jony1401 | 3:933dd59ad44d | 26 | int initialVcap, |
jony1401 | 3:933dd59ad44d | 27 | uint32_t initialError, |
jony1401 | 3:933dd59ad44d | 28 | uint8_t initCC, |
jony1401 | 3:933dd59ad44d | 29 | uint8_t initSentCC): |
jony1401 | 0:ee3787c8e209 | 30 | ble(_ble), |
jony1401 | 2:d02255d8c36f | 31 | lp8State(LP8_STATE_CHARACTERISTIC_UUID, &co2ValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 32 | lp8Temp(LP8_READ_TEMP_UUID, &tempValueInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 33 | lp8Vcap(LP8_VCAP_UUID, &initialVcap, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 34 | lp8Error(LP8_ERROR_UUID, &initialError, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 2:d02255d8c36f | 35 | lp8WriteCC(LP8_WRITE_UUID, &initCC), |
jony1401 | 3:933dd59ad44d | 36 | lp8Written(LP8_WRITTEN_UUID, &initSentCC, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jony1401 | 3:933dd59ad44d | 37 | lp8reWriteCC(LP8_reWrite_uuid, &initCC, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
jony1401 | 0:ee3787c8e209 | 38 | { |
jony1401 | 2:d02255d8c36f | 39 | //characterisitics |
jony1401 | 3:933dd59ad44d | 40 | GattCharacteristic *charTable[] = {&lp8State, &lp8Temp, &lp8Vcap, &lp8Error, &lp8WriteCC, &lp8Written, &lp8reWriteCC }; |
jony1401 | 2:d02255d8c36f | 41 | |
jony1401 | 2:d02255d8c36f | 42 | //Service, Setup for LP8 GattService |
jony1401 | 2:d02255d8c36f | 43 | GattService lp8Service(LP8_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
jony1401 | 2:d02255d8c36f | 44 | |
jony1401 | 2:d02255d8c36f | 45 | //add gatt service to ble stack |
jony1401 | 2:d02255d8c36f | 46 | ble.addService(lp8Service); |
jony1401 | 0:ee3787c8e209 | 47 | }; |
jony1401 | 0:ee3787c8e209 | 48 | |
jony1401 | 2:d02255d8c36f | 49 | |
jony1401 | 2:d02255d8c36f | 50 | //update Gattserver with new values to characteristics |
jony1401 | 0:ee3787c8e209 | 51 | void updateCo2Value(int co2Value) |
jony1401 | 0:ee3787c8e209 | 52 | { |
jony1401 | 2:d02255d8c36f | 53 | ble.gattServer().write(lp8State.getValueAttribute().getHandle(),(uint8_t *) &co2Value, sizeof(co2Value)); |
jony1401 | 2:d02255d8c36f | 54 | }; |
jony1401 | 2:d02255d8c36f | 55 | |
jony1401 | 3:933dd59ad44d | 56 | void updateTempValue(float tempValue) |
jony1401 | 2:d02255d8c36f | 57 | { |
jony1401 | 2:d02255d8c36f | 58 | ble.gattServer().write(lp8Temp.getValueAttribute().getHandle(),(uint8_t *) &tempValue, sizeof(tempValue)); |
jony1401 | 2:d02255d8c36f | 59 | }; |
jony1401 | 2:d02255d8c36f | 60 | |
jony1401 | 2:d02255d8c36f | 61 | void updateVcapValue(int Vcap) |
jony1401 | 2:d02255d8c36f | 62 | { |
jony1401 | 2:d02255d8c36f | 63 | ble.gattServer().write(lp8Vcap.getValueAttribute().getHandle(),(uint8_t *) &Vcap, sizeof(Vcap)); |
jony1401 | 0:ee3787c8e209 | 64 | }; |
jony1401 | 2:d02255d8c36f | 65 | |
jony1401 | 2:d02255d8c36f | 66 | void updateError(uint32_t lp8ErrorValue) |
jony1401 | 2:d02255d8c36f | 67 | { |
jony1401 | 2:d02255d8c36f | 68 | ble.gattServer().write(lp8Error.getValueAttribute().getHandle(),(uint8_t *) &lp8ErrorValue, sizeof(lp8ErrorValue)); |
jony1401 | 2:d02255d8c36f | 69 | }; |
jony1401 | 2:d02255d8c36f | 70 | |
jony1401 | 3:933dd59ad44d | 71 | // |
jony1401 | 2:d02255d8c36f | 72 | void updateDataWritten(uint8_t lp8WrittenValue) |
jony1401 | 2:d02255d8c36f | 73 | { |
jony1401 | 2:d02255d8c36f | 74 | ble.gattServer().write(lp8Written.getValueAttribute().getHandle(),(uint8_t *) &lp8WrittenValue, sizeof(lp8WrittenValue)); |
jony1401 | 2:d02255d8c36f | 75 | } |
jony1401 | 3:933dd59ad44d | 76 | |
jony1401 | 3:933dd59ad44d | 77 | void updateReWrite ( uint8_t lp8ReWrite ) |
jony1401 | 3:933dd59ad44d | 78 | { |
jony1401 | 3:933dd59ad44d | 79 | ble.gattServer().write(lp8reWriteCC.getValueAttribute().getHandle(),(uint8_t *) &lp8ReWrite, sizeof(lp8ReWrite)); |
jony1401 | 3:933dd59ad44d | 80 | } |
jony1401 | 2:d02255d8c36f | 81 | |
jony1401 | 0:ee3787c8e209 | 82 | |
jony1401 | 0:ee3787c8e209 | 83 | private: |
jony1401 | 0:ee3787c8e209 | 84 | |
jony1401 | 2:d02255d8c36f | 85 | BLE &ble; // |
jony1401 | 2:d02255d8c36f | 86 | |
jony1401 | 2:d02255d8c36f | 87 | //Service Characteristics |
jony1401 | 3:933dd59ad44d | 88 | |
jony1401 | 3:933dd59ad44d | 89 | //Read sensor values |
jony1401 | 2:d02255d8c36f | 90 | ReadOnlyGattCharacteristic<int> lp8State; // |
jony1401 | 3:933dd59ad44d | 91 | ReadOnlyGattCharacteristic<float> lp8Temp; |
jony1401 | 2:d02255d8c36f | 92 | ReadOnlyGattCharacteristic<int> lp8Vcap; |
jony1401 | 2:d02255d8c36f | 93 | ReadOnlyGattCharacteristic<uint32_t> lp8Error; |
jony1401 | 3:933dd59ad44d | 94 | |
jony1401 | 3:933dd59ad44d | 95 | //Write Value |
jony1401 | 3:933dd59ad44d | 96 | WriteOnlyGattCharacteristic<uint8_t> lp8WriteCC; |
jony1401 | 3:933dd59ad44d | 97 | |
jony1401 | 3:933dd59ad44d | 98 | //read back written and cc byte(flag) |
jony1401 | 2:d02255d8c36f | 99 | ReadOnlyGattCharacteristic<uint8_t> lp8Written; |
jony1401 | 3:933dd59ad44d | 100 | ReadOnlyGattCharacteristic<uint8_t> lp8reWriteCC; |
jony1401 | 0:ee3787c8e209 | 101 | |
jony1401 | 0:ee3787c8e209 | 102 | |
jony1401 | 0:ee3787c8e209 | 103 | }; |
jony1401 | 0:ee3787c8e209 | 104 | #endif |