set up to autonomously sample with one sensor

Fork of SI1142 by Dave Cohen

Committer:
GAT27
Date:
Thu Oct 17 12:53:08 2013 +0000
Revision:
0:18ebb7348150
Child:
1:28beeb2f209b
1st edit;

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 0:18ebb7348150 7 //3.4MHz, as specified by the datasheet.
GAT27 0:18ebb7348150 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 //bias1=0;
GAT27 0:18ebb7348150 13 //bias2=0;
GAT27 0:18ebb7348150 14 //bias3=0;
GAT27 0:18ebb7348150 15 //PS1=0;
GAT27 0:18ebb7348150 16 //PS2=0;
GAT27 0:18ebb7348150 17 //PS3=0;
GAT27 0:18ebb7348150 18
GAT27 0:18ebb7348150 19 write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full
GAT27 0:18ebb7348150 20 //write_reg(PS_LED21,0xFF);
GAT27 0:18ebb7348150 21 write_reg(PS_LED21,0xAA);
GAT27 0:18ebb7348150 22 //write_reg(PS_LED3, 0x0F);
GAT27 0:18ebb7348150 23 write_reg(PS_LED3,0x0A);
GAT27 0:18ebb7348150 24 //param_set(CHLIST,0b00010111);
GAT27 0:18ebb7348150 25 //param_set(CHLIST,0x17);
GAT27 0:18ebb7348150 26 //write_reg(PARAM_WR, 0x17);
GAT27 0:18ebb7348150 27 write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
GAT27 0:18ebb7348150 28
GAT27 0:18ebb7348150 29 //write_reg(COMMAND, 0xA0|CHLIST);
GAT27 0:18ebb7348150 30 command(PARAM_SET + (CHLIST & 0x1F));
GAT27 0:18ebb7348150 31
GAT27 0:18ebb7348150 32 write_reg(INT_CFG,0);
GAT27 0:18ebb7348150 33 write_reg(IRQ_ENABLE,0);
GAT27 0:18ebb7348150 34 write_reg(IRQ_MODE1,0);
GAT27 0:18ebb7348150 35 write_reg(IRQ_MODE2,0);
GAT27 0:18ebb7348150 36
GAT27 0:18ebb7348150 37 //void write_reg(byte address, byte val) { // Write a resigter
GAT27 0:18ebb7348150 38 //Wire.beginTransmission(IR_ADDRESS);
GAT27 0:18ebb7348150 39 //Wire.write(address);
GAT27 0:18ebb7348150 40 //Wire.write(val);
GAT27 0:18ebb7348150 41 //Wire.endTransmission();
GAT27 0:18ebb7348150 42 //}
GAT27 0:18ebb7348150 43
GAT27 0:18ebb7348150 44 //char parameter = read_reg(PARAM_RD,1);
GAT27 0:18ebb7348150 45 // Serial.print("CHLIST = ");
GAT27 0:18ebb7348150 46 // Serial.println(parameter,BIN);
GAT27 0:18ebb7348150 47 //delay(1000);
GAT27 0:18ebb7348150 48 //wait(2);
GAT27 0:18ebb7348150 49 /*
GAT27 0:18ebb7348150 50 for (int i=0; i<20; i++)
GAT27 0:18ebb7348150 51 {
GAT27 0:18ebb7348150 52 //write_reg(COMMAND,0b00000101);
GAT27 0:18ebb7348150 53 write_reg(COMMAND,0x05);
GAT27 0:18ebb7348150 54 //delay(50);
GAT27 0:18ebb7348150 55 //wait(0.05);
GAT27 0:18ebb7348150 56
GAT27 0:18ebb7348150 57 //char LowB = read_reg(PS1_DATA0,1);
GAT27 0:18ebb7348150 58 //char HighB = read_reg(PS1_DATA1,1);
GAT27 0:18ebb7348150 59 LowB = read_reg(PS1_DATA0,1);
GAT27 0:18ebb7348150 60 HighB = read_reg(PS1_DATA1,1);
GAT27 0:18ebb7348150 61
GAT27 0:18ebb7348150 62 bias1 = bias1 + (((HighB * 255) + LowB) / 20);
GAT27 0:18ebb7348150 63
GAT27 0:18ebb7348150 64 LowB = read_reg(PS2_DATA0,1);
GAT27 0:18ebb7348150 65 HighB = read_reg(PS2_DATA1,1);
GAT27 0:18ebb7348150 66
GAT27 0:18ebb7348150 67 bias2 = bias2 + (((HighB * 255) + LowB) / 20);
GAT27 0:18ebb7348150 68
GAT27 0:18ebb7348150 69 LowB = read_reg(PS3_DATA0,1);
GAT27 0:18ebb7348150 70 HighB = read_reg(PS3_DATA1,1);
GAT27 0:18ebb7348150 71
GAT27 0:18ebb7348150 72 bias3 = bias3 + (((HighB * 255) + LowB) / 20);
GAT27 0:18ebb7348150 73 }*/
GAT27 0:18ebb7348150 74 }
GAT27 0:18ebb7348150 75
GAT27 0:18ebb7348150 76 void SI1143::command(char cmd)
GAT27 0:18ebb7348150 77 {
GAT27 0:18ebb7348150 78 int val;
GAT27 0:18ebb7348150 79
GAT27 0:18ebb7348150 80 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 81 while(val!=0)
GAT27 0:18ebb7348150 82 {
GAT27 0:18ebb7348150 83 write_reg(COMMAND,NOP);
GAT27 0:18ebb7348150 84 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 85 printf("%d\r\n",val);
GAT27 0:18ebb7348150 86 }
GAT27 0:18ebb7348150 87 do{
GAT27 0:18ebb7348150 88 write_reg(COMMAND,cmd);
GAT27 0:18ebb7348150 89 if(cmd==RESET) break;
GAT27 0:18ebb7348150 90 val = read_reg(RESPONSE,1);
GAT27 0:18ebb7348150 91 //printf("%d\r\n",val);
GAT27 0:18ebb7348150 92 }while(val==0);
GAT27 0:18ebb7348150 93 }
GAT27 0:18ebb7348150 94
GAT27 0:18ebb7348150 95 char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a Register
GAT27 0:18ebb7348150 96 {
GAT27 0:18ebb7348150 97 //
GAT27 0:18ebb7348150 98 //unsigned char data;
GAT27 0:18ebb7348150 99 //char cmd[2]={address,num_data};
GAT27 0:18ebb7348150 100 //int data;
GAT27 0:18ebb7348150 101
GAT27 0:18ebb7348150 102 //i2c_->start();
GAT27 0:18ebb7348150 103 //data = i2c_->read(IR_ADDRESS,cmd,2);
GAT27 0:18ebb7348150 104 //wait(0.05);
GAT27 0:18ebb7348150 105 //i2c_->stop();
GAT27 0:18ebb7348150 106
GAT27 0:18ebb7348150 107 //return data;
GAT27 0:18ebb7348150 108
GAT27 0:18ebb7348150 109 char tx[1];
GAT27 0:18ebb7348150 110 char rx[1];
GAT27 0:18ebb7348150 111
GAT27 0:18ebb7348150 112 i2c_->start();
GAT27 0:18ebb7348150 113 tx[0] = address;
GAT27 0:18ebb7348150 114 i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data);
GAT27 0:18ebb7348150 115 wait_ms(1);
GAT27 0:18ebb7348150 116 i2c_->stop();
GAT27 0:18ebb7348150 117
GAT27 0:18ebb7348150 118 i2c_->start();
GAT27 0:18ebb7348150 119 i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data);
GAT27 0:18ebb7348150 120 wait_ms(1);
GAT27 0:18ebb7348150 121 i2c_->stop();
GAT27 0:18ebb7348150 122
GAT27 0:18ebb7348150 123 return rx[0];
GAT27 0:18ebb7348150 124
GAT27 0:18ebb7348150 125 //Wire.beginTransmission(IR_ADDRESS);
GAT27 0:18ebb7348150 126 //Wire.write(address);
GAT27 0:18ebb7348150 127 //Wire.endTransmission();
GAT27 0:18ebb7348150 128
GAT27 0:18ebb7348150 129 //Wire.requestFrom(IR_ADDRESS, num_data);
GAT27 0:18ebb7348150 130
GAT27 0:18ebb7348150 131 //while(Wire.available() < num_data);
GAT27 0:18ebb7348150 132
GAT27 0:18ebb7348150 133 //return Wire.read();
GAT27 0:18ebb7348150 134 }
GAT27 0:18ebb7348150 135
GAT27 0:18ebb7348150 136 void SI1143::write_reg(char address, char num_data)
GAT27 0:18ebb7348150 137 { // Write a resigter
GAT27 0:18ebb7348150 138 //char cmd[2]={address,val};
GAT27 0:18ebb7348150 139
GAT27 0:18ebb7348150 140 //i2c_->start();
GAT27 0:18ebb7348150 141 //i2c_->write(IR_ADDRESS,cmd,1);
GAT27 0:18ebb7348150 142 //wait(0.05);
GAT27 0:18ebb7348150 143 //i2c_->stop();
GAT27 0:18ebb7348150 144
GAT27 0:18ebb7348150 145 char tx[2];
GAT27 0:18ebb7348150 146
GAT27 0:18ebb7348150 147 tx[0] = address;
GAT27 0:18ebb7348150 148 tx[1] = num_data;
GAT27 0:18ebb7348150 149 //tx[2] = data;
GAT27 0:18ebb7348150 150
GAT27 0:18ebb7348150 151 i2c_->start();
GAT27 0:18ebb7348150 152 i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2);
GAT27 0:18ebb7348150 153 wait_ms(1);
GAT27 0:18ebb7348150 154 i2c_->stop();
GAT27 0:18ebb7348150 155
GAT27 0:18ebb7348150 156 //Wire.beginTransmission(IR_ADDRESS);
GAT27 0:18ebb7348150 157 //Wire.write(address);
GAT27 0:18ebb7348150 158 //Wire.write(val);
GAT27 0:18ebb7348150 159 //Wire.endTransmission();
GAT27 0:18ebb7348150 160 }
GAT27 0:18ebb7348150 161
GAT27 0:18ebb7348150 162 void SI1143::bias()
GAT27 0:18ebb7348150 163 {
GAT27 0:18ebb7348150 164 //command(PSALS_FORCE);
GAT27 0:18ebb7348150 165 sample(0);
GAT27 0:18ebb7348150 166 bias1 = PS1;
GAT27 0:18ebb7348150 167 bias2 = PS2;
GAT27 0:18ebb7348150 168 bias3 = PS3;
GAT27 0:18ebb7348150 169 }
GAT27 0:18ebb7348150 170
GAT27 0:18ebb7348150 171 int SI1143::sample(int point)
GAT27 0:18ebb7348150 172 {
GAT27 0:18ebb7348150 173 //int data[5];
GAT27 0:18ebb7348150 174 command(PSALS_FORCE);
GAT27 0:18ebb7348150 175 //write_reg(COMMAND,0x05); // Get a reading
GAT27 0:18ebb7348150 176 //delay(5);
GAT27 0:18ebb7348150 177 //wait(0.5);
GAT27 0:18ebb7348150 178
GAT27 0:18ebb7348150 179 LowB = read_reg(ALS_VIS_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 180 HighB = read_reg(ALS_VIS_DATA1,1);
GAT27 0:18ebb7348150 181 VIS = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 182
GAT27 0:18ebb7348150 183 LowB = read_reg(ALS_IR_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 184 HighB = read_reg(ALS_IR_DATA1,1);
GAT27 0:18ebb7348150 185 IR = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 186
GAT27 0:18ebb7348150 187 LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 188 HighB = read_reg(PS1_DATA1,1);
GAT27 0:18ebb7348150 189 PS1 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 190
GAT27 0:18ebb7348150 191 LowB = read_reg(PS2_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 192 HighB = read_reg(PS2_DATA1,1);
GAT27 0:18ebb7348150 193 PS2 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 194
GAT27 0:18ebb7348150 195 LowB = read_reg(PS3_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 196 HighB = read_reg(PS3_DATA1,1);
GAT27 0:18ebb7348150 197 PS3 = (HighB * 256) + LowB;
GAT27 0:18ebb7348150 198
GAT27 0:18ebb7348150 199 if(PS1 > bias1)
GAT27 0:18ebb7348150 200 PS1 = PS1 - bias1;
GAT27 0:18ebb7348150 201 else
GAT27 0:18ebb7348150 202 PS1 = 0;
GAT27 0:18ebb7348150 203 if(PS2 > bias2)
GAT27 0:18ebb7348150 204 PS2 = PS2 - bias2;
GAT27 0:18ebb7348150 205 else
GAT27 0:18ebb7348150 206 PS2 = 0;
GAT27 0:18ebb7348150 207 if(PS3 > bias3)
GAT27 0:18ebb7348150 208 PS3 = PS3 - bias3;
GAT27 0:18ebb7348150 209 else
GAT27 0:18ebb7348150 210 PS3 = 0;
GAT27 0:18ebb7348150 211
GAT27 0:18ebb7348150 212 switch(point)
GAT27 0:18ebb7348150 213 {
GAT27 0:18ebb7348150 214 case 1: return VIS;
GAT27 0:18ebb7348150 215 case 2: return IR;
GAT27 0:18ebb7348150 216 case 3: return PS1;
GAT27 0:18ebb7348150 217 case 4: return PS2;
GAT27 0:18ebb7348150 218 case 5: return PS3;
GAT27 0:18ebb7348150 219 default: return 0;
GAT27 0:18ebb7348150 220 }
GAT27 0:18ebb7348150 221 //data[0] = VIS;
GAT27 0:18ebb7348150 222 //data[1] = IR;
GAT27 0:18ebb7348150 223 //data[2] = PS1;
GAT27 0:18ebb7348150 224 //data[3] = PS2;
GAT27 0:18ebb7348150 225 //data[4] = PS3;
GAT27 0:18ebb7348150 226
GAT27 0:18ebb7348150 227 //return data;
GAT27 0:18ebb7348150 228
GAT27 0:18ebb7348150 229 //return PS1;
GAT27 0:18ebb7348150 230 }
GAT27 0:18ebb7348150 231 /*
GAT27 0:18ebb7348150 232 int SI1143::read1()
GAT27 0:18ebb7348150 233 {
GAT27 0:18ebb7348150 234 write_reg(COMMAND,0x05); // Get a reading
GAT27 0:18ebb7348150 235 //delay(5);
GAT27 0:18ebb7348150 236 //wait(0.5);
GAT27 0:18ebb7348150 237
GAT27 0:18ebb7348150 238 LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
GAT27 0:18ebb7348150 239 HighB = read_reg(PS1_DATA1,1);
GAT27 0:18ebb7348150 240 PS1 = ((HighB * 255) + LowB) - bias1;
GAT27 0:18ebb7348150 241
GAT27 0:18ebb7348150 242 return PS1;
GAT27 0:18ebb7348150 243 }
GAT27 0:18ebb7348150 244
GAT27 0:18ebb7348150 245 int SI1143::read2()
GAT27 0:18ebb7348150 246 {
GAT27 0:18ebb7348150 247 write_reg(COMMAND,0x05); // Get a reading
GAT27 0:18ebb7348150 248 //delay(5);
GAT27 0:18ebb7348150 249 //wait(0.5);
GAT27 0:18ebb7348150 250
GAT27 0:18ebb7348150 251 LowB = read_reg(PS2_DATA0,1); // Read the data for the second LED
GAT27 0:18ebb7348150 252 HighB = read_reg(PS2_DATA1,1);
GAT27 0:18ebb7348150 253 PS2 = (HighB * 255) + LowB - bias2;
GAT27 0:18ebb7348150 254
GAT27 0:18ebb7348150 255 return PS2;
GAT27 0:18ebb7348150 256 }
GAT27 0:18ebb7348150 257
GAT27 0:18ebb7348150 258 int SI1143::read3()
GAT27 0:18ebb7348150 259 {
GAT27 0:18ebb7348150 260 write_reg(COMMAND,0x05); // Get a reading
GAT27 0:18ebb7348150 261 //delay(5);
GAT27 0:18ebb7348150 262 //wait(0.5);
GAT27 0:18ebb7348150 263
GAT27 0:18ebb7348150 264 LowB = read_reg(PS3_DATA0,1); // Read the data for the third LED
GAT27 0:18ebb7348150 265 HighB = read_reg(PS3_DATA1,1);
GAT27 0:18ebb7348150 266 PS3 = (HighB * 255) + LowB - bias3;
GAT27 0:18ebb7348150 267
GAT27 0:18ebb7348150 268 return PS3;
GAT27 0:18ebb7348150 269 }*/