MAXREFDES117# Code Documentation  V01.00
Heart Rate / SpO2 Monitor
 All Files Functions Variables Macros Pages
main.cpp
Go to the documentation of this file.
1 
27 /*******************************************************************************
28 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included
38 * in all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
41 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
44 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 * OTHER DEALINGS IN THE SOFTWARE.
47 *
48 * Except as contained in this notice, the name of Maxim Integrated
49 * Products, Inc. shall not be used except as stated in the Maxim Integrated
50 * Products, Inc. Branding Policy.
51 *
52 * The mere transfer of this software does not imply any licenses
53 * of trade secrets, proprietary technology, copyrights, patents,
54 * trademarks, maskwork rights, or any other form of intellectual
55 * property whatsoever. Maxim Integrated Products, Inc. retains all
56 * ownership rights.
57 *******************************************************************************
58 */
74 #include "mbed.h"
75 #include "algorithm.h"
76 #include "MAX30102.h"
77 
78 #define MAX_BRIGHTNESS 255
79 
80 uint32_t aun_ir_buffer[500]; //IR LED sensor data
81 int32_t n_ir_buffer_length; //data length
82 uint32_t aun_red_buffer[500]; //Red LED sensor data
83 int32_t n_sp02; //SPO2 value
84 int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
85 int32_t n_heart_rate; //heart rate value
86 int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
87 uint8_t uch_dummy;
88 
89 Serial pc(USBTX, USBRX); //initializes the serial port
90 #ifdef TARGET_KL25Z
91 PwmOut led(PTB18); //initializes the pwm output that connects to the on board LED
92 DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102
93 #endif
94 #ifdef TARGET_K64F
95 DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102
96 #endif
97 #ifdef TARGET_MAX32600MBED
98 PwmOut led(LED_RED); //initializes the pwm output that connects to the on board LED
99 DigitalIn INT(P2_0); //pin P20 connects to the interrupt output pin of the MAX30102
100 #endif
101 
102 // the setup routine runs once when you press reset:
103 int main() {
104  uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats
105  int i;
106  int32_t n_brightness;
107  float f_temp;
108 
109  maxim_max30102_reset(); //resets the MAX30102
110  // initialize serial communication at 115200 bits per second:
111  pc.baud(115200);
112  pc.format(8,SerialBase::None,1);
113  wait(1);
114 
115  //read and clear status register
117 
118  //wait until the user presses a key
119  while(pc.readable()==0)
120  {
121  pc.printf("\x1B[2J"); //clear terminal program screen
122  pc.printf("Press any key to start conversion\n\r");
123  wait(1);
124  }
125  uch_dummy=getchar();
126 
127  maxim_max30102_init(); //initializes the MAX30102
128 
129 
130  n_brightness=0;
131  un_min=0x3FFFF;
132  un_max=0;
133 
134  n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
135 
136  //read the first 500 samples, and determine the signal range
137  for(i=0;i<n_ir_buffer_length;i++)
138  {
139  while(INT.read()==1); //wait until the interrupt pin asserts
140 
141  maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO
142 
143  if(un_min>aun_red_buffer[i])
144  un_min=aun_red_buffer[i]; //update signal min
145  if(un_max<aun_red_buffer[i])
146  un_max=aun_red_buffer[i]; //update signal max
147  pc.printf("red=");
148  pc.printf("%i", aun_red_buffer[i]);
149  pc.printf(", ir=");
150  pc.printf("%i\n\r", aun_ir_buffer[i]);
151  }
152  un_prev_data=aun_red_buffer[i];
153 
154 
155  //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
157 
158  //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
159  while(1)
160  {
161  i=0;
162  un_min=0x3FFFF;
163  un_max=0;
164 
165  //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
166  for(i=100;i<500;i++)
167  {
168  aun_red_buffer[i-100]=aun_red_buffer[i];
169  aun_ir_buffer[i-100]=aun_ir_buffer[i];
170 
171  //update the signal min and max
172  if(un_min>aun_red_buffer[i])
173  un_min=aun_red_buffer[i];
174  if(un_max<aun_red_buffer[i])
175  un_max=aun_red_buffer[i];
176  }
177 
178  //take 100 sets of samples before calculating the heart rate.
179  for(i=400;i<500;i++)
180  {
181  un_prev_data=aun_red_buffer[i-1];
182  while(INT.read()==1);
184 
185  if(aun_red_buffer[i]>un_prev_data)
186  {
187  f_temp=aun_red_buffer[i]-un_prev_data;
188  f_temp/=(un_max-un_min);
189  f_temp*=MAX_BRIGHTNESS;
190  n_brightness-=(int)f_temp;
191  if(n_brightness<0)
192  n_brightness=0;
193  }
194  else
195  {
196  f_temp=un_prev_data-aun_red_buffer[i];
197  f_temp/=(un_max-un_min);
198  f_temp*=MAX_BRIGHTNESS;
199  n_brightness+=(int)f_temp;
200  if(n_brightness>MAX_BRIGHTNESS)
201  n_brightness=MAX_BRIGHTNESS;
202  }
203 #if defined(TARGET_KL25Z) || defined(TARGET_MAX32600MBED)
204  led.write(1-(float)n_brightness/256);
205 #endif
206  //send samples and calculation result to terminal program through UART
207  pc.printf("red=");
208  pc.printf("%i", aun_red_buffer[i]);
209  pc.printf(", ir=");
210  pc.printf("%i", aun_ir_buffer[i]);
211  pc.printf(", HR=%i, ", n_heart_rate);
212  pc.printf("HRvalid=%i, ", ch_hr_valid);
213  pc.printf("SpO2=%i, ", n_sp02);
214  pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid);
215  }
217  }
218 }
219