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 13:23:48 2015 +0000
Revision:
71:7b6a488af957
Child:
72:a15b8451829f
Initial cycle speed / cadence

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 71:7b6a488af957 73 csc(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR, valueBytes.getPointer(),
tenfoot 71:7b6a488af957 74 valueBytes.getNumValueBytes(), SpeedCadenceValueBytes::MAX_BYTES,
tenfoot 71:7b6a488af957 75 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
tenfoot 71:7b6a488af957 76 scLocation(UUID_SENSOR_LOCATION_CHAR, &location),
tenfoot 71:7b6a488af957 77 controlPoint(UUID_SC_CONTROL_POINT_CHAR, &controlPointValue) {
tenfoot 71:7b6a488af957 78 setupService();
tenfoot 71:7b6a488af957 79 }
tenfoot 71:7b6a488af957 80
tenfoot 71:7b6a488af957 81 /**
tenfoot 71:7b6a488af957 82 * @brief Set a new value for wheel revolutions.
tenfoot 71:7b6a488af957 83 *
tenfoot 71:7b6a488af957 84 * @param[in] wheelCounter
tenfoot 71:7b6a488af957 85 * Total wheel revolutions.
tenfoot 71:7b6a488af957 86 * @param[in] eventTime
tenfoot 71:7b6a488af957 87 * Time of event.
tenfoot 71:7b6a488af957 88 */
tenfoot 71:7b6a488af957 89 void updateWheelCounter(uint32_t wheelCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 90 valueBytes.updateWheelCounter(wheelCounter, eventTime);
tenfoot 71:7b6a488af957 91 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 92 }
tenfoot 71:7b6a488af957 93
tenfoot 71:7b6a488af957 94 /**
tenfoot 71:7b6a488af957 95 * @brief Set a new value for crank revolutions.
tenfoot 71:7b6a488af957 96 *
tenfoot 71:7b6a488af957 97 * @param[in] crankCounter
tenfoot 71:7b6a488af957 98 * Total crank revolutions.
tenfoot 71:7b6a488af957 99 * @param[in] eventTime
tenfoot 71:7b6a488af957 100 * Time of event.
tenfoot 71:7b6a488af957 101 */
tenfoot 71:7b6a488af957 102 void updateCrankCounter(uint16_t crankCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 103 valueBytes.updateCrankCounter(crankCounter, eventTime);
tenfoot 71:7b6a488af957 104 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 105 }
tenfoot 71:7b6a488af957 106
tenfoot 71:7b6a488af957 107 void updateCounters(uint32_t wheelCounter, uint16_t crankCounter, uint16_t eventTime) {
tenfoot 71:7b6a488af957 108 valueBytes.updateWheelCounter(wheelCounter, eventTime);
tenfoot 71:7b6a488af957 109 valueBytes.updateCrankCounter(crankCounter, eventTime);
tenfoot 71:7b6a488af957 110 ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
tenfoot 71:7b6a488af957 111 }
tenfoot 71:7b6a488af957 112
tenfoot 71:7b6a488af957 113 /**
tenfoot 71:7b6a488af957 114 * This callback allows the CyclingSpeedAndCadenceService to receive updates to the
tenfoot 71:7b6a488af957 115 * controlPoint Characteristic.
tenfoot 71:7b6a488af957 116 *
tenfoot 71:7b6a488af957 117 * @param[in] params
tenfoot 71:7b6a488af957 118 * Information about the characterisitc being updated.
tenfoot 71:7b6a488af957 119 */
tenfoot 71:7b6a488af957 120 virtual void onDataWritten(const GattWriteCallbackParams *params) {
tenfoot 71:7b6a488af957 121 if (params->handle == controlPoint.getValueAttribute().getHandle()) {
tenfoot 71:7b6a488af957 122 /* Do something here if the new value is 1; else you can override this method by
tenfoot 71:7b6a488af957 123 * extending this class.
tenfoot 71:7b6a488af957 124 * @NOTE: if you are extending this class, be sure to also call
tenfoot 71:7b6a488af957 125 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
tenfoot 71:7b6a488af957 126 * your constructor.
tenfoot 71:7b6a488af957 127 */
tenfoot 71:7b6a488af957 128 }
tenfoot 71:7b6a488af957 129 }
tenfoot 71:7b6a488af957 130
tenfoot 71:7b6a488af957 131 protected:
tenfoot 71:7b6a488af957 132 void setupService(void) {
tenfoot 71:7b6a488af957 133 GattCharacteristic *charTable[] = {&csc, &scLocation, &controlPoint};
tenfoot 71:7b6a488af957 134 GattService cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
tenfoot 71:7b6a488af957 135
tenfoot 71:7b6a488af957 136 ble.addService(cscService);
tenfoot 71:7b6a488af957 137 ble.onDataWritten(this, &CyclingSpeedAndCadenceService::onDataWritten);
tenfoot 71:7b6a488af957 138 }
tenfoot 71:7b6a488af957 139
tenfoot 71:7b6a488af957 140 protected:
tenfoot 71:7b6a488af957 141 /* Private internal representation for the bytes used to work with the value of the speed cadence characteristic. */
tenfoot 71:7b6a488af957 142 struct SpeedCadenceValueBytes {
tenfoot 71:7b6a488af957 143 static const uint16_t MAX_BYTES = (1 + 4 + 2 + 2 + 2);
tenfoot 71:7b6a488af957 144 static const uint8_t FLAG_WHEEL_PRESENT = (1 << 0);
tenfoot 71:7b6a488af957 145 static const uint8_t FLAG_CRANK_PRESENT = (1 << 1);
tenfoot 71:7b6a488af957 146
tenfoot 71:7b6a488af957 147 SpeedCadenceValueBytes()
tenfoot 71:7b6a488af957 148 : flags(0)
tenfoot 71:7b6a488af957 149 {
tenfoot 71:7b6a488af957 150 updateWheelCounter(1, 0);
tenfoot 71:7b6a488af957 151 updateCrankCounter(1, 0);
tenfoot 71:7b6a488af957 152 }
tenfoot 71:7b6a488af957 153
tenfoot 71:7b6a488af957 154 void updateWheelCounter(uint32_t _wheelCounter, uint16_t _when) {
tenfoot 71:7b6a488af957 155 flags |= FLAG_WHEEL_PRESENT;
tenfoot 71:7b6a488af957 156 wheelCounter = _wheelCounter;
tenfoot 71:7b6a488af957 157 lastWheelEvent = _when;
tenfoot 71:7b6a488af957 158 }
tenfoot 71:7b6a488af957 159
tenfoot 71:7b6a488af957 160 void updateCrankCounter(uint16_t _crankCounter, uint16_t _when) {
tenfoot 71:7b6a488af957 161 flags |= FLAG_CRANK_PRESENT;
tenfoot 71:7b6a488af957 162 crankCounter = _crankCounter;
tenfoot 71:7b6a488af957 163 lastCrankEvent = _when;
tenfoot 71:7b6a488af957 164 }
tenfoot 71:7b6a488af957 165
tenfoot 71:7b6a488af957 166 uint8_t *getPointer(void) {
tenfoot 71:7b6a488af957 167 pack();
tenfoot 71:7b6a488af957 168 return valueBytes;
tenfoot 71:7b6a488af957 169 }
tenfoot 71:7b6a488af957 170
tenfoot 71:7b6a488af957 171 const uint8_t *getPointer(void) const {
tenfoot 71:7b6a488af957 172 pack();
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 71:7b6a488af957 183 void pack() const
tenfoot 71:7b6a488af957 184 {
tenfoot 71:7b6a488af957 185 valueBytes[0] = flags;
tenfoot 71:7b6a488af957 186 uint8_t* p = &valueBytes[1];
tenfoot 71:7b6a488af957 187 if (flags & FLAG_WHEEL_PRESENT)
tenfoot 71:7b6a488af957 188 {
tenfoot 71:7b6a488af957 189 *(uint32_t*)(p) = wheelCounter;
tenfoot 71:7b6a488af957 190 p += 4;
tenfoot 71:7b6a488af957 191 *(uint16_t*)(p) = lastWheelEvent;
tenfoot 71:7b6a488af957 192 p += 2;
tenfoot 71:7b6a488af957 193 }
tenfoot 71:7b6a488af957 194 if (flags & FLAG_CRANK_PRESENT)
tenfoot 71:7b6a488af957 195 {
tenfoot 71:7b6a488af957 196 *(uint16_t*)(p) = crankCounter;
tenfoot 71:7b6a488af957 197 p += 2;
tenfoot 71:7b6a488af957 198 *(uint16_t*)(p) = lastCrankEvent;
tenfoot 71:7b6a488af957 199 p += 2;
tenfoot 71:7b6a488af957 200 }
tenfoot 71:7b6a488af957 201 }
tenfoot 71:7b6a488af957 202
tenfoot 71:7b6a488af957 203 uint8_t flags;
tenfoot 71:7b6a488af957 204 uint32_t wheelCounter;
tenfoot 71:7b6a488af957 205 uint16_t lastWheelEvent;
tenfoot 71:7b6a488af957 206 uint16_t crankCounter;
tenfoot 71:7b6a488af957 207 uint16_t lastCrankEvent;
tenfoot 71:7b6a488af957 208 mutable uint8_t valueBytes[MAX_BYTES];
tenfoot 71:7b6a488af957 209 };
tenfoot 71:7b6a488af957 210
tenfoot 71:7b6a488af957 211 protected:
tenfoot 71:7b6a488af957 212 BLE &ble;
tenfoot 71:7b6a488af957 213
tenfoot 71:7b6a488af957 214 SpeedCadenceValueBytes valueBytes;
tenfoot 71:7b6a488af957 215 uint8_t controlPointValue;
tenfoot 71:7b6a488af957 216
tenfoot 71:7b6a488af957 217 GattCharacteristic csc;
tenfoot 71:7b6a488af957 218 ReadOnlyGattCharacteristic<uint8_t> scLocation;
tenfoot 71:7b6a488af957 219 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
tenfoot 71:7b6a488af957 220 };
tenfoot 71:7b6a488af957 221
tenfoot 71:7b6a488af957 222 #endif /* #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__*/