Bluetooth Heart Rate Monitor using Maxim MAX32630FTHR & MAXREFDES117
Fork of mbed-os-example-ble-HeartRate by
Diff: source/main.cpp
- Revision:
- 47:8ae30057edc0
- Parent:
- 43:fb2855f7754b
- Child:
- 49:5be03f287022
diff -r 7d2e7f4a8e22 -r 8ae30057edc0 source/main.cpp --- a/source/main.cpp Thu Nov 23 14:00:21 2017 +0000 +++ b/source/main.cpp Tue Dec 05 20:11:10 2017 +0000 @@ -19,13 +19,35 @@ #include "ble/BLE.h" #include "ble/Gap.h" #include "ble/services/HeartRateService.h" +#include "algorithm.h" +#include "MAX30102.h" -DigitalOut led1(LED1, 1); +#define MAX_BRIGHTNESS 255 + + + +DigitalOut led2(LED2, 1); + +uint32_t aun_ir_buffer[500]; //IR LED sensor data +int32_t n_ir_buffer_length; //data length +uint32_t aun_red_buffer[500]; //Red LED sensor data +int32_t n_sp02; //SPO2 value +int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid +int32_t n_heart_rate; //heart rate value +int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid +uint8_t uch_dummy; + +Serial pc(USBTX, USBRX); //initializes the serial port +#ifdef TARGET_MAX32630FTHR +PwmOut led1(LED_RED); //initializes the pwm output that connects to the on board LED +DigitalIn INT(P3_0); //pin P30 connects to the interrupt output pin of the MAX30102 +#endif const static char DEVICE_NAME[] = "HRM"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; static uint8_t hrmCounter = 100; // init HRM to 100bps +//static uint8_t hrmCounter = n_heart_rate; static HeartRateService *hrServicePtr; static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); @@ -39,7 +61,7 @@ // Do blocking calls or whatever is necessary for sensor polling. // In our case, we simply update the HRM measurement. hrmCounter++; - + // 100 <= HRM bps <=175 if (hrmCounter == 175) { hrmCounter = 100; @@ -50,7 +72,7 @@ void periodicCallback(void) { - led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ + led2 = !led2; /* Do blinky on LED2 while we're waiting for BLE events */ if (BLE::Instance().getGapState().connected) { eventQueue.call(updateSensorValue); @@ -115,13 +137,123 @@ int main() { - eventQueue.call_every(500, periodicCallback); + + //eventQueue.call_every(500, periodicCallback); + + //BLE &ble = BLE::Instance(); + //ble.onEventsToProcess(scheduleBleEventsProcessing); + //ble.init(bleInitComplete); + + //eventQueue.dispatch_forever(); + + uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats + int i; + int32_t n_brightness; + float f_temp; + + + + maxim_max30102_reset(); //resets the MAX30102 - BLE &ble = BLE::Instance(); - ble.onEventsToProcess(scheduleBleEventsProcessing); - ble.init(bleInitComplete); + + //read and clear status register + maxim_max30102_read_reg(0,&uch_dummy); + + + //uch_dummy=getchar(); + + maxim_max30102_init(); //initializes the MAX30102 + + n_brightness=0; + un_min=0x3FFFF; + un_max=0; + + n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps + + //read the first 500 samples, and determine the signal range + for(i=0;i<n_ir_buffer_length;i++) + { + while(INT.read()==1); //wait until the interrupt pin asserts + + maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO + + if(un_min>aun_red_buffer[i]) + un_min=aun_red_buffer[i]; //update signal min + if(un_max<aun_red_buffer[i]) + un_max=aun_red_buffer[i]; //update signal max + + } + un_prev_data=aun_red_buffer[i]; + + //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples) + maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); + + + + //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second + while(1) + { + + eventQueue.call_every(500, periodicCallback); + + //BLE &ble = BLE::Instance(); + //ble.onEventsToProcess(scheduleBleEventsProcessing); + //ble.init(bleInitComplete); + + i=0; + un_min=0x3FFFF; + un_max=0; + + //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top + for(i=100;i<500;i++) + { + aun_red_buffer[i-100]=aun_red_buffer[i]; + aun_ir_buffer[i-100]=aun_ir_buffer[i]; + + //update the signal min and max + if(un_min>aun_red_buffer[i]) + un_min=aun_red_buffer[i]; + if(un_max<aun_red_buffer[i]) + un_max=aun_red_buffer[i]; + } + + //take 100 sets of samples before calculating the heart rate. + for(i=400;i<500;i++) + { + un_prev_data=aun_red_buffer[i-1]; + while(INT.read()==1); + maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); + + if(aun_red_buffer[i]>un_prev_data) + { + f_temp=aun_red_buffer[i]-un_prev_data; + f_temp/=(un_max-un_min); + f_temp*=MAX_BRIGHTNESS; + n_brightness-=(int)f_temp; + if(n_brightness<0) + n_brightness=0; + } + else + { + f_temp=un_prev_data-aun_red_buffer[i]; + f_temp/=(un_max-un_min); + f_temp*=MAX_BRIGHTNESS; + n_brightness+=(int)f_temp; + if(n_brightness>MAX_BRIGHTNESS) + n_brightness=MAX_BRIGHTNESS; + } +#if defined(TARGET_KL25Z) || defined(TARGET_MAX32630FTHR) + led1.write(1-(float)n_brightness/256); +#endif - eventQueue.dispatch_forever(); - return 0; + } + maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); + + BLE &ble = BLE::Instance(); + ble.onEventsToProcess(scheduleBleEventsProcessing); + ble.init(bleInitComplete); + + eventQueue.dispatch_forever(); + } }