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