uint32_t aun_ir_buffer[500]; //infrared LED sensor data
uint32_t aun_red_buffer[500]; //red LED sensor data
int32_t n_ir_buffer_length; //data length
float n_spo2; //SPO2 value
int8_t ch_spo2_valid; //indicator to show if the SPO2 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;
}
int main(void)
{
delay_init(); //延時(shí)函數(shù)初始化
NVIC_Configuration();
USART1_DMA_Init(115200); //PA9:TX PA10:RX 接WIFI模塊 與手機(jī)相連發(fā)送狀態(tài)數(shù)據(jù)
gpio_init(); //實(shí)際是 max30102的中斷線
bsp_InitI2C();
maxim_max30102_reset(); //resets the MAX30102
maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_dummy); //Reads/clears the interrupt status register
maxim_max30102_init(); //initialize the MAX30102
while(1)
{
loop();
}
}
// the loop routine runs over and over again forever:
void loop(void)
{
uint32_t un_min, un_max, un_prev_data, un_brightness; //variables to calculate the on-board LED brightness that reflects the heartbeats
int32_t i;
float f_temp;
un_brightness = 0;
un_min = 0x3FFFF;
un_max = 0;
n_ir_buffer_length = 150; //buffer length of 150 stores 3 seconds of samples running at 50sps 150的緩沖區(qū)長度存儲(chǔ)了以50sps運(yùn)行的3秒的樣本
//read the first 150 samples, and determine the signal range
for(i = 0; i < n_ir_buffer_length; i++)
{
while(KEY0 == 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 150 samples (first 3 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
//Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second 連續(xù)從MAX30102取樣。心率和SpO2每1秒計(jì)算一次
while(1)
{
i = 0;
un_min = 0x3FFFF;
un_max = 0;
//update the signal min and max 更新信號(hào)的最小值和最大值
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];
}
// Throw out up to 1 out of every 5 valid samples if wacky 每5個(gè)有效樣本中就有1個(gè)是古怪的
if (hrValidCnt == 4)
{
hrThrowOutSamp = 1;
hrValidCnt = 0;
for (i = 12; i < 16; i++)
{
if (n_heart_rate < hr_buf[ i] + 10)
{
hrThrowOutSamp = 0;
hrValidCnt = 4;
}
}
}
else
{
hrValidCnt = hrValidCnt + 1;
}
if (hrThrowOutSamp == 0)
{
// Shift New Sample into buffer
for(i = 0; i < 15; i++)
{
hr_buf[ i] = hr_buf[i + 1];
}
hr_buf[15] = n_heart_rate;
// Update buffer fill value
if (hrBuffFilled < 16)
{
hrBuffFilled = hrBuffFilled + 1;
}
// Throw out up to 1 out of every 5 valid samples if wacky
if (spo2ValidCnt == 4)
{
spo2ThrowOutSamp = 1;
spo2ValidCnt = 0;
for (i = 12; i < 16; i++)
{
if (n_spo2 > spo2_buf[ i] - 10)
{
spo2ThrowOutSamp = 0;
spo2ValidCnt = 4;
}
}
}
else
{
spo2ValidCnt = spo2ValidCnt + 1;
}
if (spo2ThrowOutSamp == 0)
{
// Shift New Sample into buffer
for(i = 0; i < 15; i++)
{
spo2_buf[ i] = spo2_buf[i + 1];
}
spo2_buf[15] = n_spo2;
// Update buffer fill value
if (spo2BuffFilled < 16)
{
spo2BuffFilled = spo2BuffFilled + 1;
}