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:
Thu Oct 17 12:53:08 2013 +0000
Child:
1:28beeb2f209b
Commit message:
1st edit;

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI1143.cpp	Thu Oct 17 12:53:08 2013 +0000
@@ -0,0 +1,269 @@
+#include "SI1143.h"
+
+SI1143::SI1143(PinName sda, PinName scl)
+{
+    wait_ms(30);
+    i2c_ = new I2C(sda, scl);
+    //3.4MHz, as specified by the datasheet.
+    i2c_->frequency(3400000);
+    
+    command(RESET);
+    wait_ms(30);
+    //bias1=0;
+    //bias2=0;
+    //bias3=0;
+    //PS1=0;
+    //PS2=0;
+    //PS3=0;
+    
+    write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full
+    //write_reg(PS_LED21,0xFF);
+    write_reg(PS_LED21,0xAA);
+    //write_reg(PS_LED3, 0x0F);
+    write_reg(PS_LED3,0x0A);
+  //param_set(CHLIST,0b00010111);
+    //param_set(CHLIST,0x17);
+    //write_reg(PARAM_WR, 0x17);
+    write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
+    
+    //write_reg(COMMAND, 0xA0|CHLIST);
+    command(PARAM_SET + (CHLIST & 0x1F));
+    
+    write_reg(INT_CFG,0);
+    write_reg(IRQ_ENABLE,0);
+    write_reg(IRQ_MODE1,0);
+    write_reg(IRQ_MODE2,0);
+  
+  //void write_reg(byte address, byte val) {  // Write a resigter
+  //Wire.beginTransmission(IR_ADDRESS); 
+  //Wire.write(address);      
+  //Wire.write(val);       
+  //Wire.endTransmission();     
+//}
+  
+    //char parameter = read_reg(PARAM_RD,1);
+ // Serial.print("CHLIST = ");
+ // Serial.println(parameter,BIN);
+  //delay(1000);
+    //wait(2);
+    /*
+    for (int i=0; i<20; i++)
+    {
+          //write_reg(COMMAND,0b00000101);
+        write_reg(COMMAND,0x05);
+          //delay(50);
+        //wait(0.05);
+          
+          //char LowB = read_reg(PS1_DATA0,1);
+          //char HighB = read_reg(PS1_DATA1,1);
+        LowB = read_reg(PS1_DATA0,1);
+        HighB = read_reg(PS1_DATA1,1);
+          
+        bias1 = bias1 + (((HighB * 255) + LowB) / 20);
+          
+        LowB = read_reg(PS2_DATA0,1);
+        HighB = read_reg(PS2_DATA1,1);
+          
+        bias2 = bias2 + (((HighB * 255) + LowB) / 20);
+          
+        LowB = read_reg(PS3_DATA0,1);
+        HighB = read_reg(PS3_DATA1,1);
+          
+        bias3 = bias3 + (((HighB * 255) + LowB) / 20);
+    }*/
+}
+
+void SI1143::command(char cmd)
+{
+    int val;
+    
+    val = read_reg(RESPONSE,1);
+    while(val!=0)
+    {
+        write_reg(COMMAND,NOP);
+        val = read_reg(RESPONSE,1);
+        printf("%d\r\n",val);
+    }
+    do{
+        write_reg(COMMAND,cmd);
+        if(cmd==RESET) break;
+        val = read_reg(RESPONSE,1);
+        //printf("%d\r\n",val);
+    }while(val==0);
+}
+
+char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a Register
+{
+    //
+  //unsigned char data;
+    //char cmd[2]={address,num_data};
+    //int data;
+    
+    //i2c_->start();
+    //data = i2c_->read(IR_ADDRESS,cmd,2);
+    //wait(0.05);
+    //i2c_->stop();
+    
+    //return data;
+    
+    char tx[1];
+    char rx[1];
+    
+    i2c_->start();
+    tx[0] = address;
+    i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data);
+    wait_ms(1);
+    i2c_->stop();
+    
+    i2c_->start();
+    i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data);
+    wait_ms(1);
+    i2c_->stop();
+    
+    return rx[0];
+
+  //Wire.beginTransmission(IR_ADDRESS);
+  //Wire.write(address);
+  //Wire.endTransmission();
+
+  //Wire.requestFrom(IR_ADDRESS, num_data);
+  
+  //while(Wire.available() < num_data);
+  
+  //return Wire.read();
+}
+
+void SI1143::write_reg(char address, char num_data)
+{  // Write a resigter
+    //char cmd[2]={address,val};
+    
+    //i2c_->start();
+    //i2c_->write(IR_ADDRESS,cmd,1);
+    //wait(0.05);
+    //i2c_->stop();
+    
+    char tx[2];
+
+    tx[0] = address;
+    tx[1] = num_data;
+    //tx[2] = data;
+    
+    i2c_->start();
+    i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
+    wait_ms(1);
+    i2c_->stop();
+    
+  //Wire.beginTransmission(IR_ADDRESS); 
+  //Wire.write(address);      
+  //Wire.write(val);       
+  //Wire.endTransmission();     
+}
+
+void SI1143::bias()
+{
+    //command(PSALS_FORCE);
+    sample(0);
+    bias1 = PS1;
+    bias2 = PS2;
+    bias3 = PS3;
+}
+
+int SI1143::sample(int point)
+{
+    //int data[5];
+    command(PSALS_FORCE);
+    //write_reg(COMMAND,0x05); // Get a reading
+    //delay(5);
+    //wait(0.5);
+    
+    LowB = read_reg(ALS_VIS_DATA0,1); // Read the data for the first LED
+    HighB = read_reg(ALS_VIS_DATA1,1);
+    VIS = (HighB * 256) + LowB;
+    
+    LowB = read_reg(ALS_IR_DATA0,1); // Read the data for the first LED
+    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 first LED
+    HighB = read_reg(PS2_DATA1,1);
+    PS2 = (HighB * 256) + LowB;
+    
+    LowB = read_reg(PS3_DATA0,1); // Read the data for the first LED
+    HighB = read_reg(PS3_DATA1,1);
+    PS3 = (HighB * 256) + LowB;
+    
+    if(PS1 > bias1)
+        PS1 = PS1 - bias1;
+    else
+        PS1 = 0;
+    if(PS2 > bias2)
+        PS2 = PS2 - bias2;
+    else
+        PS2 = 0;
+    if(PS3 > bias3)
+        PS3 = PS3 - bias3;
+    else
+        PS3 = 0;
+    
+    switch(point)
+    {
+        case 1:     return VIS;
+        case 2:     return IR;
+        case 3:     return PS1;
+        case 4:     return PS2;
+        case 5:     return PS3;
+        default:    return 0;
+    }
+    //data[0] = VIS;
+    //data[1] = IR;
+    //data[2] = PS1;
+    //data[3] = PS2;
+    //data[4] = PS3;
+    
+    //return data;
+    
+    //return PS1;
+}
+/*
+int SI1143::read1()
+{
+    write_reg(COMMAND,0x05); // Get a reading
+    //delay(5);
+    //wait(0.5);
+    
+    LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
+    HighB = read_reg(PS1_DATA1,1);
+    PS1 = ((HighB * 255) + LowB) - bias1;
+    
+    return PS1;
+}
+        
+int SI1143::read2()
+{
+    write_reg(COMMAND,0x05); // Get a reading
+    //delay(5);
+    //wait(0.5);
+    
+    LowB = read_reg(PS2_DATA0,1);  // Read the data for the second LED
+    HighB = read_reg(PS2_DATA1,1);
+    PS2 = (HighB * 255) + LowB - bias2;
+    
+    return PS2;
+}
+        
+int SI1143::read3()
+{
+    write_reg(COMMAND,0x05); // Get a reading
+    //delay(5);
+    //wait(0.5);
+    
+    LowB = read_reg(PS3_DATA0,1);  // Read the data for the third LED
+    HighB = read_reg(PS3_DATA1,1);
+    PS3 = (HighB * 255) + LowB - bias3;
+    
+    return PS3;
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI1143.h	Thu Oct 17 12:53:08 2013 +0000
@@ -0,0 +1,157 @@
+#ifndef SI1143_h
+#define SI1143_h
+
+#include "mbed.h"
+
+#define IR_ADDRESS      0x5A
+#define HW_KEY_VAL0     0x17     //Value to write into the HW Key register
+
+// register addresses
+#define PART_ID        0x00
+#define REV_ID         0x01
+#define SEQ_ID         0x02  //Si114x-A11 (MAJOR_SEQ=1, MINOR_SEQ=1)
+#define INT_CFG        0x03
+#define IRQ_ENABLE     0x04
+#define IRQ_MODE1      0x05
+#define IRQ_MODE2      0x06
+#define HW_KEY         0x07
+
+#define MEAS_RATE      0x08
+#define ALS_RATE       0x09
+#define PS_RATE        0x0A
+
+#define ALS_LOW_TH0    0x0B
+#define ALS_LOW_TH1    0x0C
+#define ALS_HI_TH0     0x0D
+#define ALS_HI_TH1     0x0E
+
+#define PS_LED21       0x0F
+#define PS_LED3        0x10
+
+#define PS1_TH0        0x11
+#define PS1_TH1        0x12
+#define PS2_TH0        0x13
+#define PS2_TH1        0x14
+#define PS3_TH0        0x15
+
+#define PS3_TH1        0x16
+#define PARAM_WR       0x17
+#define COMMAND        0x18
+
+#define RESPONSE       0x20
+#define IRQ_STATUS     0x21
+
+#define ALS_VIS_DATA0  0x22
+#define ALS_VIS_DATA1  0x23
+#define ALS_IR_DATA0   0x24
+#define ALS_IR_DATA1   0x25
+
+#define PS1_DATA0      0x26
+#define PS1_DATA1      0x27
+#define PS2_DATA0      0x28
+#define PS2_DATA1      0x29
+#define PS3_DATA0      0x2A
+#define PS3_DATA1      0x2B
+
+
+#define AUX_DATA0      0x2C
+#define AUX_DATA1      0x2D
+
+#define PARAM_RD       0x2E
+#define CHIP_STAT      0x30
+#define ANA_IN_KEY     0x3B
+
+// Command Register Values
+
+#define PARAM_QUERY         0x80    //Value is ORed with Parameter Offset
+#define PARAM_SET           0xA0    //Value is ORed with Parameter Offset
+#define PARAM_AND           0xC0    //Value is ORed with Parameter Offset
+#define PARAM_OR            0xE0    //Value is ORed with Parameter Offset
+#define NOP                 0x00
+#define RESET               0x01
+#define BUSADDR             0x02
+#define PS_FORCE            0x05
+#define ALS_FORCE           0x06
+#define PSALS_FORCE         0x07
+#define PS_PAUSE            0x09
+#define ALS_PAUSE           0x0A
+#define PSALS_PAUSE         0x0B
+#define PS_AUTO             0x0D
+#define ALS_AUTO            0x0E
+#define PSALS_AUTO          0x0F
+
+// ram addresses
+
+#define I2C_ADDR                  0x00
+#define CHLIST                    0x01
+#define PSLED12_SELECT            0x02  
+#define PSLED3_SELECT             0x03
+#define FILTER_EN                 0x04
+#define PS_ENCODING               0x05
+#define ALS_ENCODING              0x06
+#define PS1_ADCMUX                0x07
+#define PS2_ADCMUX                0x08
+#define PS3_ADCMUX                0x09
+#define PS_ADC_COUNTER            0x0A
+#define PS_ADC_GAIN               0x0B
+#define PS_ADC_MISC               0x0C
+//#define ALS_IR_ADCMUX             0x0E
+//#define AUX_ADCMUX                0x0F
+#define ALS1_ADCMUX               0x0D
+#define ALS2_ADCMUX               0x0E
+#define ALS3_ADCMUX               0x0F
+#define ALS_VIS_ADC_COUNTER       0x10
+#define ALS_VIS_ADC_GAIN          0x11
+#define ALS_VIS_ADC_MISC          0x12
+#define ALS_HYST                  0x16
+#define PS_HYST                   0x17
+#define PS_HISTORY                0x18
+#define ALS_HISTORY               0x19
+#define ADC_OFFSET                0x1A
+#define SLEEP_CTRL                0x1B
+#define LED_REC                   0x1C
+#define ALS_IR_ADC_COUNTER        0x1D
+#define ALS_IR_ADC_GAIN           0x1E
+#define ALS_IR_ADC_MISC           0x1F
+
+//Measurement Channel List
+#define PS1_TASK               0x01
+#define PS2_TASK               0x02
+#define PS3_TASK               0x04
+#define ALS_VIS_TASK           0x10
+#define ALS_IR_TASK            0x20
+#define AUX_TASK               0x40
+
+class SI1143
+{
+    public:
+    
+        SI1143(PinName sda, PinName scl);
+        
+        int sample(int point);
+        
+        void bias(void);
+        
+        //int read1(void);
+        
+        //int read2(void);
+        
+        //int read3(void);
+        
+    private:
+    
+        I2C* i2c_;
+        //int reg, val;
+        int bias1,bias2,bias3,PS1,PS2,PS3,VIS,IR;
+        char LowB,HighB;
+        
+        //void command(int address, int num_data);
+        void command(char cmd);
+        
+        char read_reg(/*unsigned*/ char address, int num_data); // Read a Register
+        
+        void write_reg(char address, char num_data);// Write a resigter
+        
+};
+
+#endif
\ No newline at end of file