Odometry Pedometer using nRF51822 and ADXL345

Dependencies:   ADXL345 BLE_API mbed nRF51822

Fork of BLE_CycleSpeedCadence by Robert Walker

Committer:
tenfoot
Date:
Sun Aug 16 14:46:43 2015 +0000
Revision:
72:a15b8451829f
Parent:
71:7b6a488af957
Child:
73:bae88c99c2ae
First working revision

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 71:7b6a488af957 31 /**
tenfoot 71:7b6a488af957 32 * @enum SensorLocation
tenfoot 71:7b6a488af957 33 * @brief Location of sensor on bike.
tenfoot 71:7b6a488af957 34 */
tenfoot 71:7b6a488af957 35 enum {
tenfoot 71:7b6a488af957 36 LOCATION_OTHER, /*!< Other */
tenfoot 71:7b6a488af957 37 LOCATION_TOP_OF_SHOE, /*!< Top of shoe */
tenfoot 71:7b6a488af957 38 LOCATION_IN_SHOE, /*!< In shoe */
tenfoot 71:7b6a488af957 39 LOCATION_HIP, /*!< Hip */
tenfoot 71:7b6a488af957 40 LOCATION_FRONT_WHEEL, /*!< Front Wheel */
tenfoot 71:7b6a488af957 41 LOCATION_LEFT_CRANK, /*!< Left Crank */
tenfoot 71:7b6a488af957 42 LOCATION_RIGHT_CRANK, /*!< Right Crank */
tenfoot 71:7b6a488af957 43 LOCATION_LEFT_PEDAL, /*!< Left Pedal */
tenfoot 71:7b6a488af957 44 LOCATION_RIGHT_PEDAL, /*!< Right Pedal */
tenfoot 71:7b6a488af957 45 LOCATION_FRONT_HUB, /*!< Front Hub */
tenfoot 71:7b6a488af957 46 LOCATION_REAR_DROPOUT, /*!< Rear Dropout */
tenfoot 71:7b6a488af957 47 LOCATION_CHAINSTAY, /*!< Chainstay */
tenfoot 71:7b6a488af957 48 LOCATION_REAR_WHEEL, /*!< Rear Wheel */
tenfoot 71:7b6a488af957 49 LOCATION_REAR_HUB, /*!< Rear Hub */
tenfoot 71:7b6a488af957 50 LOCATION_CHEST, /*!< Chest */
tenfoot 71:7b6a488af957 51 };
tenfoot 71:7b6a488af957 52
tenfoot 71:7b6a488af957 53 enum {
tenfoot 71:7b6a488af957 54 UUID_SENSOR_LOCATION_CHAR = 0x2A5D,
tenfoot 71:7b6a488af957 55 UUID_SC_CONTROL_POINT_CHAR = 0x2A55
tenfoot 71:7b6a488af957 56 };
tenfoot 71:7b6a488af957 57
tenfoot 71:7b6a488af957 58 public:
tenfoot 71:7b6a488af957 59 /**
tenfoot 71:7b6a488af957 60 * @brief Constructor with initial counter values.
tenfoot 71:7b6a488af957 61 *
tenfoot 71:7b6a488af957 62 * @param[ref] _ble
tenfoot 71:7b6a488af957 63 * Reference to the underlying BLE.
tenfoot 71:7b6a488af957 64 * @param[in] wheelCounter (32-bit)
tenfoot 71:7b6a488af957 65 * initial value for the wheel counter.
tenfoot 71:7b6a488af957 66 * @param[in] crankCounter (32-bit)
tenfoot 71:7b6a488af957 67 * initial value for the crank counter.
tenfoot 71:7b6a488af957 68 * @param[in] location
tenfoot 71:7b6a488af957 69 * Sensor's location.
tenfoot 71:7b6a488af957 70 */
tenfoot 71:7b6a488af957 71 CyclingSpeedAndCadenceService(BLE &_ble, uint8_t location) :
tenfoot 71:7b6a488af957 72 ble(_ble),
tenfoot 72:a15b8451829f 73 feature(3),
tenfoot 71:7b6a488af957 74 csc(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR, valueBytes.getPointer(),
tenfoot 71:7b6a488af957 75 valueBytes.getNumValueBytes(), SpeedCadenceValueBytes::MAX_BYTES,
tenfoot 71:7b6a488af957 76 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
tenfoot 72:a15b8451829f 77 cscFeat(GattCharacteristic::UUID_CSC_FEATURE_CHAR, (uint8_t*)&feature,
tenfoot 72:a15b8451829f 78 2, 2,
tenfoot 72:a15b8451829f 79 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
tenfoot 71:7b6a488af957 80 scLocation(UUID_SENSOR_LOCATION_CHAR, &location),
tenfoot 71:7b6a488af957 81 controlPoint(UUID_SC_CONTROL_POINT_CHAR, &controlPointValue) {
tenfoot 71:7b6a488af957 82 setupService();
tenfoot 71:7b6a488af957 83 }
tenfoot 71:7b6a488af957 84
tenfoot 71:7b6a488af957 85 /**
tenfoot 71:7b6a488af957 86 * @brief Set a new value for wheel revolutions.
tenfoot 71:7b6a488af957 87 *
tenfoot 71:7b6a488af957 88 * @param[in] wheelCounter
tenfoot 71:7b6a488af957 89 * Total wheel revolutions.
tenfoot 71:7b6a488af957 90 * @param[in] eventTime
tenfoot 71:7b6a488af957 91 * Time of event.
tenfoot 71:7b6a488af957 92 */
tenfoot 71:7b6a488af957 93 void updateWheelCounter(uint32_t wheelCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 94 valueBytes.updateWheelCounter(wheelCounter, eventTime);
tenfoot 71:7b6a488af957 95 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 96 }
tenfoot 71:7b6a488af957 97
tenfoot 71:7b6a488af957 98 /**
tenfoot 71:7b6a488af957 99 * @brief Set a new value for crank revolutions.
tenfoot 71:7b6a488af957 100 *
tenfoot 71:7b6a488af957 101 * @param[in] crankCounter
tenfoot 71:7b6a488af957 102 * Total crank revolutions.
tenfoot 71:7b6a488af957 103 * @param[in] eventTime
tenfoot 71:7b6a488af957 104 * Time of event.
tenfoot 71:7b6a488af957 105 */
tenfoot 71:7b6a488af957 106 void updateCrankCounter(uint16_t crankCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 107 valueBytes.updateCrankCounter(crankCounter, eventTime);
tenfoot 71:7b6a488af957 108 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 109 }
tenfoot 71:7b6a488af957 110
tenfoot 71:7b6a488af957 111 void updateCounters(uint32_t wheelCounter, uint16_t crankCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 112 valueBytes.updateWheelCounter(wheelCounter, eventTime);
tenfoot 71:7b6a488af957 113 valueBytes.updateCrankCounter(crankCounter, eventTime);
tenfoot 71:7b6a488af957 114 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 115 }
tenfoot 71:7b6a488af957 116
tenfoot 71:7b6a488af957 117 /**
tenfoot 71:7b6a488af957 118 * This callback allows the CyclingSpeedAndCadenceService to receive updates to the
tenfoot 71:7b6a488af957 119 * controlPoint Characteristic.
tenfoot 71:7b6a488af957 120 *
tenfoot 71:7b6a488af957 121 * @param[in] params
tenfoot 71:7b6a488af957 122 * Information about the characterisitc being updated.
tenfoot 71:7b6a488af957 123 */
tenfoot 71:7b6a488af957 124 virtual void onDataWritten(const GattWriteCallbackParams *params) {
tenfoot 71:7b6a488af957 125 if (params->handle == controlPoint.getValueAttribute().getHandle()) {
tenfoot 71:7b6a488af957 126 /* Do something here if the new value is 1; else you can override this method by
tenfoot 71:7b6a488af957 127 * extending this class.
tenfoot 71:7b6a488af957 128 * @NOTE: if you are extending this class, be sure to also call
tenfoot 71:7b6a488af957 129 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
tenfoot 71:7b6a488af957 130 * your constructor.
tenfoot 71:7b6a488af957 131 */
tenfoot 71:7b6a488af957 132 }
tenfoot 71:7b6a488af957 133 }
tenfoot 71:7b6a488af957 134
tenfoot 71:7b6a488af957 135 protected:
tenfoot 71:7b6a488af957 136 void setupService(void) {
tenfoot 72:a15b8451829f 137 GattCharacteristic *charTable[] = {&csc, &cscFeat, &scLocation, &controlPoint};
tenfoot 71:7b6a488af957 138 GattService cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
tenfoot 71:7b6a488af957 139
tenfoot 71:7b6a488af957 140 ble.addService(cscService);
tenfoot 71:7b6a488af957 141 ble.onDataWritten(this, &CyclingSpeedAndCadenceService::onDataWritten);
tenfoot 71:7b6a488af957 142 }
tenfoot 71:7b6a488af957 143
tenfoot 71:7b6a488af957 144 protected:
tenfoot 71:7b6a488af957 145 /* Private internal representation for the bytes used to work with the value of the speed cadence characteristic. */
tenfoot 71:7b6a488af957 146 struct SpeedCadenceValueBytes {
tenfoot 71:7b6a488af957 147 static const uint16_t MAX_BYTES = (1 + 4 + 2 + 2 + 2);
tenfoot 71:7b6a488af957 148 static const uint8_t FLAG_WHEEL_PRESENT = (1 << 0);
tenfoot 71:7b6a488af957 149 static const uint8_t FLAG_CRANK_PRESENT = (1 << 1);
tenfoot 71:7b6a488af957 150
tenfoot 71:7b6a488af957 151 SpeedCadenceValueBytes()
tenfoot 71:7b6a488af957 152 : flags(0)
tenfoot 71:7b6a488af957 153 {
tenfoot 71:7b6a488af957 154 updateWheelCounter(1, 0);
tenfoot 71:7b6a488af957 155 updateCrankCounter(1, 0);
tenfoot 71:7b6a488af957 156 }
tenfoot 71:7b6a488af957 157
tenfoot 71:7b6a488af957 158 void updateWheelCounter(uint32_t _wheelCounter, uint16_t _when) {
tenfoot 71:7b6a488af957 159 flags |= FLAG_WHEEL_PRESENT;
tenfoot 71:7b6a488af957 160 wheelCounter = _wheelCounter;
tenfoot 71:7b6a488af957 161 lastWheelEvent = _when;
tenfoot 72:a15b8451829f 162 pack();
tenfoot 71:7b6a488af957 163 }
tenfoot 71:7b6a488af957 164
tenfoot 71:7b6a488af957 165 void updateCrankCounter(uint16_t _crankCounter, uint16_t _when) {
tenfoot 71:7b6a488af957 166 flags |= FLAG_CRANK_PRESENT;
tenfoot 71:7b6a488af957 167 crankCounter = _crankCounter;
tenfoot 71:7b6a488af957 168 lastCrankEvent = _when;
tenfoot 72:a15b8451829f 169 pack();
tenfoot 71:7b6a488af957 170 }
tenfoot 71:7b6a488af957 171
tenfoot 71:7b6a488af957 172 uint8_t *getPointer(void) {
tenfoot 71:7b6a488af957 173 return valueBytes;
tenfoot 71:7b6a488af957 174 }
tenfoot 71:7b6a488af957 175
tenfoot 71:7b6a488af957 176 unsigned getNumValueBytes(void) const {
tenfoot 71:7b6a488af957 177 return 1 +
tenfoot 71:7b6a488af957 178 ((flags & FLAG_WHEEL_PRESENT) ? (4+2) : 0) +
tenfoot 71:7b6a488af957 179 ((flags & FLAG_CRANK_PRESENT) ? (2+2) : 0);
tenfoot 71:7b6a488af957 180 }
tenfoot 71:7b6a488af957 181
tenfoot 71:7b6a488af957 182 private:
tenfoot 72:a15b8451829f 183 void pack()
tenfoot 71:7b6a488af957 184 {
tenfoot 71:7b6a488af957 185 valueBytes[0] = flags;
tenfoot 72:a15b8451829f 186 unsigned p = 1;
tenfoot 71:7b6a488af957 187 if (flags & FLAG_WHEEL_PRESENT)
tenfoot 71:7b6a488af957 188 {
tenfoot 72:a15b8451829f 189 valueBytes[p++] = wheelCounter & 0xFF;
tenfoot 72:a15b8451829f 190 valueBytes[p++] = (wheelCounter >> 8) & 0xFF;
tenfoot 72:a15b8451829f 191 valueBytes[p++] = (wheelCounter >> 16) & 0xFF;
tenfoot 72:a15b8451829f 192 valueBytes[p++] = (wheelCounter >> 24) & 0xFF;
tenfoot 72:a15b8451829f 193 valueBytes[p++] = lastWheelEvent & 0xFF;
tenfoot 72:a15b8451829f 194 valueBytes[p++] = (lastWheelEvent >> 8) & 0xFF;
tenfoot 71:7b6a488af957 195 }
tenfoot 71:7b6a488af957 196 if (flags & FLAG_CRANK_PRESENT)
tenfoot 71:7b6a488af957 197 {
tenfoot 72:a15b8451829f 198 valueBytes[p++] = crankCounter & 0xFF;
tenfoot 72:a15b8451829f 199 valueBytes[p++] = (crankCounter >> 8) & 0xFF;
tenfoot 72:a15b8451829f 200 valueBytes[p++] = lastCrankEvent & 0xFF;
tenfoot 72:a15b8451829f 201 valueBytes[p++] = (lastCrankEvent >> 8) & 0xFF;
tenfoot 71:7b6a488af957 202 }
tenfoot 71:7b6a488af957 203 }
tenfoot 71:7b6a488af957 204
tenfoot 71:7b6a488af957 205 uint8_t flags;
tenfoot 71:7b6a488af957 206 uint32_t wheelCounter;
tenfoot 71:7b6a488af957 207 uint16_t lastWheelEvent;
tenfoot 71:7b6a488af957 208 uint16_t crankCounter;
tenfoot 71:7b6a488af957 209 uint16_t lastCrankEvent;
tenfoot 72:a15b8451829f 210 uint8_t valueBytes[MAX_BYTES];
tenfoot 71:7b6a488af957 211 };
tenfoot 71:7b6a488af957 212
tenfoot 71:7b6a488af957 213 protected:
tenfoot 71:7b6a488af957 214 BLE &ble;
tenfoot 71:7b6a488af957 215
tenfoot 71:7b6a488af957 216 SpeedCadenceValueBytes valueBytes;
tenfoot 72:a15b8451829f 217 uint16_t feature;
tenfoot 71:7b6a488af957 218 uint8_t controlPointValue;
tenfoot 71:7b6a488af957 219
tenfoot 71:7b6a488af957 220 GattCharacteristic csc;
tenfoot 72:a15b8451829f 221 GattCharacteristic cscFeat;
tenfoot 71:7b6a488af957 222 ReadOnlyGattCharacteristic<uint8_t> scLocation;
tenfoot 71:7b6a488af957 223 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
tenfoot 71:7b6a488af957 224 };
tenfoot 71:7b6a488af957 225
tenfoot 71:7b6a488af957 226 #endif /* #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__*/