Library for using multiple SI1142 sensor

Fork of SI1141 by Dave Cohen

Revision:
5:3fadc61598bc
Parent:
4:af8f820733e0
Child:
7:f1303b85db58
--- a/SI1143.cpp	Thu Oct 17 23:23:33 2013 +0000
+++ b/SI1143.cpp	Mon Oct 21 20:12:56 2013 +0000
@@ -36,7 +36,7 @@
 {
     wait_ms(30);
     i2c_ = new I2C(sda, scl);
-    //3.4MHz, as specified by the datasheet, but DO NOT USE.
+    // 3.4MHz, as specified by the datasheet, but DO NOT USE.
     //i2c_->frequency(3400000);
     
     restart();
@@ -47,7 +47,8 @@
     command(RESET);
     wait_ms(30);
     
-    write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full
+    // Setting up LED Power to full
+    write_reg(HW_KEY,HW_KEY_VAL0);
     write_reg(PS_LED21,0xAA);
     write_reg(PS_LED3,0x0A);
     write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
@@ -99,80 +100,119 @@
 void SI1143::write_reg(char address, char num_data) // Write a resigter
 {  
     char tx[2];
-
+    
+    //i2c_->start();
     tx[0] = address;
     tx[1] = num_data;
-    
-    //i2c_->start();
     i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
     wait_ms(1);
     //i2c_->stop();   
 }
 
-void SI1143::bias()
+void SI1143::bias(int ready, int repeat)
 {
-    sample(0);
-    bias1 = PS1;
-    bias2 = PS2;
-    bias3 = PS3;
+    wait(ready);
+    bias1 = get_ps1(repeat);
+    bias2 = get_ps2(repeat);
+    bias3 = get_ps3(repeat);
 }
 
-int SI1143::sample(int point)
+int SI1143::get_ps1(int repeat) // Read the data for the first LED
 {
-    //int data[5];
+    int stack = 0;
+    
     command(PSALS_FORCE);
     
-    LowB = read_reg(ALS_VIS_DATA0,1); // Read the data for ambient light
-    HighB = read_reg(ALS_VIS_DATA1,1);
-    VIS = (HighB * 256) + LowB;
-    
-    LowB = read_reg(ALS_IR_DATA0,1); // Read the data for infrared light
-    HighB = read_reg(ALS_IR_DATA1,1);
-    IR = (HighB * 256) + LowB;
-    
-    LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
-    HighB = read_reg(PS1_DATA1,1);
-    PS1 = (HighB * 256) + LowB;
-    
-    LowB = read_reg(PS2_DATA0,1); // Read the data for the second LED
-    HighB = read_reg(PS2_DATA1,1);
-    PS2 = (HighB * 256) + LowB;
-    
-    LowB = read_reg(PS3_DATA0,1); // Read the data for the third LED
-    HighB = read_reg(PS3_DATA1,1);
-    PS3 = (HighB * 256) + LowB;
+    for(int r=repeat; r>0; r=r-1)
+    {
+        LowB = read_reg(PS1_DATA0,1);
+        HighB = read_reg(PS1_DATA1,1);
+        stack = stack + (HighB * 256) + LowB;
+    }
+    PS1 = stack / repeat;
     
     if(PS1 > bias1)
         PS1 = PS1 - bias1;
     else
         PS1 = 0;
+    
+    return PS1;
+}
+
+int SI1143::get_ps2(int repeat) // Read the data for the second LED
+{
+    int stack = 0;
+    
+    command(PSALS_FORCE);
+    
+    for(int r=repeat; r>0; r=r-1)
+    {
+        LowB = read_reg(PS2_DATA0,1);
+        HighB = read_reg(PS2_DATA1,1);
+        stack = stack + (HighB * 256) + LowB;
+    }
+    PS2 = stack / repeat;
+    
     if(PS2 > bias2)
         PS2 = PS2 - bias2;
     else
         PS2 = 0;
+    
+    return PS2;
+}
+
+int SI1143::get_ps3(int repeat) // Read the data for the third LED
+{
+    int stack = 0;
+    
+    command(PSALS_FORCE);
+    
+    for(int r=repeat; r>0; r=r-1)
+    {
+        LowB = read_reg(PS3_DATA0,1);
+        HighB = read_reg(PS3_DATA1,1);
+        stack = stack + (HighB * 256) + LowB;
+    }
+    PS3 = stack / repeat;
+    
     if(PS3 > bias3)
         PS3 = PS3 - bias3;
     else
         PS3 = 0;
     
-    switch(point)
-    {
-        case 1:     return PS1;
-        case 2:     return PS2;
-        case 3:     return PS3;
-        case 4:     return VIS;
-        case 5:     return IR;
-        default:    return 0;
-    }
-    //data[0] = VIS;
-    //data[1] = IR;
-    //data[2] = PS1;
-    //data[3] = PS2;
-    //data[4] = PS3;
-    
-    //return data;
-    
-    //return PS1;
+    return PS3;
 }
 
+int SI1143::get_vis(int repeat) // Read the data for ambient light
+{
+    int stack = 0;
+    
+    command(PSALS_FORCE);
+    
+    for(int r=repeat; r>0; r=r-1)
+    {
+        LowB = read_reg(ALS_VIS_DATA0,1);
+        HighB = read_reg(ALS_VIS_DATA1,1);
+        VIS = stack + (HighB * 256) + LowB;
+    }
+    VIS = stack / repeat;
+    
+    return VIS;
+}
 
+int SI1143::get_ir(int repeat) // Read the data for infrared light
+{
+    int stack = 0;
+    
+    command(PSALS_FORCE);
+    
+    for(int r=repeat; r>0; r=r-1)
+    {
+        LowB = read_reg(ALS_IR_DATA0,1);
+        HighB = read_reg(ALS_IR_DATA1,1);
+        IR = stack + (HighB * 256) + LowB;
+    }
+    IR = stack / repeat;
+    
+    return IR;
+}