This repository will be used for Bluetooth World 2018.
Fork of BluetoothAsia2018Peripheral by
peripheral_src.txt
- Committer:
- krenbluetoothsig
- Date:
- 2018-08-06
- Revision:
- 14:fcab3952e945
- Parent:
- 13:f55fa8ef1591
File content as of revision 14:fcab3952e945:
//DIRECTORY: ./microbit/microbit-dal/inc/bluetooth/MicroBitAnimationService.h
//**@code **********************//
// TODO: memory for our Animation characteristics.
uint8_t animation_type_buffer[1];
uint8_t animation_status_buffer[1];
uint8_t animation_control_buffer[1];
//**@endcode *******************//
//**@code **********************//
// TODO: handles on this service's characterisitics.
GattAttribute::Handle_t animationTypeCharacteristicHandle;
GattAttribute::Handle_t animationStatusCharacteristicHandle;
GattAttribute::Handle_t animationControlCharacteristicHandle;
//**@endcode *******************//
//DIRECTORY: ./microbit/microbit-dal/source/bluetooth/MicroBitAnimationService.cpp
//**@code **********************//
// TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties.
GattCharacteristic animationTypeCharacteristic(
MicroBitAnimationServiceAnimationTypeCharacteristicUUID,
(uint8_t *)animation_type_buffer,
1,
1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
GattCharacteristic animationStatusCharacteristic(
MicroBitAnimationServiceAnimationStatusCharacteristicUUID,
(uint8_t *)animation_status_buffer,
1,
1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic animationControlCharacteristic(
MicroBitAnimationServiceAnimationControlCharacteristicUUID,
(uint8_t *)animation_control_buffer,
1,
1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
//**@endcode *******************//
//**@code **********************//
// TODO: initialise characteristic value buffers.
animation_type_buffer[0] = 0x00;
animation_status_buffer[0] = 0x00;
animation_control_buffer[0] = 0x00;
//**@endcode *******************//
//**@code **********************//
// TODO: create GattService containing the three characteristics
// create an array of our characteristics so we can pass them to the service when we create it
GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic};
//**@endcode *******************//
//**@code **********************//
// TODO: create the animation service and specify its characteristics
GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
//**@endcode *******************//
//**@code **********************//
// TODO: add the service to the Bluetooth attribute table
ble.addService(service);
//**@endcode *******************//
//**@code **********************//
// TODO: take a note of the handles of our new characteristics now that they're in the attribute table
animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle();
animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle();
animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle();
//**@endcode *******************//
//**@code **********************//
// TODO: register a callback function for when a characteristic is written to
ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten);
//**@endcode *******************//
/**
* Callback. Invoked when any of our attributes are written to
*/
void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params)
{
//**@code **********************//
// TODO: handle write to animation type characteristic
if (params->handle == animationTypeCharacteristicHandle && params->len == 1) {
uint8_t *value = (uint8_t *)params->data;
uint16_t event_value = static_cast<uint16_t>(value[0]);
MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value);
return;
}
//**@endcode *******************//
//**@code **********************//
// TODO: handle write to animation control characteristic
if (params->handle == animationControlCharacteristicHandle && params->len == 1) {
uint8_t *value = (uint8_t *)params->data;
uint16_t event_value = static_cast<uint16_t>(value[0]);
MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value);
return;
}
//**@endcode *******************//
}
void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e)
{
//**@code **********************//
// TODO: notify connected client of change to the animation status characteristic
if (ble.getGapState().connected)
{
animation_status_buffer[0] = e.value;
ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1);
}
//**@endcode *******************//
}
