Cycle speed and cadence example for the BLE API using nRF51822 native mode drivers
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
CyclingSpeedAndCadenceService.h
- Committer:
- tenfoot
- Date:
- 2015-08-16
- Revision:
- 72:a15b8451829f
- Parent:
- 71:7b6a488af957
- Child:
- 73:bae88c99c2ae
File content as of revision 72:a15b8451829f:
/* * Copyright (c) 2015 Robert Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__ #define __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__ #include "ble/BLE.h" /** * @class CyclingSpeedAndCadenceService * @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> * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.cycling_speed_and_cadence.xml <br> * CSC Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.csc_measurement.xml <br> * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.sensor_location.xml */ class CyclingSpeedAndCadenceService { public: /** * @enum SensorLocation * @brief Location of sensor on bike. */ enum { LOCATION_OTHER, /*!< Other */ LOCATION_TOP_OF_SHOE, /*!< Top of shoe */ LOCATION_IN_SHOE, /*!< In shoe */ LOCATION_HIP, /*!< Hip */ LOCATION_FRONT_WHEEL, /*!< Front Wheel */ LOCATION_LEFT_CRANK, /*!< Left Crank */ LOCATION_RIGHT_CRANK, /*!< Right Crank */ LOCATION_LEFT_PEDAL, /*!< Left Pedal */ LOCATION_RIGHT_PEDAL, /*!< Right Pedal */ LOCATION_FRONT_HUB, /*!< Front Hub */ LOCATION_REAR_DROPOUT, /*!< Rear Dropout */ LOCATION_CHAINSTAY, /*!< Chainstay */ LOCATION_REAR_WHEEL, /*!< Rear Wheel */ LOCATION_REAR_HUB, /*!< Rear Hub */ LOCATION_CHEST, /*!< Chest */ }; enum { UUID_SENSOR_LOCATION_CHAR = 0x2A5D, UUID_SC_CONTROL_POINT_CHAR = 0x2A55 }; public: /** * @brief Constructor with initial counter values. * * @param[ref] _ble * Reference to the underlying BLE. * @param[in] wheelCounter (32-bit) * initial value for the wheel counter. * @param[in] crankCounter (32-bit) * initial value for the crank counter. * @param[in] location * Sensor's location. */ CyclingSpeedAndCadenceService(BLE &_ble, uint8_t location) : ble(_ble), feature(3), csc(GattCharacteristic::UUID_CSC_MEASUREMENT_CHAR, valueBytes.getPointer(), valueBytes.getNumValueBytes(), SpeedCadenceValueBytes::MAX_BYTES, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), cscFeat(GattCharacteristic::UUID_CSC_FEATURE_CHAR, (uint8_t*)&feature, 2, 2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), scLocation(UUID_SENSOR_LOCATION_CHAR, &location), controlPoint(UUID_SC_CONTROL_POINT_CHAR, &controlPointValue) { setupService(); } /** * @brief Set a new value for wheel revolutions. * * @param[in] wheelCounter * Total wheel revolutions. * @param[in] eventTime * Time of event. */ void updateWheelCounter(uint32_t wheelCounter, uint16_t eventTime) { valueBytes.updateWheelCounter(wheelCounter, eventTime); ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); } /** * @brief Set a new value for crank revolutions. * * @param[in] crankCounter * Total crank revolutions. * @param[in] eventTime * Time of event. */ void updateCrankCounter(uint16_t crankCounter, uint16_t eventTime) { valueBytes.updateCrankCounter(crankCounter, eventTime); ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); } void updateCounters(uint32_t wheelCounter, uint16_t crankCounter, uint16_t eventTime) { valueBytes.updateWheelCounter(wheelCounter, eventTime); valueBytes.updateCrankCounter(crankCounter, eventTime); ble.gattServer().write(csc.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); } /** * This callback allows the CyclingSpeedAndCadenceService to receive updates to the * controlPoint Characteristic. * * @param[in] params * Information about the characterisitc being updated. */ virtual void onDataWritten(const GattWriteCallbackParams *params) { if (params->handle == controlPoint.getValueAttribute().getHandle()) { /* Do something here if the new value is 1; else you can override this method by * extending this class. * @NOTE: if you are extending this class, be sure to also call * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in * your constructor. */ } } protected: void setupService(void) { GattCharacteristic *charTable[] = {&csc, &cscFeat, &scLocation, &controlPoint}; GattService cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(cscService); ble.onDataWritten(this, &CyclingSpeedAndCadenceService::onDataWritten); } protected: /* Private internal representation for the bytes used to work with the value of the speed cadence characteristic. */ struct SpeedCadenceValueBytes { static const uint16_t MAX_BYTES = (1 + 4 + 2 + 2 + 2); static const uint8_t FLAG_WHEEL_PRESENT = (1 << 0); static const uint8_t FLAG_CRANK_PRESENT = (1 << 1); SpeedCadenceValueBytes() : flags(0) { updateWheelCounter(1, 0); updateCrankCounter(1, 0); } void updateWheelCounter(uint32_t _wheelCounter, uint16_t _when) { flags |= FLAG_WHEEL_PRESENT; wheelCounter = _wheelCounter; lastWheelEvent = _when; pack(); } void updateCrankCounter(uint16_t _crankCounter, uint16_t _when) { flags |= FLAG_CRANK_PRESENT; crankCounter = _crankCounter; lastCrankEvent = _when; pack(); } uint8_t *getPointer(void) { return valueBytes; } unsigned getNumValueBytes(void) const { return 1 + ((flags & FLAG_WHEEL_PRESENT) ? (4+2) : 0) + ((flags & FLAG_CRANK_PRESENT) ? (2+2) : 0); } private: void pack() { valueBytes[0] = flags; unsigned p = 1; if (flags & FLAG_WHEEL_PRESENT) { valueBytes[p++] = wheelCounter & 0xFF; valueBytes[p++] = (wheelCounter >> 8) & 0xFF; valueBytes[p++] = (wheelCounter >> 16) & 0xFF; valueBytes[p++] = (wheelCounter >> 24) & 0xFF; valueBytes[p++] = lastWheelEvent & 0xFF; valueBytes[p++] = (lastWheelEvent >> 8) & 0xFF; } if (flags & FLAG_CRANK_PRESENT) { valueBytes[p++] = crankCounter & 0xFF; valueBytes[p++] = (crankCounter >> 8) & 0xFF; valueBytes[p++] = lastCrankEvent & 0xFF; valueBytes[p++] = (lastCrankEvent >> 8) & 0xFF; } } uint8_t flags; uint32_t wheelCounter; uint16_t lastWheelEvent; uint16_t crankCounter; uint16_t lastCrankEvent; uint8_t valueBytes[MAX_BYTES]; }; protected: BLE &ble; SpeedCadenceValueBytes valueBytes; uint16_t feature; uint8_t controlPointValue; GattCharacteristic csc; GattCharacteristic cscFeat; ReadOnlyGattCharacteristic<uint8_t> scLocation; WriteOnlyGattCharacteristic<uint8_t> controlPoint; }; #endif /* #ifndef __BLE_CYCLING_SPEED_AND_CADENCE_SERVICE_H__*/