Kai Ren / microbit-dal

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitAnimationService.cpp Source File

MicroBitAnimationService.cpp

00001 /*
00002 The MIT License (MIT)
00003  
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006  
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013  
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016  
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025  
00026 /**
00027   * Class definition for a MicroBit BLE Animation Service.
00028   * Provides a BLE gateway onto an Animation Model.
00029   */
00030  
00031 #include "MicroBitConfig.h"
00032 #include "MicroBitAnimationService.h"
00033 #include "ble/UUID.h"
00034 #include "Animator.h"
00035  
00036 /**
00037   * Constructor.
00038   * Create a representation of the AnimationService
00039   * @param _ble The instance of a BLE device that we're running on.
00040   */
00041 MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) :
00042         ble(_ble)
00043 {
00044     // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties.
00045  
00046     // TODO: initialise characteristic value buffers
00047  
00048     // Set default security requirements - optional
00049     // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
00050     // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
00051     // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
00052  
00053     // TODO: create GattService containing the three characteristics
00054     // create an array of our characteristics so we can pass them to the service when we create it
00055  
00056     // TODO: create the animation service and specify its characteristics
00057  
00058     // TODO: add the service to the Bluetooth attribute table
00059  
00060     // TODO: take a note of the handles of our new characteristics now that they're in the attribute table
00061  
00062     // TODO: register a callback function for when a characteristic is written to
00063     
00064     // subscribe to animation status events so they can be notified to the connected client
00065     if (EventModel::defaultEventBus)
00066         EventModel::defaultEventBus->listen(ANIMATION_STATUS_EVENT, MICROBIT_EVT_ANY, this, &MicroBitAnimationService::animationStatusUpdate,  MESSAGE_BUS_LISTENER_IMMEDIATE);
00067 }
00068  
00069  
00070 /**
00071   * Callback. Invoked when any of our attributes are written via BLE.
00072   */
00073 void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params)
00074 {
00075     // TODO: handle write to animation type characteristic
00076     
00077     // TODO: handle write to animation control characteristic       
00078 }
00079  
00080 void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e)
00081 {
00082     // TODO: notify connected client of change to the animation status characteristic
00083 }
00084  
00085 const uint8_t  MicroBitAnimationServiceUUID[] = {
00086     0xe9,0x5d,0x71,0x70,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00087 };
00088  
00089 const uint8_t  MicroBitAnimationServiceAnimationTypeCharacteristicUUID[] = {
00090     0xe9,0x5d,0xC3,0x06,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00091 };
00092  
00093 const uint8_t  MicroBitAnimationServiceAnimationStatusCharacteristicUUID[] = {
00094     0xe9,0x5d,0x45,0x92,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00095 };
00096  
00097 const uint8_t  MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = {
00098     0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00099 };
00100