Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BluetoothAsiaPeripheral by
peripheral_src.txt@5:e778ea479b18, 2018-03-14 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Wed Mar 14 09:20:50 2018 +0000
- Revision:
- 5:e778ea479b18
- Child:
- 6:f296b217bb4c
1. NEW: add peripheral_src.txt which includes peripheral session code snippet and guide.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krenbluetoothsig | 5:e778ea479b18 | 1 | MicroBitAnimationService.h |
krenbluetoothsig | 5:e778ea479b18 | 2 | |
krenbluetoothsig | 5:e778ea479b18 | 3 | // TODO: memory for our Animation characteristics. |
krenbluetoothsig | 5:e778ea479b18 | 4 | uint8_t animation_type_buffer[1]; |
krenbluetoothsig | 5:e778ea479b18 | 5 | uint8_t animation_status_buffer[1]; |
krenbluetoothsig | 5:e778ea479b18 | 6 | uint8_t animation_control_buffer[1]; |
krenbluetoothsig | 5:e778ea479b18 | 7 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 8 | |
krenbluetoothsig | 5:e778ea479b18 | 9 | // TODO: handles on this service's characterisitics. |
krenbluetoothsig | 5:e778ea479b18 | 10 | GattAttribute::Handle_t animationTypeCharacteristicHandle; |
krenbluetoothsig | 5:e778ea479b18 | 11 | GattAttribute::Handle_t animationStatusCharacteristicHandle; |
krenbluetoothsig | 5:e778ea479b18 | 12 | GattAttribute::Handle_t animationControlCharacteristicHandle; |
krenbluetoothsig | 5:e778ea479b18 | 13 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 14 | |
krenbluetoothsig | 5:e778ea479b18 | 15 | |
krenbluetoothsig | 5:e778ea479b18 | 16 | MicroBitAnimationService.cpp |
krenbluetoothsig | 5:e778ea479b18 | 17 | // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties. |
krenbluetoothsig | 5:e778ea479b18 | 18 | GattCharacteristic animationTypeCharacteristic( |
krenbluetoothsig | 5:e778ea479b18 | 19 | MicroBitAnimationServiceAnimationTypeCharacteristicUUID, |
krenbluetoothsig | 5:e778ea479b18 | 20 | (uint8_t *)animation_type_buffer, |
krenbluetoothsig | 5:e778ea479b18 | 21 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 22 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 23 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
krenbluetoothsig | 5:e778ea479b18 | 24 | |
krenbluetoothsig | 5:e778ea479b18 | 25 | GattCharacteristic animationStatusCharacteristic( |
krenbluetoothsig | 5:e778ea479b18 | 26 | MicroBitAnimationServiceAnimationStatusCharacteristicUUID, |
krenbluetoothsig | 5:e778ea479b18 | 27 | (uint8_t *)animation_status_buffer, |
krenbluetoothsig | 5:e778ea479b18 | 28 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 29 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 30 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
krenbluetoothsig | 5:e778ea479b18 | 31 | |
krenbluetoothsig | 5:e778ea479b18 | 32 | GattCharacteristic animationControlCharacteristic( |
krenbluetoothsig | 5:e778ea479b18 | 33 | MicroBitAnimationServiceAnimationControlCharacteristicUUID, |
krenbluetoothsig | 5:e778ea479b18 | 34 | (uint8_t *)animation_control_buffer, |
krenbluetoothsig | 5:e778ea479b18 | 35 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 36 | 1, |
krenbluetoothsig | 5:e778ea479b18 | 37 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
krenbluetoothsig | 5:e778ea479b18 | 38 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 39 | |
krenbluetoothsig | 5:e778ea479b18 | 40 | // TODO: initialise characteristic value buffers. |
krenbluetoothsig | 5:e778ea479b18 | 41 | animation_type_buffer[0] = 0x00; |
krenbluetoothsig | 5:e778ea479b18 | 42 | animation_status_buffer[0] = 0x00; |
krenbluetoothsig | 5:e778ea479b18 | 43 | animation_control_buffer[0] = 0x00; |
krenbluetoothsig | 5:e778ea479b18 | 44 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 45 | |
krenbluetoothsig | 5:e778ea479b18 | 46 | // TODO: create GattService containing the three characteristics |
krenbluetoothsig | 5:e778ea479b18 | 47 | // create an array of our characteristics so we can pass them to the service when we create it |
krenbluetoothsig | 5:e778ea479b18 | 48 | GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic}; |
krenbluetoothsig | 5:e778ea479b18 | 49 | |
krenbluetoothsig | 5:e778ea479b18 | 50 | // create the animation service and specify its characteristics. 3rd arg is the number of characteristics. |
krenbluetoothsig | 5:e778ea479b18 | 51 | GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
krenbluetoothsig | 5:e778ea479b18 | 52 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 53 | |
krenbluetoothsig | 5:e778ea479b18 | 54 | // TODO: add the service to the Bluetooth attribute table |
krenbluetoothsig | 5:e778ea479b18 | 55 | ble.addService(service); |
krenbluetoothsig | 5:e778ea479b18 | 56 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 57 | |
krenbluetoothsig | 5:e778ea479b18 | 58 | // TODO: take a note of the handles of our new characteristics now that they're in the attribute table |
krenbluetoothsig | 5:e778ea479b18 | 59 | animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle(); |
krenbluetoothsig | 5:e778ea479b18 | 60 | animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle(); |
krenbluetoothsig | 5:e778ea479b18 | 61 | animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle(); |
krenbluetoothsig | 5:e778ea479b18 | 62 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 63 | |
krenbluetoothsig | 5:e778ea479b18 | 64 | // TODO: register a callback function for when a characteristic is written to |
krenbluetoothsig | 5:e778ea479b18 | 65 | ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten); |
krenbluetoothsig | 5:e778ea479b18 | 66 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 67 | |
krenbluetoothsig | 5:e778ea479b18 | 68 | /** |
krenbluetoothsig | 5:e778ea479b18 | 69 | * Callback. Invoked when any of our attributes are written to |
krenbluetoothsig | 5:e778ea479b18 | 70 | */ |
krenbluetoothsig | 5:e778ea479b18 | 71 | void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) |
krenbluetoothsig | 5:e778ea479b18 | 72 | { |
krenbluetoothsig | 5:e778ea479b18 | 73 | |
krenbluetoothsig | 5:e778ea479b18 | 74 | // TODO: handle write to animation type characteristic |
krenbluetoothsig | 5:e778ea479b18 | 75 | if (params->handle == animationTypeCharacteristicHandle && params->len == 1) { |
krenbluetoothsig | 5:e778ea479b18 | 76 | uint8_t *value = (uint8_t *)params->data; |
krenbluetoothsig | 5:e778ea479b18 | 77 | uint16_t event_value = static_cast<uint16_t>(value[0]); |
krenbluetoothsig | 5:e778ea479b18 | 78 | MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value); |
krenbluetoothsig | 5:e778ea479b18 | 79 | return; |
krenbluetoothsig | 5:e778ea479b18 | 80 | } |
krenbluetoothsig | 5:e778ea479b18 | 81 | |
krenbluetoothsig | 5:e778ea479b18 | 82 | // TODO: handle write to animation control characteristic |
krenbluetoothsig | 5:e778ea479b18 | 83 | if (params->handle == animationControlCharacteristicHandle && params->len == 1) { |
krenbluetoothsig | 5:e778ea479b18 | 84 | uint8_t *value = (uint8_t *)params->data; |
krenbluetoothsig | 5:e778ea479b18 | 85 | uint16_t event_value = static_cast<uint16_t>(value[0]); |
krenbluetoothsig | 5:e778ea479b18 | 86 | MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value); |
krenbluetoothsig | 5:e778ea479b18 | 87 | return; |
krenbluetoothsig | 5:e778ea479b18 | 88 | } |
krenbluetoothsig | 5:e778ea479b18 | 89 | } |
krenbluetoothsig | 5:e778ea479b18 | 90 | ctrl+S then ctrl+B |
krenbluetoothsig | 5:e778ea479b18 | 91 | |
krenbluetoothsig | 5:e778ea479b18 | 92 | void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) |
krenbluetoothsig | 5:e778ea479b18 | 93 | { |
krenbluetoothsig | 5:e778ea479b18 | 94 | // TODO: notify connected client of change to the animation status characteristic |
krenbluetoothsig | 5:e778ea479b18 | 95 | if (ble.getGapState().connected) |
krenbluetoothsig | 5:e778ea479b18 | 96 | { |
krenbluetoothsig | 5:e778ea479b18 | 97 | animation_status_buffer[0] = e.value; |
krenbluetoothsig | 5:e778ea479b18 | 98 | ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1); |
krenbluetoothsig | 5:e778ea479b18 | 99 | } |
krenbluetoothsig | 5:e778ea479b18 | 100 | } |
krenbluetoothsig | 5:e778ea479b18 | 101 | ctrl+S then ctrl+B |