fsdfds
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
MicroBitAccelerometerService.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 EVENT 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 the custom MicroBit Accelerometer Service. 00028 * Provides a BLE service to remotely read the state of the accelerometer, and configure its behaviour. 00029 */ 00030 #include "MicroBitConfig.h" 00031 #include "ble/UUID.h" 00032 00033 #include "MicroBitAccelerometerService.h" 00034 00035 /** 00036 * Constructor. 00037 * Create a representation of the AccelerometerService 00038 * @param _ble The instance of a BLE device that we're running on. 00039 * @param _accelerometer An instance of MicroBitAccelerometer. 00040 */ 00041 MicroBitAccelerometerService::MicroBitAccelerometerService(BLEDevice &_ble, MicroBitAccelerometer &_accelerometer) : 00042 ble(_ble), accelerometer(_accelerometer) 00043 { 00044 // Create the data structures that represent each of our characteristics in Soft Device. 00045 GattCharacteristic accelerometerDataCharacteristic(MicroBitAccelerometerServiceDataUUID, (uint8_t *)accelerometerDataCharacteristicBuffer, 0, 00046 sizeof(accelerometerDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00047 00048 GattCharacteristic accelerometerPeriodCharacteristic(MicroBitAccelerometerServicePeriodUUID, (uint8_t *)&accelerometerPeriodCharacteristicBuffer, 0, 00049 sizeof(accelerometerPeriodCharacteristicBuffer), 00050 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00051 00052 // Initialise our characteristic values. 00053 accelerometerDataCharacteristicBuffer[0] = 0; 00054 accelerometerDataCharacteristicBuffer[1] = 0; 00055 accelerometerDataCharacteristicBuffer[2] = 0; 00056 accelerometerPeriodCharacteristicBuffer = accelerometer.getPeriod(); 00057 00058 // Set default security requirements 00059 accelerometerDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00060 accelerometerPeriodCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00061 00062 GattCharacteristic *characteristics[] = {&accelerometerDataCharacteristic, &accelerometerPeriodCharacteristic}; 00063 GattService service(MicroBitAccelerometerServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00064 00065 ble.addService(service); 00066 00067 accelerometerDataCharacteristicHandle = accelerometerDataCharacteristic.getValueHandle(); 00068 accelerometerPeriodCharacteristicHandle = accelerometerPeriodCharacteristic.getValueHandle(); 00069 00070 ble.gattServer().write(accelerometerDataCharacteristicHandle,(uint8_t *)accelerometerDataCharacteristicBuffer, sizeof(accelerometerDataCharacteristicBuffer)); 00071 ble.gattServer().write(accelerometerPeriodCharacteristicHandle, (const uint8_t *)&accelerometerPeriodCharacteristicBuffer, sizeof(accelerometerPeriodCharacteristicBuffer)); 00072 00073 ble.onDataWritten(this, &MicroBitAccelerometerService::onDataWritten); 00074 00075 if (EventModel::defaultEventBus) 00076 EventModel::defaultEventBus->listen(MICROBIT_ID_ACCELEROMETER, MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE, this, &MicroBitAccelerometerService::accelerometerUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); 00077 } 00078 00079 /** 00080 * Callback. Invoked when any of our attributes are written via BLE. 00081 */ 00082 void MicroBitAccelerometerService::onDataWritten(const GattWriteCallbackParams *params) 00083 { 00084 if (params->handle == accelerometerPeriodCharacteristicHandle && params->len >= sizeof(accelerometerPeriodCharacteristicBuffer)) 00085 { 00086 accelerometerPeriodCharacteristicBuffer = *((uint16_t *)params->data); 00087 accelerometer.setPeriod(accelerometerPeriodCharacteristicBuffer); 00088 00089 // The accelerometer will choose the nearest period to that requested that it can support 00090 // Read back the ACTUAL period it is using, and report this back. 00091 accelerometerPeriodCharacteristicBuffer = accelerometer.getPeriod(); 00092 ble.gattServer().write(accelerometerPeriodCharacteristicHandle, (const uint8_t *)&accelerometerPeriodCharacteristicBuffer, sizeof(accelerometerPeriodCharacteristicBuffer)); 00093 } 00094 } 00095 00096 /** 00097 * Accelerometer update callback 00098 */ 00099 void MicroBitAccelerometerService::accelerometerUpdate(MicroBitEvent) 00100 { 00101 if (ble.getGapState().connected) 00102 { 00103 accelerometerDataCharacteristicBuffer[0] = accelerometer.getX(); 00104 accelerometerDataCharacteristicBuffer[1] = accelerometer.getY(); 00105 accelerometerDataCharacteristicBuffer[2] = accelerometer.getZ(); 00106 00107 ble.gattServer().notify(accelerometerDataCharacteristicHandle,(uint8_t *)accelerometerDataCharacteristicBuffer, sizeof(accelerometerDataCharacteristicBuffer)); 00108 } 00109 } 00110 00111 const uint8_t MicroBitAccelerometerServiceUUID[] = { 00112 0xe9,0x5d,0x07,0x53,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00113 }; 00114 00115 const uint8_t MicroBitAccelerometerServiceDataUUID[] = { 00116 0xe9,0x5d,0xca,0x4b,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00117 }; 00118 00119 const uint8_t MicroBitAccelerometerServicePeriodUUID[] = { 00120 0xe9,0x5d,0xfb,0x24,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00121 };
Generated on Wed Jul 13 2022 00:58:03 by 1.7.2