Cycle speed and cadence example for the BLE API using nRF51822 native mode drivers

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
tenfoot
Date:
Sun Aug 23 14:05:22 2015 +0000
Revision:
74:98dee483c173
Parent:
73:bae88c99c2ae
Fix feature report

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tenfoot 71:7b6a488af957 1 /*
tenfoot 71:7b6a488af957 2 * Copyright (c) 2015 Robert Walker
tenfoot 71:7b6a488af957 3 *
tenfoot 71:7b6a488af957 4 * Licensed under the Apache License, Version 2.0 (the "License");
tenfoot 71:7b6a488af957 5 * you may not use this file except in compliance with the License.
tenfoot 71:7b6a488af957 6 * You may obtain a copy of the License at
tenfoot 71:7b6a488af957 7 *
tenfoot 71:7b6a488af957 8 * http://www.apache.org/licenses/LICENSE-2.0
tenfoot 71:7b6a488af957 9 *
tenfoot 71:7b6a488af957 10 * Unless required by applicable law or agreed to in writing, software
tenfoot 71:7b6a488af957 11 * distributed under the License is distributed on an "AS IS" BASIS,
tenfoot 71:7b6a488af957 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tenfoot 71:7b6a488af957 13 * See the License for the specific language governing permissions and
tenfoot 71:7b6a488af957 14 * limitations under the License.
tenfoot 71:7b6a488af957 15 */
tenfoot 71:7b6a488af957 16
tenfoot 71:7b6a488af957 17 #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__
tenfoot 71:7b6a488af957 18 #define __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__
tenfoot 71:7b6a488af957 19
tenfoot 71:7b6a488af957 20 #include "ble/BLE.h"
tenfoot 71:7b6a488af957 21
tenfoot 71:7b6a488af957 22 /**
tenfoot 71:7b6a488af957 23 * @class CyclingSpeedAndCadenceService
tenfoot 71:7b6a488af957 24 * @brief BLE Service for Cycling Speed and Cadence. This BLE Service contains the location of the sensor, the total wheel revolutions, total crank revolutiosn. <br>
tenfoot 71:7b6a488af957 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.cycling_speed_and_cadence.xml <br>
tenfoot 71:7b6a488af957 26 * CSC Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.csc_measurement.xml <br>
tenfoot 71:7b6a488af957 27 * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.sensor_location.xml
tenfoot 71:7b6a488af957 28 */
tenfoot 71:7b6a488af957 29 class CyclingSpeedAndCadenceService {
tenfoot 71:7b6a488af957 30 public:
tenfoot 74:98dee483c173 31 static const uint8_t FLAG_WHEEL_PRESENT = (1 << 0);
tenfoot 74:98dee483c173 32 static const uint8_t FLAG_CRANK_PRESENT = (1 << 1);
tenfoot 74:98dee483c173 33
tenfoot 73:bae88c99c2ae 34 enum Mode {
tenfoot 74:98dee483c173 35 MODE_SPEED = FLAG_WHEEL_PRESENT,
tenfoot 74:98dee483c173 36 MODE_CADENCE = FLAG_CRANK_PRESENT,
tenfoot 74:98dee483c173 37 MODE_SPEED_CADENCE = FLAG_WHEEL_PRESENT | FLAG_CRANK_PRESENT
tenfoot 73:bae88c99c2ae 38 };
tenfoot 73:bae88c99c2ae 39
tenfoot 71:7b6a488af957 40 /**
tenfoot 71:7b6a488af957 41 * @enum SensorLocation
tenfoot 71:7b6a488af957 42 * @brief Location of sensor on bike.
tenfoot 71:7b6a488af957 43 */
tenfoot 71:7b6a488af957 44 enum {
tenfoot 71:7b6a488af957 45 LOCATION_OTHER, /*!< Other */
tenfoot 71:7b6a488af957 46 LOCATION_TOP_OF_SHOE, /*!< Top of shoe */
tenfoot 71:7b6a488af957 47 LOCATION_IN_SHOE, /*!< In shoe */
tenfoot 71:7b6a488af957 48 LOCATION_HIP, /*!< Hip */
tenfoot 71:7b6a488af957 49 LOCATION_FRONT_WHEEL, /*!< Front Wheel */
tenfoot 71:7b6a488af957 50 LOCATION_LEFT_CRANK, /*!< Left Crank */
tenfoot 71:7b6a488af957 51 LOCATION_RIGHT_CRANK, /*!< Right Crank */
tenfoot 71:7b6a488af957 52 LOCATION_LEFT_PEDAL, /*!< Left Pedal */
tenfoot 71:7b6a488af957 53 LOCATION_RIGHT_PEDAL, /*!< Right Pedal */
tenfoot 71:7b6a488af957 54 LOCATION_FRONT_HUB, /*!< Front Hub */
tenfoot 71:7b6a488af957 55 LOCATION_REAR_DROPOUT, /*!< Rear Dropout */
tenfoot 71:7b6a488af957 56 LOCATION_CHAINSTAY, /*!< Chainstay */
tenfoot 71:7b6a488af957 57 LOCATION_REAR_WHEEL, /*!< Rear Wheel */
tenfoot 71:7b6a488af957 58 LOCATION_REAR_HUB, /*!< Rear Hub */
tenfoot 71:7b6a488af957 59 LOCATION_CHEST, /*!< Chest */
tenfoot 71:7b6a488af957 60 };
tenfoot 71:7b6a488af957 61
tenfoot 71:7b6a488af957 62 enum {
tenfoot 71:7b6a488af957 63 UUID_SENSOR_LOCATION_CHAR = 0x2A5D,
tenfoot 71:7b6a488af957 64 UUID_SC_CONTROL_POINT_CHAR = 0x2A55
tenfoot 71:7b6a488af957 65 };
tenfoot 71:7b6a488af957 66
tenfoot 71:7b6a488af957 67 public:
tenfoot 71:7b6a488af957 68 /**
tenfoot 71:7b6a488af957 69 * @brief Constructor with initial counter values.
tenfoot 71:7b6a488af957 70 *
tenfoot 71:7b6a488af957 71 * @param[ref] _ble
tenfoot 71:7b6a488af957 72 * Reference to the underlying BLE.
tenfoot 71:7b6a488af957 73 * @param[in] wheelCounter (32-bit)
tenfoot 71:7b6a488af957 74 * initial value for the wheel counter.
tenfoot 71:7b6a488af957 75 * @param[in] crankCounter (32-bit)
tenfoot 71:7b6a488af957 76 * initial value for the crank counter.
tenfoot 71:7b6a488af957 77 * @param[in] location
tenfoot 71:7b6a488af957 78 * Sensor's location.
tenfoot 71:7b6a488af957 79 */
tenfoot 73:bae88c99c2ae 80 CyclingSpeedAndCadenceService(BLE &_ble, Mode _mode, uint8_t location) :
tenfoot 71:7b6a488af957 81 ble(_ble),
tenfoot 73:bae88c99c2ae 82 value(_mode),
tenfoot 73:bae88c99c2ae 83 mode(_mode),
tenfoot 73:bae88c99c2ae 84 csc(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR,
tenfoot 73:bae88c99c2ae 85 value.bytes,
tenfoot 73:bae88c99c2ae 86 value.getSize(), value.getSize(),
tenfoot 71:7b6a488af957 87 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
tenfoot 73:bae88c99c2ae 88 cscFeat(GattCharacteristic::UUID_CSC_FEATURE_CHAR, (uint8_t*)&mode,
tenfoot 72:a15b8451829f 89 2, 2,
tenfoot 72:a15b8451829f 90 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
tenfoot 71:7b6a488af957 91 scLocation(UUID_SENSOR_LOCATION_CHAR, &location),
tenfoot 71:7b6a488af957 92 controlPoint(UUID_SC_CONTROL_POINT_CHAR, &controlPointValue) {
tenfoot 71:7b6a488af957 93 setupService();
tenfoot 71:7b6a488af957 94 }
tenfoot 71:7b6a488af957 95
tenfoot 71:7b6a488af957 96 /**
tenfoot 71:7b6a488af957 97 * @brief Set a new value for wheel revolutions.
tenfoot 71:7b6a488af957 98 *
tenfoot 71:7b6a488af957 99 * @param[in] wheelCounter
tenfoot 71:7b6a488af957 100 * Total wheel revolutions.
tenfoot 71:7b6a488af957 101 * @param[in] eventTime
tenfoot 71:7b6a488af957 102 * Time of event.
tenfoot 71:7b6a488af957 103 */
tenfoot 73:bae88c99c2ae 104 void updateWheelCounter(uint32_t wheelCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 105 {
tenfoot 73:bae88c99c2ae 106 value.updateWheelCounter(wheelCounter, eventTime);
tenfoot 73:bae88c99c2ae 107 sendUpdate();
tenfoot 71:7b6a488af957 108 }
tenfoot 71:7b6a488af957 109
tenfoot 71:7b6a488af957 110 /**
tenfoot 71:7b6a488af957 111 * @brief Set a new value for crank revolutions.
tenfoot 71:7b6a488af957 112 *
tenfoot 71:7b6a488af957 113 * @param[in] crankCounter
tenfoot 71:7b6a488af957 114 * Total crank revolutions.
tenfoot 71:7b6a488af957 115 * @param[in] eventTime
tenfoot 71:7b6a488af957 116 * Time of event.
tenfoot 71:7b6a488af957 117 */
tenfoot 73:bae88c99c2ae 118 void updateCrankCounter(uint16_t crankCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 119 {
tenfoot 73:bae88c99c2ae 120 value.updateCrankCounter(crankCounter, eventTime);
tenfoot 73:bae88c99c2ae 121 sendUpdate();
tenfoot 71:7b6a488af957 122 }
tenfoot 71:7b6a488af957 123
tenfoot 73:bae88c99c2ae 124 void updateCounters(uint32_t wheelCounter, uint16_t crankCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 125 {
tenfoot 73:bae88c99c2ae 126 value.updateCounters(wheelCounter, crankCounter, eventTime);
tenfoot 73:bae88c99c2ae 127 sendUpdate();
tenfoot 71:7b6a488af957 128 }
tenfoot 71:7b6a488af957 129
tenfoot 71:7b6a488af957 130 /**
tenfoot 71:7b6a488af957 131 * This callback allows the CyclingSpeedAndCadenceService to receive updates to the
tenfoot 71:7b6a488af957 132 * controlPoint Characteristic.
tenfoot 71:7b6a488af957 133 *
tenfoot 71:7b6a488af957 134 * @param[in] params
tenfoot 71:7b6a488af957 135 * Information about the characterisitc being updated.
tenfoot 71:7b6a488af957 136 */
tenfoot 71:7b6a488af957 137 virtual void onDataWritten(const GattWriteCallbackParams *params) {
tenfoot 71:7b6a488af957 138 if (params->handle == controlPoint.getValueAttribute().getHandle()) {
tenfoot 71:7b6a488af957 139 /* Do something here if the new value is 1; else you can override this method by
tenfoot 71:7b6a488af957 140 * extending this class.
tenfoot 71:7b6a488af957 141 * @NOTE: if you are extending this class, be sure to also call
tenfoot 71:7b6a488af957 142 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
tenfoot 71:7b6a488af957 143 * your constructor.
tenfoot 71:7b6a488af957 144 */
tenfoot 71:7b6a488af957 145 }
tenfoot 71:7b6a488af957 146 }
tenfoot 71:7b6a488af957 147
tenfoot 71:7b6a488af957 148 protected:
tenfoot 71:7b6a488af957 149 void setupService(void) {
tenfoot 72:a15b8451829f 150 GattCharacteristic *charTable[] = {&csc, &cscFeat, &scLocation, &controlPoint};
tenfoot 71:7b6a488af957 151 GattService cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
tenfoot 71:7b6a488af957 152
tenfoot 71:7b6a488af957 153 ble.addService(cscService);
tenfoot 71:7b6a488af957 154 ble.onDataWritten(this, &CyclingSpeedAndCadenceService::onDataWritten);
tenfoot 71:7b6a488af957 155 }
tenfoot 71:7b6a488af957 156
tenfoot 71:7b6a488af957 157
tenfoot 73:bae88c99c2ae 158 void sendUpdate()
tenfoot 73:bae88c99c2ae 159 {
tenfoot 73:bae88c99c2ae 160 ble.gattServer().write(csc.getValueHandle(), value.bytes, value.getSize());
tenfoot 73:bae88c99c2ae 161 }
tenfoot 71:7b6a488af957 162
tenfoot 73:bae88c99c2ae 163 protected:
tenfoot 73:bae88c99c2ae 164 static const uint16_t MAX_BYTES = (1 + 4 + 2 + 2 + 2);
tenfoot 73:bae88c99c2ae 165 union SpeedCadenceValue
tenfoot 73:bae88c99c2ae 166 {
tenfoot 73:bae88c99c2ae 167 SpeedCadenceValue(uint16_t mode)
tenfoot 71:7b6a488af957 168 {
tenfoot 73:bae88c99c2ae 169 switch (mode)
tenfoot 71:7b6a488af957 170 {
tenfoot 73:bae88c99c2ae 171 case MODE_SPEED:
tenfoot 73:bae88c99c2ae 172 v.flags = FLAG_WHEEL_PRESENT;
tenfoot 73:bae88c99c2ae 173 break;
tenfoot 73:bae88c99c2ae 174 case MODE_CADENCE:
tenfoot 73:bae88c99c2ae 175 v.flags = FLAG_CRANK_PRESENT;
tenfoot 73:bae88c99c2ae 176 break;
tenfoot 73:bae88c99c2ae 177 case MODE_SPEED_CADENCE:
tenfoot 73:bae88c99c2ae 178 v.flags = FLAG_WHEEL_PRESENT | FLAG_CRANK_PRESENT;
tenfoot 73:bae88c99c2ae 179 break;
tenfoot 73:bae88c99c2ae 180 default:
tenfoot 73:bae88c99c2ae 181 v.flags = 0;
tenfoot 73:bae88c99c2ae 182 break;
tenfoot 71:7b6a488af957 183 }
tenfoot 71:7b6a488af957 184 }
tenfoot 71:7b6a488af957 185
tenfoot 73:bae88c99c2ae 186 void updateWheelCounter(uint32_t wheelCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 187 {
tenfoot 73:bae88c99c2ae 188 switch(v.flags)
tenfoot 73:bae88c99c2ae 189 {
tenfoot 73:bae88c99c2ae 190 case FLAG_WHEEL_PRESENT:
tenfoot 73:bae88c99c2ae 191 v.v.speed.wheelCounter = wheelCounter;
tenfoot 73:bae88c99c2ae 192 v.v.speed.lastWheelEvent = eventTime;
tenfoot 73:bae88c99c2ae 193 break;
tenfoot 73:bae88c99c2ae 194 case FLAG_WHEEL_PRESENT | FLAG_CRANK_PRESENT:
tenfoot 73:bae88c99c2ae 195 v.v.speedCadence.wheelCounter = wheelCounter;
tenfoot 73:bae88c99c2ae 196 v.v.speedCadence.lastWheelEvent = eventTime;
tenfoot 73:bae88c99c2ae 197 break;
tenfoot 73:bae88c99c2ae 198 default:
tenfoot 73:bae88c99c2ae 199 break;
tenfoot 73:bae88c99c2ae 200 }
tenfoot 73:bae88c99c2ae 201 }
tenfoot 73:bae88c99c2ae 202
tenfoot 73:bae88c99c2ae 203 void updateCrankCounter(uint16_t crankCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 204 {
tenfoot 73:bae88c99c2ae 205 switch(v.flags)
tenfoot 73:bae88c99c2ae 206 {
tenfoot 73:bae88c99c2ae 207 case FLAG_CRANK_PRESENT:
tenfoot 73:bae88c99c2ae 208 v.v.cadence.crankCounter = crankCounter;
tenfoot 73:bae88c99c2ae 209 v.v.cadence.lastCrankEvent = eventTime;
tenfoot 73:bae88c99c2ae 210 break;
tenfoot 73:bae88c99c2ae 211 case FLAG_WHEEL_PRESENT | FLAG_CRANK_PRESENT:
tenfoot 73:bae88c99c2ae 212 v.v.speedCadence.crankCounter = crankCounter;
tenfoot 73:bae88c99c2ae 213 v.v.speedCadence.lastCrankEvent = eventTime;
tenfoot 73:bae88c99c2ae 214 break;
tenfoot 73:bae88c99c2ae 215 default:
tenfoot 73:bae88c99c2ae 216 break;
tenfoot 73:bae88c99c2ae 217 }
tenfoot 73:bae88c99c2ae 218 }
tenfoot 73:bae88c99c2ae 219
tenfoot 73:bae88c99c2ae 220 void updateCounters(uint32_t wheelCounter, uint16_t crankCounter, uint16_t eventTime)
tenfoot 73:bae88c99c2ae 221 {
tenfoot 73:bae88c99c2ae 222 switch(v.flags)
tenfoot 73:bae88c99c2ae 223 {
tenfoot 73:bae88c99c2ae 224 case FLAG_WHEEL_PRESENT:
tenfoot 73:bae88c99c2ae 225 v.v.speed.wheelCounter = wheelCounter;
tenfoot 73:bae88c99c2ae 226 v.v.speed.lastWheelEvent = eventTime;
tenfoot 73:bae88c99c2ae 227 break;
tenfoot 73:bae88c99c2ae 228 case FLAG_CRANK_PRESENT:
tenfoot 73:bae88c99c2ae 229 v.v.cadence.crankCounter = crankCounter;
tenfoot 73:bae88c99c2ae 230 v.v.cadence.lastCrankEvent = eventTime;
tenfoot 73:bae88c99c2ae 231 break;
tenfoot 73:bae88c99c2ae 232 case FLAG_WHEEL_PRESENT | FLAG_CRANK_PRESENT:
tenfoot 73:bae88c99c2ae 233 v.v.speedCadence.wheelCounter = wheelCounter;
tenfoot 73:bae88c99c2ae 234 v.v.speedCadence.lastWheelEvent = eventTime;
tenfoot 73:bae88c99c2ae 235 v.v.speedCadence.crankCounter = crankCounter;
tenfoot 73:bae88c99c2ae 236 v.v.speedCadence.lastCrankEvent = eventTime;
tenfoot 73:bae88c99c2ae 237 break;
tenfoot 73:bae88c99c2ae 238 default:
tenfoot 73:bae88c99c2ae 239 break;
tenfoot 73:bae88c99c2ae 240 }
tenfoot 73:bae88c99c2ae 241 }
tenfoot 73:bae88c99c2ae 242
tenfoot 73:bae88c99c2ae 243 uint16_t getSize()
tenfoot 73:bae88c99c2ae 244 {
tenfoot 73:bae88c99c2ae 245 return 1 +
tenfoot 73:bae88c99c2ae 246 ((v.flags & FLAG_WHEEL_PRESENT) ? (4+2) : 0) +
tenfoot 73:bae88c99c2ae 247 ((v.flags & FLAG_CRANK_PRESENT) ? (2+2) : 0);
tenfoot 73:bae88c99c2ae 248 }
tenfoot 73:bae88c99c2ae 249
tenfoot 73:bae88c99c2ae 250 __packed struct Value
tenfoot 73:bae88c99c2ae 251 {
tenfoot 73:bae88c99c2ae 252 uint8_t flags;
tenfoot 73:bae88c99c2ae 253 __packed union
tenfoot 73:bae88c99c2ae 254 {
tenfoot 73:bae88c99c2ae 255 __packed struct Speed
tenfoot 73:bae88c99c2ae 256 {
tenfoot 73:bae88c99c2ae 257 uint32_t wheelCounter;
tenfoot 73:bae88c99c2ae 258 uint16_t lastWheelEvent;
tenfoot 73:bae88c99c2ae 259 } speed;
tenfoot 73:bae88c99c2ae 260 __packed struct Cadence
tenfoot 73:bae88c99c2ae 261 {
tenfoot 73:bae88c99c2ae 262 uint16_t crankCounter;
tenfoot 73:bae88c99c2ae 263 uint16_t lastCrankEvent;
tenfoot 73:bae88c99c2ae 264 } cadence;
tenfoot 73:bae88c99c2ae 265 __packed struct SpeedCadence
tenfoot 73:bae88c99c2ae 266 {
tenfoot 73:bae88c99c2ae 267 uint32_t wheelCounter;
tenfoot 73:bae88c99c2ae 268 uint16_t lastWheelEvent;
tenfoot 73:bae88c99c2ae 269 uint16_t crankCounter;
tenfoot 73:bae88c99c2ae 270 uint16_t lastCrankEvent;
tenfoot 73:bae88c99c2ae 271 } speedCadence;
tenfoot 73:bae88c99c2ae 272 } v;
tenfoot 73:bae88c99c2ae 273 } v;
tenfoot 73:bae88c99c2ae 274 uint8_t bytes[1+4+2+2+2];
tenfoot 71:7b6a488af957 275 };
tenfoot 71:7b6a488af957 276
tenfoot 71:7b6a488af957 277 protected:
tenfoot 71:7b6a488af957 278 BLE &ble;
tenfoot 71:7b6a488af957 279
tenfoot 73:bae88c99c2ae 280 SpeedCadenceValue value;
tenfoot 73:bae88c99c2ae 281 uint16_t mode;
tenfoot 71:7b6a488af957 282 uint8_t controlPointValue;
tenfoot 71:7b6a488af957 283
tenfoot 71:7b6a488af957 284 GattCharacteristic csc;
tenfoot 72:a15b8451829f 285 GattCharacteristic cscFeat;
tenfoot 71:7b6a488af957 286 ReadOnlyGattCharacteristic<uint8_t> scLocation;
tenfoot 71:7b6a488af957 287 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
tenfoot 71:7b6a488af957 288 };
tenfoot 71:7b6a488af957 289
tenfoot 71:7b6a488af957 290 #endif /* #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__*/