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:
Sat Aug 23 09:31:01 2014 +0000
Parent:
3:a155da1cbde3
Child:
5:ed9a4f932fcf
Commit message:
Ticker working on 100ms intervals with BLE enabled but no analog activity

Changed in this revision

SampleChannel.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SampleChannel.cpp	Thu Aug 21 11:26:26 2014 +0000
+++ b/SampleChannel.cpp	Sat Aug 23 09:31:01 2014 +0000
@@ -32,9 +32,9 @@
 void SampleChannel::AddSample(uint16_t val)
 {
     testSampleCount++;
-    if (testSampleCount == 100)
+    if (testSampleCount == 1000)
     {
-        pLocalLogger->printf("%d AvgT %d, AvgW %d, low %d\n", val, GetAvgOfTriggerWindow(), GetAvgOutsideTriggerWindow(), GetLowest());
+//        pLocalLogger->printf("%d AvgT %d, AvgW %d, low %d\n", val, GetAvgOfTriggerWindow(), GetAvgOutsideTriggerWindow(), GetLowest());
         testSampleCount = 0;
     }
             
@@ -120,14 +120,10 @@
 bool SampleChannel::CheckTrigger()
 {
     // Check if the samples in the trigger window are significantly different from the average
-    for (int i = 0; i < TRIGGER_WINDOW; i++)
-    {
-        int spike = GetAvgOfTriggerWindow() - GetAvgOutsideTriggerWindow();
-        if (spike < 0)
-            spike = -spike;
-        return (spike > sampleThreshold);
-    }
-    return false;
+    int spike = GetAvgOfTriggerWindow() - GetAvgOutsideTriggerWindow();
+    if (spike < 0)
+        spike = -spike;
+    return spike > sampleThreshold;
 }
     
 uint8_t *SampleChannel::GetSamples()
--- a/main.cpp	Thu Aug 21 11:26:26 2014 +0000
+++ b/main.cpp	Sat Aug 23 09:31:01 2014 +0000
@@ -5,7 +5,7 @@
 
  */
 
-#define LOG_LEVEL_INFO
+#define LOG_LEVEL_DEBUG
 #include "Puck.h"
 #include "SampleChannel.h"
 
@@ -22,6 +22,9 @@
 // Sample interval (uS)
 uint32_t sampleIntervalUs = 100000;
 
+// Interrupt driven ticker to do the sampling
+Ticker sampleTicker;
+
 // Sample Channels
 SampleChannel sampleChannels[] =
 {
@@ -36,6 +39,53 @@
 int lastSampleTime = 0;
 const int MIN_MS_BETWEEN_SAMPLES = 2000;
 
+// Function called in interrupt driven ticker to handle sampling
+static volatile int serviceCount = 0;
+void SampleService()
+{
+    serviceCount++;
+    
+    return;
+    
+    // service all channel's state machines
+    bool isAnyChannelSampling = false;
+    for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
+    {
+        sampleChannels[chanIdx].Service();
+        if (sampleChannels[chanIdx].IsSampling())
+            isAnyChannelSampling = true;
+    }
+
+    if (!isAnyChannelSampling)
+    {
+        int curTimerVal = intervalTimer.read_ms();
+        if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES))
+        {
+            
+            // check each channel to see if it's been triggered
+            bool anythingTriggered = false;
+            for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
+            {
+                if (sampleChannels[chanIdx].CheckTrigger())
+                {
+                    anythingTriggered = true;
+                    LOG_INFO("Triggered\n");
+                    break;
+                }
+            }
+            if(anythingTriggered)
+            {
+                for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
+                {
+                    sampleChannels[chanIdx].StartSampling();
+                }
+                // Set timer to disallow repeated readings
+                lastTriggerTime = curTimerVal;                
+            }
+        }
+    }
+}
+    
 void onThresholdSet(uint8_t* value)
 {
     uint16_t threshold = value[0] * 256 + value[1];
@@ -57,7 +107,11 @@
     uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3];
     LOG_INFO("SampleInterval(uS)=%d\n", intervalUs);
     if (intervalUs <= 1000000)
+    {
         sampleIntervalUs = intervalUs;
+//        sampleTicker.detach();
+//        sampleTicker.attach_us(&SampleService, sampleIntervalUs);
+    }
 }
 
 int main(void) 
@@ -105,66 +159,34 @@
     
     // Start timer
     intervalTimer.start();
+    
+    // Start ticker to service the sampling
+    sampleTicker.attach_us(&SampleService, sampleIntervalUs);
 
     // Wait for something to be found
+    unsigned int lastPuckDriveTime = 0;
     while(true)
     {
-        // service all channel's state machines
-        bool isAnyChannelSampling = false;
+        // Service the puck
+        if ((intervalTimer.read_ms() - lastPuckDriveTime >= 1000) || (intervalTimer.read_ms() < lastPuckDriveTime))
+        {
+            //puck->drive();
+            lastPuckDriveTime = intervalTimer.read_ms();
+            LOG_INFO("Puck Service at %u, tickers = %u\n", intervalTimer.read_ms(), serviceCount);
+        }
+        
+        continue;
+
+        // Check for data ready
         for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
         {
-            sampleChannels[chanIdx].Service();
             if (sampleChannels[chanIdx].AreSamplesReady())
             {
                // Set the value of the characteristic
-                puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen());
+                //puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen());
                 sampleChannels[chanIdx].StopSampling();
                 LOG_INFO("StopSampling\n");
             }
-            if (sampleChannels[chanIdx].IsSampling())
-                isAnyChannelSampling = true;
         }
-
-        if (!isAnyChannelSampling)
-        {
-            // Service the puck
-            puck->drive();
-        
-            int curTimerVal = intervalTimer.read_ms();
-            if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES))
-            {
-                
-                // check each channel to see if it's been triggered
-                bool anythingTriggered = false;
-                for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
-                {
-                    if (sampleChannels[chanIdx].CheckTrigger())
-                    {
-                        anythingTriggered = true;
-                        LOG_INFO("Triggered\n");
-                        break;
-                    }
-                }
-                if(anythingTriggered)
-                {
-                    for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++)
-                    {
-                        sampleChannels[chanIdx].StartSampling();
-                    }
-                    // Set timer to disallow repeated readings
-                    lastTriggerTime = curTimerVal;                
-                }
-            }
-        }
-        else
-        {
-            wait_us(sampleIntervalUs);
-        }
-                
-        // Inter-sample interval
-        //while ((intervalTimer.read_us() - lastSampleTime < sampleIntervalUs) && (intervalTimer.read_us() - lastSampleTime > 0))
-        //    wait_us(100);
-//        lastSampleTime = intervalTimer.read_us();
-//        LOG_INFO("Timer %d\n", intervalTimer.read_ms());
     }
 }