YUFEI JIANG / Mbed 2 deprecated CSSE4011_BLE_IMU

Dependencies:   BLE_API_Tiny_BLE MPU6050-DMP-Seeed-Tiny-BLE mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPUService.h Source File

MPUService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_MPU_SERVICE_H__
00018 #define __BLE_MPU_SERVICE_H__
00019 #include "mbed.h"
00020 #include "Stream.h"
00021 #include "UUID.h"
00022 #include "BLEDevice.h"
00023 
00024 extern const uint16_t MPUServiceShortUUID;
00025 extern const uint16_t MPUServiceYPRCharacteristicShortUUID;
00026 
00027 extern const uint16_t MPUServiceAccelCharacteristicShortUUID;
00028 extern const uint16_t MPUServiceQuatCharacteristicShortUUID;
00029 extern const uint16_t MPUServiceTimeCharacteristicShortUUID;
00030 extern const uint16_t MPUServiceSettingCharacteristicShortUUID;
00031 
00032 extern const uint8_t  MPUServiceBaseUUID[LENGTH_OF_LONG_UUID];
00033 extern const uint8_t  MPUServiceUUID[LENGTH_OF_LONG_UUID];
00034 extern const uint8_t  MPUServiceUUID_reversed[LENGTH_OF_LONG_UUID];
00035 
00036 extern const uint8_t  MPUServiceYPRCharacteristicUUID[LENGTH_OF_LONG_UUID];
00037 
00038 extern const uint8_t  MPUServiceAccelCharacteristicUUID[LENGTH_OF_LONG_UUID];
00039 extern const uint8_t  MPUServiceQuatCharacteristicUUID[LENGTH_OF_LONG_UUID];
00040 extern const uint8_t  MPUServiceTimeCharacteristicUUID[LENGTH_OF_LONG_UUID];
00041 extern const uint8_t  MPUServiceSettingCharacteristicUUID[LENGTH_OF_LONG_UUID];
00042 /**
00043 * @class MPUService
00044 * @brief BLE Service to enable MPU6050 over BLE
00045 */
00046 class MPUService
00047 {
00048 
00049 public:
00050     /**< Maximum length of data (in bytes) that can be transmitted by the UART service module to the peer. */
00051     static const unsigned GATT_MTU_SIZE_DEFAULT          = 23;
00052     static const unsigned GATT_MTU_SIZE_POSITION         = 10;
00053     static const unsigned BLE_MPU_SERVICE_MAX_DATA_LEN = (GATT_MTU_SIZE_DEFAULT - 3);
00054     static const unsigned BLE_MPU_SERVICE_MAX_POSITION_DATA_LEN = (GATT_MTU_SIZE_POSITION - 3);
00055 
00056 public:
00057 
00058     /**
00059     * @param[ref] ble
00060     *                 BLEDevice object for the underlying controller.
00061     */
00062     MPUService (BLEDevice &_ble, float *initialYPRData) :
00063         ble(_ble),
00064         receiveBuffer(),
00065         yprValueBytes(initialYPRData),
00066         numBytesReceived(),
00067         receiveBufferIndex(),
00068         
00069         YPRCharacteristic(MPUServiceYPRCharacteristicShortUUID, (YawPitchRollValueBytes *)yprValueBytes.getPointer(),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00070         settingCharacteristic(MPUServiceSettingCharacteristicShortUUID, receiveBuffer, 1, BLE_MPU_SERVICE_MAX_DATA_LEN,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
00071         
00072         static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00073         if (serviceAdded) {
00074             return;
00075         }
00076         
00077         GattCharacteristic *charTable[] = {&settingCharacteristic, &YPRCharacteristic};
00078         GattService         mpuService(MPUServiceShortUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00079         ble.addService(mpuService);
00080         serviceAdded = true;
00081         ble.onDataWritten(this, &MPUService::onDataWritten);
00082     }
00083 
00084 
00085    
00086 
00087 
00088     /**
00089      * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service.
00090      */
00091     uint16_t getSettingCharacteristic() {
00092         return settingCharacteristic.getValueAttribute().getHandle();
00093     }
00094 
00095 
00096 
00097 
00098     /**
00099     * @brief Update the temperature being broadcast
00100     *
00101     * @param[in] temperature
00102     *                   Floating point value of the temperature
00103     *
00104     */
00105     void updateYawPitchRoll(float *yprData) {
00106         if (ble.getGapState().connected) {
00107             yprValueBytes.updateYawPitchRoll(yprData);
00108             ble.updateCharacteristicValue(YPRCharacteristic.getValueAttribute().getHandle(), yprValueBytes.getPointer(), sizeof(YawPitchRollValueBytes));
00109         }
00110     }
00111 
00112 
00113 
00114 
00115 
00116 private:
00117     /**
00118      * This callback allows the UART service to receive updates to the
00119      * txCharacteristic. The application should forward the call to this
00120      * function from the global onDataWritten() callback handler; or if that's
00121      * not used, this method can be used as a callback directly.
00122      */
00123     void onDataWritten(const GattCharacteristicWriteCBParams *params) {
00124         if (params->charHandle == getSettingCharacteristic()) {
00125             uint16_t bytesRead = params->len;
00126             if (bytesRead <= BLE_MPU_SERVICE_MAX_DATA_LEN) {
00127                 numBytesReceived   = bytesRead;
00128                 receiveBufferIndex = 0;
00129                 memcpy(receiveBuffer, params->data, numBytesReceived);
00130             }
00131         }
00132     }
00133 
00134 
00135 
00136 
00137 private:
00138     /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
00139     struct YawPitchRollValueBytes {
00140         static const unsigned OFFSET_OF_FLAGS    = 0;
00141         //static const unsigned OFFSET_OF_VALUE    = OFFSET_OF_FLAGS + sizeof(uint8_t);
00142         static const unsigned SIZEOF_VALUE_BYTES = 3*sizeof(uint16_t);
00143 
00144 
00145         YawPitchRollValueBytes(float *yprData) : bytes() {
00146             /* assumption: temperature values are expressed in Celsius */
00147             updateYawPitchRoll(yprData);
00148         }
00149 
00150 
00151         void updateYawPitchRoll(float *yprData) {
00152             
00153             /*
00154             int32_t yaw_ieee11073 = quick_ieee11073_from_float(yprData[0]);
00155             int32_t pitch_ieee11073 = quick_ieee11073_from_float(yprData[1]);
00156             int32_t roll_ieee11073 = quick_ieee11073_from_float(yprData[2]);
00157             */
00158             uint16_t yaw_ieee11073   = (uint16_t)(100*(yprData[0]+180));
00159             uint16_t pitch_ieee11073 = (uint16_t)(100*(yprData[1]+180));
00160             uint16_t roll_ieee11073  = (uint16_t)(100*(yprData[2]+180));
00161             memcpy(&bytes[0], &yaw_ieee11073, sizeof(uint16_t));
00162             memcpy(&bytes[OFFSET_OF_FLAGS+sizeof(uint16_t)], &pitch_ieee11073, sizeof(uint16_t));
00163             memcpy(&bytes[OFFSET_OF_FLAGS+2*sizeof(uint16_t)], &roll_ieee11073, sizeof(uint16_t));
00164         }
00165 
00166         uint8_t       *getPointer(void) {
00167             return bytes;
00168         }
00169 
00170         const uint8_t *getPointer(void) const {
00171             return bytes;
00172         }
00173 
00174     private:
00175         /**
00176          * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
00177          * @param temperature The temperature as a float.
00178          * @return The temperature in 11073-20601 FLOAT-Type format.
00179          */
00180         int32_t quick_ieee11073_from_float(float yprData) {
00181             int8_t  exponent = 0xFE; //exponent is -2
00182             int32_t mantissa = (int32_t)(yprData * 100);
00183             return (((int32_t)exponent) << 24) | mantissa;
00184         }
00185 
00186     private:
00187         /* First byte = 8-bit flags, Second field is a float holding the temperature value. */
00188         /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
00189         uint8_t bytes[SIZEOF_VALUE_BYTES];
00190     };
00191 
00192 
00193 
00194 
00195 
00196 private:
00197     BLEDevice          &ble;
00198 
00199     uint8_t             receiveBuffer[BLE_MPU_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which we receive
00200                                                                        *   inbound data before forwarding it to the
00201                                                                        *   application. */
00202     uint8_t             YPRBuffer[BLE_MPU_SERVICE_MAX_DATA_LEN];
00203 
00204 
00205     uint8_t             numBytesReceived ;
00206     uint8_t             receiveBufferIndex;
00207 
00208     YawPitchRollValueBytes  yprValueBytes;
00209 
00210 
00211     ReadOnlyGattCharacteristic<YawPitchRollValueBytes>  YPRCharacteristic;
00212 
00213     GattCharacteristic  settingCharacteristic;
00214 
00215 };
00216 
00217 #endif /* #ifndef __BLE_MPU_SERVICE_H__*/