IDT Optical Sensors / Mbed 2 deprecated OB1203_IDT

Dependencies:   mbed OB1203_example_driver

Revision:
4:3aee2ca931b8
Parent:
3:d3a1fc19bee6
Child:
5:ebe305e08430
--- a/main.cpp	Wed May 09 20:44:12 2018 +0000
+++ b/main.cpp	Mon May 14 16:58:30 2018 +0000
@@ -1,12 +1,13 @@
 #include "mbed.h"
 #include "OB1203.h"
 #include "math.h"
-
+//#include "SoftI2C.h"
 
 #define intb_pin D3
 
 
 I2C i2c(I2C_SDA,I2C_SCL);
+//SoftI2C i2c(I2C_SDA,I2C_SCL);
 InterruptIn intb(intb_pin);
 DigitalOut sda_pullup(D11,1);
 DigitalOut scl_pullup(D12,1);
@@ -20,21 +21,25 @@
 
 
 bool mode = 1; //0 for PS_LS, 1 for PPG
+bool meas_ps = 0;
 bool spo2 = 1; //0 for HR, 1 for SpO2
 bool intFlagged =0;
-
+bool afull = 1; //use Afull interrupt--otherwise PPG new data interrupt
+bool meas_temp = 0;
 
 void defaultConfig()
 {
-    ob1203.ls_rate = LS_RATE(2); //100ms
-    ob1203.ls_res = LS_RES(2); //18bit 100ms
-    ob1203.ls_gain = LS_GAIN(1); //gain 3 default (range)
+    meas_temp ? ob1203.temp_en = TEMP_ON : ob1203.temp_en = TEMP_OFF;
+
+    ob1203.ls_res = LS_RES(0); //2= 18bit 100ms, 0= max res
+    ob1203.ls_rate = LS_RATE(4); //2 =100ms, 4 = 500ms
+    ob1203.ls_gain = LS_GAIN(3); //gain 3 default (range)
     ob1203.ls_thres_hi = 0x000FFFFF;
     ob1203.ls_thres_lo = 0;
     ob1203.ls_sai = LS_SAI_OFF;
     ob1203.ls_mode = RGB_MODE;
     ob1203.ls_en = LS_ON;
-    
+
     ob1203.ps_sai_en = PS_SAI_OFF;
     if(mode)
     {
@@ -61,7 +66,8 @@
     ob1203.ps_can_ana = PS_CAN_ANA_0;
     ob1203.ps_digital_can = 0;
     ob1203.ps_hys_level = 0;
-    ob1203.ps_current = 0x3FF;
+    meas_ps ? ob1203.ps_current = 0x3FF : ob1203.ps_current = 0x000;
+//    ob1203.ps_current = 0;
     ob1203.ps_thres_hi = 0xFF;
     ob1203.ps_thres_lo = 0x00;
     
@@ -70,29 +76,39 @@
     ob1203.ls_var_mode = LS_THRES_INT_MODE;
     ob1203.ls_int_en = LS_INT_ON;
     ob1203.ppg_ps_en = PPG_PS_ON;
-    ob1203.afull_int_en = AFULL_INT_ON;
-    ob1203.ppg_int_en = PPG_INT_OFF;
+
     ob1203.ps_logic_mode = PS_INT_READ_CLEARS;
     ob1203.ps_int_en = PS_INT_ON;
     ob1203.ls_persist = LS_PERSIST(2);
     ob1203.ps_persist = PS_PERSIST(2);
-
+    //int
+    if(afull)
+    {
+        ob1203.afull_int_en = AFULL_INT_ON;
+        ob1203.ppg_int_en = PPG_INT_OFF;
+    }
+    else
+    {     
+        ob1203.afull_int_en = AFULL_INT_OFF;
+        ob1203.ppg_int_en = PPG_INT_ON;
+    }
     //PPG
-    ob1203.ir_current = 0x1FF;
+    ob1203.ir_current = 275;
     if (spo2)
     {
-        ob1203.r_current = 0x1FF;
+//        ob1203.r_current = 0x0FF;
+    ob1203.r_current = 255;
     }
     else 
     {   
         ob1203.r_current = 0;
     }
-    ob1203.ppg_ps_gain = PPG_PS_GAIN_2;
+    ob1203.ppg_ps_gain = PPG_PS_GAIN_4;
     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(3);
+    ob1203.ppg_avg = PPG_AVG(2); //2^n
     ob1203.ppg_rate = PPG_RATE(1);
     ob1203.ppg_pwidth = PPG_PWIDTH(3);
     ob1203.ppg_freq = PPG_FREQ_60HZ;
@@ -114,7 +130,7 @@
     }
     else    
     {
-        ob1203.init_ps_rgb();
+       ob1203.init_ps_rgb();
     }
 }
 
