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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SI1143.cpp Source File

SI1143.cpp

00001 /**
00002  * @author Guillermo A Torijano
00003  * 
00004  * @section LICENSE
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  *
00024  * @section DESCRIPTION
00025  *
00026  * Parallax SI1143 Gesture Sensor.
00027  *
00028  * Datasheet:
00029  *
00030  * http://www.silabs.com/Support%20Documents/TechnicalDocs/Si114x.pdf
00031  */
00032 
00033 #include "SI1143.h"
00034 
00035 SI1143::SI1143(PinName sda, PinName scl)
00036 {
00037     wait_ms(30);
00038     i2c_ = new I2C(sda, scl);
00039     // 3.4MHz, as specified by the datasheet, but DO NOT USE.
00040     //i2c_->frequency(3400000);
00041     
00042     restart();
00043 }
00044 
00045 void SI1143::restart()
00046 {
00047     command(RESET);
00048     wait_ms(30);
00049     
00050     // Setting up LED Power to full
00051     write_reg(HW_KEY,HW_KEY_VAL0);
00052     write_reg(PS_LED21,0xAA);
00053     write_reg(PS_LED3,0x0A);
00054     write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
00055     
00056     command(PARAM_SET + (CHLIST & 0x1F));
00057     
00058     write_reg(INT_CFG,0);
00059     write_reg(IRQ_ENABLE,0);
00060     write_reg(IRQ_MODE1,0);
00061     write_reg(IRQ_MODE2,0);
00062 }
00063 
00064 void SI1143::command(char cmd)
00065 {
00066     int val;
00067     
00068     val = read_reg(RESPONSE,1);
00069     while(val!=0)
00070     {
00071         write_reg(COMMAND,NOP);
00072         val = read_reg(RESPONSE,1);
00073     }
00074     do{
00075         write_reg(COMMAND,cmd);
00076         if(cmd==RESET) break;
00077         val = read_reg(RESPONSE,1);
00078     }while(val==0);
00079 }
00080 
00081 char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register
00082 {
00083     char tx[1];
00084     char rx[1];
00085     
00086     //i2c_->start();
00087     tx[0] = address;
00088     i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data);
00089     wait_ms(1);
00090     //i2c_->stop();
00091     
00092     //i2c_->start();
00093     i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data);
00094     wait_ms(1);
00095     //i2c_->stop();
00096     
00097     return rx[0];
00098 }
00099 
00100 void SI1143::write_reg(char address, char num_data) // Write a resigter
00101 {  
00102     char tx[2];
00103     
00104     //i2c_->start();
00105     tx[0] = address;
00106     tx[1] = num_data;
00107     i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
00108     wait_ms(1);
00109     //i2c_->stop();   
00110 }
00111 
00112 void SI1143::bias(int ready, int repeat)
00113 {
00114     wait(ready);
00115     bias1 = get_ps1(repeat);
00116     bias2 = get_ps2(repeat);
00117     bias3 = get_ps3(repeat);
00118 }
00119 
00120 int SI1143::get_ps1(int repeat) // Read the data for the first LED
00121 {
00122     int stack = 0;
00123     
00124     command(PSALS_FORCE);
00125     
00126     for(int r=repeat; r>0; r=r-1)
00127     {
00128         LowB = read_reg(PS1_DATA0,1);
00129         HighB = read_reg(PS1_DATA1,1);
00130         stack = stack + (HighB * 256) + LowB;
00131     }
00132     PS1 = stack / repeat;
00133     
00134     if(PS1 > bias1)
00135         PS1 = PS1 - bias1;
00136     else
00137         PS1 = 0;
00138     
00139     return PS1;
00140 }
00141 
00142 int SI1143::get_ps2(int repeat) // Read the data for the second LED
00143 {
00144     int stack = 0;
00145     
00146     command(PSALS_FORCE);
00147     
00148     for(int r=repeat; r>0; r=r-1)
00149     {
00150         LowB = read_reg(PS2_DATA0,1);
00151         HighB = read_reg(PS2_DATA1,1);
00152         stack = stack + (HighB * 256) + LowB;
00153     }
00154     PS2 = stack / repeat;
00155     
00156     if(PS2 > bias2)
00157         PS2 = PS2 - bias2;
00158     else
00159         PS2 = 0;
00160     
00161     return PS2;
00162 }
00163 
00164 int SI1143::get_ps3(int repeat) // Read the data for the third LED
00165 {
00166     int stack = 0;
00167     
00168     command(PSALS_FORCE);
00169     
00170     for(int r=repeat; r>0; r=r-1)
00171     {
00172         LowB = read_reg(PS3_DATA0,1);
00173         HighB = read_reg(PS3_DATA1,1);
00174         stack = stack + (HighB * 256) + LowB;
00175     }
00176     PS3 = stack / repeat;
00177     
00178     if(PS3 > bias3)
00179         PS3 = PS3 - bias3;
00180     else
00181         PS3 = 0;
00182     
00183     return PS3;
00184 }
00185 
00186 int SI1143::get_vis(int repeat) // Read the data for ambient light
00187 {
00188     int stack = 0;
00189     
00190     command(PSALS_FORCE);
00191     
00192     for(int r=repeat; r>0; r=r-1)
00193     {
00194         LowB = read_reg(ALS_VIS_DATA0,1);
00195         HighB = read_reg(ALS_VIS_DATA1,1);
00196         VIS = stack + (HighB * 256) + LowB;
00197     }
00198     VIS = stack / repeat;
00199     
00200     return VIS;
00201 }
00202 
00203 int SI1143::get_ir(int repeat) // Read the data for infrared light
00204 {
00205     int stack = 0;
00206     
00207     command(PSALS_FORCE);
00208     
00209     for(int r=repeat; r>0; r=r-1)
00210     {
00211         LowB = read_reg(ALS_IR_DATA0,1);
00212         HighB = read_reg(ALS_IR_DATA1,1);
00213         IR = stack + (HighB * 256) + LowB;
00214     }
00215     IR = stack / repeat;
00216     
00217     return IR;
00218 }