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 RD117_MBED by
main.cpp
00001 /** \file main.cpp ****************************************************** 00002 * 00003 * Project: MAXREFDES117# 00004 * Filename: main.cpp 00005 * Description: This module contains the Main application for the MAXREFDES117 example program. 00006 * 00007 * 00008 * -------------------------------------------------------------------- 00009 * 00010 * This code follows the following naming conventions: 00011 * 00012 * char ch_pmod_value 00013 * char (array) s_pmod_s_string[16] 00014 * float f_pmod_value 00015 * int32_t n_pmod_value 00016 * int32_t (array) an_pmod_value[16] 00017 * int16_t w_pmod_value 00018 * int16_t (array) aw_pmod_value[16] 00019 * uint16_t uw_pmod_value 00020 * uint16_t (array) auw_pmod_value[16] 00021 * uint8_t uch_pmod_value 00022 * uint8_t (array) auch_pmod_buffer[16] 00023 * uint32_t un_pmod_value 00024 * int32_t * pn_pmod_value 00025 * 00026 * ------------------------------------------------------------------------- */ 00027 /******************************************************************************* 00028 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00029 * 00030 * Permission is hereby granted, free of charge, to any person obtaining a 00031 * copy of this software and associated documentation files (the "Software"), 00032 * to deal in the Software without restriction, including without limitation 00033 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00034 * and/or sell copies of the Software, and to permit persons to whom the 00035 * Software is furnished to do so, subject to the following conditions: 00036 * 00037 * The above copyright notice and this permission notice shall be included 00038 * in all copies or substantial portions of the Software. 00039 * 00040 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00041 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00042 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00043 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00044 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00045 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00046 * OTHER DEALINGS IN THE SOFTWARE. 00047 * 00048 * Except as contained in this notice, the name of Maxim Integrated 00049 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00050 * Products, Inc. Branding Policy. 00051 * 00052 * The mere transfer of this software does not imply any licenses 00053 * of trade secrets, proprietary technology, copyrights, patents, 00054 * trademarks, maskwork rights, or any other form of intellectual 00055 * property whatsoever. Maxim Integrated Products, Inc. retains all 00056 * ownership rights. 00057 ******************************************************************************* 00058 */ 00059 /*!\mainpage Main Page 00060 * 00061 * \section intro_sec Introduction 00062 * 00063 * This is the code documentation for the MAXREFDES117# subsystem reference design. 00064 * 00065 * The Files page contains the File List page and the Globals page. 00066 * 00067 * The Globals page contains the Functions, Variables, and Macros sub-pages. 00068 * 00069 * \image html MAXREFDES117_Block_Diagram.png "MAXREFDES117# System Block Diagram" 00070 * 00071 * \image html MAXREFDES117_firmware_Flowchart.png "MAXREFDES117# Firmware Flowchart" 00072 * 00073 */ 00074 #include "mbed.h" 00075 #include "algorithm.h" 00076 #include "MAX30102.h" 00077 #include "TextLCD.h" 00078 00079 TextLCD lcd(PTC9, PTD3, PTD2, PTD0, PTD5, PTA13); // rs, e, d4-d7 00080 00081 #define MAX_BRIGHTNESS 255 00082 00083 uint32_t aun_ir_buffer[500]; //IR LED sensor data 00084 int32_t n_ir_buffer_length; //data length 00085 uint32_t aun_red_buffer[500]; //Red LED sensor data 00086 int32_t n_sp02; //SPO2 value 00087 int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid 00088 int32_t n_heart_rate; //heart rate value 00089 int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid 00090 uint8_t uch_dummy; 00091 00092 Serial pc(USBTX, USBRX); //initializes the serial port 00093 #ifdef TARGET_KL25Z 00094 PwmOut led(PTB18); //initializes the pwm output that connects to the on board LED 00095 DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102 00096 #endif 00097 #ifdef TARGET_K64F 00098 DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102 00099 #endif 00100 #ifdef TARGET_MAX32600MBED 00101 PwmOut led(LED_RED); //initializes the pwm output that connects to the on board LED 00102 DigitalIn INT(P2_0); //pin P20 connects to the interrupt output pin of the MAX30102 00103 #endif 00104 00105 // the setup routine runs once when you press reset: 00106 int main() { 00107 uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats 00108 int i; 00109 int32_t n_brightness; 00110 float f_temp; 00111 00112 maxim_max30102_reset(); //resets the MAX30102 00113 // initialize serial communication at 115200 bits per second: 00114 pc.baud(115200); 00115 pc.format(8,SerialBase::None,1); 00116 wait(1); 00117 00118 //read and clear status register 00119 maxim_max30102_read_reg(0,&uch_dummy); 00120 00121 //wait until the user presses a key 00122 while(pc.readable()==0) 00123 { 00124 pc.printf("\x1B[2J"); //clear terminal program screen 00125 pc.printf("Press any key to start conversion\n\r"); 00126 lcd.cls(); 00127 lcd.printf(" Press any key to start\n"); 00128 wait(1); 00129 lcd.cls(); 00130 } 00131 uch_dummy=getchar(); 00132 00133 maxim_max30102_init(); //initializes the MAX30102 00134 00135 00136 n_brightness=0; 00137 un_min=0x3FFFF; 00138 un_max=0; 00139 00140 n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps 00141 00142 //read the first 500 samples, and determine the signal range 00143 for(i=0;i<n_ir_buffer_length;i++) 00144 { 00145 while(INT.read()==1); //wait until the interrupt pin asserts 00146 00147 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO 00148 00149 if(un_min>aun_red_buffer[i]) 00150 un_min=aun_red_buffer[i]; //update signal min 00151 if(un_max<aun_red_buffer[i]) 00152 un_max=aun_red_buffer[i]; //update signal max 00153 pc.printf("red="); 00154 pc.printf("%i", aun_red_buffer[i]); 00155 pc.printf(", ir="); 00156 pc.printf("%i\n\r", aun_ir_buffer[i]); 00157 00158 00159 lcd.cls(); 00160 lcd.printf(" red=%i\n", aun_red_buffer[i]); 00161 00162 lcd.printf(" ir=%i", aun_ir_buffer[i]); 00163 00164 } 00165 00166 un_prev_data=aun_red_buffer[i]; 00167 00168 00169 //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples) 00170 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); 00171 00172 //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second 00173 while(1) 00174 { 00175 i=0; 00176 un_min=0x3FFFF; 00177 un_max=0; 00178 00179 //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top 00180 for(i=100;i<500;i++) 00181 { 00182 aun_red_buffer[i-100]=aun_red_buffer[i]; 00183 aun_ir_buffer[i-100]=aun_ir_buffer[i]; 00184 00185 //update the signal min and max 00186 if(un_min>aun_red_buffer[i]) 00187 un_min=aun_red_buffer[i]; 00188 if(un_max<aun_red_buffer[i]) 00189 un_max=aun_red_buffer[i]; 00190 } 00191 00192 //take 100 sets of samples before calculating the heart rate. 00193 for(i=400;i<500;i++) 00194 { 00195 un_prev_data=aun_red_buffer[i-1]; 00196 while(INT.read()==1); 00197 maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); 00198 00199 if(aun_red_buffer[i]>un_prev_data) 00200 { 00201 f_temp=aun_red_buffer[i]-un_prev_data; 00202 f_temp/=(un_max-un_min); 00203 f_temp*=MAX_BRIGHTNESS; 00204 n_brightness-=(int)f_temp; 00205 if(n_brightness<0) 00206 n_brightness=0; 00207 } 00208 else 00209 { 00210 f_temp=un_prev_data-aun_red_buffer[i]; 00211 f_temp/=(un_max-un_min); 00212 f_temp*=MAX_BRIGHTNESS; 00213 n_brightness+=(int)f_temp; 00214 if(n_brightness>MAX_BRIGHTNESS) 00215 n_brightness=MAX_BRIGHTNESS; 00216 } 00217 #if defined(TARGET_KL25Z) || defined(TARGET_MAX32600MBED) 00218 led.write(1-(float)n_brightness/256); 00219 #endif 00220 //send samples and calculation result to terminal program through UART 00221 pc.printf("red="); 00222 pc.printf("%i", aun_red_buffer[i]); 00223 pc.printf(", ir="); 00224 pc.printf("%i", aun_ir_buffer[i]); 00225 pc.printf(", HR=%i, ", n_heart_rate); 00226 pc.printf("HRvalid=%i, ", ch_hr_valid); 00227 pc.printf("SpO2=%i, ", n_sp02); 00228 pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid); 00229 00230 lcd.cls(); 00231 //lcd.printf("red="); 00232 //cd.printf("%i", aun_red_buffer[i]); 00233 //lcd.printf("ir="); 00234 //lcd.printf("%i\n\r", aun_ir_buffer[i]); 00235 lcd.printf("HR=%i ", n_heart_rate); 00236 lcd.printf("HRv=%i\n", ch_hr_valid); 00237 lcd.printf("SpO2=%i ", n_sp02); 00238 lcd.printf("SP2V=%i", ch_spo2_valid); 00239 00240 } 00241 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); 00242 } 00243 } 00244
Generated on Wed Jul 13 2022 11:29:51 by