@@ -145,8 +161,11 @@
 
 int main() 
 {
-    uint32_t running_avg = 8;
+    int numInts = 0;
+    uint32_t running_avg = 65;
     char avg_ptr = 0;
+    int baseline_ptr = 0;
+    uint32_t running_baseline = 850;
     int first = 1;
     uint32_t IRavg = 0;
     uint32_t Ravg = 0;
@@ -155,9 +174,16 @@
     uint32_t prevAvg = 0;
     uint32_t IR_avg_buffer[running_avg];
     uint32_t R_avg_buffer[running_avg];
+    uint32_t IR_baseline_buffer[running_baseline];
+    uint32_t R_baseline_buffer[running_baseline];
+    uint32_t IRbaseline_prev=0;;
+    uint32_t Rbaseline_prev=0;;
+    uint32_t IRbaseline=0;
+    uint32_t Rbaseline=0;
+    uint32_t prevBaseline=0;
     i2c.frequency( 400000 );
     char valid;
-    uint32_t ps_ls_data[6];
+    uint32_t ps_ls_data[7];
     char samples2Read = 4; //samples, e.g. 4 samples * 3 bytes = 12 bytes (or 2 SpO2 samples)
     char fifoBuffer[samples2Read*3];
     uint32_t ppgData[samples2Read];  
@@ -187,77 +213,126 @@
         {
             if(!intb.read())
             {
+                if(!afull)
+                {
+                    numInts++; //get data every other interrupt if ppg new data interrupt mode
+                }
+                else
+                {
+                    numInts = samples2Read;
+                }
                 intFlagged = 1;
-            }
-            if(intFlagged)
-            {
-                
-                ob1203.getFifoSamples(samples2Read,fifoBuffer);
-//                for (int n=0;n<samples2Read*3;n++)
-//                {
-//                    pc.printf("%02X ",fifoBuffer[n]);
-//                }
-//                pc.printf("\r\n");
-                ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData);
-//                for (int n=0;n<samples2Read;n++)
-//                {
-//                    pc.printf("%d ",ppgData[n]);
-//                }
-//                pc.printf("\r\n");
-                if(first) //populate average buffers the first time we run
+                if( (numInts < samples2Read) && !afull)
+                {
+                    ob1203.get_status();
+                }
+                else if(intFlagged && (numInts==samples2Read) )
                 {
-                    for(int n=0;n<running_avg;n++)
+                    numInts = 0;
+                    ob1203.getFifoSamples(samples2Read,fifoBuffer);
+    //                for (int n=0;n<samples2Read*3;n++)
+    //                {
+    //                    pc.printf("%02X ",fifoBuffer[n]);
+    //                }
+    //                pc.printf("\r\n");
+                    ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData);
+    //                for (int n=0;n<samples2Read;n++)
+    //                {
+    //                    pc.printf("%d ",ppgData[n]);
+    //                }
+    //                pc.printf("\r\n");
+                    if(first) //populate average and baseline buffers the first time we run
                     {
-                        IR_avg_buffer[n] = ppgData[0];
-                        IRavg += IR_avg_buffer[n];
+                        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];
+                            }
+                            
+                        }
+                        for( int n=0;n<running_baseline;n++)
+                        {
+//                            IR_baseline_buffer[n] = ppgData[0];
+                            IR_baseline_buffer[n] = 0;
+                            if(spo2)
+                            {
+//                                R_baseline_buffer[n] = ppgData[1];  
+                                 R_baseline_buffer[n] = 0;
+                            }
+                        }
+                        first = 0;
+//                        IRbaseline = running_baseline*ppgData[0];
+//                        Rbaseline = running_baseline*ppgData[1]; 
+                    }
+                    
+//                    IRbaseline = running_baseline*ppgData[0];             
+                    for (int n=0;n<samples2Read/2;n++)
+                    {
+                        ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++;
+                        ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_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
+                        
+                        IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over
+                        IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer
+                        IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new
+                        
                         if(spo2)
                         {
-                            R_avg_buffer[n] = ppgData[1];
-                            Ravg += R_avg_buffer[n];
+                            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]);
+    
+                            Rbaseline_prev = R_baseline_buffer[baseline_ptr];
+                            R_baseline_buffer[baseline_ptr] = ppgData[2*n+1];
+                            Rbaseline += (R_baseline_buffer[baseline_ptr] - Rbaseline_prev);
+                            
+                            
+                            pc.printf("%d,%d\r\n",IRavg/running_avg,Ravg/running_avg);
+//                            pc.printf("%d,%d\r\n",IRavg/running_avg-IRbaseline/running_baseline, Ravg/running_avg-Rbaseline/running_baseline);
+    
+//                            pc.printf("%d,%d,%d,%d\r\n",Ravg/running_avg,Rbaseline/running_baseline,Rbaseline_prev,Ravg/running_avg-Rbaseline/running_baseline);
                         }
-                        
+                        else
+                        {
+                            ( avg_ptr+1 == running_avg ) ? avg_ptr = 0 : avg_ptr++;
+                            ( baseline_ptr+1 == running_baseline) ? baseline_ptr = 0 : baseline_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);
+                            
+                            IRbaseline_prev = IR_baseline_buffer[baseline_ptr]; //load the sample you are about to write over
+                            IR_baseline_buffer[baseline_ptr] = ppgData[2*n]; //load the new sample in the buffer
+                            prevBaseline = IRbaseline;
+                            IRbaseline += (IR_baseline_buffer[baseline_ptr] - IRbaseline_prev); //update the average by removing the old sample and adding the new
+                            
+                            pc.printf("%d\r\n%d\r\n",prevAvg/running_avg-prevBaseline/running_baseline,IRavg/running_avg-IRbaseline/running_baseline);
+
+    //                        pc.printf("%d\r\n%d\r\n",ppgData[2*n],ppgData[2*n+1]);
+                            
+                        }
                     }
-                    first = 0;
+                intFlagged = 0;   
                 }
-                                
-                for (int n=0;n<samples2Read/2;n++)
-                {
-                    ( 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;   
-        
-            }
-        }
+            } //end if !intb
+        }// end mode
         else
         {
             wait_ms(sample_delay);
             if(ob1203.dataIsNew())
             {
                 valid = ob1203.get_ps_ls_data(ps_ls_data);
-                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]);
+//                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]);
+                  pc.printf("%d %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],ps_ls_data[6]);
+            
             }
 //            else
 //            {