iotio
Fork of Nucleo_BLE_DemoApp by
DemoAppService.h@3:b1f65162c95b, 2016-10-29 (annotated)
- Committer:
- 16038618
- Date:
- Sat Oct 29 14:16:34 2016 +0000
- Revision:
- 3:b1f65162c95b
- Parent:
- 2:510cac0a0250
IOTIO
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 | 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; |
16038618 | 3:b1f65162c95b | 24 | GattCharacteristic humiditychar; |
16038618 | 3:b1f65162c95b | 25 | GattCharacteristic temperaturechar; |
16038618 | 3:b1f65162c95b | 26 | GattCharacteristic winddirectionchar; |
16038618 | 3:b1f65162c95b | 27 | GattCharacteristic pressurechar; |
berlingeradam | 1:fd7adeffebbe | 28 | DemoAppCallback eventCallback; |
berlingeradam | 0:866f2c528c01 | 29 | GattCharacteristic slider1Characteristic; |
berlingeradam | 0:866f2c528c01 | 30 | GattCharacteristic slider2Characteristic; |
berlingeradam | 0:866f2c528c01 | 31 | GattCharacteristic buttonCharacteristic; |
berlingeradam | 0:866f2c528c01 | 32 | |
berlingeradam | 0:866f2c528c01 | 33 | uint16_t lastButtonPressed; |
berlingeradam | 0:866f2c528c01 | 34 | uint16_t slider1Value; |
berlingeradam | 0:866f2c528c01 | 35 | uint16_t slider2Value; |
berlingeradam | 0:866f2c528c01 | 36 | public: |
16038618 | 3:b1f65162c95b | 37 | DemoAppService(BLEDevice &_ble, uint16_t humidity, int16_t temperature, uint16_t winddirection, uint32_t pressure) : |
berlingeradam | 0:866f2c528c01 | 38 | ble(_ble), |
16038618 | 3:b1f65162c95b | 39 | humiditychar(GattCharacteristic::UUID_HUMIDITY_CHAR, (uint8_t *)&humidity, |
16038618 | 3:b1f65162c95b | 40 | sizeof(uint16_t), sizeof(uint16_t), |
16038618 | 3:b1f65162c95b | 41 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
16038618 | 3:b1f65162c95b | 42 | |
16038618 | 3:b1f65162c95b | 43 | temperaturechar(GattCharacteristic::UUID_TEMPERATURE_CHAR, (uint8_t *)&temperature, |
16038618 | 3:b1f65162c95b | 44 | sizeof(int16_t), sizeof(int16_t), |
16038618 | 3:b1f65162c95b | 45 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
16038618 | 3:b1f65162c95b | 46 | |
16038618 | 3:b1f65162c95b | 47 | winddirectionchar(GattCharacteristic::UUID_TRUE_WIND_DIRECTION_CHAR, (uint8_t *)&winddirection, |
16038618 | 3:b1f65162c95b | 48 | sizeof(uint16_t), sizeof(uint16_t), |
16038618 | 3:b1f65162c95b | 49 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
16038618 | 3:b1f65162c95b | 50 | |
16038618 | 3:b1f65162c95b | 51 | pressurechar(GattCharacteristic::UUID_PRESSURE_CHAR, (uint8_t *)&pressure, |
16038618 | 3:b1f65162c95b | 52 | sizeof(uint32_t), sizeof(uint32_t), |
16038618 | 3:b1f65162c95b | 53 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
berlingeradam | 1:fd7adeffebbe | 54 | eventCallback(NULL), |
berlingeradam | 1:fd7adeffebbe | 55 | slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 2, 2, |
berlingeradam | 0:866f2c528c01 | 56 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
berlingeradam | 1:fd7adeffebbe | 57 | slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 2, 2, |
berlingeradam | 0:866f2c528c01 | 58 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
berlingeradam | 1:fd7adeffebbe | 59 | buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 2, 2, |
berlingeradam | 0:866f2c528c01 | 60 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){ |
16038618 | 3:b1f65162c95b | 61 | |
16038618 | 3:b1f65162c95b | 62 | GattCharacteristic *charTable[] = {&humiditychar, &temperaturechar, }; |
16038618 | 3:b1f65162c95b | 63 | GattCharacteristic *charTable2[] = {&winddirectionchar, &pressurechar}; |
16038618 | 3:b1f65162c95b | 64 | |
16038618 | 3:b1f65162c95b | 65 | GattService EnvironmentalService(GattService::UUID_ENVIRONMENTAL_SENSING_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
16038618 | 3:b1f65162c95b | 66 | GattService EnvironmentalService2(GattService::UUID_ENVIRONMENTAL_SENSING_SERVICE, charTable2, sizeof(charTable2) / sizeof(GattCharacteristic *)); |
16038618 | 3:b1f65162c95b | 67 | |
16038618 | 3:b1f65162c95b | 68 | ble.addService(EnvironmentalService); |
16038618 | 3:b1f65162c95b | 69 | ble.addService(EnvironmentalService2); |
16038618 | 3:b1f65162c95b | 70 | |
16038618 | 3:b1f65162c95b | 71 | // ble.onDataWritten(this, &DemoAppService::onDataWritten); |
berlingeradam | 0:866f2c528c01 | 72 | } |
berlingeradam | 0:866f2c528c01 | 73 | |
16038618 | 3:b1f65162c95b | 74 | /* Set a new 16-bit value for the humidity measurement. */ |
16038618 | 3:b1f65162c95b | 75 | void updateHumidity(uint16_t humidity) { |
16038618 | 3:b1f65162c95b | 76 | ble.updateCharacteristicValue(humiditychar.getValueAttribute().getHandle(), (uint8_t *)&humidity, sizeof(uint16_t)); |
16038618 | 3:b1f65162c95b | 77 | } |
16038618 | 3:b1f65162c95b | 78 | |
16038618 | 3:b1f65162c95b | 79 | /* Set a new 16-bit value for the temperature measurement. */ |
16038618 | 3:b1f65162c95b | 80 | void updateTemperature(int16_t temperature) { |
16038618 | 3:b1f65162c95b | 81 | ble.updateCharacteristicValue(temperaturechar.getValueAttribute().getHandle(), (uint8_t *)&temperature, sizeof(int16_t)); |
16038618 | 3:b1f65162c95b | 82 | } |
16038618 | 3:b1f65162c95b | 83 | |
16038618 | 3:b1f65162c95b | 84 | |
16038618 | 3:b1f65162c95b | 85 | /* Set a new 16-bit value for the wind direction measurement. */ |
16038618 | 3:b1f65162c95b | 86 | void updateWinddirection(uint16_t winddirection) { |
16038618 | 3:b1f65162c95b | 87 | ble.updateCharacteristicValue(winddirectionchar.getValueAttribute().getHandle(), (uint8_t *)&winddirection, sizeof(uint16_t)); |
16038618 | 3:b1f65162c95b | 88 | } |
16038618 | 3:b1f65162c95b | 89 | |
16038618 | 3:b1f65162c95b | 90 | /* Set a new 32-bit value for the pressure measurement. */ |
16038618 | 3:b1f65162c95b | 91 | void updatePressure(uint32_t pressure) { |
16038618 | 3:b1f65162c95b | 92 | ble.updateCharacteristicValue(pressurechar.getValueAttribute().getHandle(), (uint8_t *)&pressure, sizeof(uint32_t)); |
16038618 | 3:b1f65162c95b | 93 | } |
16038618 | 3:b1f65162c95b | 94 | |
16038618 | 3:b1f65162c95b | 95 | |
16038618 | 3:b1f65162c95b | 96 | |
berlingeradam | 1:fd7adeffebbe | 97 | void setCallback(DemoAppCallback callback){eventCallback = callback;} |
berlingeradam | 1:fd7adeffebbe | 98 | |
berlingeradam | 1:fd7adeffebbe | 99 | void waitForEvent(void){ble.waitForEvent(); } |
berlingeradam | 1:fd7adeffebbe | 100 | |
berlingeradam | 0:866f2c528c01 | 101 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
berlingeradam | 1:fd7adeffebbe | 102 | uint8_t event = 0; |
berlingeradam | 0:866f2c528c01 | 103 | DEBUG("Demo service onWrite\n\r"); |
berlingeradam | 0:866f2c528c01 | 104 | if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 105 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 106 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 107 | memcpy(&slider1Value, params->data, 2); |
berlingeradam | 0:866f2c528c01 | 108 | DEBUG("Slider1: %d\n\r", slider1Value); |
berlingeradam | 1:fd7adeffebbe | 109 | event |= EVENT_SLIDER1_CHANGED; |
berlingeradam | 0:866f2c528c01 | 110 | } |
berlingeradam | 0:866f2c528c01 | 111 | } |
berlingeradam | 0:866f2c528c01 | 112 | else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 113 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 114 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 115 | memcpy(&slider2Value, params->data, 2); |
berlingeradam | 0:866f2c528c01 | 116 | DEBUG("Slider2: %d\n\r", slider2Value); |
berlingeradam | 1:fd7adeffebbe | 117 | event |= EVENT_SLIDER2_CHANGED; |
berlingeradam | 0:866f2c528c01 | 118 | } |
berlingeradam | 0:866f2c528c01 | 119 | } |
berlingeradam | 0:866f2c528c01 | 120 | else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) { |
berlingeradam | 0:866f2c528c01 | 121 | uint16_t bytesRead = params->len; |
berlingeradam | 0:866f2c528c01 | 122 | if (bytesRead == 2) { |
berlingeradam | 0:866f2c528c01 | 123 | memcpy(&lastButtonPressed, params->data, 2); |
berlingeradam | 1:fd7adeffebbe | 124 | event |= EVENT_BUTTON_CHANGED; |
berlingeradam | 0:866f2c528c01 | 125 | } |
berlingeradam | 0:866f2c528c01 | 126 | } |
berlingeradam | 1:fd7adeffebbe | 127 | if(event && eventCallback){ |
berlingeradam | 1:fd7adeffebbe | 128 | eventCallback(event); |
berlingeradam | 1:fd7adeffebbe | 129 | } |
berlingeradam | 0:866f2c528c01 | 130 | } |
berlingeradam | 0:866f2c528c01 | 131 | |
berlingeradam | 0:866f2c528c01 | 132 | uint16_t getSlider1Value()const{return slider1Value;} |
berlingeradam | 0:866f2c528c01 | 133 | uint16_t getSlider2Value()const{return slider2Value;} |
berlingeradam | 0:866f2c528c01 | 134 | }; |
berlingeradam | 0:866f2c528c01 | 135 | |
berlingeradam | 1:fd7adeffebbe | 136 | DemoAppService *startDemoBLE(const char* name); |
berlingeradam | 1:fd7adeffebbe | 137 | |
berlingeradam | 0:866f2c528c01 | 138 | #endif |