I've got some basic filter code setup (but not yet tested).

Dependencies:   BLE_API Queue mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

sampler.cpp

Committer:
roysandberg
Date:
2015-05-17
Revision:
61:1de72bdab0ef
Child:
62:8e2fbe131b53

File content as of revision 61:1de72bdab0ef:

#include "mbed.h"

// Use a ticker (interrupt routine) to sample the ADC at a fast rate (is 200Khz the max?), and generate an average value
// Use another ticker to grab the average value at 1Khz, which is the maximum useful sampling rate for HRV data collection


// These need to divide evenly into each other
#define SAMPLING_RATE_HZ 4000   
#define READ_OUT_RATE_HZ 50  

#define SAMPLES_PER_READOUT (SAMPLING_RATE_HZ/READ_OUT_RATE_HZ)

AnalogIn ECG(P0_1);  
Ticker sampling_rate;
int NumReadings=0;
uint32_t ReadingTotal=0;
void (*SamplingFunction)(uint32_t);


void ADC_read()
{    
    NumReadings++;
    ReadingTotal += ECG.read_u16();    
    if (NumReadings == SAMPLES_PER_READOUT) 
    {
        SamplingFunction(ReadingTotal/NumReadings);
        ReadingTotal = 0;
        NumReadings = 0;
    }                                                                                                 
}

void setup_sampler(void (*function)(uint32_t)) 
{
    SamplingFunction = function;
    sampling_rate.attach(&ADC_read, 1.0/SAMPLING_RATE_HZ);
}