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