Using the MBED BLE library and Nordic Puck library this is a simple scoring application using Bluetooth LE. It monitors three analog inputs and triggers on reception of a pulse on any one recording data for a short period on all three. This is then published via BLE characteristics. It's a demonstrator for a new UI dev toolkit that is under development.

Dependencies:   Puck mbed

Fork of Example_Puck_BLE by Nordic Semiconductor

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Tue Aug 26 14:13:31 2014 +0000
Parent:
9:f2b2ebc6d908
Commit message:
Working with 3 channels

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f2b2ebc6d908 -r a2ba0cef85aa main.cpp
--- a/main.cpp	Sun Aug 24 20:50:22 2014 +0000
+++ b/main.cpp	Tue Aug 26 14:13:31 2014 +0000
@@ -9,6 +9,7 @@
 #include "Puck.h"
 #include "SampleChannel.h"
 
+// This is a singleton for the Nordic Puck library which helps with some BLE init, etc.
 Puck* puck = &Puck::getPuck();
 
 // Gatt characteristic and service UUIDs
@@ -17,10 +18,11 @@
 const UUID DIVISOR_GATT_CHARACTERISTIC =        stringToUUID("nod.score1.div  ");
 const UUID INTERVAL_US_GATT_CHARACTERISTIC =    stringToUUID("nod.score1.intus");
 
+// Three channels for scoring
 const int NUM_SAMPLE_CHANNELS = 3;
 
 // Sample interval (uS)
-uint32_t sampleIntervalUs = 10000;
+volatile uint32_t sampleIntervalUs = 10000;
 
 // Interrupt driven ticker to do the sampling
 Timeout sampleTimeout;
@@ -37,17 +39,16 @@
 Timer intervalTimer;
 int lastTriggerTime = 0;
 int lastSampleTime = 0;
-const int MIN_MS_BETWEEN_SAMPLES = 2000;
+const int MIN_MS_BETWEEN_TRIGGERS = 2000;
 
 // Function called in interrupt driven timeout to handle sampling
 static volatile int serviceCount = 0;
 void SampleService()
 {
+    // For debug timing
     serviceCount++;
     
-    sampleTimeout.attach_us(&SampleService, sampleIntervalUs);
-
-    // service all channel's state machines
+    // Service all channel's state machines
     bool isAnyChannelSampling = false;
     for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
     {
@@ -55,14 +56,16 @@
         if (sampleChannels[chanIdx].IsSampling())
             isAnyChannelSampling = true;
     }
-
+    
+    // Check if any channel is being sampled already (if so don't check for triggering)
     if (!isAnyChannelSampling)
     {
+        // Check for triggering only if previous trigger was a reasonable time ago
         int curTimerVal = intervalTimer.read_ms();
-        if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES))
-        {
-            
-            // check each channel to see if it's been triggered
+        // Check for lastTriggerTime < curTimerVal is to handle (not perfectly) overflow/reset of intervalTimer
+        if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_TRIGGERS))
+        {            
+            // Check each channel to see if it's been triggered
             bool anythingTriggered = false;
             for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
             {
@@ -73,19 +76,23 @@
                     break;
                 }
             }
+            // If any channel has triggered ...
             if(anythingTriggered)
             {
+                // Start sampling on all channels
                 for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
-                {
                     sampleChannels[chanIdx].StartSampling();
-                }
                 // Set timer to disallow repeated readings
                 lastTriggerTime = curTimerVal;                
             }
         }
     }
+    
+    // Request a callback to this function after the sample interval
+    sampleTimeout.attach_us(&SampleService, sampleIntervalUs);
 }
-    
+
+// ThresholdSet ... BLE characteristic callback
 void onThresholdSet(uint8_t* value)
 {
     uint16_t threshold = value[0] * 256 + value[1];
@@ -94,6 +101,7 @@
         sampleChannels[chanIdx].SetThreshold(threshold);
 }
 
+// DivisorSet ... BLE characteristic callback
 void onDivisorSet(uint8_t* value)
 {
     uint16_t divisor = value[0] * 256 + value[1];
@@ -102,18 +110,17 @@
         sampleChannels[chanIdx].SetDivisor(divisor);
 }
 
+// Inverval ... BLE characteristic callback
 void onIntervalSet(uint8_t* value)
 {
     uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3];
     LOG_INFO("SampleInterval(uS)=%d\n", intervalUs);
+    // Interval timer is restarted in the Ticker callback so just need to store this value
     if (intervalUs <= 1000000)
-    {
         sampleIntervalUs = intervalUs;
-//        sampleTicker.detach();
-//        sampleTicker.attach_us(&SampleService, sampleIntervalUs);
-    }
 }
 
+// Main - Setup BLE and service the trigger sampling and BLE
 int main(void) 
 {
     
@@ -179,8 +186,17 @@
         if ((nowTime - lastPuckDriveTime >= 1000) || (nowTime < lastPuckDriveTime))
         {
             unsigned int elapsed = nowTime - lastPuckDriveTime;
-            LOG_INFO("%u E%u C%u DC%u TC%u DTC%u L%u DL%u\n", nowTime, elapsed, serviceCount, serviceCount-lastServiceCount, nowTime/serviceCount, elapsed/(serviceCount-lastServiceCount), driveLoops, driveLoops-lastDriveLoops);
-            lastPuckDriveTime = intervalTimer.read_ms();
+            LOG_INFO("E%u C%u DC%u DTC%u L%u DL%u\n", elapsed, serviceCount, serviceCount-lastServiceCount, elapsed/(serviceCount-lastServiceCount), driveLoops, driveLoops-lastDriveLoops);
+            
+            // Check for overflow of timer
+            if (nowTime > 100000)
+            {
+                intervalTimer.reset();
+                nowTime = 0;
+            }
+                
+            // Record last timer value
+            lastPuckDriveTime = nowTime;
             lastDriveLoops = driveLoops;
             lastServiceCount = serviceCount;
         }