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