Simple bluetooth low energy service, featuring three characteristics - two sliders and one button
Fork of Nucleo_BLE_DemoApp by
Diff: DemoAppService.h
- Revision:
- 1:fd7adeffebbe
- Parent:
- 0:866f2c528c01
- Child:
- 2:510cac0a0250
--- a/DemoAppService.h Sun May 10 15:41:38 2015 +0000 +++ b/DemoAppService.h Wed May 27 18:39:23 2015 +0000 @@ -7,8 +7,14 @@ #include "BLEDevice.h" #include "Utils.h" +typedef void (*DemoAppCallback)(uint8_t event); + class DemoAppService { public: + static const uint8_t EVENT_SLIDER1_CHANGED = 0x01; + static const uint8_t EVENT_SLIDER2_CHANGED = 0x02; + static const uint8_t EVENT_BUTTON_CHANGED = 0x04; + static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID]; static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID]; static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID]; @@ -16,6 +22,7 @@ protected: BLEDevice &ble; + DemoAppCallback eventCallback; GattCharacteristic slider1Characteristic; GattCharacteristic slider2Characteristic; GattCharacteristic buttonCharacteristic; @@ -26,27 +33,33 @@ public: DemoAppService(BLEDevice &_ble) : ble(_ble), - slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 1, 2, + eventCallback(NULL), + slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 2, 2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), - slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 1, 2, + slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 2, 2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), - buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 1, 2, + buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 2, 2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){ GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic}; GattService demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); - ble.addService(demoService); ble.onDataWritten(this, &DemoAppService::onDataWritten); } + void setCallback(DemoAppCallback callback){eventCallback = callback;} + + void waitForEvent(void){ble.waitForEvent(); } + virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { + uint8_t event = 0; DEBUG("Demo service onWrite\n\r"); if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) { uint16_t bytesRead = params->len; if (bytesRead == 2) { memcpy(&slider1Value, params->data, 2); DEBUG("Slider1: %d\n\r", slider1Value); + event |= EVENT_SLIDER1_CHANGED; } } else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) { @@ -54,18 +67,25 @@ if (bytesRead == 2) { memcpy(&slider2Value, params->data, 2); DEBUG("Slider2: %d\n\r", slider2Value); + event |= EVENT_SLIDER2_CHANGED; } } else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) { uint16_t bytesRead = params->len; if (bytesRead == 2) { memcpy(&lastButtonPressed, params->data, 2); + event |= EVENT_BUTTON_CHANGED; } } + if(event && eventCallback){ + eventCallback(event); + } } uint16_t getSlider1Value()const{return slider1Value;} uint16_t getSlider2Value()const{return slider2Value;} }; +DemoAppService *startDemoBLE(const char* name); + #endif \ No newline at end of file