This repository is for Bluetooth Asia 2018 developer training, peripheral session.

Dependencies:   microbit

Fork of BluetoothAsiaPeripheral by Kaiser Ren

Revision:
5:e778ea479b18
Child:
6:f296b217bb4c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/peripheral_src.txt	Wed Mar 14 09:20:50 2018 +0000
@@ -0,0 +1,101 @@
+MicroBitAnimationService.h
+
+    // TODO: memory for our Animation characteristics.
+    uint8_t   animation_type_buffer[1];
+    uint8_t   animation_status_buffer[1];
+    uint8_t   animation_control_buffer[1];
+ctrl+S then ctrl+B
+
+    // TODO: handles on this service's characterisitics.
+    GattAttribute::Handle_t animationTypeCharacteristicHandle;
+    GattAttribute::Handle_t animationStatusCharacteristicHandle;
+    GattAttribute::Handle_t animationControlCharacteristicHandle;
+ctrl+S then ctrl+B
+
+
+MicroBitAnimationService.cpp
+    // 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);
+ctrl+S then ctrl+B
+
+    // TODO: initialise characteristic value buffers.
+    animation_type_buffer[0]    = 0x00;
+    animation_status_buffer[0]  = 0x00;
+    animation_control_buffer[0] = 0x00;
+ctrl+S then ctrl+B
+
+    // 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};
+
+    // create the animation service and specify its characteristics. 3rd arg is the number of characteristics.
+    GattService         service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+ctrl+S then ctrl+B
+
+    // TODO: add the service to the Bluetooth attribute table
+    ble.addService(service);
+ctrl+S then ctrl+B
+
+    // 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();
+ctrl+S then ctrl+B
+
+    // TODO: register a callback function for when a characteristic is written to
+    ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten);
+ctrl+S then ctrl+B
+
+/**
+  * Callback. Invoked when any of our attributes are written to
+  */
+void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params)
+{
+   
+    // 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;
+    }
+
+    // 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;
+    }
+}
+ctrl+S then ctrl+B
+
+void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e)
+{
+    // 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);
+    }
+}
+ctrl+S then ctrl+B
\ No newline at end of file