IDT Optical Sensors / Mbed 2 deprecated OB1203_IDT

Dependencies:   mbed OB1203_example_driver

Committer:
laserdad
Date:
Thu Oct 18 18:27:33 2018 +0000
Revision:
20:fcf071379cba
Parent:
19:6e584392d4a7
Child:
21:97a0d4167902
updated mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
laserdad 0:e9ae48b7f6f6 1 #include "mbed.h"
laserdad 0:e9ae48b7f6f6 2 #include "OB1203.h"
laserdad 0:e9ae48b7f6f6 3 #include "math.h"
laserdad 19:6e584392d4a7 4
laserdad 0:e9ae48b7f6f6 5
laserdad 14:ae0be10b73cd 6
laserdad 14:ae0be10b73cd 7 //normal
laserdad 14:ae0be10b73cd 8 I2C i2c(I2C_SDA,I2C_SCL); //instantiate an i2c object from its class
laserdad 0:e9ae48b7f6f6 9 #define intb_pin D3
laserdad 0:e9ae48b7f6f6 10
laserdad 14:ae0be10b73cd 11 //slave board//
laserdad 14:ae0be10b73cd 12 //I2C i2c(D12,PA_7);
laserdad 14:ae0be10b73cd 13 //#define intb_pin D10
laserdad 14:ae0be10b73cd 14
laserdad 14:ae0be10b73cd 15 InterruptIn intb(intb_pin); //declare an interrupt pin for INTB
laserdad 0:e9ae48b7f6f6 16
laserdad 14:ae0be10b73cd 17 //setting three 3 pins high to function as pullups for SDA, SCL and INTB
laserdad 14:ae0be10b73cd 18 //Connect these pins to SDA, SCL and INTB by 2.7K resistors.
laserdad 14:ae0be10b73cd 19 //5K resistors also work usually
laserdad 14:ae0be10b73cd 20 //DigitalOut sda_pullup(D10,1);
laserdad 14:ae0be10b73cd 21 //DigitalOut scl_pullup(D11,1);
laserdad 14:ae0be10b73cd 22 //DigitalOut intb_pullup(D12,1);
laserdad 13:5ce84c68066e 23
laserdad 14:ae0be10b73cd 24 OB1203 ob1203(&i2c); //instantiate the OB1203 object from its class and pass i2c object
laserdad 14:ae0be10b73cd 25 Serial pc(USBTX, USBRX,256000); //create a serial port for printing data to a pc
laserdad 20:fcf071379cba 26 //Serial pc(USBTX, USBRX,38400); //create a serial port for printing data to a pc
laserdad 14:ae0be10b73cd 27 Timer t; //use a microsecond timer for time stamping data
laserdad 0:e9ae48b7f6f6 28
laserdad 1:fb6b2b4d5d8a 29
laserdad 6:aaa2f8fb5123 30 //USER CONFIGURABLE*********
laserdad 7:3ed0f0522b43 31 bool mode = 1; //0 for PS_LS, 1 for PPG
laserdad 7:3ed0f0522b43 32 bool meas_ps = 1;
laserdad 12:5de5f550e765 33 bool spo2 = 1; //0 for HR, 1 for SpO2
laserdad 4:3aee2ca931b8 34 bool afull = 1; //use Afull interrupt--otherwise PPG new data interrupt
laserdad 14:ae0be10b73cd 35 bool meas_temp = 0;
laserdad 13:5ce84c68066e 36 bool printAvg = 0; //default 0 print raw data
laserdad 13:5ce84c68066e 37 bool redAGC = 1;
laserdad 19:6e584392d4a7 38 bool irAGC = 1;
laserdad 18:b7226b560838 39 bool trim_oscillator = 0;
laserdad 19:6e584392d4a7 40 bool printCurrent = 0;
laserdad 6:aaa2f8fb5123 41 //****************************
laserdad 6:aaa2f8fb5123 42
laserdad 6:aaa2f8fb5123 43 //internal settings
laserdad 6:aaa2f8fb5123 44 bool intFlagged =0;
laserdad 6:aaa2f8fb5123 45 int sample_delay = 25; //ms
laserdad 1:fb6b2b4d5d8a 46
laserdad 14:ae0be10b73cd 47 void defaultConfig() //populate the default settings here
laserdad 0:e9ae48b7f6f6 48 {
laserdad 14:ae0be10b73cd 49 //high accuracy oscillator trim overwrite option
laserdad 14:ae0be10b73cd 50 ob1203.osc_trim = 0x3F; //max trim code =0x3F
laserdad 14:ae0be10b73cd 51 //temperature sensor settings (hidden registers)
laserdad 4:3aee2ca931b8 52 meas_temp ? ob1203.temp_en = TEMP_ON : ob1203.temp_en = TEMP_OFF;
laserdad 14:ae0be10b73cd 53 //LS settings
laserdad 12:5de5f550e765 54 ob1203.ls_res = LS_RES(2); //2= 18bit 100ms, 0= max res
laserdad 12:5de5f550e765 55 ob1203.ls_rate = LS_RATE(2); //2 =100ms, 4 = 500ms
laserdad 4:3aee2ca931b8 56 ob1203.ls_gain = LS_GAIN(3); //gain 3 default (range)
laserdad 1:fb6b2b4d5d8a 57 ob1203.ls_thres_hi = 0x000FFFFF;
laserdad 0:e9ae48b7f6f6 58 ob1203.ls_thres_lo = 0;
laserdad 0:e9ae48b7f6f6 59 ob1203.ls_sai = LS_SAI_OFF;
laserdad 0:e9ae48b7f6f6 60 ob1203.ls_mode = RGB_MODE;
laserdad 0:e9ae48b7f6f6 61 ob1203.ls_en = LS_ON;
laserdad 14:ae0be10b73cd 62 //PS and PPG settings
laserdad 0:e9ae48b7f6f6 63 ob1203.ps_sai_en = PS_SAI_OFF;
laserdad 5:ebe305e08430 64 // ob1203.ps_sai_en = PS_SAI_ON;
laserdad 1:fb6b2b4d5d8a 65 if(mode)
laserdad 1:fb6b2b4d5d8a 66 {
laserdad 20:fcf071379cba 67 spo2 ? ob1203.ppg_ps_mode = SPO2_MODE : ob1203.ppg_ps_mode = HR_MODE;
laserdad 1:fb6b2b4d5d8a 68 }
laserdad 1:fb6b2b4d5d8a 69 else
laserdad 1:fb6b2b4d5d8a 70 {
laserdad 1:fb6b2b4d5d8a 71 ob1203.ppg_ps_mode = PS_MODE;
laserdad 1:fb6b2b4d5d8a 72 }
laserdad 12:5de5f550e765 73 ob1203.ps_pulses = PS_PULSES(3);
laserdad 1:fb6b2b4d5d8a 74 // pc.printf("ps_pulses = %02X\r\n",ob1203.ps_pulses);
laserdad 7:3ed0f0522b43 75 ob1203.ps_pwidth = PS_PWIDTH(1);
laserdad 7:3ed0f0522b43 76 ob1203.ps_rate = PS_RATE(4);
laserdad 1:fb6b2b4d5d8a 77 // pc.printf("ps_rate = %02X\r\n",ob1203.ps_rate);
laserdad 12:5de5f550e765 78 ob1203.ps_avg_en = PS_AVG_OFF;
laserdad 0:e9ae48b7f6f6 79 ob1203.ps_can_ana = PS_CAN_ANA_0;
laserdad 14:ae0be10b73cd 80 ob1203.ps_digital_can = 150;
laserdad 0:e9ae48b7f6f6 81 ob1203.ps_hys_level = 0;
laserdad 14:ae0be10b73cd 82 meas_ps ? ob1203.ps_current = 0x1FF : ob1203.ps_current = 0x000;
laserdad 4:3aee2ca931b8 83 // ob1203.ps_current = 0;
laserdad 0:e9ae48b7f6f6 84 ob1203.ps_thres_hi = 0xFF;
laserdad 0:e9ae48b7f6f6 85 ob1203.ps_thres_lo = 0x00;
laserdad 0:e9ae48b7f6f6 86
laserdad 0:e9ae48b7f6f6 87 //interrupts
laserdad 0:e9ae48b7f6f6 88 ob1203.ls_int_sel = LS_INT_SEL_W;
laserdad 0:e9ae48b7f6f6 89 ob1203.ls_var_mode = LS_THRES_INT_MODE;
laserdad 0:e9ae48b7f6f6 90 ob1203.ls_int_en = LS_INT_ON;
laserdad 0:e9ae48b7f6f6 91 ob1203.ppg_ps_en = PPG_PS_ON;
laserdad 4:3aee2ca931b8 92
laserdad 18:b7226b560838 93 mode ? ob1203.ps_logic_mode = PS_INT_READ_CLEARS : ob1203.ps_logic_mode = PS_INT_LOGIC;
laserdad 0:e9ae48b7f6f6 94 ob1203.ps_int_en = PS_INT_ON;
laserdad 0:e9ae48b7f6f6 95 ob1203.ls_persist = LS_PERSIST(2);
laserdad 0:e9ae48b7f6f6 96 ob1203.ps_persist = PS_PERSIST(2);
laserdad 11:640cced1ee5d 97
laserdad 11:640cced1ee5d 98
laserdad 11:640cced1ee5d 99 //BIO SETTINGS
laserdad 4:3aee2ca931b8 100 //int
laserdad 4:3aee2ca931b8 101 if(afull)
laserdad 4:3aee2ca931b8 102 {
laserdad 4:3aee2ca931b8 103 ob1203.afull_int_en = AFULL_INT_ON;
laserdad 4:3aee2ca931b8 104 ob1203.ppg_int_en = PPG_INT_OFF;
laserdad 4:3aee2ca931b8 105 }
laserdad 4:3aee2ca931b8 106 else
laserdad 4:3aee2ca931b8 107 {
laserdad 4:3aee2ca931b8 108 ob1203.afull_int_en = AFULL_INT_OFF;
laserdad 4:3aee2ca931b8 109 ob1203.ppg_int_en = PPG_INT_ON;
laserdad 4:3aee2ca931b8 110 }
laserdad 0:e9ae48b7f6f6 111 //PPG
laserdad 13:5ce84c68066e 112 ob1203.ir_current = 0x1AF; //max 1023. 3FF
laserdad 3:d3a1fc19bee6 113 if (spo2)
laserdad 3:d3a1fc19bee6 114 {
laserdad 4:3aee2ca931b8 115 // ob1203.r_current = 0x0FF;
laserdad 13:5ce84c68066e 116 ob1203.r_current = 0x1AF; //max 511. 1FF
laserdad 3:d3a1fc19bee6 117 }
laserdad 3:d3a1fc19bee6 118 else
laserdad 3:d3a1fc19bee6 119 {
laserdad 3:d3a1fc19bee6 120 ob1203.r_current = 0;
laserdad 3:d3a1fc19bee6 121 }
laserdad 10:7fbf298ee05f 122 ob1203.ppg_ps_gain = PPG_PS_GAIN_1;
laserdad 0:e9ae48b7f6f6 123 ob1203.ppg_pow_save = PPG_POW_SAVE_OFF;
laserdad 0:e9ae48b7f6f6 124 ob1203.led_flip = LED_FLIP_OFF;
laserdad 11:640cced1ee5d 125 ob1203.ch1_can_ana = PPG_CH1_CAN(0);
laserdad 11:640cced1ee5d 126 ob1203.ch2_can_ana = PPG_CH2_CAN(0);
laserdad 14:ae0be10b73cd 127 //use rate 1 with pulse width 3 and average 4, or rate 3 with pulse width 4 and average 3 for 100 sps (50Hz basis) or 120 sps sample rate (60Hz basis)
laserdad 14:ae0be10b73cd 128 ob1203.ppg_avg = PPG_AVG(4); //2^n averages
laserdad 14:ae0be10b73cd 129 ob1203.ppg_rate = PPG_RATE(1);
laserdad 12:5de5f550e765 130 ob1203.ppg_pwidth = PPG_PWIDTH(3);
laserdad 12:5de5f550e765 131 ob1203.ppg_freq = PPG_FREQ_50HZ;
laserdad 12:5de5f550e765 132 // ob1203.ppg_freq = PPG_FREQ_60HZ;
laserdad 14:ae0be10b73cd 133 ob1203.bio_trim = 3; //max 3 --this dims the ADC sensitivity, but reduces noise
laserdad 14:ae0be10b73cd 134 ob1203.led_trim = 0x00; //can use to overwrite trim setting and max out the current
laserdad 14:ae0be10b73cd 135 ob1203.ppg_LED_settling = PPG_LED_SETTLING(2); //hidden regstier for adjusting LED setting time (not a factor for noise)
laserdad 14:ae0be10b73cd 136 ob1203.ppg_ALC_track = PPG_ALC_TRACK(2); //hidden register for adjusting ALC track and hold time (not a factor for noise)
laserdad 14:ae0be10b73cd 137 ob1203.diff = DIFF_ON; //hidden register for turning off subtraction of residual ambient light after ALC
laserdad 14:ae0be10b73cd 138 ob1203.alc = ALC_ON; //hidden register for turning off ambient light cancelleation track and hold circuit
laserdad 14:ae0be10b73cd 139 ob1203.sig_out = SIGNAL_OUT; //hidden register for selecting ambient sample or LED sample if DIFF is off
laserdad 0:e9ae48b7f6f6 140 ob1203.fifo_rollover_en = FIFO_ROLL_ON;
laserdad 13:5ce84c68066e 141 ob1203.fifo_afull_advance_warning = AFULL_ADVANCE_WARNING(0x0F); //warn as quickly as possible (after 17 samples with 0x0F)
laserdad 13:5ce84c68066e 142
laserdad 14:ae0be10b73cd 143 //run initialization according to user compile settings
laserdad 14:ae0be10b73cd 144
laserdad 0:e9ae48b7f6f6 145 if(mode)
laserdad 0:e9ae48b7f6f6 146 {
laserdad 3:d3a1fc19bee6 147 if(spo2)
laserdad 3:d3a1fc19bee6 148 {
laserdad 3:d3a1fc19bee6 149 ob1203.init_spo2();
laserdad 3:d3a1fc19bee6 150 }
laserdad 3:d3a1fc19bee6 151 else
laserdad 3:d3a1fc19bee6 152 {
laserdad 3:d3a1fc19bee6 153 ob1203.init_hr();
laserdad 3:d3a1fc19bee6 154 }
laserdad 0:e9ae48b7f6f6 155 }
laserdad 0:e9ae48b7f6f6 156 else
laserdad 0:e9ae48b7f6f6 157 {
laserdad 5:ebe305e08430 158 meas_ps ? ob1203.init_ps_rgb() : ob1203.init_rgb();
laserdad 0:e9ae48b7f6f6 159 }
laserdad 14:ae0be10b73cd 160 if(trim_oscillator)
laserdad 14:ae0be10b73cd 161 ob1203.setOscTrim();
laserdad 14:ae0be10b73cd 162
laserdad 0:e9ae48b7f6f6 163 }
laserdad 0:e9ae48b7f6f6 164
laserdad 1:fb6b2b4d5d8a 165 void regDump(uint8_t Addr, uint8_t startByte, uint8_t endByte)
laserdad 1:fb6b2b4d5d8a 166 {
laserdad 1:fb6b2b4d5d8a 167 /*print the values of up to 20 registers--buffer limit, e.g.*/
laserdad 1:fb6b2b4d5d8a 168 char regData[20];
laserdad 1:fb6b2b4d5d8a 169 int numBytes;
laserdad 1:fb6b2b4d5d8a 170 if (endByte>=startByte) {
laserdad 1:fb6b2b4d5d8a 171 numBytes = (endByte-startByte+1) < 20 ? (endByte-startByte+1) : 20;
laserdad 1:fb6b2b4d5d8a 172 } else {
laserdad 1:fb6b2b4d5d8a 173 numBytes=1;
laserdad 1:fb6b2b4d5d8a 174 }
laserdad 1:fb6b2b4d5d8a 175
laserdad 1:fb6b2b4d5d8a 176 regData[0] = startByte;
laserdad 1:fb6b2b4d5d8a 177 i2c.write(Addr,regData,1,true);
laserdad 1:fb6b2b4d5d8a 178 i2c.read(Addr, regData, numBytes);
laserdad 1:fb6b2b4d5d8a 179 for(int n=0; n<numBytes; n++) {
laserdad 1:fb6b2b4d5d8a 180 pc.printf("%02X, %02X \r\n", startByte+n, regData[n]);
laserdad 1:fb6b2b4d5d8a 181 }
laserdad 1:fb6b2b4d5d8a 182 }
laserdad 1:fb6b2b4d5d8a 183
laserdad 0:e9ae48b7f6f6 184
laserdad 0:e9ae48b7f6f6 185 void intEvent(void)
laserdad 0:e9ae48b7f6f6 186 {
laserdad 0:e9ae48b7f6f6 187 intFlagged = 1;
laserdad 0:e9ae48b7f6f6 188 }
laserdad 0:e9ae48b7f6f6 189
laserdad 0:e9ae48b7f6f6 190 int main()
laserdad 14:ae0be10b73cd 191 /*This program is messy bacause it allows several different print options and interrupt modes
laserdad 14:ae0be10b73cd 192 Most of the variables are associated with an optional FIR filter to make data pretty.
laserdad 14:ae0be10b73cd 193 */
laserdad 0:e9ae48b7f6f6 194 {
laserdad 4:3aee2ca931b8 195 int numInts = 0;
laserdad 10:7fbf298ee05f 196 uint32_t running_avg = 10;
laserdad 10:7fbf298ee05f 197 uint32_t ps_running_avg = 100;
laserdad 7:3ed0f0522b43 198 uint32_t ps_avg_ptr = 0;
laserdad 3:d3a1fc19bee6 199 char avg_ptr = 0;
laserdad 4:3aee2ca931b8 200 int baseline_ptr = 0;
laserdad 10:7fbf298ee05f 201 uint32_t running_baseline = 240;
laserdad 3:d3a1fc19bee6 202 int first = 1;
laserdad 3:d3a1fc19bee6 203 uint32_t IRavg = 0;
laserdad 3:d3a1fc19bee6 204 uint32_t Ravg = 0;
laserdad 3:d3a1fc19bee6 205 uint32_t IRprev = 0;
laserdad 3:d3a1fc19bee6 206 uint32_t Rprev = 0;
laserdad 3:d3a1fc19bee6 207 uint32_t prevAvg = 0;
laserdad 3:d3a1fc19bee6 208 uint32_t IR_avg_buffer[running_avg];
laserdad 3:d3a1fc19bee6 209 uint32_t R_avg_buffer[running_avg];
laserdad 4:3aee2ca931b8 210 uint32_t IR_baseline_buffer[running_baseline];
laserdad 4:3aee2ca931b8 211 uint32_t R_baseline_buffer[running_baseline];
laserdad 4:3aee2ca931b8 212 uint32_t IRbaseline_prev=0;;
laserdad 4:3aee2ca931b8 213 uint32_t Rbaseline_prev=0;;
laserdad 4:3aee2ca931b8 214 uint32_t IRbaseline=0;
laserdad 4:3aee2ca931b8 215 uint32_t Rbaseline=0;
laserdad 4:3aee2ca931b8 216 uint32_t prevBaseline=0;
laserdad 7:3ed0f0522b43 217 uint32_t PSavg = 0;
laserdad 7:3ed0f0522b43 218 uint32_t PSprev = 0;
laserdad 7:3ed0f0522b43 219 uint32_t PS_avg_buffer[ps_running_avg];
laserdad 7:3ed0f0522b43 220
laserdad 13:5ce84c68066e 221 bool update = 0;
laserdad 19:6e584392d4a7 222
laserdad 14:ae0be10b73cd 223
laserdad 7:3ed0f0522b43 224 for (int n=0;n<ps_running_avg;n++)
laserdad 7:3ed0f0522b43 225 {
laserdad 7:3ed0f0522b43 226 PS_avg_buffer[n] =0;
laserdad 7:3ed0f0522b43 227 }
laserdad 7:3ed0f0522b43 228
laserdad 14:ae0be10b73cd 229 i2c.frequency( 400000 ); //always use max speed I2C
laserdad 0:e9ae48b7f6f6 230 char valid;
laserdad 14:ae0be10b73cd 231 uint32_t ps_ls_data[7]; //array for storing parsed samples
laserdad 20:fcf071379cba 232 char samples2Read = 2; //FIFO samples, e.g. 4 samples * 3 bytes = 12 bytes (or 2 SpO2 samples)
laserdad 2:ee175f471ecb 233 char fifoBuffer[samples2Read*3];
laserdad 0:e9ae48b7f6f6 234 uint32_t ppgData[samples2Read];
laserdad 0:e9ae48b7f6f6 235
laserdad 1:fb6b2b4d5d8a 236
laserdad 1:fb6b2b4d5d8a 237 pc.printf("register settings\r\n");
laserdad 1:fb6b2b4d5d8a 238 regDump(OB1203_ADDR,0,19);
laserdad 1:fb6b2b4d5d8a 239 regDump(OB1203_ADDR,20,39);
laserdad 1:fb6b2b4d5d8a 240 regDump(OB1203_ADDR,40,59);
laserdad 1:fb6b2b4d5d8a 241 regDump(OB1203_ADDR,60,77);
laserdad 1:fb6b2b4d5d8a 242
laserdad 1:fb6b2b4d5d8a 243 pc.printf("do initial config\r\n");
laserdad 14:ae0be10b73cd 244 defaultConfig(); //do the ASIC configuration now
laserdad 1:fb6b2b4d5d8a 245
laserdad 1:fb6b2b4d5d8a 246 pc.printf("print new register config\r\n");
laserdad 1:fb6b2b4d5d8a 247 regDump(OB1203_ADDR,0,19);
laserdad 1:fb6b2b4d5d8a 248 regDump(OB1203_ADDR,20,39);
laserdad 1:fb6b2b4d5d8a 249 regDump(OB1203_ADDR,40,59);
laserdad 1:fb6b2b4d5d8a 250 regDump(OB1203_ADDR,60,77);
laserdad 1:fb6b2b4d5d8a 251
laserdad 0:e9ae48b7f6f6 252
laserdad 14:ae0be10b73cd 253 intb.fall(&intEvent); //attach a falling interrupt
laserdad 14:ae0be10b73cd 254 t.start(); //start microsecond timer for datalogging
laserdad 5:ebe305e08430 255
laserdad 0:e9ae48b7f6f6 256 while(1)
laserdad 0:e9ae48b7f6f6 257 {
laserdad 14:ae0be10b73cd 258 if(mode) //PPG case*******************************************************
laserdad 0:e9ae48b7f6f6 259 {
laserdad 14:ae0be10b73cd 260 if(!intb.read()) //check the intb to see if it is low (or could check variable set by ISR here)
laserdad 2:ee175f471ecb 261 {
laserdad 14:ae0be10b73cd 262 if(!afull) //if you are using sample completion interrupts
laserdad 4:3aee2ca931b8 263 {
laserdad 14:ae0be10b73cd 264 numInts++; //increment counter to get data every so many interrupts if ppg new data interrupt mode
laserdad 4:3aee2ca931b8 265 }
laserdad 4:3aee2ca931b8 266 else
laserdad 4:3aee2ca931b8 267 {
laserdad 4:3aee2ca931b8 268 numInts = samples2Read;
laserdad 4:3aee2ca931b8 269 }
laserdad 2:ee175f471ecb 270 intFlagged = 1;
laserdad 4:3aee2ca931b8 271 if( (numInts < samples2Read) && !afull)
laserdad 4:3aee2ca931b8 272 {
laserdad 14:ae0be10b73cd 273 ob1203.get_status(); //clear interrupt--not time to get data yet
laserdad 4:3aee2ca931b8 274 }
laserdad 14:ae0be10b73cd 275 else if(intFlagged && (numInts==samples2Read) ) //time to get data
laserdad 3:d3a1fc19bee6 276 {
laserdad 4:3aee2ca931b8 277 numInts = 0;
laserdad 4:3aee2ca931b8 278 ob1203.getFifoSamples(samples2Read,fifoBuffer);
laserdad 4:3aee2ca931b8 279 // for (int n=0;n<samples2Read*3;n++)
laserdad 4:3aee2ca931b8 280 // {
laserdad 4:3aee2ca931b8 281 // pc.printf("%02X ",fifoBuffer[n]);
laserdad 4:3aee2ca931b8 282 // }
laserdad 4:3aee2ca931b8 283 // pc.printf("\r\n");
laserdad 4:3aee2ca931b8 284 ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData);
laserdad 4:3aee2ca931b8 285 // for (int n=0;n<samples2Read;n++)
laserdad 4:3aee2ca931b8 286 // {
laserdad 4:3aee2ca931b8 287 // pc.printf("%d ",ppgData[n]);
laserdad 4:3aee2ca931b8 288 // }
laserdad 4:3aee2ca931b8 289 // pc.printf("\r\n");
laserdad 4:3aee2ca931b8 290 if(first) //populate average and baseline buffers the first time we run
laserdad 3:d3a1fc19bee6 291 {
laserdad 4:3aee2ca931b8 292 for(int n=0;n<running_avg;n++)
laserdad 4:3aee2ca931b8 293 {
laserdad 4:3aee2ca931b8 294 IR_avg_buffer[n] = ppgData[0];
laserdad 4:3aee2ca931b8 295 IRavg += IR_avg_buffer[n];
laserdad 4:3aee2ca931b8 296 if(spo2)
laserdad 4:3aee2ca931b8 297 {
laserdad 4:3aee2ca931b8 298 R_avg_buffer[n] = ppgData[1];
laserdad 4:3aee2ca931b8 299 Ravg += R_avg_buffer[n];
laserdad 4:3aee2ca931b8 300 }
laserdad 4:3aee2ca931b8 301
laserdad 4:3aee2ca931b8 302 }
laserdad 4:3aee2ca931b8 303 for( int n=0;n<running_baseline;n++)
laserdad 4:3aee2ca931b8 304 {
laserdad 4:3aee2ca931b8 305 IR_baseline_buffer[n] = 0;
laserdad 4:3aee2ca931b8 306 if(spo2)
laserdad 4:3aee2ca931b8 307 {
laserdad 4:3aee2ca931b8 308 R_baseline_buffer[n] = 0;
laserdad 4:3aee2ca931b8 309 }
laserdad 4:3aee2ca931b8 310 }
laserdad 4:3aee2ca931b8 311 first = 0;
laserdad 13:5ce84c68066e 312 }//end if first
laserdad 13:5ce84c68066e 313
laserdad 4:3aee2ca931b8 314 for (int n=0;n<samples2Read/2;n++)
laserdad 4:3aee2ca931b8 315 {
laserdad 4:3aee2ca931b8 316 ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++;
laserdad 4:3aee2ca931b8 317 ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++;
laserdad 4:3aee2ca931b8 318 IRprev = IR_avg_buffer[avg_ptr]; //load the sample you are about to write over
laserdad 4:3aee2ca931b8 319 IR_avg_buffer[avg_ptr] = ppgData[2*n]; //load the new sample in the buffer
laserdad 4:3aee2ca931b8 320 IRavg += (IR_avg_buffer[avg_ptr] - IRprev); //update the average by removing the old sample and adding the new
laserdad 4:3aee2ca931b8 321
laserdad 4:3aee2ca931b8 322 IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over
laserdad 4:3aee2ca931b8 323 IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer
laserdad 4:3aee2ca931b8 324 IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new
laserdad 4:3aee2ca931b8 325
laserdad 14:ae0be10b73cd 326 if(spo2) //print two columsn of data
laserdad 3:d3a1fc19bee6 327 {
laserdad 4:3aee2ca931b8 328 Rprev = R_avg_buffer[avg_ptr];
laserdad 4:3aee2ca931b8 329 R_avg_buffer[avg_ptr] = ppgData[2*n+1];
laserdad 4:3aee2ca931b8 330 Ravg += (R_avg_buffer[avg_ptr] - Rprev);
IDTsanjoseuser 8:21738f9c5835 331
laserdad 4:3aee2ca931b8 332 // pc.printf("%d, %d, %d, %d\r\n",IRavg/running_avg,Ravg/running_avg, IR_avg_buffer[avg_ptr],R_avg_buffer[avg_ptr]);
laserdad 4:3aee2ca931b8 333
laserdad 4:3aee2ca931b8 334 Rbaseline_prev = R_baseline_buffer[baseline_ptr];
laserdad 4:3aee2ca931b8 335 R_baseline_buffer[baseline_ptr] = ppgData[2*n+1];
laserdad 4:3aee2ca931b8 336 Rbaseline += (R_baseline_buffer[baseline_ptr] - Rbaseline_prev);
laserdad 4:3aee2ca931b8 337
laserdad 13:5ce84c68066e 338 if(printAvg)
laserdad 13:5ce84c68066e 339 {
IDTsanjoseuser 8:21738f9c5835 340 //PRINT AVG DATA
laserdad 13:5ce84c68066e 341 pc.printf("%d,%d,%d\r\n",t.read_us(),IRavg/running_avg,Ravg/running_avg);
laserdad 4:3aee2ca931b8 342 // pc.printf("%d,%d\r\n",IRavg/running_avg-IRbaseline/running_baseline, Ravg/running_avg-Rbaseline/running_baseline);
laserdad 4:3aee2ca931b8 343 // pc.printf("%d,%d,%d,%d\r\n",Ravg/running_avg,Rbaseline/running_baseline,Rbaseline_prev,Ravg/running_avg-Rbaseline/running_baseline);
laserdad 13:5ce84c68066e 344 }
laserdad 13:5ce84c68066e 345 else
laserdad 13:5ce84c68066e 346 {
laserdad 13:5ce84c68066e 347 //PRINT RAW DATA
laserdad 14:ae0be10b73cd 348 if(printCurrent)
laserdad 14:ae0be10b73cd 349 {
laserdad 14:ae0be10b73cd 350 pc.printf("%d, %d, %d, %d, %d\r\n",t.read_us(),ppgData[2*n],ppgData[2*n+1],ob1203.ir_current,ob1203.r_current); //print with us counter time stamp (use only with slower data rates or averaging as this slows down the data printing);
laserdad 14:ae0be10b73cd 351 }
laserdad 14:ae0be10b73cd 352 else
laserdad 14:ae0be10b73cd 353 {
laserdad 14:ae0be10b73cd 354 pc.printf("%d, %d, %d\r\n",t.read_us(),ppgData[2*n],ppgData[2*n+1]); //print with us counter time stamp (use only with slower data rates or averaging as this slows down the data printing);
laserdad 13:5ce84c68066e 355 // pc.printf("%d, %d\r\n",ppgData[2*n],ppgData[2*n+1]); //print without us timer (faster)
laserdad 14:ae0be10b73cd 356 }
laserdad 13:5ce84c68066e 357 }
laserdad 13:5ce84c68066e 358 }//end SpO2 case
laserdad 14:ae0be10b73cd 359 else //HR mode print one column of data
laserdad 4:3aee2ca931b8 360 {
laserdad 4:3aee2ca931b8 361 ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++;
laserdad 4:3aee2ca931b8 362 ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++;
laserdad 4:3aee2ca931b8 363 IRprev = IR_avg_buffer[avg_ptr];
laserdad 4:3aee2ca931b8 364 IR_avg_buffer[avg_ptr] = ppgData[2*n+1];
laserdad 4:3aee2ca931b8 365 prevAvg = IRavg;
laserdad 4:3aee2ca931b8 366 IRavg += (IR_avg_buffer[avg_ptr] - IRprev);
laserdad 4:3aee2ca931b8 367
laserdad 4:3aee2ca931b8 368 IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over
laserdad 4:3aee2ca931b8 369 IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer
laserdad 4:3aee2ca931b8 370 prevBaseline = IRbaseline;
laserdad 4:3aee2ca931b8 371 IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new
IDTsanjoseuser 8:21738f9c5835 372
laserdad 4:3aee2ca931b8 373
laserdad 13:5ce84c68066e 374 if(printAvg)
laserdad 13:5ce84c68066e 375 {
IDTsanjoseuser 8:21738f9c5835 376 //PRINT AVG DATA
laserdad 13:5ce84c68066e 377 pc.printf("%d\r\n%d\r\n",prevAvg/running_avg-prevBaseline/running_baseline,IRavg/running_avg-IRbaseline/running_baseline);
laserdad 13:5ce84c68066e 378 }
laserdad 13:5ce84c68066e 379 else
laserdad 13:5ce84c68066e 380 {
laserdad 13:5ce84c68066e 381 //PRINT RAW DATA
laserdad 13:5ce84c68066e 382 pc.printf("%d\r\n%d\r\n",ppgData[2*n],ppgData[2*n+1]);
laserdad 13:5ce84c68066e 383 // pc.printf("%d, %d, %d\r\n",t.read_us(),ppgData[2*n],ppgData[2*n+1]); //print with us counter time stamp (use only with slower data rates or averaging as this slows down the data printing);
laserdad 13:5ce84c68066e 384
laserdad 13:5ce84c68066e 385 }
laserdad 13:5ce84c68066e 386 }//HR case
laserdad 13:5ce84c68066e 387 if(n+1==samples2Read/2)
laserdad 13:5ce84c68066e 388 {
laserdad 19:6e584392d4a7 389 if(irAGC)
laserdad 19:6e584392d4a7 390 {
laserdad 19:6e584392d4a7 391 ob1203.do_agc(ppgData[2*n],0);
laserdad 19:6e584392d4a7 392 }//end IR AGC case
laserdad 13:5ce84c68066e 393 if(spo2 && redAGC)
laserdad 13:5ce84c68066e 394 {
laserdad 19:6e584392d4a7 395 ob1203.do_agc(ppgData[2*n+1],1);
laserdad 19:6e584392d4a7 396 }//enr R AGC case
laserdad 19:6e584392d4a7 397 if(ob1203.update)
laserdad 19:6e584392d4a7 398 {
laserdad 19:6e584392d4a7 399 ob1203.setPPGcurrent();
laserdad 19:6e584392d4a7 400 ob1203.resetFIFO();
laserdad 19:6e584392d4a7 401 ob1203.update=0;
laserdad 19:6e584392d4a7 402 }
laserdad 4:3aee2ca931b8 403 }
laserdad 13:5ce84c68066e 404 }//end sample loop
laserdad 13:5ce84c68066e 405
laserdad 13:5ce84c68066e 406
laserdad 13:5ce84c68066e 407
laserdad 13:5ce84c68066e 408 intFlagged = 0;
laserdad 13:5ce84c68066e 409
laserdad 13:5ce84c68066e 410 }//end if time to read samples
laserdad 4:3aee2ca931b8 411 } //end if !intb
laserdad 14:ae0be10b73cd 412 }// end mode (PPG case)
laserdad 14:ae0be10b73cd 413 //*********************************************************************
laserdad 14:ae0be10b73cd 414 else //LS and PS measurement case
laserdad 0:e9ae48b7f6f6 415 {
laserdad 1:fb6b2b4d5d8a 416 wait_ms(sample_delay);
laserdad 5:ebe305e08430 417 if( meas_ps ? ob1203.psIsNew() : ob1203.lsIsNew() )
laserdad 1:fb6b2b4d5d8a 418 {
laserdad 5:ebe305e08430 419 meas_ps ? valid = ob1203.get_ps_ls_data(ps_ls_data) : valid = ob1203.get_ls_data(ps_ls_data);
laserdad 7:3ed0f0522b43 420 if(meas_ps)
laserdad 7:3ed0f0522b43 421 {
laserdad 7:3ed0f0522b43 422 ( ps_avg_ptr+1 == ps_running_avg ) ? ps_avg_ptr = 0 : ps_avg_ptr++;
laserdad 7:3ed0f0522b43 423 PSprev = PS_avg_buffer[ps_avg_ptr]; //load the sample you are about to write over
laserdad 7:3ed0f0522b43 424 PS_avg_buffer[ps_avg_ptr] = ps_ls_data[0]; //load the new sample in the buffer
laserdad 7:3ed0f0522b43 425 PSavg += (PS_avg_buffer[ps_avg_ptr] - PSprev); //update the average by removing the old sample and adding the new
laserdad 7:3ed0f0522b43 426 // pc.printf("%d %d %d %d %d ",ps_avg_ptr, PSprev, PS_avg_buffer[ps_avg_ptr],ps_ls_data[0],PSavg);
laserdad 7:3ed0f0522b43 427 }
laserdad 5:ebe305e08430 428 if (meas_temp)
laserdad 5:ebe305e08430 429 {
laserdad 5:ebe305e08430 430 ob1203.setMainConfig();
laserdad 5:ebe305e08430 431 }
laserdad 12:5de5f550e765 432 pc.printf("%d, %d %d %d %d %d\r\n",ps_ls_data[0],ps_ls_data[1],ps_ls_data[2],ps_ls_data[3],ps_ls_data[4],ps_ls_data[5]);
laserdad 12:5de5f550e765 433 // pc.printf("%d, %d %d %d %d %d %d %d\r\n",(PSavg/ps_running_avg),ps_ls_data[0],ps_ls_data[1],ps_ls_data[2],ps_ls_data[3],ps_ls_data[4],ps_ls_data[5],ps_ls_data[6]);
laserdad 4:3aee2ca931b8 434
laserdad 1:fb6b2b4d5d8a 435 }
laserdad 2:ee175f471ecb 436 // else
laserdad 2:ee175f471ecb 437 // {
laserdad 2:ee175f471ecb 438 // pc.printf("status = %04X\r\n",ob1203.get_status() );
laserdad 2:ee175f471ecb 439 // }
laserdad 0:e9ae48b7f6f6 440 }
laserdad 1:fb6b2b4d5d8a 441 }//end while
laserdad 1:fb6b2b4d5d8a 442 }//end main