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

Revision:
72:a15b8451829f
Parent:
71:7b6a488af957
Child:
73:bae88c99c2ae
--- a/CyclingSpeedAndCadenceService.h	Sun Aug 16 13:23:48 2015 +0000
+++ b/CyclingSpeedAndCadenceService.h	Sun Aug 16 14:46:43 2015 +0000
@@ -70,9 +70,13 @@
      */
     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();
@@ -130,7 +134,7 @@
 
 protected:
     void setupService(void) {
-        GattCharacteristic *charTable[] = {&csc, &scLocation, &controlPoint};
+        GattCharacteristic *charTable[] = {&csc, &cscFeat, &scLocation, &controlPoint};
         GattService         cscService(GattService::UUID_CYCLING_SPEED_AND_CADENCE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
         ble.addService(cscService);
@@ -155,21 +159,17 @@
             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) {
-            pack();
-            return valueBytes;
-        }
-
-        const uint8_t *getPointer(void) const {
-            pack();
             return valueBytes;
         }
 
@@ -180,23 +180,25 @@
         }
 
     private:
-        void pack() const
+        void pack()
         {
             valueBytes[0] = flags;
-            uint8_t* p = &valueBytes[1];
+            unsigned p = 1;
             if (flags & FLAG_WHEEL_PRESENT)
             {
-                *(uint32_t*)(p) = wheelCounter;
-                p += 4;
-                *(uint16_t*)(p) = lastWheelEvent;
-                p += 2;
+                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)
             {
-                *(uint16_t*)(p) = crankCounter;
-                p += 2;
-                *(uint16_t*)(p) = lastCrankEvent;
-                p += 2;
+                valueBytes[p++] = crankCounter & 0xFF;
+                valueBytes[p++] = (crankCounter >>  8) & 0xFF;
+                valueBytes[p++] = lastCrankEvent & 0xFF;
+                valueBytes[p++] = (lastCrankEvent >>  8) & 0xFF;
             }
         }
         
@@ -205,16 +207,18 @@
         uint16_t lastWheelEvent;
         uint16_t crankCounter;
         uint16_t lastCrankEvent;
-        mutable uint8_t valueBytes[MAX_BYTES];
+        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;
 };