Library for using multiple SI1142 sensor

Fork of SI1141 by Dave Cohen

Revision:
10:a89555c4f62a
Parent:
9:31d4e955ee92
diff -r 31d4e955ee92 -r a89555c4f62a SI1143.cpp
--- a/SI1143.cpp	Fri May 23 21:34:54 2014 +0000
+++ b/SI1143.cpp	Wed Jun 04 23:12:42 2014 +0000
@@ -32,32 +32,57 @@
 
 #include "SI1143.h"
 
-SI1143::SI1143(PinName sda, PinName scl)
+SI1143::SI1143(I2C* comm, PinName vcc, PinName enable, char offset)
 {
     wait_ms(30);
-    i2c_ = new I2C(sda, scl);
-    // 3.4MHz, as specified by the datasheet, but DO NOT USE.
-    //i2c_->frequency(3400000);
+    
+    on = new DigitalOut(vcc);
+    en = new DigitalOut(enable);
     
-    restart();
+    on->write(0);
+    en->write(0);
+    wait_ms(30);
+
+    adrs = IR_ADDRESS;
+    i2c_ = comm;
+    
+    restart(offset);
 }
 
-void SI1143::restart()
-{
-    command(RESET);
+void SI1143::restart(char offset)
+{   
+    DigitalOut l3(LED3);
+    l3 = 0;
+    
+    wait_ms(30);
+    on->write(1);
+    wait_ms(30);
+    en->write(1);
     wait_ms(30);
     
+    //printf("%d\r\n",adrs);
+    
+    command(RESET);
+    l3 = 1;   
+    wait_ms(30);
     //Do this because you have to
     write_reg(HW_KEY,HW_KEY_VAL0);
+    //Give new address
+    write_reg(PARAM_WR, adrs + offset);
+    //write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
+    command(PARAM_SET + (I2C_ADDR & 0x1F));
+
+    //set new address
+    command(BUSADDR);
+    
+    adrs = adrs + offset;
     
     // Setting up LED Power to FULL JUICE
-    write_reg(PS_LED21,0x0F); // Was 0x0A
+    write_reg(PS_LED21,0xCC); // Was 0x0A
     write_reg(PS_LED3,0x00);
-    //write_reg(PS_LED21,0xAA);
-    //write_reg(PS_LED3,0x0A);
     
     //Activate ALS_IR, ALS_VIS, and PS1
-    write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK);
+    write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK);
     //write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
     command(PARAM_SET + (CHLIST & 0x1F));
     
@@ -67,7 +92,7 @@
     command(PARAM_SET + (PS_ADC_MISC & 0x1F));
     */
     //Set PS_ADC_COUNTER to lower time between ADC samples
-    write_reg(PARAM_WR, 0x00);
+    write_reg(PARAM_WR, 0x07);
     command(PARAM_SET + (PS_ADC_COUNTER & 0x1F));
     
     //Turn up the juice on PS ACD GAIN!
@@ -76,12 +101,12 @@
     
     
     write_reg(INT_CFG,0x01); // Configure Interrupts 
-    write_reg(IRQ_ENABLE,0x04); // Enable PS1
+    write_reg(IRQ_ENABLE,0x04); // Enable PS1 (0x04) Enable PS1 & PS2 (0x0C)
     write_reg(IRQ_MODE1,0);
     write_reg(IRQ_MODE2,0);
     
-    write_reg(MEAS_RATE,0x94); //0x84 - wakes up every 10 ms
-    write_reg(PS_RATE,0x08); //0x84 - wakes up every time measurement clock wakes up
+    //write_reg(MEAS_RATE,0x94); //0x84 - wakes up every 10 ms
+    //write_reg(PS_RATE,0x08); //0x8 - wakes up every time measurement clock wakes up
     
     //write_reg(PS_ADC_MISC,0x24);
     //write_reg(PS1_ADCMUX,0x03);
@@ -90,13 +115,16 @@
 void SI1143::command(char cmd)
 {
     int val;
+    DigitalOut l2(LED2);
     
     val = read_reg(RESPONSE,1);
     while(val!=0)
     {
         write_reg(COMMAND,NOP);
         val = read_reg(RESPONSE,1);
+        l2 = 1;
     }
+    l2 = 0;
     do{
         write_reg(COMMAND,cmd);
         if(cmd==RESET) break;
@@ -111,12 +139,12 @@
     
     //i2c_->start();
     tx[0] = address;
-    i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data);
+    i2c_->write((adrs << 1) & 0xFE, tx, num_data);
     wait_ms(1);
     //i2c_->stop();
     
     //i2c_->start();
-    i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data);
+    i2c_->read((adrs << 1) | 0x01, rx, num_data);
     wait_ms(1);
     //i2c_->stop();
     
@@ -130,7 +158,7 @@
     //i2c_->start();
     tx[0] = address;
     tx[1] = num_data;
-    i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
+    i2c_->write((adrs << 1) & 0xFE, tx, 2);
     wait_ms(1);
     //i2c_->stop();   
 }
@@ -140,7 +168,7 @@
     wait(ready);
     //bias1 = get_ps1(repeat);
     bias1 = 0;
-    bias2 = get_ps2(repeat);
+    bias2 = 0;
     bias3 = get_ps3(repeat);
 }
 
@@ -209,6 +237,21 @@
     return PS2;
 }
 
+int SI1143::read_ps2() // Read the data for the first LED (Used in autonomous mode)
+{
+    
+    LowB = read_reg(PS2_DATA0,1);
+    HighB = read_reg(PS2_DATA1,1);
+    PS2 = (HighB * 256) + LowB;
+    
+    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;
@@ -268,4 +311,29 @@
 void SI1143::clear_int() // Clear Interrupt register when an interrupt occurs.
 {
     write_reg(IRQ_STATUS, 0x3F); // Writes one to every interrupt status, thereby clearing them
+}
+
+char SI1143::read_PSint() // Clear Interrupt register when an interrupt occurs.
+{
+    char tmp;
+    char res;
+    
+    DigitalOut led3a(LED3);
+    DigitalOut led4a(LED4);
+    
+    tmp = read_reg(IRQ_STATUS, 1); // Reads iterrupt register
+    if( (tmp & 0x0C) == 0x0C ){
+        res = INT_PS12;
+    }
+    else if( (tmp & 0x0C) == 0x04 ){
+        res = INT_PS1;
+    }
+    else if( (tmp & 0x0C) == 0x08 ){
+        res = INT_PS2;
+    }
+    else {
+        res = 0;  
+    }
+    return res;    
+    
 }
\ No newline at end of file