set up to autonomously sample with one sensor

Fork of SI1142 by Dave Cohen

Committer:
GAT27
Date:
Thu Oct 17 20:52:40 2013 +0000
Revision:
1:28beeb2f209b
Parent:
0:18ebb7348150
Child:
2:21381f11a5af
working, shows all 3 led in forced mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GAT27 0:18ebb7348150 1 #include "SI1143.h"
GAT27 0:18ebb7348150 2
GAT27 0:18ebb7348150 3 SI1143::SI1143(PinName sda, PinName scl)
GAT27 0:18ebb7348150 4 {
GAT27 0:18ebb7348150 5 wait_ms(30);
GAT27 0:18ebb7348150 6 i2c_ = new I2C(sda, scl);
GAT27 1:28beeb2f209b 7 //3.4MHz, as specified by the datasheet. (DO NOT USE)
GAT27 1:28beeb2f209b 8 //i2c_->frequency(3400000);
GAT27 0:18ebb7348150 9
GAT27 0:18ebb7348150 10 command(RESET);
GAT27 0:18ebb7348150 11 wait_ms(30);
GAT27 0:18ebb7348150 12
GAT27 0:18ebb7348150 13 write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full
GAT27 0:18ebb7348150 14 write_reg(PS_LED21,0xAA);
GAT27 0:18ebb7348150 15 write_reg(PS_LED3,0x0A);
GAT27 0:18ebb7348150 16 write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
GAT27 0:18ebb7348150 17
GAT27 0:18ebb7348150 18 command(PARAM_SET + (CHLIST & 0x1F));
GAT27 0:18ebb7348150 19
GAT27 0:18ebb7348150 20 write_reg(INT_CFG,0);
GAT27 0:18ebb7348150 21 write_reg(IRQ_ENABLE,0);
GAT27 0:18ebb7348150 22 write_reg(IRQ_MODE1,0);
GAT27 0:18ebb7348150 23 write_reg(IRQ_MODE2,0);
GAT27 0:18ebb7348150 24 }
GAT27 0:18ebb7348150 25
GAT27 0:18ebb7348150 26 void SI1143::command(char cmd)
GAT27 0:18ebb7348150 27 {
GAT27 0:18ebb7348150 28 int val;
GAT27 0:18ebb7348150 29
GAT27 0:18ebb7348150 30 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 31 while(val!=0)
GAT27 0:18ebb7348150 32 {
GAT27 0:18ebb7348150 33 write_reg(COMMAND,NOP);
GAT27 0:18ebb7348150 34 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 35 }
GAT27 0:18ebb7348150 36 do{
GAT27 0:18ebb7348150 37 write_reg(COMMAND,cmd);
GAT27 0:18ebb7348150 38 if(cmd==RESET) break;
GAT27 0:18ebb7348150 39 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 40 }while(val==0);
GAT27 0:18ebb7348150 41 }
GAT27 0:18ebb7348150 42
GAT27 1:28beeb2f209b 43 char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register
GAT27 0:18ebb7348150 44 {
GAT27 0:18ebb7348150 45 char tx[1];
GAT27 0:18ebb7348150 46 char rx[1];
GAT27 0:18ebb7348150 47
GAT27 1:28beeb2f209b 48 //i2c_->start();
GAT27 0:18ebb7348150 49 tx[0] = address;
GAT27 0:18ebb7348150 50 i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data);
GAT27 0:18ebb7348150 51 wait_ms(1);
GAT27 1:28beeb2f209b 52 //i2c_->stop();
GAT27 0:18ebb7348150 53
GAT27 1:28beeb2f209b 54 //i2c_->start();
GAT27 0:18ebb7348150 55 i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data);
GAT27 0:18ebb7348150 56 wait_ms(1);
GAT27 1:28beeb2f209b 57 //i2c_->stop();
GAT27 0:18ebb7348150 58
GAT27 0:18ebb7348150 59 return rx[0];
GAT27 0:18ebb7348150 60 }
GAT27 0:18ebb7348150 61
GAT27 1:28beeb2f209b 62 void SI1143::write_reg(char address, char num_data) // Write a resigter
GAT27 1:28beeb2f209b 63 {
GAT27 0:18ebb7348150 64 char tx[2];
GAT27 0:18ebb7348150 65
GAT27 0:18ebb7348150 66 tx[0] = address;
GAT27 0:18ebb7348150 67 tx[1] = num_data;
GAT27 0:18ebb7348150 68
GAT27 1:28beeb2f209b 69 //i2c_->start();
GAT27 0:18ebb7348150 70 i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
GAT27 0:18ebb7348150 71 wait_ms(1);
GAT27 1:28beeb2f209b 72 //i2c_->stop();
GAT27 0:18ebb7348150 73 }
GAT27 0:18ebb7348150 74
GAT27 0:18ebb7348150 75 void SI1143::bias()
GAT27 0:18ebb7348150 76 {
GAT27 0:18ebb7348150 77 sample(0);
GAT27 0:18ebb7348150 78 bias1 = PS1;
GAT27 0:18ebb7348150 79 bias2 = PS2;
GAT27 0:18ebb7348150 80 bias3 = PS3;
GAT27 0:18ebb7348150 81 }
GAT27 0:18ebb7348150 82
GAT27 0:18ebb7348150 83 int SI1143::sample(int point)
GAT27 0:18ebb7348150 84 {
GAT27 0:18ebb7348150 85 //int data[5];
GAT27 0:18ebb7348150 86 command(PSALS_FORCE);
GAT27 0:18ebb7348150 87
GAT27 1:28beeb2f209b 88 LowB = read_reg(ALS_VIS_DATA0,1); // Read the data for ambient light
GAT27 0:18ebb7348150 89 HighB = read_reg(ALS_VIS_DATA1,1);
GAT27 0:18ebb7348150 90 VIS = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 91
GAT27 1:28beeb2f209b 92 LowB = read_reg(ALS_IR_DATA0,1); // Read the data for infrared light
GAT27 0:18ebb7348150 93 HighB = read_reg(ALS_IR_DATA1,1);
GAT27 0:18ebb7348150 94 IR = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 95
GAT27 0:18ebb7348150 96 LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 97 HighB = read_reg(PS1_DATA1,1);
GAT27 0:18ebb7348150 98 PS1 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 99
GAT27 1:28beeb2f209b 100 LowB = read_reg(PS2_DATA0,1); // Read the data for the second LED
GAT27 0:18ebb7348150 101 HighB = read_reg(PS2_DATA1,1);
GAT27 0:18ebb7348150 102 PS2 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 103
GAT27 1:28beeb2f209b 104 LowB = read_reg(PS3_DATA0,1); // Read the data for the third LED
GAT27 0:18ebb7348150 105 HighB = read_reg(PS3_DATA1,1);
GAT27 0:18ebb7348150 106 PS3 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 107
GAT27 0:18ebb7348150 108 if(PS1 > bias1)
GAT27 0:18ebb7348150 109 PS1 = PS1 - bias1;
GAT27 0:18ebb7348150 110 else
GAT27 0:18ebb7348150 111 PS1 = 0;
GAT27 0:18ebb7348150 112 if(PS2 > bias2)
GAT27 0:18ebb7348150 113 PS2 = PS2 - bias2;
GAT27 0:18ebb7348150 114 else
GAT27 0:18ebb7348150 115 PS2 = 0;
GAT27 0:18ebb7348150 116 if(PS3 > bias3)
GAT27 0:18ebb7348150 117 PS3 = PS3 - bias3;
GAT27 0:18ebb7348150 118 else
GAT27 0:18ebb7348150 119 PS3 = 0;
GAT27 0:18ebb7348150 120
GAT27 0:18ebb7348150 121 switch(point)
GAT27 0:18ebb7348150 122 {
GAT27 0:18ebb7348150 123 case 1: return VIS;
GAT27 0:18ebb7348150 124 case 2: return IR;
GAT27 0:18ebb7348150 125 case 3: return PS1;
GAT27 0:18ebb7348150 126 case 4: return PS2;
GAT27 0:18ebb7348150 127 case 5: return PS3;
GAT27 0:18ebb7348150 128 default: return 0;
GAT27 0:18ebb7348150 129 }
GAT27 0:18ebb7348150 130 //data[0] = VIS;
GAT27 0:18ebb7348150 131 //data[1] = IR;
GAT27 0:18ebb7348150 132 //data[2] = PS1;
GAT27 0:18ebb7348150 133 //data[3] = PS2;
GAT27 0:18ebb7348150 134 //data[4] = PS3;
GAT27 0:18ebb7348150 135
GAT27 0:18ebb7348150 136 //return data;
GAT27 0:18ebb7348150 137
GAT27 0:18ebb7348150 138 //return PS1;
GAT27 0:18ebb7348150 139 }