Simple bluetooth low energy service, featuring three characteristics - two sliders and one button

Fork of Nucleo_BLE_DemoApp by Cortex Challenge Team

Committer:
berlingeradam
Date:
Wed May 27 18:39:23 2015 +0000
Revision:
1:fd7adeffebbe
Parent:
0:866f2c528c01
Child:
2:510cac0a0250
Factory function for easy initialization

Who changed what in which revision?

UserRevisionLine numberNew 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 1:fd7adeffebbe 10 typedef void (*DemoAppCallback)(uint8_t event);
berlingeradam 1:fd7adeffebbe 11
berlingeradam 0:866f2c528c01 12 class DemoAppService {
berlingeradam 0:866f2c528c01 13 public:
berlingeradam 1:fd7adeffebbe 14 static const uint8_t EVENT_SLIDER1_CHANGED = 0x01;
berlingeradam 1:fd7adeffebbe 15 static const uint8_t EVENT_SLIDER2_CHANGED = 0x02;
berlingeradam 1:fd7adeffebbe 16 static const uint8_t EVENT_BUTTON_CHANGED = 0x04;
berlingeradam 1:fd7adeffebbe 17
berlingeradam 0:866f2c528c01 18 static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 19 static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 20 static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 21 static const uint8_t buttonCharacteristicUUID[LENGTH_OF_LONG_UUID];
berlingeradam 0:866f2c528c01 22 protected:
berlingeradam 0:866f2c528c01 23 BLEDevice &ble;
berlingeradam 0:866f2c528c01 24
berlingeradam 1:fd7adeffebbe 25 DemoAppCallback eventCallback;
berlingeradam 0:866f2c528c01 26 GattCharacteristic slider1Characteristic;
berlingeradam 0:866f2c528c01 27 GattCharacteristic slider2Characteristic;
berlingeradam 0:866f2c528c01 28 GattCharacteristic buttonCharacteristic;
berlingeradam 0:866f2c528c01 29
berlingeradam 0:866f2c528c01 30 uint16_t lastButtonPressed;
berlingeradam 0:866f2c528c01 31 uint16_t slider1Value;
berlingeradam 0:866f2c528c01 32 uint16_t slider2Value;
berlingeradam 0:866f2c528c01 33 public:
berlingeradam 0:866f2c528c01 34 DemoAppService(BLEDevice &_ble) :
berlingeradam 0:866f2c528c01 35 ble(_ble),
berlingeradam 1:fd7adeffebbe 36 eventCallback(NULL),
berlingeradam 1:fd7adeffebbe 37 slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 2, 2,
berlingeradam 0:866f2c528c01 38 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
berlingeradam 1:fd7adeffebbe 39 slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 2, 2,
berlingeradam 0:866f2c528c01 40 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
berlingeradam 1:fd7adeffebbe 41 buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 2, 2,
berlingeradam 0:866f2c528c01 42 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
berlingeradam 0:866f2c528c01 43
berlingeradam 0:866f2c528c01 44 GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic};
berlingeradam 0:866f2c528c01 45 GattService demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
berlingeradam 0:866f2c528c01 46 ble.addService(demoService);
berlingeradam 0:866f2c528c01 47 ble.onDataWritten(this, &DemoAppService::onDataWritten);
berlingeradam 0:866f2c528c01 48 }
berlingeradam 0:866f2c528c01 49
berlingeradam 1:fd7adeffebbe 50 void setCallback(DemoAppCallback callback){eventCallback = callback;}
berlingeradam 1:fd7adeffebbe 51
berlingeradam 1:fd7adeffebbe 52 void waitForEvent(void){ble.waitForEvent(); }
berlingeradam 1:fd7adeffebbe 53
berlingeradam 0:866f2c528c01 54 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
berlingeradam 1:fd7adeffebbe 55 uint8_t event = 0;
berlingeradam 0:866f2c528c01 56 DEBUG("Demo service onWrite\n\r");
berlingeradam 0:866f2c528c01 57 if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 58 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 59 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 60 memcpy(&slider1Value, params->data, 2);
berlingeradam 0:866f2c528c01 61 DEBUG("Slider1: %d\n\r", slider1Value);
berlingeradam 1:fd7adeffebbe 62 event |= EVENT_SLIDER1_CHANGED;
berlingeradam 0:866f2c528c01 63 }
berlingeradam 0:866f2c528c01 64 }
berlingeradam 0:866f2c528c01 65 else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 66 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 67 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 68 memcpy(&slider2Value, params->data, 2);
berlingeradam 0:866f2c528c01 69 DEBUG("Slider2: %d\n\r", slider2Value);
berlingeradam 1:fd7adeffebbe 70 event |= EVENT_SLIDER2_CHANGED;
berlingeradam 0:866f2c528c01 71 }
berlingeradam 0:866f2c528c01 72 }
berlingeradam 0:866f2c528c01 73 else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) {
berlingeradam 0:866f2c528c01 74 uint16_t bytesRead = params->len;
berlingeradam 0:866f2c528c01 75 if (bytesRead == 2) {
berlingeradam 0:866f2c528c01 76 memcpy(&lastButtonPressed, params->data, 2);
berlingeradam 1:fd7adeffebbe 77 event |= EVENT_BUTTON_CHANGED;
berlingeradam 0:866f2c528c01 78 }
berlingeradam 0:866f2c528c01 79 }
berlingeradam 1:fd7adeffebbe 80 if(event && eventCallback){
berlingeradam 1:fd7adeffebbe 81 eventCallback(event);
berlingeradam 1:fd7adeffebbe 82 }
berlingeradam 0:866f2c528c01 83 }
berlingeradam 0:866f2c528c01 84
berlingeradam 0:866f2c528c01 85 uint16_t getSlider1Value()const{return slider1Value;}
berlingeradam 0:866f2c528c01 86 uint16_t getSlider2Value()const{return slider2Value;}
berlingeradam 0:866f2c528c01 87 };
berlingeradam 0:866f2c528c01 88
berlingeradam 1:fd7adeffebbe 89 DemoAppService *startDemoBLE(const char* name);
berlingeradam 1:fd7adeffebbe 90
berlingeradam 0:866f2c528c01 91 #endif