Alexandru Radu
/
RD117_LCD_copy
gg
Fork of RD117_MBED by
main.cpp@5:ad4ccdc49727, 2017-05-02 (annotated)
- Committer:
- radualex
- Date:
- Tue May 02 08:45:41 2017 +0000
- Revision:
- 5:ad4ccdc49727
- Parent:
- 4:5273ab1085ab
eeti
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MaximGordon | 0:346a7fa07998 | 1 | /** \file main.cpp ****************************************************** |
MaximGordon | 0:346a7fa07998 | 2 | * |
MaximGordon | 0:346a7fa07998 | 3 | * Project: MAXREFDES117# |
MaximGordon | 0:346a7fa07998 | 4 | * Filename: main.cpp |
MaximGordon | 0:346a7fa07998 | 5 | * Description: This module contains the Main application for the MAXREFDES117 example program. |
MaximGordon | 0:346a7fa07998 | 6 | * |
MaximGordon | 0:346a7fa07998 | 7 | * |
MaximGordon | 0:346a7fa07998 | 8 | * -------------------------------------------------------------------- |
MaximGordon | 0:346a7fa07998 | 9 | * |
MaximGordon | 0:346a7fa07998 | 10 | * This code follows the following naming conventions: |
MaximGordon | 0:346a7fa07998 | 11 | * |
MaximGordon | 4:5273ab1085ab | 12 | * char ch_pmod_value |
MaximGordon | 4:5273ab1085ab | 13 | * char (array) s_pmod_s_string[16] |
MaximGordon | 4:5273ab1085ab | 14 | * float f_pmod_value |
MaximGordon | 4:5273ab1085ab | 15 | * int32_t n_pmod_value |
MaximGordon | 4:5273ab1085ab | 16 | * int32_t (array) an_pmod_value[16] |
MaximGordon | 4:5273ab1085ab | 17 | * int16_t w_pmod_value |
MaximGordon | 4:5273ab1085ab | 18 | * int16_t (array) aw_pmod_value[16] |
MaximGordon | 4:5273ab1085ab | 19 | * uint16_t uw_pmod_value |
MaximGordon | 4:5273ab1085ab | 20 | * uint16_t (array) auw_pmod_value[16] |
MaximGordon | 4:5273ab1085ab | 21 | * uint8_t uch_pmod_value |
MaximGordon | 4:5273ab1085ab | 22 | * uint8_t (array) auch_pmod_buffer[16] |
MaximGordon | 4:5273ab1085ab | 23 | * uint32_t un_pmod_value |
MaximGordon | 4:5273ab1085ab | 24 | * int32_t * pn_pmod_value |
MaximGordon | 0:346a7fa07998 | 25 | * |
MaximGordon | 0:346a7fa07998 | 26 | * ------------------------------------------------------------------------- */ |
MaximGordon | 0:346a7fa07998 | 27 | /******************************************************************************* |
MaximGordon | 4:5273ab1085ab | 28 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
MaximGordon | 0:346a7fa07998 | 29 | * |
MaximGordon | 0:346a7fa07998 | 30 | * Permission is hereby granted, free of charge, to any person obtaining a |
MaximGordon | 0:346a7fa07998 | 31 | * copy of this software and associated documentation files (the "Software"), |
MaximGordon | 0:346a7fa07998 | 32 | * to deal in the Software without restriction, including without limitation |
MaximGordon | 0:346a7fa07998 | 33 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MaximGordon | 0:346a7fa07998 | 34 | * and/or sell copies of the Software, and to permit persons to whom the |
MaximGordon | 0:346a7fa07998 | 35 | * Software is furnished to do so, subject to the following conditions: |
MaximGordon | 0:346a7fa07998 | 36 | * |
MaximGordon | 0:346a7fa07998 | 37 | * The above copyright notice and this permission notice shall be included |
MaximGordon | 0:346a7fa07998 | 38 | * in all copies or substantial portions of the Software. |
MaximGordon | 0:346a7fa07998 | 39 | * |
MaximGordon | 0:346a7fa07998 | 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
MaximGordon | 0:346a7fa07998 | 41 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
MaximGordon | 0:346a7fa07998 | 42 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
MaximGordon | 0:346a7fa07998 | 43 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
MaximGordon | 0:346a7fa07998 | 44 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
MaximGordon | 0:346a7fa07998 | 45 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
MaximGordon | 0:346a7fa07998 | 46 | * OTHER DEALINGS IN THE SOFTWARE. |
MaximGordon | 0:346a7fa07998 | 47 | * |
MaximGordon | 0:346a7fa07998 | 48 | * Except as contained in this notice, the name of Maxim Integrated |
MaximGordon | 0:346a7fa07998 | 49 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
MaximGordon | 0:346a7fa07998 | 50 | * Products, Inc. Branding Policy. |
MaximGordon | 0:346a7fa07998 | 51 | * |
MaximGordon | 0:346a7fa07998 | 52 | * The mere transfer of this software does not imply any licenses |
MaximGordon | 0:346a7fa07998 | 53 | * of trade secrets, proprietary technology, copyrights, patents, |
MaximGordon | 0:346a7fa07998 | 54 | * trademarks, maskwork rights, or any other form of intellectual |
MaximGordon | 0:346a7fa07998 | 55 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
MaximGordon | 0:346a7fa07998 | 56 | * ownership rights. |
MaximGordon | 0:346a7fa07998 | 57 | ******************************************************************************* |
MaximGordon | 0:346a7fa07998 | 58 | */ |
MaximGordon | 0:346a7fa07998 | 59 | /*!\mainpage Main Page |
MaximGordon | 0:346a7fa07998 | 60 | * |
MaximGordon | 0:346a7fa07998 | 61 | * \section intro_sec Introduction |
MaximGordon | 0:346a7fa07998 | 62 | * |
MaximGordon | 0:346a7fa07998 | 63 | * This is the code documentation for the MAXREFDES117# subsystem reference design. |
MaximGordon | 4:5273ab1085ab | 64 | * |
MaximGordon | 4:5273ab1085ab | 65 | * The Files page contains the File List page and the Globals page. |
MaximGordon | 4:5273ab1085ab | 66 | * |
MaximGordon | 4:5273ab1085ab | 67 | * The Globals page contains the Functions, Variables, and Macros sub-pages. |
MaximGordon | 0:346a7fa07998 | 68 | * |
MaximGordon | 0:346a7fa07998 | 69 | * \image html MAXREFDES117_Block_Diagram.png "MAXREFDES117# System Block Diagram" |
MaximGordon | 4:5273ab1085ab | 70 | * |
MaximGordon | 0:346a7fa07998 | 71 | * \image html MAXREFDES117_firmware_Flowchart.png "MAXREFDES117# Firmware Flowchart" |
MaximGordon | 0:346a7fa07998 | 72 | * |
MaximGordon | 0:346a7fa07998 | 73 | */ |
MaximGordon | 0:346a7fa07998 | 74 | #include "mbed.h" |
MaximGordon | 0:346a7fa07998 | 75 | #include "algorithm.h" |
MaximGordon | 0:346a7fa07998 | 76 | #include "MAX30102.h" |
radualex | 5:ad4ccdc49727 | 77 | #include "TextLCD.h" |
radualex | 5:ad4ccdc49727 | 78 | |
radualex | 5:ad4ccdc49727 | 79 | TextLCD lcd(PTC9, PTD3, PTD2, PTD0, PTD5, PTA13); // rs, e, d4-d7 |
MaximGordon | 0:346a7fa07998 | 80 | |
MaximGordon | 0:346a7fa07998 | 81 | #define MAX_BRIGHTNESS 255 |
MaximGordon | 0:346a7fa07998 | 82 | |
MaximGordon | 3:7c0fb55eb3ff | 83 | uint32_t aun_ir_buffer[500]; //IR LED sensor data |
MaximGordon | 0:346a7fa07998 | 84 | int32_t n_ir_buffer_length; //data length |
MaximGordon | 3:7c0fb55eb3ff | 85 | uint32_t aun_red_buffer[500]; //Red LED sensor data |
MaximGordon | 0:346a7fa07998 | 86 | int32_t n_sp02; //SPO2 value |
MaximGordon | 0:346a7fa07998 | 87 | int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid |
MaximGordon | 0:346a7fa07998 | 88 | int32_t n_heart_rate; //heart rate value |
MaximGordon | 0:346a7fa07998 | 89 | int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid |
MaximGordon | 0:346a7fa07998 | 90 | uint8_t uch_dummy; |
MaximGordon | 0:346a7fa07998 | 91 | |
MaximGordon | 0:346a7fa07998 | 92 | Serial pc(USBTX, USBRX); //initializes the serial port |
MaximGordon | 0:346a7fa07998 | 93 | #ifdef TARGET_KL25Z |
MaximGordon | 0:346a7fa07998 | 94 | PwmOut led(PTB18); //initializes the pwm output that connects to the on board LED |
MaximGordon | 0:346a7fa07998 | 95 | DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102 |
MaximGordon | 0:346a7fa07998 | 96 | #endif |
MaximGordon | 0:346a7fa07998 | 97 | #ifdef TARGET_K64F |
MaximGordon | 0:346a7fa07998 | 98 | DigitalIn INT(PTD1); //pin PTD1 connects to the interrupt output pin of the MAX30102 |
MaximGordon | 0:346a7fa07998 | 99 | #endif |
MaximGordon | 0:346a7fa07998 | 100 | #ifdef TARGET_MAX32600MBED |
MaximGordon | 0:346a7fa07998 | 101 | PwmOut led(LED_RED); //initializes the pwm output that connects to the on board LED |
MaximGordon | 0:346a7fa07998 | 102 | DigitalIn INT(P2_0); //pin P20 connects to the interrupt output pin of the MAX30102 |
MaximGordon | 0:346a7fa07998 | 103 | #endif |
MaximGordon | 0:346a7fa07998 | 104 | |
MaximGordon | 0:346a7fa07998 | 105 | // the setup routine runs once when you press reset: |
MaximGordon | 0:346a7fa07998 | 106 | int main() { |
MaximGordon | 0:346a7fa07998 | 107 | uint32_t un_min, un_max, un_prev_data; //variables to calculate the on-board LED brightness that reflects the heartbeats |
MaximGordon | 0:346a7fa07998 | 108 | int i; |
MaximGordon | 0:346a7fa07998 | 109 | int32_t n_brightness; |
MaximGordon | 0:346a7fa07998 | 110 | float f_temp; |
MaximGordon | 0:346a7fa07998 | 111 | |
MaximGordon | 0:346a7fa07998 | 112 | maxim_max30102_reset(); //resets the MAX30102 |
MaximGordon | 0:346a7fa07998 | 113 | // initialize serial communication at 115200 bits per second: |
MaximGordon | 0:346a7fa07998 | 114 | pc.baud(115200); |
MaximGordon | 0:346a7fa07998 | 115 | pc.format(8,SerialBase::None,1); |
MaximGordon | 0:346a7fa07998 | 116 | wait(1); |
MaximGordon | 0:346a7fa07998 | 117 | |
MaximGordon | 0:346a7fa07998 | 118 | //read and clear status register |
MaximGordon | 0:346a7fa07998 | 119 | maxim_max30102_read_reg(0,&uch_dummy); |
MaximGordon | 0:346a7fa07998 | 120 | |
MaximGordon | 0:346a7fa07998 | 121 | //wait until the user presses a key |
MaximGordon | 0:346a7fa07998 | 122 | while(pc.readable()==0) |
MaximGordon | 0:346a7fa07998 | 123 | { |
MaximGordon | 4:5273ab1085ab | 124 | pc.printf("\x1B[2J"); //clear terminal program screen |
MaximGordon | 0:346a7fa07998 | 125 | pc.printf("Press any key to start conversion\n\r"); |
radualex | 5:ad4ccdc49727 | 126 | lcd.cls(); |
radualex | 5:ad4ccdc49727 | 127 | lcd.printf(" Press any key to start\n"); |
MaximGordon | 0:346a7fa07998 | 128 | wait(1); |
radualex | 5:ad4ccdc49727 | 129 | lcd.cls(); |
MaximGordon | 0:346a7fa07998 | 130 | } |
MaximGordon | 0:346a7fa07998 | 131 | uch_dummy=getchar(); |
MaximGordon | 0:346a7fa07998 | 132 | |
MaximGordon | 0:346a7fa07998 | 133 | maxim_max30102_init(); //initializes the MAX30102 |
MaximGordon | 0:346a7fa07998 | 134 | |
MaximGordon | 0:346a7fa07998 | 135 | |
MaximGordon | 0:346a7fa07998 | 136 | n_brightness=0; |
MaximGordon | 0:346a7fa07998 | 137 | un_min=0x3FFFF; |
MaximGordon | 0:346a7fa07998 | 138 | un_max=0; |
MaximGordon | 0:346a7fa07998 | 139 | |
MaximGordon | 0:346a7fa07998 | 140 | n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps |
MaximGordon | 0:346a7fa07998 | 141 | |
MaximGordon | 0:346a7fa07998 | 142 | //read the first 500 samples, and determine the signal range |
MaximGordon | 0:346a7fa07998 | 143 | for(i=0;i<n_ir_buffer_length;i++) |
MaximGordon | 0:346a7fa07998 | 144 | { |
MaximGordon | 0:346a7fa07998 | 145 | while(INT.read()==1); //wait until the interrupt pin asserts |
MaximGordon | 0:346a7fa07998 | 146 | |
MaximGordon | 3:7c0fb55eb3ff | 147 | maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); //read from MAX30102 FIFO |
MaximGordon | 0:346a7fa07998 | 148 | |
MaximGordon | 3:7c0fb55eb3ff | 149 | if(un_min>aun_red_buffer[i]) |
MaximGordon | 3:7c0fb55eb3ff | 150 | un_min=aun_red_buffer[i]; //update signal min |
MaximGordon | 3:7c0fb55eb3ff | 151 | if(un_max<aun_red_buffer[i]) |
MaximGordon | 3:7c0fb55eb3ff | 152 | un_max=aun_red_buffer[i]; //update signal max |
MaximGordon | 0:346a7fa07998 | 153 | pc.printf("red="); |
MaximGordon | 3:7c0fb55eb3ff | 154 | pc.printf("%i", aun_red_buffer[i]); |
MaximGordon | 0:346a7fa07998 | 155 | pc.printf(", ir="); |
MaximGordon | 3:7c0fb55eb3ff | 156 | pc.printf("%i\n\r", aun_ir_buffer[i]); |
radualex | 5:ad4ccdc49727 | 157 | |
radualex | 5:ad4ccdc49727 | 158 | |
radualex | 5:ad4ccdc49727 | 159 | lcd.cls(); |
radualex | 5:ad4ccdc49727 | 160 | lcd.printf(" red=%i\n", aun_red_buffer[i]); |
radualex | 5:ad4ccdc49727 | 161 | |
radualex | 5:ad4ccdc49727 | 162 | lcd.printf(" ir=%i", aun_ir_buffer[i]); |
radualex | 5:ad4ccdc49727 | 163 | |
MaximGordon | 0:346a7fa07998 | 164 | } |
radualex | 5:ad4ccdc49727 | 165 | |
MaximGordon | 3:7c0fb55eb3ff | 166 | un_prev_data=aun_red_buffer[i]; |
MaximGordon | 0:346a7fa07998 | 167 | |
MaximGordon | 0:346a7fa07998 | 168 | |
MaximGordon | 0:346a7fa07998 | 169 | //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples) |
MaximGordon | 4:5273ab1085ab | 170 | 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); |
MaximGordon | 0:346a7fa07998 | 171 | |
MaximGordon | 0:346a7fa07998 | 172 | //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second |
MaximGordon | 0:346a7fa07998 | 173 | while(1) |
MaximGordon | 0:346a7fa07998 | 174 | { |
MaximGordon | 0:346a7fa07998 | 175 | i=0; |
MaximGordon | 0:346a7fa07998 | 176 | un_min=0x3FFFF; |
MaximGordon | 0:346a7fa07998 | 177 | un_max=0; |
MaximGordon | 0:346a7fa07998 | 178 | |
MaximGordon | 0:346a7fa07998 | 179 | //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top |
MaximGordon | 0:346a7fa07998 | 180 | for(i=100;i<500;i++) |
MaximGordon | 0:346a7fa07998 | 181 | { |
MaximGordon | 3:7c0fb55eb3ff | 182 | aun_red_buffer[i-100]=aun_red_buffer[i]; |
MaximGordon | 3:7c0fb55eb3ff | 183 | aun_ir_buffer[i-100]=aun_ir_buffer[i]; |
MaximGordon | 0:346a7fa07998 | 184 | |
MaximGordon | 0:346a7fa07998 | 185 | //update the signal min and max |
MaximGordon | 3:7c0fb55eb3ff | 186 | if(un_min>aun_red_buffer[i]) |
MaximGordon | 3:7c0fb55eb3ff | 187 | un_min=aun_red_buffer[i]; |
MaximGordon | 3:7c0fb55eb3ff | 188 | if(un_max<aun_red_buffer[i]) |
MaximGordon | 3:7c0fb55eb3ff | 189 | un_max=aun_red_buffer[i]; |
MaximGordon | 0:346a7fa07998 | 190 | } |
MaximGordon | 0:346a7fa07998 | 191 | |
MaximGordon | 0:346a7fa07998 | 192 | //take 100 sets of samples before calculating the heart rate. |
MaximGordon | 0:346a7fa07998 | 193 | for(i=400;i<500;i++) |
MaximGordon | 0:346a7fa07998 | 194 | { |
MaximGordon | 3:7c0fb55eb3ff | 195 | un_prev_data=aun_red_buffer[i-1]; |
MaximGordon | 0:346a7fa07998 | 196 | while(INT.read()==1); |
MaximGordon | 3:7c0fb55eb3ff | 197 | maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i)); |
MaximGordon | 0:346a7fa07998 | 198 | |
MaximGordon | 3:7c0fb55eb3ff | 199 | if(aun_red_buffer[i]>un_prev_data) |
MaximGordon | 0:346a7fa07998 | 200 | { |
MaximGordon | 3:7c0fb55eb3ff | 201 | f_temp=aun_red_buffer[i]-un_prev_data; |
MaximGordon | 0:346a7fa07998 | 202 | f_temp/=(un_max-un_min); |
MaximGordon | 0:346a7fa07998 | 203 | f_temp*=MAX_BRIGHTNESS; |
MaximGordon | 0:346a7fa07998 | 204 | n_brightness-=(int)f_temp; |
MaximGordon | 0:346a7fa07998 | 205 | if(n_brightness<0) |
MaximGordon | 0:346a7fa07998 | 206 | n_brightness=0; |
MaximGordon | 0:346a7fa07998 | 207 | } |
MaximGordon | 0:346a7fa07998 | 208 | else |
MaximGordon | 0:346a7fa07998 | 209 | { |
MaximGordon | 3:7c0fb55eb3ff | 210 | f_temp=un_prev_data-aun_red_buffer[i]; |
MaximGordon | 0:346a7fa07998 | 211 | f_temp/=(un_max-un_min); |
MaximGordon | 0:346a7fa07998 | 212 | f_temp*=MAX_BRIGHTNESS; |
MaximGordon | 0:346a7fa07998 | 213 | n_brightness+=(int)f_temp; |
MaximGordon | 0:346a7fa07998 | 214 | if(n_brightness>MAX_BRIGHTNESS) |
MaximGordon | 0:346a7fa07998 | 215 | n_brightness=MAX_BRIGHTNESS; |
MaximGordon | 0:346a7fa07998 | 216 | } |
MaximGordon | 0:346a7fa07998 | 217 | #if defined(TARGET_KL25Z) || defined(TARGET_MAX32600MBED) |
MaximGordon | 0:346a7fa07998 | 218 | led.write(1-(float)n_brightness/256); |
MaximGordon | 0:346a7fa07998 | 219 | #endif |
MaximGordon | 0:346a7fa07998 | 220 | //send samples and calculation result to terminal program through UART |
MaximGordon | 0:346a7fa07998 | 221 | pc.printf("red="); |
MaximGordon | 4:5273ab1085ab | 222 | pc.printf("%i", aun_red_buffer[i]); |
MaximGordon | 0:346a7fa07998 | 223 | pc.printf(", ir="); |
MaximGordon | 3:7c0fb55eb3ff | 224 | pc.printf("%i", aun_ir_buffer[i]); |
MaximGordon | 4:5273ab1085ab | 225 | pc.printf(", HR=%i, ", n_heart_rate); |
MaximGordon | 4:5273ab1085ab | 226 | pc.printf("HRvalid=%i, ", ch_hr_valid); |
MaximGordon | 4:5273ab1085ab | 227 | pc.printf("SpO2=%i, ", n_sp02); |
MaximGordon | 4:5273ab1085ab | 228 | pc.printf("SPO2Valid=%i\n\r", ch_spo2_valid); |
radualex | 5:ad4ccdc49727 | 229 | |
radualex | 5:ad4ccdc49727 | 230 | lcd.cls(); |
radualex | 5:ad4ccdc49727 | 231 | //lcd.printf("red="); |
radualex | 5:ad4ccdc49727 | 232 | //cd.printf("%i", aun_red_buffer[i]); |
radualex | 5:ad4ccdc49727 | 233 | //lcd.printf("ir="); |
radualex | 5:ad4ccdc49727 | 234 | //lcd.printf("%i\n\r", aun_ir_buffer[i]); |
radualex | 5:ad4ccdc49727 | 235 | lcd.printf("HR=%i ", n_heart_rate); |
radualex | 5:ad4ccdc49727 | 236 | lcd.printf("HRv=%i\n", ch_hr_valid); |
radualex | 5:ad4ccdc49727 | 237 | lcd.printf("SpO2=%i ", n_sp02); |
radualex | 5:ad4ccdc49727 | 238 | lcd.printf("SP2V=%i", ch_spo2_valid); |
radualex | 5:ad4ccdc49727 | 239 | |
MaximGordon | 0:346a7fa07998 | 240 | } |
MaximGordon | 4:5273ab1085ab | 241 | 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); |
MaximGordon | 0:346a7fa07998 | 242 | } |
MaximGordon | 0:346a7fa07998 | 243 | } |
MaximGordon | 0:346a7fa07998 | 244 |