Starting library to get the SI1143 Gesture Sensor to work. Currently only works in forced conversion mode.

Dependents:   Gesture_Sensor Gesture_Arm_Robot mbed_gesture Gesture_Sensor_Sample ... more

Files at this revision

API Documentation at this revision

Comitter:
GAT27
Date:
Mon Oct 21 20:12:56 2013 +0000
Parent:
4:af8f820733e0
Child:
6:50b60d59d568
Commit message:
Slight modification for faster reads.

Changed in this revision

SI1143.cpp Show annotated file Show diff for this revision Revisions of this file
SI1143.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
+}
--- a/SI1143.h	Thu Oct 17 23:23:33 2013 +0000
+++ b/SI1143.h	Mon Oct 21 20:12:56 2013 +0000
@@ -178,28 +178,58 @@
         SI1143(PinName sda, PinName scl);
         
         /**
-         * Takes a sample from the device and returns a raw output.
-         *
-         * @param   point Used to get a specific value from the device.
-         *          A value of 1 will output proximity of led1, a value of 2 will output proximity of led2,
-         *          a value of 3 will output proximity of led3, a value of 4 will output ambient light,
-         *          a value of 5 will output infrared light, and any other value will output 0.
-         * @return  In forced conversion output mode, will display a raw output minus any baseline,
-         *          where as the greater the value, the closer the object is to the device
-         *          (applies to proximity leds).
+         * Restarts the device.
          */
-        int sample(int point);
+        void restart(void);
         
         /**
          * Creates a baseline for sampling measurements.
          * Should be done early in your code and after a reset.
          */
-        void bias(void);
+        void bias(int ready, int repeat);
+        
+        /**
+         * Takes a number of samples from the proximity of led1 and returns a raw output.
+         *
+         * @param   repeat Tells how many samples to get from the device. Each sample takes 4 ms.
+         * @return  In forced conversion output mode, will display a raw output of the average sample
+         *          minus any baseline, where as the greater the value, the closer the object is to the device.
+         */
+        int get_ps1(int repeat);
+        
+        /**
+         * Takes a number of samples from the proximity of led2 and returns a raw output.
+         *
+         * @param   repeat Tells how many samples to get from the device. Each sample takes 4 ms.
+         * @return  In forced conversion output mode, will display a raw output of the average sample
+         *          minus any baseline, where as the greater the value, the closer the object is to the device.
+         */
+        int get_ps2(int repeat);
         
         /**
-         * Restarts the device.
+         * Takes a number of samples from the proximity of led3 and returns a raw output.
+         *
+         * @param   repeat Tells how many samples to get from the device. Each sample takes 4 ms.
+         * @return  In forced conversion output mode, will display a raw output of the average sample
+         *          minus any baseline, where as the greater the value, the closer the object is to the device.
          */
-        void restart(void);
+        int get_ps3(int repeat);
+        
+        /**
+         * Takes a number of samples for ambient light on device and returns a raw output.
+         *
+         * @param   repeat Tells how many samples to get from the device. Each sample takes 4 ms.
+         * @return  In forced conversion output mode, will display a raw output of the average sample.
+         */
+        int get_vis(int repeat);
+        
+        /**
+         * Takes a number of samples for infrared light on device and returns a raw output.
+         *
+         * @param   repeat Tells how many samples to get from the device. Each sample takes 4 ms.
+         * @return  In forced conversion output mode, will display a raw output of the average sample.
+         */
+        int get_ir(int repeat);
         
     private: