SpO2 algorithm used in MAX30101 onboard MAX32620HSP. Algorithm is not perfect but it works
Dependencies: mbed MAX14720 USBDevice
main.cpp
00001 #include "mbed.h" 00002 #include "MAX14720.h" 00003 #include "MAX30102.h" 00004 #include "USBSerial.h" 00005 #include "System.h" 00006 #include "algorithm.h" 00007 00008 /// define the HVOUT Boost Voltage default for the MAX14720 PMIC 00009 #define HVOUT_VOLTAGE 4500 // set to 4500 mV 00010 00011 #define MAX14720_I2C_SLAVE_ADDR (0x54) 00012 00013 #define MAX_BRIGHTNESS 255 00014 00015 uint32_t aun_ir_buffer[500]; //IR LED sensor data 00016 int32_t n_ir_buffer_length; //data length 00017 uint32_t aun_red_buffer[500]; //Red LED sensor data 00018 int32_t n_sp02; //SPO2 value 00019 int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid 00020 int32_t n_heart_rate; //heart rate value 00021 int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid 00022 uint8_t uch_dummy; 00023 00024 //Serial pc(USBTX, USBRX); //initializes the serial port 00025 /// Define with Maxim VID and a Maxim assigned PID, set to version 0x0001 and non-blocking 00026 00027 USBSerial usbSerial(0x0b6a, 0x0122, 0x0001, false); 00028 00029 /// I2C Master 2 00030 I2C i2c2(I2C2_SDA, I2C2_SCL); // used by MAX14720, MAX30101, LIS2DH 00031 /// SPI Master 0 with SPI0_SS for use with MAX30001 00032 00033 MAX14720 max14720(&i2c2, MAX14720_I2C_SLAVE_ADDR); 00034 DigitalOut led(LED1); 00035 DigitalIn INT(P4_0); 00036 int main(){ 00037 //--------------------------------------------------- 00038 // hold results for returning functions 00039 int result; 00040 // initialize HVOUT on the MAX14720 PMIC 00041 result = max14720.init(); 00042 if (result == MAX14720_ERROR){ 00043 printf("Error initializing MAX14720"); 00044 } 00045 max14720.boostEn = MAX14720::BOOST_ENABLED; 00046 max14720.boostSetVoltage(HVOUT_VOLTAGE); 00047 //--------------------------------------------------- 00048 uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats 00049 int i; 00050 int32_t n_brightness; 00051 float f_temp; 00052 long unBlockedValue = 0; 00053 00054 maxim_max30102_reset(); //resets the MAX30102 00055 wait(1); 00056 maxim_max30102_init(); //initializes the MAX30102 00057 00058 n_brightness=0; 00059 un_min=0x3FFFF; 00060 un_max=0; 00061 00062 n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps 00063 00064 //read the first 500 samples, and determine the signal range 00065 for(i=0;i<n_ir_buffer_length;i++) 00066 { 00067 while(INT.read()==1); //wait until the interrupt pin asserts 00068 00069 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO 00070 00071 if(un_min>aun_red_buffer[i]) 00072 un_min=aun_red_buffer[i]; //update signal min 00073 if(un_max<aun_red_buffer[i]) 00074 un_max=aun_red_buffer[i]; //update signal max 00075 /*usbSerial.printf("red="); 00076 usbSerial.printf("%i", aun_red_buffer[i]); 00077 usbSerial.printf(", ir="); 00078 usbSerial.printf("%i\n\r", aun_ir_buffer[i]);*/ 00079 } 00080 un_prev_data=aun_red_buffer[i]; 00081 00082 00083 //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples) 00084 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); 00085 00086 //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second 00087 00088 00089 while (1) { 00090 i=0; 00091 un_min=0x3FFFF; 00092 un_max=0; 00093 00094 //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top 00095 for(i=200;i<500;i++) 00096 { 00097 aun_red_buffer[i-200]=aun_red_buffer[i]; 00098 aun_ir_buffer[i-200]=aun_ir_buffer[i]; 00099 00100 //update the signal min and max 00101 if(un_min>aun_red_buffer[i]) 00102 un_min=aun_red_buffer[i]; 00103 if(un_max<aun_red_buffer[i]) 00104 un_max=aun_red_buffer[i]; 00105 } 00106 00107 //take 100 sets of samples before calculating the heart rate. 00108 for(i=300;i<500;i++) 00109 { 00110 un_prev_data=aun_red_buffer[i-1]; 00111 while(INT.read()==1); 00112 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); 00113 00114 if(aun_red_buffer[i]>un_prev_data) 00115 { 00116 f_temp=aun_red_buffer[i]-un_prev_data; 00117 f_temp/=(un_max-un_min); 00118 f_temp*=MAX_BRIGHTNESS; 00119 n_brightness-=(int)f_temp; 00120 if(n_brightness<0) 00121 n_brightness=0; 00122 } 00123 else 00124 { 00125 f_temp=un_prev_data-aun_red_buffer[i]; 00126 f_temp/=(un_max-un_min); 00127 f_temp*=MAX_BRIGHTNESS; 00128 n_brightness+=(int)f_temp; 00129 if(n_brightness>MAX_BRIGHTNESS) 00130 n_brightness=MAX_BRIGHTNESS; 00131 } 00132 //send samples and calculation result to terminal program through UART 00133 /*usbSerial.printf("red="); 00134 usbSerial.printf("%i", aun_red_buffer[i]); 00135 usbSerial.printf(", ir="); 00136 usbSerial.printf("%i", aun_ir_buffer[i]);*/ 00137 usbSerial.printf("HR=%i\t", n_heart_rate); 00138 //usbSerial.printf("HRvalid=%i, ", ch_hr_valid); 00139 usbSerial.printf("SpO2=%i\n\r", n_sp02); 00140 //usbSerial.printf("SPO2Valid=%i\n\r", ch_spo2_valid); 00141 } 00142 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); 00143 } 00144 }
Generated on Mon Jul 18 2022 08:49:57 by
1.7.2