A simple extension to exististing sample
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
ButtonService.h@6:c18ec2f18b33, 2015-06-12 (annotated)
- Committer:
- blinky
- Date:
- Fri Jun 12 16:33:21 2015 +0000
- Revision:
- 6:c18ec2f18b33
- Parent:
- 5:bf55ff59a71f
Blinky sample modifed by blinky.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:28f095301cb2 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:28f095301cb2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:28f095301cb2 | 3 | * |
rgrover1 | 0:28f095301cb2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:28f095301cb2 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:28f095301cb2 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:28f095301cb2 | 7 | * |
rgrover1 | 0:28f095301cb2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:28f095301cb2 | 9 | * |
rgrover1 | 0:28f095301cb2 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:28f095301cb2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:28f095301cb2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:28f095301cb2 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:28f095301cb2 | 14 | * limitations under the License. |
rgrover1 | 0:28f095301cb2 | 15 | */ |
rgrover1 | 0:28f095301cb2 | 16 | |
rgrover1 | 0:28f095301cb2 | 17 | #ifndef __BLE_BUTTON_SERVICE_H__ |
rgrover1 | 0:28f095301cb2 | 18 | #define __BLE_BUTTON_SERVICE_H__ |
rgrover1 | 0:28f095301cb2 | 19 | |
blinky | 5:bf55ff59a71f | 20 | class ButtonServiceA |
blinky | 5:bf55ff59a71f | 21 | { |
rgrover1 | 0:28f095301cb2 | 22 | public: |
rgrover1 | 0:28f095301cb2 | 23 | const static uint16_t BUTTON_SERVICE_UUID = 0xA000; |
rgrover1 | 1:7202df456146 | 24 | const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001; |
rgrover1 | 0:28f095301cb2 | 25 | |
blinky | 5:bf55ff59a71f | 26 | ButtonServiceA(BLEDevice &_ble, bool buttonPressedInitial) : |
blinky | 5:bf55ff59a71f | 27 | ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, &buttonPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 0:28f095301cb2 | 28 | GattCharacteristic *charTable[] = {&buttonState}; |
blinky | 5:bf55ff59a71f | 29 | GattService buttonService(ButtonServiceA::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 0:28f095301cb2 | 30 | ble.addService(buttonService); |
rgrover1 | 0:28f095301cb2 | 31 | } |
rgrover1 | 0:28f095301cb2 | 32 | |
blinky | 5:bf55ff59a71f | 33 | void updateButtonState(int newState) { |
blinky | 5:bf55ff59a71f | 34 | ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool)); |
blinky | 5:bf55ff59a71f | 35 | } |
blinky | 5:bf55ff59a71f | 36 | |
blinky | 5:bf55ff59a71f | 37 | private: |
blinky | 5:bf55ff59a71f | 38 | BLEDevice &ble; |
blinky | 5:bf55ff59a71f | 39 | ReadOnlyGattCharacteristic<bool> buttonState; |
blinky | 5:bf55ff59a71f | 40 | }; |
blinky | 5:bf55ff59a71f | 41 | |
blinky | 5:bf55ff59a71f | 42 | class ButtonServiceB |
blinky | 5:bf55ff59a71f | 43 | { |
blinky | 5:bf55ff59a71f | 44 | public: |
blinky | 5:bf55ff59a71f | 45 | const static uint16_t BUTTON_SERVICE_UUID = 0xB000; |
blinky | 5:bf55ff59a71f | 46 | const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xB001; |
blinky | 6:c18ec2f18b33 | 47 | const static uint16_t READWRITE_CHARACTERISTIC_UUID = 0xB002; |
blinky | 5:bf55ff59a71f | 48 | |
blinky | 5:bf55ff59a71f | 49 | ButtonServiceB(BLEDevice &_ble, bool buttonPressedInitial) : |
blinky | 6:c18ec2f18b33 | 50 | ble(_ble), |
blinky | 6:c18ec2f18b33 | 51 | buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, &buttonPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
blinky | 6:c18ec2f18b33 | 52 | readWrite(READWRITE_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { |
blinky | 6:c18ec2f18b33 | 53 | |
blinky | 6:c18ec2f18b33 | 54 | GattCharacteristic *charTable[] = {&buttonState, &readWrite}; |
blinky | 5:bf55ff59a71f | 55 | GattService buttonService(ButtonServiceB::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
blinky | 5:bf55ff59a71f | 56 | ble.addService(buttonService); |
blinky | 5:bf55ff59a71f | 57 | } |
blinky | 5:bf55ff59a71f | 58 | |
blinky | 5:bf55ff59a71f | 59 | void updateButtonState(int newState) { |
rgrover1 | 0:28f095301cb2 | 60 | ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool)); |
rgrover1 | 0:28f095301cb2 | 61 | } |
rgrover1 | 0:28f095301cb2 | 62 | |
rgrover1 | 0:28f095301cb2 | 63 | private: |
rgrover1 | 0:28f095301cb2 | 64 | BLEDevice &ble; |
rgrover1 | 0:28f095301cb2 | 65 | ReadOnlyGattCharacteristic<bool> buttonState; |
blinky | 6:c18ec2f18b33 | 66 | ReadWriteGattCharacteristic<uint8_t> readWrite; |
rgrover1 | 0:28f095301cb2 | 67 | }; |
rgrover1 | 0:28f095301cb2 | 68 | |
rgrover1 | 0:28f095301cb2 | 69 | #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */ |