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

Files at this revision

API Documentation at this revision

Comitter:
tenfoot
Date:
Sun Aug 23 15:02:10 2015 +0000
Parent:
74:98dee483c173
Commit message:
Simulated crank / wheel events

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Aug 23 14:05:22 2015 +0000
+++ b/main.cpp	Sun Aug 23 15:02:10 2015 +0000
@@ -21,32 +21,46 @@
 #include "ble/services/DeviceInformationService.h"
 
 BLE  ble;
-DigitalOut led1(LED1);
+Timer timer;
 
 const static char     DEVICE_NAME[]        = "CSC1";
 static const uint16_t uuid16_list[]        = {GattService::UUID_CYCLING_SPEED_AND_CADENCE,
                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
-static volatile bool  triggerSensorPolling = false;
+
+uint32_t t = 0;
+uint32_t nextWheel = 0;
+uint32_t nextCrank = 0;
+static volatile bool  triggerWheel = false;
+static volatile bool  triggerCrank = false;
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     ble.gap().startAdvertising(); // restart advertising
 }
 
-void periodicCallback(void)
+
+void onTick(void)
 {
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
+    ++t;
+    
+    if (t >= nextWheel)
+    {
+        triggerWheel = true;
+        nextWheel += 7 + (rand() % 10);
+    }
+    
+    if (t >= nextCrank)
+    {
+        triggerCrank = true;    
+        nextCrank += 8 + (rand() % 20);
+    }
 }
 
 int main(void)
 {
-    led1 = 1;
     Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
+    ticker.attach(onTick, 0.1);
+    timer.start();
 
     ble.init();
     ble.gap().onDisconnection(disconnectionCallback);
@@ -69,22 +83,38 @@
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
     ble.gap().startAdvertising();
+    
+    nextWheel = 7 + (rand() % 10);
+    nextCrank = 8 + (rand() % 20);
 
     // infinite loop
-    uint16_t when = 0;
     while (1) {
         // check for trigger from periodicCallback()
-        if (triggerSensorPolling && ble.getGapState().connected) {
-            triggerSensorPolling = false;
-
-            // Do blocking calls or whatever is necessary for sensor polling.
-            // In our case, we simply update the HRM measurement.
-            wheelCounter += 3;
-            crankCounter++;
-            when += 1024;
-
-            // update bps
-            cscService.updateCounters(wheelCounter, crankCounter, when);
+        if (ble.getGapState().connected)
+        {
+            if (triggerCrank && triggerWheel)
+            {
+                uint16_t when = (timer.read() * 1024);
+                cscService.updateCounters(++wheelCounter, ++crankCounter, when);
+                triggerWheel = false;
+                triggerCrank = false;
+            }
+            else if (triggerWheel)
+            {
+                uint16_t when = (timer.read() * 1024);
+                cscService.updateWheelCounter(++wheelCounter, when);
+                triggerWheel = false;
+            }
+            else if (triggerCrank)
+            {
+                uint16_t when = (timer.read() * 1024);
+                cscService.updateCrankCounter(++crankCounter, when);
+                triggerCrank = false;
+            }
+            else
+            {
+                ble.waitForEvent(); // low power wait for event
+            }
         } else {
             ble.waitForEvent(); // low power wait for event
         }