Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLEHeartRateMon by
Diff: source/main.cpp
- Revision:
- 49:5be03f287022
- Parent:
- 47:8ae30057edc0
--- a/source/main.cpp Tue Dec 05 20:11:10 2017 +0000
+++ b/source/main.cpp Mon Dec 11 16:22:50 2017 +0000
@@ -36,8 +36,13 @@
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;
+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;
+uint8_t hrmCounter;
-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
@@ -46,8 +51,6 @@
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);
@@ -57,28 +60,81 @@
BLE::Instance().gap().startAdvertising(); // restart advertising
}
+void checkHR(){
+
+ 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
+
+
+ }
+ 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);
+}
+
void updateSensorValue() {
// 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;
- }
+
+ checkHR();
+
+ hrmCounter = n_heart_rate;
+
hrServicePtr->updateHeartRate(hrmCounter);
}
void periodicCallback(void)
{
- led2 = !led2; /* Do blinky on LED2 while we're waiting for BLE events */
if (BLE::Instance().getGapState().connected) {
eventQueue.call(updateSensorValue);
}
}
+
+
void onBleInitError(BLE &ble, ble_error_t error)
{
(void)ble;
@@ -137,22 +193,6 @@
int main()
{
-
- //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
@@ -160,8 +200,6 @@
maxim_max30102_read_reg(0,&uch_dummy);
- //uch_dummy=getchar();
-
maxim_max30102_init(); //initializes the MAX30102
n_brightness=0;
@@ -189,71 +227,17 @@
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
-
-
- }
- 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();
+
+
}
}
