BLE Nano LED control
Dependencies: BLE_API mbed nRF51822
ble_nanoled_service.cpp@2:f4709d271f13, 2016-12-24 (annotated)
- Committer:
- hiro99ma
- Date:
- Sat Dec 24 15:18:47 2016 +0000
- Revision:
- 2:f4709d271f13
- Parent:
- 0:355d86799c43
delete File Header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hiro99ma | 0:355d86799c43 | 1 | #include "ble_nanoled_service.h" |
hiro99ma | 0:355d86799c43 | 2 | #include "Led.h" |
hiro99ma | 0:355d86799c43 | 3 | |
hiro99ma | 0:355d86799c43 | 4 | namespace { |
hiro99ma | 0:355d86799c43 | 5 | // The Nordic UART Service |
hiro99ma | 0:355d86799c43 | 6 | const uint8_t UUID_BASE[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
hiro99ma | 0:355d86799c43 | 7 | const uint8_t UUID_TX[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
hiro99ma | 0:355d86799c43 | 8 | const uint8_t UUID_RX[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
hiro99ma | 0:355d86799c43 | 9 | } |
hiro99ma | 0:355d86799c43 | 10 | |
hiro99ma | 0:355d86799c43 | 11 | |
hiro99ma | 0:355d86799c43 | 12 | void ServiceUart::addService(BLE& ble) |
hiro99ma | 0:355d86799c43 | 13 | { |
hiro99ma | 0:355d86799c43 | 14 | mChars[0] = new GattCharacteristic( |
hiro99ma | 0:355d86799c43 | 15 | UUID_TX, |
hiro99ma | 0:355d86799c43 | 16 | mTxPayload, |
hiro99ma | 0:355d86799c43 | 17 | 1, |
hiro99ma | 0:355d86799c43 | 18 | TXRX_BUF_LEN, |
hiro99ma | 0:355d86799c43 | 19 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
hiro99ma | 0:355d86799c43 | 20 | ); |
hiro99ma | 0:355d86799c43 | 21 | mChars[1] = new GattCharacteristic( |
hiro99ma | 0:355d86799c43 | 22 | UUID_RX, |
hiro99ma | 0:355d86799c43 | 23 | mRxPayload, |
hiro99ma | 0:355d86799c43 | 24 | 1, |
hiro99ma | 0:355d86799c43 | 25 | TXRX_BUF_LEN, |
hiro99ma | 0:355d86799c43 | 26 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
hiro99ma | 0:355d86799c43 | 27 | ); |
hiro99ma | 0:355d86799c43 | 28 | GattService* pService = new GattService(UUID_BASE, mChars, CHAR_NUM); |
hiro99ma | 0:355d86799c43 | 29 | ble.addService(*pService); |
hiro99ma | 0:355d86799c43 | 30 | } |
hiro99ma | 0:355d86799c43 | 31 | |
hiro99ma | 0:355d86799c43 | 32 | |
hiro99ma | 0:355d86799c43 | 33 | void ServiceUart::writtenHandler(BLE& ble, const GattWriteCallbackParams* params) |
hiro99ma | 0:355d86799c43 | 34 | { |
hiro99ma | 0:355d86799c43 | 35 | if (params->handle == mChars[0]->getValueAttribute().getHandle()) { |
hiro99ma | 0:355d86799c43 | 36 | uint8_t buf[TXRX_BUF_LEN]; |
hiro99ma | 0:355d86799c43 | 37 | uint16_t bytesRead; |
hiro99ma | 0:355d86799c43 | 38 | |
hiro99ma | 0:355d86799c43 | 39 | ble.readCharacteristicValue(mChars[0]->getValueAttribute().getHandle(), buf, &bytesRead); |
hiro99ma | 0:355d86799c43 | 40 | if(buf[0] == 0x01) { |
hiro99ma | 0:355d86799c43 | 41 | LedOn(); |
hiro99ma | 0:355d86799c43 | 42 | } else { |
hiro99ma | 0:355d86799c43 | 43 | LedOff(); |
hiro99ma | 0:355d86799c43 | 44 | } |
hiro99ma | 0:355d86799c43 | 45 | } |
hiro99ma | 0:355d86799c43 | 46 | } |
hiro99ma | 0:355d86799c43 | 47 | |
hiro99ma | 0:355d86799c43 | 48 | void ServiceUart::readHandler(BLE& ble, const GattReadCallbackParams* params) |
hiro99ma | 0:355d86799c43 | 49 | { |
hiro99ma | 0:355d86799c43 | 50 | if (params->handle == mChars[1]->getValueAttribute().getHandle()) { |
hiro99ma | 0:355d86799c43 | 51 | //... |
hiro99ma | 0:355d86799c43 | 52 | } |
hiro99ma | 0:355d86799c43 | 53 | } |
hiro99ma | 0:355d86799c43 | 54 |