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
Diff: main.cpp
- Revision:
- 3:d3a1fc19bee6
- Parent:
- 2:ee175f471ecb
- Child:
- 4:3aee2ca931b8
--- a/main.cpp Thu May 03 04:32:09 2018 +0000 +++ b/main.cpp Wed May 09 20:44:12 2018 +0000 @@ -19,7 +19,8 @@ int sample_delay = 25; //ms -bool mode = 1; //0 for PS_LS, 1 for SPO2 +bool mode = 1; //0 for PS_LS, 1 for PPG +bool spo2 = 1; //0 for HR, 1 for SpO2 bool intFlagged =0; @@ -37,7 +38,15 @@ ob1203.ps_sai_en = PS_SAI_OFF; if(mode) { - ob1203.ppg_ps_mode = SPO2_MODE; + if (spo2) + { + ob1203.ppg_ps_mode = SPO2_MODE; + } + else + { + ob1203.ppg_ps_mode = HR_MODE; + } + } else { @@ -45,8 +54,8 @@ } ob1203.ps_pulses = PS_PULSES(2); // pc.printf("ps_pulses = %02X\r\n",ob1203.ps_pulses); - ob1203.ps_pwidth = PS_PWIDTH(1); - ob1203.ps_rate = PS_RATE(5); + ob1203.ps_pwidth = PS_PWIDTH(4); + ob1203.ps_rate = PS_RATE(3); // pc.printf("ps_rate = %02X\r\n",ob1203.ps_rate); ob1203.ps_avg_en = PS_AVG_OFF; ob1203.ps_can_ana = PS_CAN_ANA_0; @@ -69,24 +78,39 @@ ob1203.ps_persist = PS_PERSIST(2); //PPG - ob1203.ir_current = 0x1CF; - ob1203.r_current = 0x1FF; + ob1203.ir_current = 0x1FF; + if (spo2) + { + ob1203.r_current = 0x1FF; + } + else + { + ob1203.r_current = 0; + } ob1203.ppg_ps_gain = PPG_PS_GAIN_2; ob1203.ppg_pow_save = PPG_POW_SAVE_OFF; ob1203.led_flip = LED_FLIP_OFF; ob1203.ch1_can_ana = PPG_CH1_CAN(0); ob1203.ch2_can_ana = PPG_CH2_CAN(0); - ob1203.ppg_avg = PPG_AVG(0); - ob1203.ppg_pwidth = PPG_PWIDTH(4); + ob1203.ppg_avg = PPG_AVG(3); + ob1203.ppg_rate = PPG_RATE(1); + ob1203.ppg_pwidth = PPG_PWIDTH(3); ob1203.ppg_freq = PPG_FREQ_60HZ; // ob1203.ppg_freq = PPG_FREQ_50HZ; - ob1203.ppg_rate = PPG_RATE(1); + ob1203.fifo_rollover_en = FIFO_ROLL_ON; ob1203.fifo_afull_samples_left = AFULL_SAMPLES_LEFT(8); if(mode) { - ob1203.init_spo2(); + if(spo2) + { + ob1203.init_spo2(); + } + else + { + ob1203.init_hr(); + } } else { @@ -121,6 +145,16 @@ int main() { + uint32_t running_avg = 8; + char avg_ptr = 0; + int first = 1; + uint32_t IRavg = 0; + uint32_t Ravg = 0; + uint32_t IRprev = 0; + uint32_t Rprev = 0; + uint32_t prevAvg = 0; + uint32_t IR_avg_buffer[running_avg]; + uint32_t R_avg_buffer[running_avg]; i2c.frequency( 400000 ); char valid; uint32_t ps_ls_data[6]; @@ -146,7 +180,7 @@ intb.fall(&intEvent); - + t.start(); while(1) { if(mode) @@ -170,9 +204,48 @@ // pc.printf("%d ",ppgData[n]); // } // pc.printf("\r\n"); + if(first) //populate average buffers the first time we run + { + for(int n=0;n<running_avg;n++) + { + IR_avg_buffer[n] = ppgData[0]; + IRavg += IR_avg_buffer[n]; + if(spo2) + { + R_avg_buffer[n] = ppgData[1]; + Ravg += R_avg_buffer[n]; + } + + } + first = 0; + } + for (int n=0;n<samples2Read/2;n++) { - pc.printf("%d, %d\r\n",ppgData[2*n],ppgData[2*n+1]); + ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; + IRprev = IR_avg_buffer[avg_ptr]; //load the sample you are about to write over + IR_avg_buffer[avg_ptr] = ppgData[2*n]; //load the new sample in the buffer + IRavg += (IR_avg_buffer[avg_ptr] - IRprev); //update the average by removing the old sample and adding the new + if(spo2) + { + Rprev = R_avg_buffer[avg_ptr]; + R_avg_buffer[avg_ptr] = ppgData[2*n+1]; + Ravg += (R_avg_buffer[avg_ptr] - Rprev); + // pc.printf("%d, %d, %d\r\n",t.read_us(),ppgData[2*n],ppgData[2*n+1]); +// pc.printf("%d, %d, %d, %d\r\n",IRavg/running_avg,Ravg/running_avg, IR_avg_buffer[avg_ptr],R_avg_buffer[avg_ptr]); + pc.printf("%d,%d\r\n",IRavg/running_avg,Ravg/running_avg); + } + else + { + ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++; + IRprev = IR_avg_buffer[avg_ptr]; + IR_avg_buffer[avg_ptr] = ppgData[2*n+1]; + prevAvg = IRavg; + IRavg += (IR_avg_buffer[avg_ptr] - IRprev); + pc.printf("%d\r\n%d\r\n",prevAvg/running_avg,IRavg/running_avg); +// pc.printf("%d\r\n%d\r\n",ppgData[2*n],ppgData[2*n+1]); + + } } intFlagged = 0;