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

SI1143.cpp

Committer:
GAT27
Date:
2013-10-17
Revision:
1:28beeb2f209b
Parent:
0:18ebb7348150
Child:
2:21381f11a5af

File content as of revision 1:28beeb2f209b:

#include "SI1143.h"

SI1143::SI1143(PinName sda, PinName scl)
{
    wait_ms(30);
    i2c_ = new I2C(sda, scl);
    //3.4MHz, as specified by the datasheet. (DO NOT USE)
    //i2c_->frequency(3400000);
    
    command(RESET);
    wait_ms(30);
    
    write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full
    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);
    
    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 SI1143::command(char cmd)
{
    int val;
    
    val = read_reg(RESPONSE,1);
    while(val!=0)
    {
        write_reg(COMMAND,NOP);
        val = read_reg(RESPONSE,1);
    }
    do{
        write_reg(COMMAND,cmd);
        if(cmd==RESET) break;
        val = read_reg(RESPONSE,1);
    }while(val==0);
}

char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register
{
    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];
}

void SI1143::write_reg(char address, char num_data) // Write a resigter
{  
    char tx[2];

    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()
{
    sample(0);
    bias1 = PS1;
    bias2 = PS2;
    bias3 = PS3;
}

int SI1143::sample(int point)
{
    //int data[5];
    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;
    
    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;
}