Library for using multiple SI1142 sensor
Fork of SI1141 by
SI1143.cpp@5:3fadc61598bc, 2013-10-21 (annotated)
- Committer:
- GAT27
- Date:
- Mon Oct 21 20:12:56 2013 +0000
- Revision:
- 5:3fadc61598bc
- Parent:
- 4:af8f820733e0
- Child:
- 7:f1303b85db58
Slight modification for faster reads.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GAT27 | 2:21381f11a5af | 1 | /** |
GAT27 | 2:21381f11a5af | 2 | * @author Guillermo A Torijano |
GAT27 | 2:21381f11a5af | 3 | * |
GAT27 | 2:21381f11a5af | 4 | * @section LICENSE |
GAT27 | 2:21381f11a5af | 5 | * |
GAT27 | 2:21381f11a5af | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
GAT27 | 2:21381f11a5af | 7 | * of this software and associated documentation files (the "Software"), to deal |
GAT27 | 2:21381f11a5af | 8 | * in the Software without restriction, including without limitation the rights |
GAT27 | 2:21381f11a5af | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
GAT27 | 2:21381f11a5af | 10 | * copies of the Software, and to permit persons to whom the Software is |
GAT27 | 2:21381f11a5af | 11 | * furnished to do so, subject to the following conditions: |
GAT27 | 2:21381f11a5af | 12 | * |
GAT27 | 2:21381f11a5af | 13 | * The above copyright notice and this permission notice shall be included in |
GAT27 | 2:21381f11a5af | 14 | * all copies or substantial portions of the Software. |
GAT27 | 2:21381f11a5af | 15 | * |
GAT27 | 2:21381f11a5af | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
GAT27 | 2:21381f11a5af | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
GAT27 | 2:21381f11a5af | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
GAT27 | 2:21381f11a5af | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
GAT27 | 2:21381f11a5af | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
GAT27 | 2:21381f11a5af | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
GAT27 | 2:21381f11a5af | 22 | * THE SOFTWARE. |
GAT27 | 2:21381f11a5af | 23 | * |
GAT27 | 2:21381f11a5af | 24 | * @section DESCRIPTION |
GAT27 | 2:21381f11a5af | 25 | * |
GAT27 | 2:21381f11a5af | 26 | * Parallax SI1143 Gesture Sensor. |
GAT27 | 2:21381f11a5af | 27 | * |
GAT27 | 2:21381f11a5af | 28 | * Datasheet: |
GAT27 | 2:21381f11a5af | 29 | * |
GAT27 | 2:21381f11a5af | 30 | * http://www.silabs.com/Support%20Documents/TechnicalDocs/Si114x.pdf |
GAT27 | 2:21381f11a5af | 31 | */ |
GAT27 | 2:21381f11a5af | 32 | |
GAT27 | 0:18ebb7348150 | 33 | #include "SI1143.h" |
GAT27 | 0:18ebb7348150 | 34 | |
GAT27 | 0:18ebb7348150 | 35 | SI1143::SI1143(PinName sda, PinName scl) |
GAT27 | 0:18ebb7348150 | 36 | { |
GAT27 | 0:18ebb7348150 | 37 | wait_ms(30); |
GAT27 | 0:18ebb7348150 | 38 | i2c_ = new I2C(sda, scl); |
GAT27 | 5:3fadc61598bc | 39 | // 3.4MHz, as specified by the datasheet, but DO NOT USE. |
GAT27 | 1:28beeb2f209b | 40 | //i2c_->frequency(3400000); |
GAT27 | 0:18ebb7348150 | 41 | |
GAT27 | 4:af8f820733e0 | 42 | restart(); |
GAT27 | 4:af8f820733e0 | 43 | } |
GAT27 | 4:af8f820733e0 | 44 | |
GAT27 | 4:af8f820733e0 | 45 | void SI1143::restart() |
GAT27 | 4:af8f820733e0 | 46 | { |
GAT27 | 0:18ebb7348150 | 47 | command(RESET); |
GAT27 | 0:18ebb7348150 | 48 | wait_ms(30); |
GAT27 | 0:18ebb7348150 | 49 | |
GAT27 | 5:3fadc61598bc | 50 | // Setting up LED Power to full |
GAT27 | 5:3fadc61598bc | 51 | write_reg(HW_KEY,HW_KEY_VAL0); |
GAT27 | 0:18ebb7348150 | 52 | write_reg(PS_LED21,0xAA); |
GAT27 | 0:18ebb7348150 | 53 | write_reg(PS_LED3,0x0A); |
GAT27 | 0:18ebb7348150 | 54 | write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK); |
GAT27 | 0:18ebb7348150 | 55 | |
GAT27 | 0:18ebb7348150 | 56 | command(PARAM_SET + (CHLIST & 0x1F)); |
GAT27 | 0:18ebb7348150 | 57 | |
GAT27 | 0:18ebb7348150 | 58 | write_reg(INT_CFG,0); |
GAT27 | 0:18ebb7348150 | 59 | write_reg(IRQ_ENABLE,0); |
GAT27 | 0:18ebb7348150 | 60 | write_reg(IRQ_MODE1,0); |
GAT27 | 0:18ebb7348150 | 61 | write_reg(IRQ_MODE2,0); |
GAT27 | 0:18ebb7348150 | 62 | } |
GAT27 | 0:18ebb7348150 | 63 | |
GAT27 | 0:18ebb7348150 | 64 | void SI1143::command(char cmd) |
GAT27 | 0:18ebb7348150 | 65 | { |
GAT27 | 0:18ebb7348150 | 66 | int val; |
GAT27 | 0:18ebb7348150 | 67 | |
GAT27 | 0:18ebb7348150 | 68 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 69 | while(val!=0) |
GAT27 | 0:18ebb7348150 | 70 | { |
GAT27 | 0:18ebb7348150 | 71 | write_reg(COMMAND,NOP); |
GAT27 | 0:18ebb7348150 | 72 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 73 | } |
GAT27 | 0:18ebb7348150 | 74 | do{ |
GAT27 | 0:18ebb7348150 | 75 | write_reg(COMMAND,cmd); |
GAT27 | 0:18ebb7348150 | 76 | if(cmd==RESET) break; |
GAT27 | 0:18ebb7348150 | 77 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 78 | }while(val==0); |
GAT27 | 0:18ebb7348150 | 79 | } |
GAT27 | 0:18ebb7348150 | 80 | |
GAT27 | 1:28beeb2f209b | 81 | char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register |
GAT27 | 0:18ebb7348150 | 82 | { |
GAT27 | 0:18ebb7348150 | 83 | char tx[1]; |
GAT27 | 0:18ebb7348150 | 84 | char rx[1]; |
GAT27 | 0:18ebb7348150 | 85 | |
GAT27 | 1:28beeb2f209b | 86 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 87 | tx[0] = address; |
GAT27 | 0:18ebb7348150 | 88 | i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data); |
GAT27 | 0:18ebb7348150 | 89 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 90 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 91 | |
GAT27 | 1:28beeb2f209b | 92 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 93 | i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data); |
GAT27 | 0:18ebb7348150 | 94 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 95 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 96 | |
GAT27 | 0:18ebb7348150 | 97 | return rx[0]; |
GAT27 | 0:18ebb7348150 | 98 | } |
GAT27 | 0:18ebb7348150 | 99 | |
GAT27 | 1:28beeb2f209b | 100 | void SI1143::write_reg(char address, char num_data) // Write a resigter |
GAT27 | 1:28beeb2f209b | 101 | { |
GAT27 | 0:18ebb7348150 | 102 | char tx[2]; |
GAT27 | 5:3fadc61598bc | 103 | |
GAT27 | 5:3fadc61598bc | 104 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 105 | tx[0] = address; |
GAT27 | 0:18ebb7348150 | 106 | tx[1] = num_data; |
GAT27 | 0:18ebb7348150 | 107 | i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2); |
GAT27 | 0:18ebb7348150 | 108 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 109 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 110 | } |
GAT27 | 0:18ebb7348150 | 111 | |
GAT27 | 5:3fadc61598bc | 112 | void SI1143::bias(int ready, int repeat) |
GAT27 | 0:18ebb7348150 | 113 | { |
GAT27 | 5:3fadc61598bc | 114 | wait(ready); |
GAT27 | 5:3fadc61598bc | 115 | bias1 = get_ps1(repeat); |
GAT27 | 5:3fadc61598bc | 116 | bias2 = get_ps2(repeat); |
GAT27 | 5:3fadc61598bc | 117 | bias3 = get_ps3(repeat); |
GAT27 | 0:18ebb7348150 | 118 | } |
GAT27 | 0:18ebb7348150 | 119 | |
GAT27 | 5:3fadc61598bc | 120 | int SI1143::get_ps1(int repeat) // Read the data for the first LED |
GAT27 | 0:18ebb7348150 | 121 | { |
GAT27 | 5:3fadc61598bc | 122 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 123 | |
GAT27 | 0:18ebb7348150 | 124 | command(PSALS_FORCE); |
GAT27 | 0:18ebb7348150 | 125 | |
GAT27 | 5:3fadc61598bc | 126 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 127 | { |
GAT27 | 5:3fadc61598bc | 128 | LowB = read_reg(PS1_DATA0,1); |
GAT27 | 5:3fadc61598bc | 129 | HighB = read_reg(PS1_DATA1,1); |
GAT27 | 5:3fadc61598bc | 130 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 131 | } |
GAT27 | 5:3fadc61598bc | 132 | PS1 = stack / repeat; |
GAT27 | 0:18ebb7348150 | 133 | |
GAT27 | 0:18ebb7348150 | 134 | if(PS1 > bias1) |
GAT27 | 0:18ebb7348150 | 135 | PS1 = PS1 - bias1; |
GAT27 | 0:18ebb7348150 | 136 | else |
GAT27 | 0:18ebb7348150 | 137 | PS1 = 0; |
GAT27 | 5:3fadc61598bc | 138 | |
GAT27 | 5:3fadc61598bc | 139 | return PS1; |
GAT27 | 5:3fadc61598bc | 140 | } |
GAT27 | 5:3fadc61598bc | 141 | |
GAT27 | 5:3fadc61598bc | 142 | int SI1143::get_ps2(int repeat) // Read the data for the second LED |
GAT27 | 5:3fadc61598bc | 143 | { |
GAT27 | 5:3fadc61598bc | 144 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 145 | |
GAT27 | 5:3fadc61598bc | 146 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 147 | |
GAT27 | 5:3fadc61598bc | 148 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 149 | { |
GAT27 | 5:3fadc61598bc | 150 | LowB = read_reg(PS2_DATA0,1); |
GAT27 | 5:3fadc61598bc | 151 | HighB = read_reg(PS2_DATA1,1); |
GAT27 | 5:3fadc61598bc | 152 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 153 | } |
GAT27 | 5:3fadc61598bc | 154 | PS2 = stack / repeat; |
GAT27 | 5:3fadc61598bc | 155 | |
GAT27 | 0:18ebb7348150 | 156 | if(PS2 > bias2) |
GAT27 | 0:18ebb7348150 | 157 | PS2 = PS2 - bias2; |
GAT27 | 0:18ebb7348150 | 158 | else |
GAT27 | 0:18ebb7348150 | 159 | PS2 = 0; |
GAT27 | 5:3fadc61598bc | 160 | |
GAT27 | 5:3fadc61598bc | 161 | return PS2; |
GAT27 | 5:3fadc61598bc | 162 | } |
GAT27 | 5:3fadc61598bc | 163 | |
GAT27 | 5:3fadc61598bc | 164 | int SI1143::get_ps3(int repeat) // Read the data for the third LED |
GAT27 | 5:3fadc61598bc | 165 | { |
GAT27 | 5:3fadc61598bc | 166 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 167 | |
GAT27 | 5:3fadc61598bc | 168 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 169 | |
GAT27 | 5:3fadc61598bc | 170 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 171 | { |
GAT27 | 5:3fadc61598bc | 172 | LowB = read_reg(PS3_DATA0,1); |
GAT27 | 5:3fadc61598bc | 173 | HighB = read_reg(PS3_DATA1,1); |
GAT27 | 5:3fadc61598bc | 174 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 175 | } |
GAT27 | 5:3fadc61598bc | 176 | PS3 = stack / repeat; |
GAT27 | 5:3fadc61598bc | 177 | |
GAT27 | 0:18ebb7348150 | 178 | if(PS3 > bias3) |
GAT27 | 0:18ebb7348150 | 179 | PS3 = PS3 - bias3; |
GAT27 | 0:18ebb7348150 | 180 | else |
GAT27 | 0:18ebb7348150 | 181 | PS3 = 0; |
GAT27 | 0:18ebb7348150 | 182 | |
GAT27 | 5:3fadc61598bc | 183 | return PS3; |
GAT27 | 0:18ebb7348150 | 184 | } |
GAT27 | 2:21381f11a5af | 185 | |
GAT27 | 5:3fadc61598bc | 186 | int SI1143::get_vis(int repeat) // Read the data for ambient light |
GAT27 | 5:3fadc61598bc | 187 | { |
GAT27 | 5:3fadc61598bc | 188 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 189 | |
GAT27 | 5:3fadc61598bc | 190 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 191 | |
GAT27 | 5:3fadc61598bc | 192 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 193 | { |
GAT27 | 5:3fadc61598bc | 194 | LowB = read_reg(ALS_VIS_DATA0,1); |
GAT27 | 5:3fadc61598bc | 195 | HighB = read_reg(ALS_VIS_DATA1,1); |
GAT27 | 5:3fadc61598bc | 196 | VIS = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 197 | } |
GAT27 | 5:3fadc61598bc | 198 | VIS = stack / repeat; |
GAT27 | 5:3fadc61598bc | 199 | |
GAT27 | 5:3fadc61598bc | 200 | return VIS; |
GAT27 | 5:3fadc61598bc | 201 | } |
GAT27 | 4:af8f820733e0 | 202 | |
GAT27 | 5:3fadc61598bc | 203 | int SI1143::get_ir(int repeat) // Read the data for infrared light |
GAT27 | 5:3fadc61598bc | 204 | { |
GAT27 | 5:3fadc61598bc | 205 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 206 | |
GAT27 | 5:3fadc61598bc | 207 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 208 | |
GAT27 | 5:3fadc61598bc | 209 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 210 | { |
GAT27 | 5:3fadc61598bc | 211 | LowB = read_reg(ALS_IR_DATA0,1); |
GAT27 | 5:3fadc61598bc | 212 | HighB = read_reg(ALS_IR_DATA1,1); |
GAT27 | 5:3fadc61598bc | 213 | IR = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 214 | } |
GAT27 | 5:3fadc61598bc | 215 | IR = stack / repeat; |
GAT27 | 5:3fadc61598bc | 216 | |
GAT27 | 5:3fadc61598bc | 217 | return IR; |
GAT27 | 5:3fadc61598bc | 218 | } |