Simple bluetooth low energy service, featuring three characteristics - two sliders and one button
DemoAppService.h@0:866f2c528c01, 2015-05-10 (annotated)
- Committer:
- berlingeradam
- Date:
- Sun May 10 15:41:38 2015 +0000
- Revision:
- 0:866f2c528c01
- Child:
- 1:fd7adeffebbe
Initial commit - 2 sliders 1 button
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
berlingeradam | 0:866f2c528c01 | 1 | #ifndef __BLE_DEMOAPP_SERVICE_H__ |
berlingeradam | 0:866f2c528c01 | 2 | #define __BLE_DEMOAPP_SERVICE_H__ |
berlingeradam | 0:866f2c528c01 | 3 | |
berlingeradam | 0:866f2c528c01 | 4 | #include "Stream.h" |
berlingeradam | 0:866f2c528c01 | 5 | |
berlingeradam | 0:866f2c528c01 | 6 | #include "UUID.h" |
berlingeradam | 0:866f2c528c01 | 7 | #include "BLEDevice.h" |
berlingeradam | 0:866f2c528c01 | 8 | #include "Utils.h" |
berlingeradam | 0:866f2c528c01 | 9 | |
berlingeradam | 0:866f2c528c01 | 10 | class DemoAppService { |
berlingeradam | 0:866f2c528c01 | 11 | public: |
berlingeradam | 0:866f2c528c01 | 12 | static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID]; |
berlingeradam | 0:866f2c528c01 | 13 | static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID]; |
berlingeradam | 0:866f2c528c01 | 14 | static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID]; |
berlingeradam | 0:866f2c528c01 | 15 | static const uint8_t buttonCharacteristicUUID[LENGTH_OF_LONG_UUID]; |
berlingeradam | 0:866f2c528c01 | 16 | protected: |
berlingeradam | 0:866f2c528c01 | 17 | BLEDevice &ble; |
berlingeradam | 0:866f2c528c01 | 18 | |
berlingeradam | 0:866f2c528c01 | 19 | GattCharacteristic slider1Characteristic; |
berlingeradam | 0:866f2c528c01 | 20 | GattCharacteristic slider2Characteristic; |
berlingeradam | 0:866f2c528c01 | 21 | GattCharacteristic buttonCharacteristic; |
berlingeradam | 0:866f2c528c01 | 22 | |
berlingeradam | 0:866f2c528c01 | 23 | uint16_t lastButtonPressed; |
berlingeradam | 0:866f2c528c01 | 24 | uint16_t slider1Value; |
berlingeradam | 0:866f2c528c01 | 25 | uint16_t slider2Value; |
berlingeradam | 0:866f2c528c01 | 26 | public: |
berlingeradam | 0:866f2c528c01 | 27 | DemoAppService(BLEDevice &_ble) : |
berlingeradam | 0:866f2c528c01 | 28 | ble(_ble), |
berlingeradam | 0:866f2c528c01 | 29 | slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 1, 2, |
berlingeradam | 0:866f2c528c01 | 30 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
berlingeradam | 0:866f2c528c01 | 31 | slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 1, 2, |
berlingeradam | 0:866f2c528c01 | 32 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
berlingeradam | 0:866f2c528c01 | 33 | buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 1, 2, |
berlingeradam | 0:866f2c528c01 | 34 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){ |
berlingeradam | 0:866f2c528c01 | 35 | |
berlingeradam | 0:866f2c528c01 | 36 | GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic}; |
berlingeradam | 0:866f2c528c01 | 37 | GattService demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
berlingeradam | 0:866f2c528c01 | 38 | |
berlingeradam | 0:866f2c528c01 | 39 | ble.addService(demoService); |
berlingeradam | 0:866f2c528c01 | 40 | ble.onDataWritten(this, &DemoAppService::onDataWritten); |
berlingeradam | 0:866f2c528c01 | 41 | } |
berlingeradam | 0:866f2c528c01 | 42 | |
berlingeradam | 0:866f2c528c01 | 43 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
berlingeradam | 0:866f2c528c01 | 44 | DEBUG("Demo service onWrite\n\r"); |
berlingeradam | 0:866f2c528c01 | 45 | if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 46 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 47 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 48 | memcpy(&slider1Value, params->data, 2); |
berlingeradam | 0:866f2c528c01 | 49 | DEBUG("Slider1: %d\n\r", slider1Value); |
berlingeradam | 0:866f2c528c01 | 50 | } |
berlingeradam | 0:866f2c528c01 | 51 | } |
berlingeradam | 0:866f2c528c01 | 52 | else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 53 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 54 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 55 | memcpy(&slider2Value, params->data, 2); |
berlingeradam | 0:866f2c528c01 | 56 | DEBUG("Slider2: %d\n\r", slider2Value); |
berlingeradam | 0:866f2c528c01 | 57 | } |
berlingeradam | 0:866f2c528c01 | 58 | } |
berlingeradam | 0:866f2c528c01 | 59 | else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 60 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 61 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 62 | memcpy(&lastButtonPressed, params->data, 2); |
berlingeradam | 0:866f2c528c01 | 63 | } |
berlingeradam | 0:866f2c528c01 | 64 | } |
berlingeradam | 0:866f2c528c01 | 65 | } |
berlingeradam | 0:866f2c528c01 | 66 | |
berlingeradam | 0:866f2c528c01 | 67 | uint16_t getSlider1Value()const{return slider1Value;} |
berlingeradam | 0:866f2c528c01 | 68 | uint16_t getSlider2Value()const{return slider2Value;} |
berlingeradam | 0:866f2c528c01 | 69 | }; |
berlingeradam | 0:866f2c528c01 | 70 | |
berlingeradam | 0:866f2c528c01 | 71 | #endif |