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@11:640cced1ee5d, 2018-06-19 (annotated)
- Committer:
- laserdad
- Date:
- Tue Jun 19 12:58:45 2018 +0000
- Revision:
- 11:640cced1ee5d
- Parent:
- 10:7fbf298ee05f
- Child:
- 12:5de5f550e765
latest
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 | 11:640cced1ee5d | 24 | bool spo2 = 0; //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 | 11:640cced1ee5d | 90 | |
laserdad | 11:640cced1ee5d | 91 | |
laserdad | 11:640cced1ee5d | 92 | //BIO SETTINGS |
laserdad | 4:3aee2ca931b8 | 93 | //int |
laserdad | 4:3aee2ca931b8 | 94 | if(afull) |
laserdad | 4:3aee2ca931b8 | 95 | { |
laserdad | 4:3aee2ca931b8 | 96 | ob1203.afull_int_en = AFULL_INT_ON; |
laserdad | 4:3aee2ca931b8 | 97 | ob1203.ppg_int_en = PPG_INT_OFF; |
laserdad | 4:3aee2ca931b8 | 98 | } |
laserdad | 4:3aee2ca931b8 | 99 | else |
laserdad | 4:3aee2ca931b8 | 100 | { |
laserdad | 4:3aee2ca931b8 | 101 | ob1203.afull_int_en = AFULL_INT_OFF; |
laserdad | 4:3aee2ca931b8 | 102 | ob1203.ppg_int_en = PPG_INT_ON; |
laserdad | 4:3aee2ca931b8 | 103 | } |
laserdad | 0:e9ae48b7f6f6 | 104 | //PPG |
laserdad | 10:7fbf298ee05f | 105 | ob1203.ir_current = 600; //max 1023. 3FF |
laserdad | 3:d3a1fc19bee6 | 106 | if (spo2) |
laserdad | 3:d3a1fc19bee6 | 107 | { |
laserdad | 4:3aee2ca931b8 | 108 | // ob1203.r_current = 0x0FF; |
laserdad | 6:aaa2f8fb5123 | 109 | ob1203.r_current = 511; //max 511. 1FF |
laserdad | 3:d3a1fc19bee6 | 110 | } |
laserdad | 3:d3a1fc19bee6 | 111 | else |
laserdad | 3:d3a1fc19bee6 | 112 | { |
laserdad | 3:d3a1fc19bee6 | 113 | ob1203.r_current = 0; |
laserdad | 3:d3a1fc19bee6 | 114 | } |
laserdad | 10:7fbf298ee05f | 115 | ob1203.ppg_ps_gain = PPG_PS_GAIN_1; |
laserdad | 0:e9ae48b7f6f6 | 116 | ob1203.ppg_pow_save = PPG_POW_SAVE_OFF; |
laserdad | 0:e9ae48b7f6f6 | 117 | ob1203.led_flip = LED_FLIP_OFF; |
laserdad | 11:640cced1ee5d | 118 | ob1203.ch1_can_ana = PPG_CH1_CAN(0); |
laserdad | 11:640cced1ee5d | 119 | ob1203.ch2_can_ana = PPG_CH2_CAN(0); |
laserdad | 11:640cced1ee5d | 120 | ob1203.ppg_avg = PPG_AVG(0); //2^n |
laserdad | 11:640cced1ee5d | 121 | ob1203.ppg_rate = PPG_RATE(4); |
laserdad | 11:640cced1ee5d | 122 | ob1203.ppg_pwidth = PPG_PWIDTH(5); |
laserdad | 11:640cced1ee5d | 123 | // ob1203.ppg_freq = PPG_FREQ_50HZ; |
laserdad | 11:640cced1ee5d | 124 | ob1203.ppg_freq = PPG_FREQ_60HZ; |
laserdad | 10:7fbf298ee05f | 125 | ob1203.bio_trim = 3; //max 3 |
laserdad | 10:7fbf298ee05f | 126 | ob1203.led_trim = 0x00; //max current |
laserdad | 10:7fbf298ee05f | 127 | ob1203.ppg_LED_settling = PPG_LED_SETTLING(2); |
laserdad | 9:98b9216f9ff3 | 128 | ob1203.ppg_ALC_track = PPG_ALC_TRACK(2); |
laserdad | 11:640cced1ee5d | 129 | ob1203.diff = DIFF_ON; |
laserdad | 11:640cced1ee5d | 130 | ob1203.alc = ALC_ON; |
laserdad | 11:640cced1ee5d | 131 | ob1203.sig_out = SIGNAL_OUT; |
laserdad | 9:98b9216f9ff3 | 132 | |
laserdad | 2:ee175f471ecb | 133 | // ob1203.ppg_freq = PPG_FREQ_50HZ; |
laserdad | 3:d3a1fc19bee6 | 134 | |
laserdad | 0:e9ae48b7f6f6 | 135 | ob1203.fifo_rollover_en = FIFO_ROLL_ON; |
laserdad | 0:e9ae48b7f6f6 | 136 | ob1203.fifo_afull_samples_left = AFULL_SAMPLES_LEFT(8); |
laserdad | 0:e9ae48b7f6f6 | 137 | |
laserdad | 0:e9ae48b7f6f6 | 138 | if(mode) |
laserdad | 0:e9ae48b7f6f6 | 139 | { |
laserdad | 3:d3a1fc19bee6 | 140 | if(spo2) |
laserdad | 3:d3a1fc19bee6 | 141 | { |
laserdad | 3:d3a1fc19bee6 | 142 | ob1203.init_spo2(); |
laserdad | 3:d3a1fc19bee6 | 143 | } |
laserdad | 3:d3a1fc19bee6 | 144 | else |
laserdad | 3:d3a1fc19bee6 | 145 | { |
laserdad | 3:d3a1fc19bee6 | 146 | ob1203.init_hr(); |
laserdad | 3:d3a1fc19bee6 | 147 | } |
laserdad | 0:e9ae48b7f6f6 | 148 | } |
laserdad | 0:e9ae48b7f6f6 | 149 | else |
laserdad | 0:e9ae48b7f6f6 | 150 | { |
laserdad | 5:ebe305e08430 | 151 | meas_ps ? ob1203.init_ps_rgb() : ob1203.init_rgb(); |
laserdad | 0:e9ae48b7f6f6 | 152 | } |
laserdad | 0:e9ae48b7f6f6 | 153 | } |
laserdad | 0:e9ae48b7f6f6 | 154 | |
laserdad | 1:fb6b2b4d5d8a | 155 | void regDump(uint8_t Addr, uint8_t startByte, uint8_t endByte) |
laserdad | 1:fb6b2b4d5d8a | 156 | { |
laserdad | 1:fb6b2b4d5d8a | 157 | /*print the values of up to 20 registers--buffer limit, e.g.*/ |
laserdad | 1:fb6b2b4d5d8a | 158 | char regData[20]; |
laserdad | 1:fb6b2b4d5d8a | 159 | int numBytes; |
laserdad | 1:fb6b2b4d5d8a | 160 | if (endByte>=startByte) { |
laserdad | 1:fb6b2b4d5d8a | 161 | numBytes = (endByte-startByte+1) < 20 ? (endByte-startByte+1) : 20; |
laserdad | 1:fb6b2b4d5d8a | 162 | } else { |
laserdad | 1:fb6b2b4d5d8a | 163 | numBytes=1; |
laserdad | 1:fb6b2b4d5d8a | 164 | } |
laserdad | 1:fb6b2b4d5d8a | 165 | |
laserdad | 1:fb6b2b4d5d8a | 166 | regData[0] = startByte; |
laserdad | 1:fb6b2b4d5d8a | 167 | i2c.write(Addr,regData,1,true); |
laserdad | 1:fb6b2b4d5d8a | 168 | i2c.read(Addr, regData, numBytes); |
laserdad | 1:fb6b2b4d5d8a | 169 | for(int n=0; n<numBytes; n++) { |
laserdad | 1:fb6b2b4d5d8a | 170 | pc.printf("%02X, %02X \r\n", startByte+n, regData[n]); |
laserdad | 1:fb6b2b4d5d8a | 171 | } |
laserdad | 1:fb6b2b4d5d8a | 172 | } |
laserdad | 1:fb6b2b4d5d8a | 173 | |
laserdad | 0:e9ae48b7f6f6 | 174 | |
laserdad | 0:e9ae48b7f6f6 | 175 | void intEvent(void) |
laserdad | 0:e9ae48b7f6f6 | 176 | { |
laserdad | 0:e9ae48b7f6f6 | 177 | intFlagged = 1; |
laserdad | 0:e9ae48b7f6f6 | 178 | } |
laserdad | 0:e9ae48b7f6f6 | 179 | |
laserdad | 0:e9ae48b7f6f6 | 180 | int main() |
laserdad | 0:e9ae48b7f6f6 | 181 | { |
laserdad | 4:3aee2ca931b8 | 182 | int numInts = 0; |
laserdad | 10:7fbf298ee05f | 183 | uint32_t running_avg = 10; |
laserdad | 10:7fbf298ee05f | 184 | uint32_t ps_running_avg = 100; |
laserdad | 7:3ed0f0522b43 | 185 | uint32_t ps_avg_ptr = 0; |
laserdad | 3:d3a1fc19bee6 | 186 | char avg_ptr = 0; |
laserdad | 4:3aee2ca931b8 | 187 | int baseline_ptr = 0; |
laserdad | 10:7fbf298ee05f | 188 | uint32_t running_baseline = 240; |
laserdad | 3:d3a1fc19bee6 | 189 | int first = 1; |
laserdad | 3:d3a1fc19bee6 | 190 | uint32_t IRavg = 0; |
laserdad | 3:d3a1fc19bee6 | 191 | uint32_t Ravg = 0; |
laserdad | 3:d3a1fc19bee6 | 192 | uint32_t IRprev = 0; |
laserdad | 3:d3a1fc19bee6 | 193 | uint32_t Rprev = 0; |
laserdad | 3:d3a1fc19bee6 | 194 | uint32_t prevAvg = 0; |
laserdad | 3:d3a1fc19bee6 | 195 | uint32_t IR_avg_buffer[running_avg]; |
laserdad | 3:d3a1fc19bee6 | 196 | uint32_t R_avg_buffer[running_avg]; |
laserdad | 4:3aee2ca931b8 | 197 | uint32_t IR_baseline_buffer[running_baseline]; |
laserdad | 4:3aee2ca931b8 | 198 | uint32_t R_baseline_buffer[running_baseline]; |
laserdad | 4:3aee2ca931b8 | 199 | uint32_t IRbaseline_prev=0;; |
laserdad | 4:3aee2ca931b8 | 200 | uint32_t Rbaseline_prev=0;; |
laserdad | 4:3aee2ca931b8 | 201 | uint32_t IRbaseline=0; |
laserdad | 4:3aee2ca931b8 | 202 | uint32_t Rbaseline=0; |
laserdad | 4:3aee2ca931b8 | 203 | uint32_t prevBaseline=0; |
laserdad | 7:3ed0f0522b43 | 204 | uint32_t PSavg = 0; |
laserdad | 7:3ed0f0522b43 | 205 | uint32_t PSprev = 0; |
laserdad | 7:3ed0f0522b43 | 206 | uint32_t PS_avg_buffer[ps_running_avg]; |
laserdad | 7:3ed0f0522b43 | 207 | |
laserdad | 7:3ed0f0522b43 | 208 | for (int n=0;n<ps_running_avg;n++) |
laserdad | 7:3ed0f0522b43 | 209 | { |
laserdad | 7:3ed0f0522b43 | 210 | PS_avg_buffer[n] =0; |
laserdad | 7:3ed0f0522b43 | 211 | } |
laserdad | 7:3ed0f0522b43 | 212 | |
laserdad | 2:ee175f471ecb | 213 | i2c.frequency( 400000 ); |
laserdad | 0:e9ae48b7f6f6 | 214 | char valid; |
laserdad | 4:3aee2ca931b8 | 215 | uint32_t ps_ls_data[7]; |
laserdad | 10:7fbf298ee05f | 216 | char samples2Read = 6; //samples, e.g. 4 samples * 3 bytes = 12 bytes (or 2 SpO2 samples) |
laserdad | 2:ee175f471ecb | 217 | char fifoBuffer[samples2Read*3]; |
laserdad | 0:e9ae48b7f6f6 | 218 | uint32_t ppgData[samples2Read]; |
laserdad | 0:e9ae48b7f6f6 | 219 | |
laserdad | 1:fb6b2b4d5d8a | 220 | |
laserdad | 1:fb6b2b4d5d8a | 221 | pc.printf("register settings\r\n"); |
laserdad | 1:fb6b2b4d5d8a | 222 | regDump(OB1203_ADDR,0,19); |
laserdad | 1:fb6b2b4d5d8a | 223 | regDump(OB1203_ADDR,20,39); |
laserdad | 1:fb6b2b4d5d8a | 224 | regDump(OB1203_ADDR,40,59); |
laserdad | 1:fb6b2b4d5d8a | 225 | regDump(OB1203_ADDR,60,77); |
laserdad | 1:fb6b2b4d5d8a | 226 | |
laserdad | 1:fb6b2b4d5d8a | 227 | pc.printf("do initial config\r\n"); |
laserdad | 0:e9ae48b7f6f6 | 228 | defaultConfig(); //rgb ps |
laserdad | 1:fb6b2b4d5d8a | 229 | |
laserdad | 1:fb6b2b4d5d8a | 230 | pc.printf("print new register config\r\n"); |
laserdad | 1:fb6b2b4d5d8a | 231 | regDump(OB1203_ADDR,0,19); |
laserdad | 1:fb6b2b4d5d8a | 232 | regDump(OB1203_ADDR,20,39); |
laserdad | 1:fb6b2b4d5d8a | 233 | regDump(OB1203_ADDR,40,59); |
laserdad | 1:fb6b2b4d5d8a | 234 | regDump(OB1203_ADDR,60,77); |
laserdad | 1:fb6b2b4d5d8a | 235 | |
laserdad | 0:e9ae48b7f6f6 | 236 | |
laserdad | 0:e9ae48b7f6f6 | 237 | intb.fall(&intEvent); |
laserdad | 3:d3a1fc19bee6 | 238 | t.start(); |
laserdad | 5:ebe305e08430 | 239 | |
laserdad | 0:e9ae48b7f6f6 | 240 | while(1) |
laserdad | 0:e9ae48b7f6f6 | 241 | { |
laserdad | 1:fb6b2b4d5d8a | 242 | if(mode) |
laserdad | 0:e9ae48b7f6f6 | 243 | { |
laserdad | 2:ee175f471ecb | 244 | if(!intb.read()) |
laserdad | 2:ee175f471ecb | 245 | { |
laserdad | 4:3aee2ca931b8 | 246 | if(!afull) |
laserdad | 4:3aee2ca931b8 | 247 | { |
laserdad | 4:3aee2ca931b8 | 248 | numInts++; //get data every other interrupt if ppg new data interrupt mode |
laserdad | 4:3aee2ca931b8 | 249 | } |
laserdad | 4:3aee2ca931b8 | 250 | else |
laserdad | 4:3aee2ca931b8 | 251 | { |
laserdad | 4:3aee2ca931b8 | 252 | numInts = samples2Read; |
laserdad | 4:3aee2ca931b8 | 253 | } |
laserdad | 2:ee175f471ecb | 254 | intFlagged = 1; |
laserdad | 4:3aee2ca931b8 | 255 | if( (numInts < samples2Read) && !afull) |
laserdad | 4:3aee2ca931b8 | 256 | { |
laserdad | 4:3aee2ca931b8 | 257 | ob1203.get_status(); |
laserdad | 4:3aee2ca931b8 | 258 | } |
laserdad | 4:3aee2ca931b8 | 259 | else if(intFlagged && (numInts==samples2Read) ) |
laserdad | 3:d3a1fc19bee6 | 260 | { |
laserdad | 4:3aee2ca931b8 | 261 | numInts = 0; |
laserdad | 4:3aee2ca931b8 | 262 | ob1203.getFifoSamples(samples2Read,fifoBuffer); |
laserdad | 4:3aee2ca931b8 | 263 | // for (int n=0;n<samples2Read*3;n++) |
laserdad | 4:3aee2ca931b8 | 264 | // { |
laserdad | 4:3aee2ca931b8 | 265 | // pc.printf("%02X ",fifoBuffer[n]); |
laserdad | 4:3aee2ca931b8 | 266 | // } |
laserdad | 4:3aee2ca931b8 | 267 | // pc.printf("\r\n"); |
laserdad | 4:3aee2ca931b8 | 268 | ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData); |
laserdad | 4:3aee2ca931b8 | 269 | // for (int n=0;n<samples2Read;n++) |
laserdad | 4:3aee2ca931b8 | 270 | // { |
laserdad | 4:3aee2ca931b8 | 271 | // pc.printf("%d ",ppgData[n]); |
laserdad | 4:3aee2ca931b8 | 272 | // } |
laserdad | 4:3aee2ca931b8 | 273 | // pc.printf("\r\n"); |
laserdad | 4:3aee2ca931b8 | 274 | if(first) //populate average and baseline buffers the first time we run |
laserdad | 3:d3a1fc19bee6 | 275 | { |
laserdad | 4:3aee2ca931b8 | 276 | for(int n=0;n<running_avg;n++) |
laserdad | 4:3aee2ca931b8 | 277 | { |
laserdad | 4:3aee2ca931b8 | 278 | IR_avg_buffer[n] = ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 279 | IRavg += IR_avg_buffer[n]; |
laserdad | 4:3aee2ca931b8 | 280 | if(spo2) |
laserdad | 4:3aee2ca931b8 | 281 | { |
laserdad | 4:3aee2ca931b8 | 282 | R_avg_buffer[n] = ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 283 | Ravg += R_avg_buffer[n]; |
laserdad | 4:3aee2ca931b8 | 284 | } |
laserdad | 4:3aee2ca931b8 | 285 | |
laserdad | 4:3aee2ca931b8 | 286 | } |
laserdad | 4:3aee2ca931b8 | 287 | for( int n=0;n<running_baseline;n++) |
laserdad | 4:3aee2ca931b8 | 288 | { |
laserdad | 4:3aee2ca931b8 | 289 | // IR_baseline_buffer[n] = ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 290 | IR_baseline_buffer[n] = 0; |
laserdad | 4:3aee2ca931b8 | 291 | if(spo2) |
laserdad | 4:3aee2ca931b8 | 292 | { |
laserdad | 4:3aee2ca931b8 | 293 | // R_baseline_buffer[n] = ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 294 | R_baseline_buffer[n] = 0; |
laserdad | 4:3aee2ca931b8 | 295 | } |
laserdad | 4:3aee2ca931b8 | 296 | } |
laserdad | 4:3aee2ca931b8 | 297 | first = 0; |
laserdad | 4:3aee2ca931b8 | 298 | // IRbaseline = running_baseline*ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 299 | // Rbaseline = running_baseline*ppgData[1]; |
laserdad | 4:3aee2ca931b8 | 300 | } |
laserdad | 4:3aee2ca931b8 | 301 | |
laserdad | 4:3aee2ca931b8 | 302 | // IRbaseline = running_baseline*ppgData[0]; |
laserdad | 4:3aee2ca931b8 | 303 | for (int n=0;n<samples2Read/2;n++) |
laserdad | 4:3aee2ca931b8 | 304 | { |
laserdad | 4:3aee2ca931b8 | 305 | ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; |
laserdad | 4:3aee2ca931b8 | 306 | ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++; |
laserdad | 4:3aee2ca931b8 | 307 | IRprev = IR_avg_buffer[avg_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 308 | IR_avg_buffer[avg_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 309 | IRavg += (IR_avg_buffer[avg_ptr] - IRprev); //update the average by removing the old sample and adding the new |
laserdad | 4:3aee2ca931b8 | 310 | |
laserdad | 4:3aee2ca931b8 | 311 | IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 312 | IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 313 | IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new |
laserdad | 4:3aee2ca931b8 | 314 | |
laserdad | 3:d3a1fc19bee6 | 315 | if(spo2) |
laserdad | 3:d3a1fc19bee6 | 316 | { |
laserdad | 4:3aee2ca931b8 | 317 | Rprev = R_avg_buffer[avg_ptr]; |
laserdad | 4:3aee2ca931b8 | 318 | R_avg_buffer[avg_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 319 | Ravg += (R_avg_buffer[avg_ptr] - Rprev); |
IDTsanjoseuser | 8:21738f9c5835 | 320 | |
laserdad | 4:3aee2ca931b8 | 321 | // 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 | 322 | |
laserdad | 4:3aee2ca931b8 | 323 | Rbaseline_prev = R_baseline_buffer[baseline_ptr]; |
laserdad | 4:3aee2ca931b8 | 324 | R_baseline_buffer[baseline_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 325 | Rbaseline += (R_baseline_buffer[baseline_ptr] - Rbaseline_prev); |
laserdad | 4:3aee2ca931b8 | 326 | |
laserdad | 4:3aee2ca931b8 | 327 | |
IDTsanjoseuser | 8:21738f9c5835 | 328 | //PRINT RAW DATA |
laserdad | 11:640cced1ee5d | 329 | 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 | 11:640cced1ee5d | 330 | // pc.printf("%d, %d\r\n",ppgData[2*n],ppgData[2*n+1]); //print without us timer (faster) |
IDTsanjoseuser | 8:21738f9c5835 | 331 | //PRINT AVG DATA |
laserdad | 10:7fbf298ee05f | 332 | // pc.printf("%d,%d,%d\r\n",t.read_us(),IRavg/running_avg,Ravg/running_avg); |
laserdad | 4:3aee2ca931b8 | 333 | // pc.printf("%d,%d\r\n",IRavg/running_avg-IRbaseline/running_baseline, Ravg/running_avg-Rbaseline/running_baseline); |
laserdad | 4:3aee2ca931b8 | 334 | |
laserdad | 4:3aee2ca931b8 | 335 | // 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 | 336 | } |
laserdad | 4:3aee2ca931b8 | 337 | else |
laserdad | 4:3aee2ca931b8 | 338 | { |
laserdad | 4:3aee2ca931b8 | 339 | ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; |
laserdad | 4:3aee2ca931b8 | 340 | ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_ptr++; |
laserdad | 4:3aee2ca931b8 | 341 | IRprev = IR_avg_buffer[avg_ptr]; |
laserdad | 4:3aee2ca931b8 | 342 | IR_avg_buffer[avg_ptr] = ppgData[2*n+1]; |
laserdad | 4:3aee2ca931b8 | 343 | prevAvg = IRavg; |
laserdad | 4:3aee2ca931b8 | 344 | IRavg += (IR_avg_buffer[avg_ptr] - IRprev); |
laserdad | 4:3aee2ca931b8 | 345 | |
laserdad | 4:3aee2ca931b8 | 346 | IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over |
laserdad | 4:3aee2ca931b8 | 347 | IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer |
laserdad | 4:3aee2ca931b8 | 348 | prevBaseline = IRbaseline; |
laserdad | 4:3aee2ca931b8 | 349 | IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new |
IDTsanjoseuser | 8:21738f9c5835 | 350 | |
laserdad | 4:3aee2ca931b8 | 351 | |
IDTsanjoseuser | 8:21738f9c5835 | 352 | //PRINT RAW DATA |
IDTsanjoseuser | 8:21738f9c5835 | 353 | pc.printf("%d\r\n%d\r\n",ppgData[2*n],ppgData[2*n+1]); |
laserdad | 9:98b9216f9ff3 | 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); |
IDTsanjoseuser | 8:21738f9c5835 | 355 | //PRINT AVG DATA |
IDTsanjoseuser | 8:21738f9c5835 | 356 | // pc.printf("%d\r\n%d\r\n",prevAvg/running_avg-prevBaseline/running_baseline,IRavg/running_avg-IRbaseline/running_baseline); |
IDTsanjoseuser | 8:21738f9c5835 | 357 | |
laserdad | 4:3aee2ca931b8 | 358 | } |
laserdad | 3:d3a1fc19bee6 | 359 | } |
laserdad | 4:3aee2ca931b8 | 360 | intFlagged = 0; |
laserdad | 3:d3a1fc19bee6 | 361 | } |
laserdad | 4:3aee2ca931b8 | 362 | } //end if !intb |
laserdad | 4:3aee2ca931b8 | 363 | }// end mode |
laserdad | 0:e9ae48b7f6f6 | 364 | else |
laserdad | 0:e9ae48b7f6f6 | 365 | { |
laserdad | 1:fb6b2b4d5d8a | 366 | wait_ms(sample_delay); |
laserdad | 5:ebe305e08430 | 367 | if( meas_ps ? ob1203.psIsNew() : ob1203.lsIsNew() ) |
laserdad | 1:fb6b2b4d5d8a | 368 | { |
laserdad | 5:ebe305e08430 | 369 | meas_ps ? valid = ob1203.get_ps_ls_data(ps_ls_data) : valid = ob1203.get_ls_data(ps_ls_data); |
laserdad | 7:3ed0f0522b43 | 370 | if(meas_ps) |
laserdad | 7:3ed0f0522b43 | 371 | { |
laserdad | 7:3ed0f0522b43 | 372 | ( ps_avg_ptr+1 == ps_running_avg ) ? ps_avg_ptr = 0 : ps_avg_ptr++; |
laserdad | 7:3ed0f0522b43 | 373 | PSprev = PS_avg_buffer[ps_avg_ptr]; //load the sample you are about to write over |
laserdad | 7:3ed0f0522b43 | 374 | PS_avg_buffer[ps_avg_ptr] = ps_ls_data[0]; //load the new sample in the buffer |
laserdad | 7:3ed0f0522b43 | 375 | PSavg += (PS_avg_buffer[ps_avg_ptr] - PSprev); //update the average by removing the old sample and adding the new |
laserdad | 7:3ed0f0522b43 | 376 | // 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 | 377 | } |
laserdad | 5:ebe305e08430 | 378 | if (meas_temp) |
laserdad | 5:ebe305e08430 | 379 | { |
laserdad | 5:ebe305e08430 | 380 | ob1203.setMainConfig(); |
laserdad | 5:ebe305e08430 | 381 | } |
laserdad | 4:3aee2ca931b8 | 382 | // 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 | 383 | 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 | 384 | |
laserdad | 1:fb6b2b4d5d8a | 385 | } |
laserdad | 2:ee175f471ecb | 386 | // else |
laserdad | 2:ee175f471ecb | 387 | // { |
laserdad | 2:ee175f471ecb | 388 | // pc.printf("status = %04X\r\n",ob1203.get_status() ); |
laserdad | 2:ee175f471ecb | 389 | // } |
laserdad | 0:e9ae48b7f6f6 | 390 | } |
laserdad | 1:fb6b2b4d5d8a | 391 | }//end while |
laserdad | 1:fb6b2b4d5d8a | 392 | }//end main |