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.
Dependencies: mbed OB1203_example_driver
main.cpp@8:21738f9c5835, 2018-05-17 (annotated)
- Committer:
- IDTsanjoseuser
- Date:
- Thu May 17 16:47:39 2018 +0000
- Revision:
- 8:21738f9c5835
- Parent:
- 7:3ed0f0522b43
- Child:
- 9:98b9216f9ff3
updated print lines
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:3aee2ca931b8 | 4 | //#include "SoftI2C.h" |
laserdad | 0:e9ae48b7f6f6 | 5 | |
laserdad | 0:e9ae48b7f6f6 | 6 | #define intb_pin D3 |
laserdad | 0:e9ae48b7f6f6 | 7 | |
laserdad | 1:fb6b2b4d5d8a | 8 | |
laserdad | 0:e9ae48b7f6f6 | 9 | I2C i2c(I2C_SDA,I2C_SCL); |
laserdad | 4:3aee2ca931b8 | 10 | //SoftI2C i2c(I2C_SDA,I2C_SCL); |
laserdad | 0:e9ae48b7f6f6 | 11 | InterruptIn intb(intb_pin); |
laserdad | 0:e9ae48b7f6f6 | 12 | DigitalOut sda_pullup(D11,1); |
laserdad | 0:e9ae48b7f6f6 | 13 | DigitalOut scl_pullup(D12,1); |
laserdad | 0:e9ae48b7f6f6 | 14 | DigitalOut intb_pullup(D13,1); |
laserdad | 0:e9ae48b7f6f6 | 15 | |
laserdad | 0:e9ae48b7f6f6 | 16 | OB1203 ob1203(&i2c); |
laserdad | 2:ee175f471ecb | 17 | Serial pc(USBTX, USBRX,256000); |
laserdad | 0:e9ae48b7f6f6 | 18 | Timer t; |
laserdad | 0:e9ae48b7f6f6 | 19 | |
laserdad | 1:fb6b2b4d5d8a | 20 | |
laserdad | 6:aaa2f8fb5123 | 21 | //USER CONFIGURABLE********* |
laserdad | 7:3ed0f0522b43 | 22 | bool mode = 1; //0 for PS_LS, 1 for PPG |
laserdad | 7:3ed0f0522b43 | 23 | bool meas_ps = 1; |
laserdad | 3:d3a1fc19bee6 | 24 | bool spo2 = 1; //0 for HR, 1 for SpO2 |
laserdad | 4:3aee2ca931b8 | 25 | bool afull = 1; //use Afull interrupt--otherwise PPG new data interrupt |
laserdad | 7:3ed0f0522b43 | 26 | bool meas_temp = 0; |
laserdad | 6:aaa2f8fb5123 | 27 | //**************************** |
laserdad | 6:aaa2f8fb5123 | 28 | |
laserdad | 6:aaa2f8fb5123 | 29 | //internal settings |
laserdad | 6:aaa2f8fb5123 | 30 | bool intFlagged =0; |
laserdad | 6:aaa2f8fb5123 | 31 | int sample_delay = 25; //ms |
laserdad | 1:fb6b2b4d5d8a | 32 | |
laserdad | 0:e9ae48b7f6f6 | 33 | void defaultConfig() |
laserdad | 0:e9ae48b7f6f6 | 34 | { |
laserdad | 4:3aee2ca931b8 | 35 | meas_temp ? ob1203.temp_en = TEMP_ON : ob1203.temp_en = TEMP_OFF; |
laserdad | 4:3aee2ca931b8 | 36 | |
laserdad | 4:3aee2ca931b8 | 37 | ob1203.ls_res = LS_RES(0); //2= 18bit 100ms, 0= max res |
laserdad | 4:3aee2ca931b8 | 38 | ob1203.ls_rate = LS_RATE(4); //2 =100ms, 4 = 500ms |
laserdad | 4:3aee2ca931b8 | 39 | ob1203.ls_gain = LS_GAIN(3); //gain 3 default (range) |
laserdad | 1:fb6b2b4d5d8a | 40 | ob1203.ls_thres_hi = 0x000FFFFF; |
laserdad | 0:e9ae48b7f6f6 | 41 | ob1203.ls_thres_lo = 0; |
laserdad | 0:e9ae48b7f6f6 | 42 | ob1203.ls_sai = LS_SAI_OFF; |
laserdad | 0:e9ae48b7f6f6 | 43 | ob1203.ls_mode = RGB_MODE; |
laserdad | 0:e9ae48b7f6f6 | 44 | ob1203.ls_en = LS_ON; |
laserdad | 4:3aee2ca931b8 | 45 | |
laserdad | 0:e9ae48b7f6f6 | 46 | ob1203.ps_sai_en = PS_SAI_OFF; |
laserdad | 5:ebe305e08430 | 47 | // ob1203.ps_sai_en = PS_SAI_ON; |
laserdad | 1:fb6b2b4d5d8a | 48 | if(mode) |
laserdad | 1:fb6b2b4d5d8a | 49 | { |
laserdad | 3:d3a1fc19bee6 | 50 | if (spo2) |
laserdad | 3:d3a1fc19bee6 | 51 | { |
laserdad | 3:d3a1fc19bee6 | 52 | ob1203.ppg_ps_mode = SPO2_MODE; |
laserdad | 3:d3a1fc19bee6 | 53 | } |
laserdad | 3:d3a1fc19bee6 | 54 | else |
laserdad | 3:d3a1fc19bee6 | 55 | { |
laserdad | 3:d3a1fc19bee6 | 56 | ob1203.ppg_ps_mode = HR_MODE; |
laserdad | 3:d3a1fc19bee6 | 57 | } |
laserdad | 3:d3a1fc19bee6 | 58 | |
laserdad | 1:fb6b2b4d5d8a | 59 | } |
laserdad | 1:fb6b2b4d5d8a | 60 | else |
laserdad | 1:fb6b2b4d5d8a | 61 | { |
laserdad | 1:fb6b2b4d5d8a | 62 | ob1203.ppg_ps_mode = PS_MODE; |
laserdad | 1:fb6b2b4d5d8a | 63 | } |
laserdad | 7:3ed0f0522b43 | 64 | ob1203.ps_pulses = PS_PULSES(8); |
laserdad | 1:fb6b2b4d5d8a | 65 | // pc.printf("ps_pulses = %02X\r\n",ob1203.ps_pulses); |
laserdad | 7:3ed0f0522b43 | 66 | ob1203.ps_pwidth = PS_PWIDTH(1); |
laserdad | 7:3ed0f0522b43 | 67 | ob1203.ps_rate = PS_RATE(4); |
laserdad | 1:fb6b2b4d5d8a | 68 | // pc.printf("ps_rate = %02X\r\n",ob1203.ps_rate); |
laserdad | 7:3ed0f0522b43 | 69 | ob1203.ps_avg_en = PS_AVG_ON; |
laserdad | 0:e9ae48b7f6f6 | 70 | ob1203.ps_can_ana = PS_CAN_ANA_0; |
laserdad | 0:e9ae48b7f6f6 | 71 | ob1203.ps_digital_can = 0; |
laserdad | 0:e9ae48b7f6f6 | 72 | ob1203.ps_hys_level = 0; |
laserdad | 4:3aee2ca931b8 | 73 | meas_ps ? ob1203.ps_current = 0x3FF : ob1203.ps_current = 0x000; |
laserdad | 4:3aee2ca931b8 | 74 | // ob1203.ps_current = 0; |
laserdad | 0:e9ae48b7f6f6 | 75 | ob1203.ps_thres_hi = 0xFF; |
laserdad | 0:e9ae48b7f6f6 | 76 | ob1203.ps_thres_lo = 0x00; |
laserdad | 0:e9ae48b7f6f6 | 77 | |
laserdad | 0:e9ae48b7f6f6 | 78 | //interrupts |
laserdad | 0:e9ae48b7f6f6 | 79 | ob1203.ls_int_sel = LS_INT_SEL_W; |
laserdad | 0:e9ae48b7f6f6 | 80 | ob1203.ls_var_mode = LS_THRES_INT_MODE; |
laserdad | 0:e9ae48b7f6f6 | 81 | ob1203.ls_int_en = LS_INT_ON; |
laserdad | 0:e9ae48b7f6f6 | 82 | ob1203.ppg_ps_en = PPG_PS_ON; |
laserdad | 4:3aee2ca931b8 | 83 | |
laserdad | 0:e9ae48b7f6f6 | 84 | ob1203.ps_logic_mode = PS_INT_READ_CLEARS; |
laserdad | 0:e9ae48b7f6f6 | 85 | ob1203.ps_int_en = PS_INT_ON; |
laserdad | 0:e9ae48b7f6f6 | 86 | ob1203.ls_persist = LS_PERSIST(2); |
laserdad | 0:e9ae48b7f6f6 | 87 | ob1203.ps_persist = PS_PERSIST(2); |
laserdad | 4:3aee2ca931b8 | 88 | //int |
laserdad | 4:3aee2ca931b8 | 89 | if(afull) |
laserdad | 4:3aee2ca931b8 | 90 | { |
laserdad | 4:3aee2ca931b8 | 91 | ob1203.afull_int_en = AFULL_INT_ON; |
laserdad | 4:3aee2ca931b8 | 92 | ob1203.ppg_int_en = PPG_INT_OFF; |
laserdad | 4:3aee2ca931b8 | 93 | } |
laserdad | 4:3aee2ca931b8 | 94 | else |
laserdad | 4:3aee2ca931b8 | 95 | { |
laserdad | 4:3aee2ca931b8 | 96 | ob1203.afull_int_en = AFULL_INT_OFF; |
laserdad | 4:3aee2ca931b8 | 97 | ob1203.ppg_int_en = PPG_INT_ON; |
laserdad | 4:3aee2ca931b8 | 98 | } |
laserdad | 0:e9ae48b7f6f6 | 99 | //PPG |
laserdad | 6:aaa2f8fb5123 | 100 | ob1203.ir_current = 511; //max 1023. 3FF |
laserdad | 3:d3a1fc19bee6 | 101 | if (spo2) |
laserdad | 3:d3a1fc19bee6 | 102 | { |
laserdad | 4:3aee2ca931b8 | 103 | // ob1203.r_current = 0x0FF; |
laserdad | 6:aaa2f8fb5123 | 104 | ob1203.r_current = 511; //max 511. 1FF |
laserdad | 3:d3a1fc19bee6 | 105 | } |
laserdad | 3:d3a1fc19bee6 | 106 | else |
laserdad | 3:d3a1fc19bee6 | 107 | { |
laserdad | 3:d3a1fc19bee6 | 108 | ob1203.r_current = 0; |
laserdad | 3:d3a1fc19bee6 | 109 | } |
IDTsanjoseuser | 8:21738f9c5835 | 110 | ob1203.ppg_ps_gain = PPG_PS_GAIN_2; |
laserdad | 0:e9ae48b7f6f6 | 111 | ob1203.ppg_pow_save = PPG_POW_SAVE_OFF; |
laserdad | 0:e9ae48b7f6f6 | 112 | ob1203.led_flip = LED_FLIP_OFF; |
laserdad | 0:e9ae48b7f6f6 | 113 | ob1203.ch1_can_ana = PPG_CH1_CAN(0); |
laserdad | 0:e9ae48b7f6f6 | 114 | ob1203.ch2_can_ana = PPG_CH2_CAN(0); |
laserdad | 7:3ed0f0522b43 | 115 | ob1203.ppg_avg = PPG_AVG(0); //2^n |
IDTsanjoseuser | 8:21738f9c5835 | 116 | ob1203.ppg_rate = PPG_RATE(3); |
IDTsanjoseuser | 8:21738f9c5835 | 117 | ob1203.ppg_pwidth = PPG_PWIDTH(4); |
laserdad | 0:e9ae48b7f6f6 | 118 | ob1203.ppg_freq = PPG_FREQ_60HZ; |
laserdad | 2:ee175f471ecb | 119 | // ob1203.ppg_freq = PPG_FREQ_50HZ; |
laserdad | 3:d3a1fc19bee6 | 120 | |
laserdad | 0:e9ae48b7f6f6 | 121 | ob1203.fifo_rollover_en = FIFO_ROLL_ON; |
laserdad | 0:e9ae48b7f6f6 | 122 | ob1203.fifo_afull_samples_left = AFULL_SAMPLES_LEFT(8); |
laserdad | 0:e9ae48b7f6f6 | 123 | |
laserdad | 0:e9ae48b7f6f6 | 124 | if(mode) |
laserdad | 0:e9ae48b7f6f6 | 125 | { |
laserdad | 3:d3a1fc19bee6 | 126 | if(spo2) |
laserdad | 3:d3a1fc19bee6 | 127 | { |
laserdad | 3:d3a1fc19bee6 | 128 | ob1203.init_spo2(); |
laserdad | 3:d3a1fc19bee6 | 129 | } |
laserdad | 3:d3a1fc19bee6 | 130 | else |
laserdad | 3:d3a1fc19bee6 | 131 | { |
laserdad | 3:d3a1fc19bee6 | 132 | ob1203.init_hr(); |
laserdad | 3:d3a1fc19bee6 | 133 | } |
laserdad | 0:e9ae48b7f6f6 | 134 | } |
laserdad | 0:e9ae48b7f6f6 | 135 | else |
laserdad | 0:e9ae48b7f6f6 | 136 | { |
laserdad | 5:ebe305e08430 | 137 | meas_ps ? ob1203.init_ps_rgb() : ob1203.init_rgb(); |
laserdad | 0:e9ae48b7f6f6 | 138 | } |
laserdad | 0:e9ae48b7f6f6 | 139 | } |
laserdad | 0:e9ae48b7f6f6 | 140 | |
laserdad | 1:fb6b2b4d5d8a | 141 | void regDump(uint8_t Addr, uint8_t startByte, uint8_t endByte) |
laserdad | 1:fb6b2b4d5d8a | 142 | { |
laserdad | 1:fb6b2b4d5d8a | 143 | /*print the values of up to 20 registers--buffer limit, e.g.*/ |
laserdad | 1:fb6b2b4d5d8a | 144 | char regData[20]; |
laserdad | 1:fb6b2b4d5d8a | 145 | int numBytes; |
laserdad | 1:fb6b2b4d5d8a | 146 | if (endByte>=startByte) { |
laserdad | 1:fb6b2b4d5d8a | 147 | numBytes = (endByte-startByte+1) < 20 ? (endByte-startByte+1) : 20; |
laserdad | 1:fb6b2b4d5d8a | 148 | } else { |
laserdad | 1:fb6b2b4d5d8a | 149 | numBytes=1; |
laserdad | 1:fb6b2b4d5d8a | 150 | } |
laserdad | 1:fb6b2b4d5d8a | 151 | |
laserdad | 1:fb6b2b4d5d8a | 152 | regData[0] = startByte; |
laserdad | 1:fb6b2b4d5d8a | 153 | i2c.write(Addr,regData,1,true); |
laserdad | 1:fb6b2b4d5d8a | 154 | i2c.read(Addr, regData, numBytes); |
laserdad | 1:fb6b2b4d5d8a | 155 | for(int n=0; n<numBytes; n++) { |
laserdad | 1:fb6b2b4d5d8a | 156 | pc.printf("%02X, %02X \r\n", startByte+n, regData[n]); |
laserdad | 1:fb6b2b4d5d8a | 157 | } |
laserdad | 1:fb6b2b4d5d8a | 158 | } |
laserdad | 1:fb6b2b4d5d8a | 159 | |
laserdad | 0:e9ae48b7f6f6 | 160 | |
laserdad | 0:e9ae48b7f6f6 | 161 | void intEvent(void) |
laserdad | 0:e9ae48b7f6f6 | 162 | { |
laserdad | 0:e9ae48b7f6f6 | 163 | intFlagged = 1; |
laserdad | 0:e9ae48b7f6f6 | 164 | } |
laserdad | 0:e9ae48b7f6f6 | 165 | |
laserdad | 0:e9ae48b7f6f6 | 166 | int main() |
laserdad | 0:e9ae48b7f6f6 | 167 | { |
laserdad | 4:3aee2ca931b8 | 168 | int numInts = 0; |
laserdad | 4:3aee2ca931b8 | 169 | uint32_t running_avg = 65; |
laserdad | 7:3ed0f0522b43 | 170 | uint32_t ps_running_avg = 1; |
laserdad | 7:3ed0f0522b43 | 171 | uint32_t ps_avg_ptr = 0; |
laserdad | 3:d3a1fc19bee6 | 172 | char avg_ptr = 0; |
laserdad | 4:3aee2ca931b8 | 173 | int baseline_ptr = 0; |
laserdad | 4:3aee2ca931b8 | 174 | uint32_t running_baseline = 850; |
laserdad | 3:d3a1fc19bee6 | 175 | int first = 1; |
laserdad | 3:d3a1fc19bee6 | 176 | uint32_t IRavg = 0; |
laserdad | 3:d3a1fc19bee6 | 177 | uint32_t Ravg = 0; |
laserdad | 3:d3a1fc19bee6 | 178 | uint32_t IRprev = 0; |
laserdad | 3:d3a1fc19bee6 | 179 | uint32_t Rprev = 0; |
laserdad | 3:d3a1fc19bee6 | 180 | uint32_t prevAvg = 0; |
laserdad | 3:d3a1fc19bee6 | 181 | uint32_t IR_avg_buffer[running_avg]; |
laserdad | 3:d3a1fc19bee6 | 182 | uint32_t R_avg_buffer[running_avg]; |
laserdad | 4:3aee2ca931b8 | 183 | uint32_t IR_baseline_buffer[running_baseline]; |
laserdad | 4:3aee2ca931b8 | 184 | uint32_t R_baseline_buffer[running_baseline]; |
laserdad | 4:3aee2ca931b8 | 185 | uint32_t IRbaseline_prev=0;; |
laserdad | 4:3aee2ca931b8 | 186 | uint32_t Rbaseline_prev=0;; |
laserdad | 4:3aee2ca931b8 | 187 | uint32_t IRbaseline=0; |
laserdad | 4:3aee2ca931b8 | 188 | uint32_t Rbaseline=0; |
laserdad | 4:3aee2ca931b8 | 189 | uint32_t prevBaseline=0; |
laserdad | 7:3ed0f0522b43 | 190 | uint32_t PSavg = 0; |
laserdad | 7:3ed0f0522b43 | 191 | uint32_t PSprev = 0; |
laserdad | 7:3ed0f0522b43 | 192 | uint32_t PS_avg_buffer[ps_running_avg]; |
laserdad | 7:3ed0f0522b43 | 193 | |
laserdad | 7:3ed0f0522b43 | 194 | for (int n=0;n<ps_running_avg;n++) |
laserdad | 7:3ed0f0522b43 | 195 | { |
laserdad | 7:3ed0f0522b43 | 196 | PS_avg_buffer[n] =0; |
laserdad | 7:3ed0f0522b43 | 197 | } |
laserdad | 7:3ed0f0522b43 | 198 | |
laserdad | 2:ee175f471ecb | 199 | i2c.frequency( 400000 ); |
laserdad | 0:e9ae48b7f6f6 | 200 | char valid; |
laserdad | 4:3aee2ca931b8 | 201 | uint32_t ps_ls_data[7]; |
laserdad | 2:ee175f471ecb | 202 | char samples2Read = 4; //samples, e.g. 4 samples * 3 bytes = 12 bytes (or 2 SpO2 samples) |
laserdad | 2:ee175f471ecb | 203 | char fifoBuffer[samples2Read*3]; |
laserdad | 0:e9ae48b7f6f6 | 204 | uint32_t ppgData[samples2Read]; |
laserdad | 0:e9ae48b7f6f6 | 205 | |
laserdad | 1:fb6b2b4d5d8a | 206 | |
laserdad | 1:fb6b2b4d5d8a | 207 | pc.printf("register settings\r\n"); |
laserdad | 1:fb6b2b4d5d8a | 208 | regDump(OB1203_ADDR,0,19); |
laserdad | 1:fb6b2b4d5d8a | 209 | regDump(OB1203_ADDR,20,39); |
laserdad | 1:fb6b2b4d5d8a | 210 | regDump(OB1203_ADDR,40,59); |
laserdad | 1:fb6b2b4d5d8a | 211 | regDump(OB1203_ADDR,60,77); |
laserdad | 1:fb6b2b4d5d8a | 212 | |
laserdad | 1:fb6b2b4d5d8a | 213 | pc.printf("do initial config\r\n"); |
laserdad | 0:e9ae48b7f6f6 | 214 | defaultConfig(); //rgb ps |
laserdad | 1:fb6b2b4d5d8a | 215 | |
laserdad | 1:fb6b2b4d5d8a | 216 | pc.printf("print new register config\r\n"); |
laserdad | 1:fb6b2b4d5d8a | 217 | regDump(OB1203_ADDR,0,19); |
laserdad | 1:fb6b2b4d5d8a | 218 | regDump(OB1203_ADDR,20,39); |
laserdad | 1:fb6b2b4d5d8a | 219 | regDump(OB1203_ADDR,40,59); |
laserdad | 1:fb6b2b4d5d8a | 220 | regDump(OB1203_ADDR,60,77); |
laserdad | 1:fb6b2b4d5d8a | 221 | |
laserdad | 0:e9ae48b7f6f6 | 222 | |
laserdad | 0:e9ae48b7f6f6 | 223 | intb.fall(&intEvent); |
laserdad | 3:d3a1fc19bee6 | 224 | t.start(); |
laserdad | 5:ebe305e08430 | 225 | |
laserdad | 0:e9ae48b7f6f6 | 226 | while(1) |
laserdad | 0:e9ae48b7f6f6 | 227 | { |
laserdad | 1:fb6b2b4d5d8a | 228 | if(mode) |
laserdad | 0:e9ae48b7f6f6 | 229 | { |
laserdad | 2:ee175f471ecb | 230 | if(!intb.read()) |
laserdad | 2:ee175f471ecb | 231 | { |
laserdad | 4:3aee2ca931b8 | 232 | if(!afull) |
laserdad | 4:3aee2ca931b8 | 233 | { |
laserdad | 4:3aee2ca931b8 | 234 | numInts++; //get data every other interrupt if ppg new data interrupt mode |
laserdad | 4:3aee2ca931b8 | 235 | } |
laserdad | 4:3aee2ca931b8 | 236 | else |
laserdad | 4:3aee2ca931b8 | 237 | { |
laserdad | 4:3aee2ca931b8 | 238 | numInts = samples2Read; |
laserdad | 4:3aee2ca931b8 | 239 | } |
laserdad | 2:ee175f471ecb | 240 | intFlagged = 1; |
laserdad | 4:3aee2ca931b8 | 241 | if( (numInts < samples2Read) && !afull) |
laserdad | 4:3aee2ca931b8 | 242 | { |
laserdad | 4:3aee2ca931b8 | 243 | ob1203.get_status(); |
laserdad | 4:3aee2ca931b8 | 244 | } |
laserdad | 4:3aee2ca931b8 | 245 | else if(intFlagged && (numInts==samples2Read) ) |
laserdad | 3:d3a1fc19bee6 | 246 | { |
laserdad | 4:3aee2ca931b8 | 247 | numInts = 0; |
laserdad | 4:3aee2ca931b8 | 248 | ob1203.getFifoSamples(samples2Read,fifoBuffer); |
laserdad | 4:3aee2ca931b8 | 249 | // for (int n=0;n<samples2Read*3;n++) |
laserdad | 4:3aee2ca931b8 | 250 | // { |
laserdad | 4:3aee2ca931b8 | 251 | // pc.printf("%02X ",fifoBuffer[n]); |
laserdad | 4:3aee2ca931b8 | 252 | // } |
laserdad | 4:3aee2ca931b8 | 253 | // pc.printf("\r\n"); |
laserdad | 4:3aee2ca931b8 | 254 | ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData); |
laserdad | 4:3aee2ca931b8 | 255 | // for (int n=0;n<samples2Read;n++) |
laserdad | 4:3aee2ca931b8 | 256 | // { |
laserdad | 4:3aee2ca931b8 | 257 | // pc.printf("%d ",ppgData[n]); |
laserdad | 4:3aee2ca931b8 | 258 | // } |
laserdad | 4:3aee2ca931b8 | 259 | // pc.printf("\r\n"); |
laserdad | 4:3aee2ca931b8 | 260 | if(first) //populate average and baseline buffers the first time we run |
laserdad | 3:d3a1fc19bee6 | 261 | { |
laserdad | 4:3aee2ca931b8 | 262 | for(int n=0;n<running_avg;n++) |
laserdad | 4:3aee2ca931b8 | 263 | { |
laserdad | 4:3aee2ca931b8 | 264 | IR_avg_buffer[n] = ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 265 | IRavg += IR_avg_buffer[n]; |
laserdad | 4:3aee2ca931b8 | 266 | if(spo2) |
laserdad | 4:3aee2ca931b8 | 267 | { |
laserdad | 4:3aee2ca931b8 | 268 | R_avg_buffer[n] = ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 269 | Ravg += R_avg_buffer[n]; |
laserdad | 4:3aee2ca931b8 | 270 | } |
laserdad | 4:3aee2ca931b8 | 271 | |
laserdad | 4:3aee2ca931b8 | 272 | } |
laserdad | 4:3aee2ca931b8 | 273 | for( int n=0;n<running_baseline;n++) |
laserdad | 4:3aee2ca931b8 | 274 | { |
laserdad | 4:3aee2ca931b8 | 275 | // IR_baseline_buffer[n] = ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 276 | IR_baseline_buffer[n] = 0; |
laserdad | 4:3aee2ca931b8 | 277 | if(spo2) |
laserdad | 4:3aee2ca931b8 | 278 | { |
laserdad | 4:3aee2ca931b8 | 279 | // R_baseline_buffer[n] = ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 280 | R_baseline_buffer[n] = 0; |
laserdad | 4:3aee2ca931b8 | 281 | } |
laserdad | 4:3aee2ca931b8 | 282 | } |
laserdad | 4:3aee2ca931b8 | 283 | first = 0; |
laserdad | 4:3aee2ca931b8 | 284 | // IRbaseline = running_baseline*ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 285 | // Rbaseline = running_baseline*ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 286 | } |
laserdad | 4:3aee2ca931b8 | 287 | |
laserdad | 4:3aee2ca931b8 | 288 | // IRbaseline = running_baseline*ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 289 | for (int n=0;n<samples2Read/2;n++) |
laserdad | 4:3aee2ca931b8 | 290 | { |
laserdad | 4:3aee2ca931b8 | 291 | ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; |
laserdad | 4:3aee2ca931b8 | 292 | ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++; |
laserdad | 4:3aee2ca931b8 | 293 | IRprev = IR_avg_buffer[avg_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 294 | IR_avg_buffer[avg_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 295 | IRavg += (IR_avg_buffer[avg_ptr] - IRprev); //update the average by removing the old sample and adding the new |
laserdad | 4:3aee2ca931b8 | 296 | |
laserdad | 4:3aee2ca931b8 | 297 | IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 298 | IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 299 | IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new |
laserdad | 4:3aee2ca931b8 | 300 | |
laserdad | 3:d3a1fc19bee6 | 301 | if(spo2) |
laserdad | 3:d3a1fc19bee6 | 302 | { |
laserdad | 4:3aee2ca931b8 | 303 | Rprev = R_avg_buffer[avg_ptr]; |
laserdad | 4:3aee2ca931b8 | 304 | R_avg_buffer[avg_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 305 | Ravg += (R_avg_buffer[avg_ptr] - Rprev); |
IDTsanjoseuser | 8:21738f9c5835 | 306 | |
laserdad | 4:3aee2ca931b8 | 307 | // 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 | 308 | |
laserdad | 4:3aee2ca931b8 | 309 | Rbaseline_prev = R_baseline_buffer[baseline_ptr]; |
laserdad | 4:3aee2ca931b8 | 310 | R_baseline_buffer[baseline_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 311 | Rbaseline += (R_baseline_buffer[baseline_ptr] - Rbaseline_prev); |
laserdad | 4:3aee2ca931b8 | 312 | |
laserdad | 4:3aee2ca931b8 | 313 | |
IDTsanjoseuser | 8:21738f9c5835 | 314 | //PRINT RAW DATA |
IDTsanjoseuser | 8:21738f9c5835 | 315 | // pc.printf("%d, %d, %d\r\n",t.read_us(),ppgData[2*n],ppgData[2*n+1]); //print with us time results (time_secs = (count - count_at_start_of_meas )/1e6; |
IDTsanjoseuser | 8:21738f9c5835 | 316 | pc.printf("%d, %d\r\n",ppgData[2*n],ppgData[2*n+1]); //print without us timer (faster) |
IDTsanjoseuser | 8:21738f9c5835 | 317 | //PRINT AVG DATA |
IDTsanjoseuser | 8:21738f9c5835 | 318 | // pc.printf("%d,%d\r\n",IRavg/running_avg,Ravg/running_avg); |
laserdad | 4:3aee2ca931b8 | 319 | // pc.printf("%d,%d\r\n",IRavg/running_avg-IRbaseline/running_baseline, Ravg/running_avg-Rbaseline/running_baseline); |
laserdad | 4:3aee2ca931b8 | 320 | |
laserdad | 4:3aee2ca931b8 | 321 | // pc.printf("%d,%d,%d,%d\r\n",Ravg/running_avg,Rbaseline/running_baseline,Rbaseline_prev,Ravg/running_avg-Rbaseline/running_baseline); |
laserdad | 3:d3a1fc19bee6 | 322 | } |
laserdad | 4:3aee2ca931b8 | 323 | else |
laserdad | 4:3aee2ca931b8 | 324 | { |
laserdad | 4:3aee2ca931b8 | 325 | ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; |
laserdad | 4:3aee2ca931b8 | 326 | ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++; |
laserdad | 4:3aee2ca931b8 | 327 | IRprev = IR_avg_buffer[avg_ptr]; |
laserdad | 4:3aee2ca931b8 | 328 | IR_avg_buffer[avg_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 329 | prevAvg = IRavg; |
laserdad | 4:3aee2ca931b8 | 330 | IRavg += (IR_avg_buffer[avg_ptr] - IRprev); |
laserdad | 4:3aee2ca931b8 | 331 | |
laserdad | 4:3aee2ca931b8 | 332 | IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 333 | IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 334 | prevBaseline = IRbaseline; |
laserdad | 4:3aee2ca931b8 | 335 | IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new |
IDTsanjoseuser | 8:21738f9c5835 | 336 | |
laserdad | 4:3aee2ca931b8 | 337 | |
IDTsanjoseuser | 8:21738f9c5835 | 338 | //PRINT RAW DATA |
IDTsanjoseuser | 8:21738f9c5835 | 339 | pc.printf("%d\r\n%d\r\n",ppgData[2*n],ppgData[2*n+1]); |
IDTsanjoseuser | 8:21738f9c5835 | 340 | |
IDTsanjoseuser | 8:21738f9c5835 | 341 | //PRINT AVG DATA |
IDTsanjoseuser | 8:21738f9c5835 | 342 | // pc.printf("%d\r\n%d\r\n",prevAvg/running_avg-prevBaseline/running_baseline,IRavg/running_avg-IRbaseline/running_baseline); |
IDTsanjoseuser | 8:21738f9c5835 | 343 | |
laserdad | 4:3aee2ca931b8 | 344 | } |
laserdad | 3:d3a1fc19bee6 | 345 | } |
laserdad | 4:3aee2ca931b8 | 346 | intFlagged = 0; |
laserdad | 3:d3a1fc19bee6 | 347 | } |
laserdad | 4:3aee2ca931b8 | 348 | } //end if !intb |
laserdad | 4:3aee2ca931b8 | 349 | }// end mode |
laserdad | 0:e9ae48b7f6f6 | 350 | else |
laserdad | 0:e9ae48b7f6f6 | 351 | { |
laserdad | 1:fb6b2b4d5d8a | 352 | wait_ms(sample_delay); |
laserdad | 5:ebe305e08430 | 353 | if( meas_ps ? ob1203.psIsNew() : ob1203.lsIsNew() ) |
laserdad | 1:fb6b2b4d5d8a | 354 | { |
laserdad | 5:ebe305e08430 | 355 | meas_ps ? valid = ob1203.get_ps_ls_data(ps_ls_data) : valid = ob1203.get_ls_data(ps_ls_data); |
laserdad | 7:3ed0f0522b43 | 356 | if(meas_ps) |
laserdad | 7:3ed0f0522b43 | 357 | { |
laserdad | 7:3ed0f0522b43 | 358 | ( ps_avg_ptr+1 == ps_running_avg ) ? ps_avg_ptr = 0 : ps_avg_ptr++; |
laserdad | 7:3ed0f0522b43 | 359 | PSprev = PS_avg_buffer[ps_avg_ptr]; //load the sample you are about to write over |
laserdad | 7:3ed0f0522b43 | 360 | PS_avg_buffer[ps_avg_ptr] = ps_ls_data[0]; //load the new sample in the buffer |
laserdad | 7:3ed0f0522b43 | 361 | PSavg += (PS_avg_buffer[ps_avg_ptr] - PSprev); //update the average by removing the old sample and adding the new |
laserdad | 7:3ed0f0522b43 | 362 | // 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 | 363 | } |
laserdad | 5:ebe305e08430 | 364 | if (meas_temp) |
laserdad | 5:ebe305e08430 | 365 | { |
laserdad | 5:ebe305e08430 | 366 | ob1203.setMainConfig(); |
laserdad | 5:ebe305e08430 | 367 | } |
laserdad | 4:3aee2ca931b8 | 368 | // 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 | 7:3ed0f0522b43 | 369 | pc.printf("%d, %d %d %d %d %d %d %d\r\n",(PSavg/ps_running_avg)>>6,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 | 370 | |
laserdad | 1:fb6b2b4d5d8a | 371 | } |
laserdad | 2:ee175f471ecb | 372 | // else |
laserdad | 2:ee175f471ecb | 373 | // { |
laserdad | 2:ee175f471ecb | 374 | // pc.printf("status = %04X\r\n",ob1203.get_status() ); |
laserdad | 2:ee175f471ecb | 375 | // } |
laserdad | 0:e9ae48b7f6f6 | 376 | } |
laserdad | 1:fb6b2b4d5d8a | 377 | }//end while |
laserdad | 1:fb6b2b4d5d8a | 378 | }//end main |