Added printing of data to the end of session not during session
Fork of SI1141_auto_sense by
SI1143.cpp@9:31d4e955ee92, 2014-05-23 (annotated)
- Committer:
- dmcohen24
- Date:
- Fri May 23 21:34:54 2014 +0000
- Revision:
- 9:31d4e955ee92
- Parent:
- 8:70933bf445af
- Child:
- 10:a89555c4f62a
autonomous sensing
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 | |
dmcohen24 | 8:70933bf445af | 50 | //Do this because you have to |
GAT27 | 5:3fadc61598bc | 51 | write_reg(HW_KEY,HW_KEY_VAL0); |
dmcohen24 | 8:70933bf445af | 52 | |
dmcohen24 | 8:70933bf445af | 53 | // Setting up LED Power to FULL JUICE |
dmcohen24 | 7:f1303b85db58 | 54 | write_reg(PS_LED21,0x0F); // Was 0x0A |
dmcohen24 | 7:f1303b85db58 | 55 | write_reg(PS_LED3,0x00); |
dmcohen24 | 7:f1303b85db58 | 56 | //write_reg(PS_LED21,0xAA); |
dmcohen24 | 7:f1303b85db58 | 57 | //write_reg(PS_LED3,0x0A); |
dmcohen24 | 8:70933bf445af | 58 | |
dmcohen24 | 8:70933bf445af | 59 | //Activate ALS_IR, ALS_VIS, and PS1 |
dmcohen24 | 7:f1303b85db58 | 60 | write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK); |
dmcohen24 | 8:70933bf445af | 61 | //write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK); |
dmcohen24 | 8:70933bf445af | 62 | command(PARAM_SET + (CHLIST & 0x1F)); |
GAT27 | 0:18ebb7348150 | 63 | |
dmcohen24 | 8:70933bf445af | 64 | /* |
dmcohen24 | 8:70933bf445af | 65 | // Set PS_RANGE to high signal range |
dmcohen24 | 8:70933bf445af | 66 | write_reg(PARAM_WR, 0x04); // 0x24 for high signal, 0x04 for normal |
dmcohen24 | 8:70933bf445af | 67 | command(PARAM_SET + (PS_ADC_MISC & 0x1F)); |
dmcohen24 | 8:70933bf445af | 68 | */ |
dmcohen24 | 8:70933bf445af | 69 | //Set PS_ADC_COUNTER to lower time between ADC samples |
dmcohen24 | 8:70933bf445af | 70 | write_reg(PARAM_WR, 0x00); |
dmcohen24 | 8:70933bf445af | 71 | command(PARAM_SET + (PS_ADC_COUNTER & 0x1F)); |
dmcohen24 | 8:70933bf445af | 72 | |
dmcohen24 | 8:70933bf445af | 73 | //Turn up the juice on PS ACD GAIN! |
dmcohen24 | 9:31d4e955ee92 | 74 | write_reg(PARAM_WR, 0x00); |
dmcohen24 | 8:70933bf445af | 75 | command(PARAM_SET + (PS_ADC_GAIN & 0x1F)); |
dmcohen24 | 8:70933bf445af | 76 | |
GAT27 | 0:18ebb7348150 | 77 | |
dmcohen24 | 9:31d4e955ee92 | 78 | write_reg(INT_CFG,0x01); // Configure Interrupts |
dmcohen24 | 9:31d4e955ee92 | 79 | write_reg(IRQ_ENABLE,0x04); // Enable PS1 |
GAT27 | 0:18ebb7348150 | 80 | write_reg(IRQ_MODE1,0); |
GAT27 | 0:18ebb7348150 | 81 | write_reg(IRQ_MODE2,0); |
dmcohen24 | 7:f1303b85db58 | 82 | |
dmcohen24 | 9:31d4e955ee92 | 83 | write_reg(MEAS_RATE,0x94); //0x84 - wakes up every 10 ms |
dmcohen24 | 9:31d4e955ee92 | 84 | write_reg(PS_RATE,0x08); //0x84 - wakes up every time measurement clock wakes up |
dmcohen24 | 9:31d4e955ee92 | 85 | |
dmcohen24 | 7:f1303b85db58 | 86 | //write_reg(PS_ADC_MISC,0x24); |
dmcohen24 | 7:f1303b85db58 | 87 | //write_reg(PS1_ADCMUX,0x03); |
GAT27 | 0:18ebb7348150 | 88 | } |
GAT27 | 0:18ebb7348150 | 89 | |
GAT27 | 0:18ebb7348150 | 90 | void SI1143::command(char cmd) |
GAT27 | 0:18ebb7348150 | 91 | { |
GAT27 | 0:18ebb7348150 | 92 | int val; |
GAT27 | 0:18ebb7348150 | 93 | |
GAT27 | 0:18ebb7348150 | 94 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 95 | while(val!=0) |
GAT27 | 0:18ebb7348150 | 96 | { |
GAT27 | 0:18ebb7348150 | 97 | write_reg(COMMAND,NOP); |
GAT27 | 0:18ebb7348150 | 98 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 99 | } |
GAT27 | 0:18ebb7348150 | 100 | do{ |
GAT27 | 0:18ebb7348150 | 101 | write_reg(COMMAND,cmd); |
GAT27 | 0:18ebb7348150 | 102 | if(cmd==RESET) break; |
GAT27 | 0:18ebb7348150 | 103 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 104 | }while(val==0); |
GAT27 | 0:18ebb7348150 | 105 | } |
GAT27 | 0:18ebb7348150 | 106 | |
GAT27 | 1:28beeb2f209b | 107 | char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register |
GAT27 | 0:18ebb7348150 | 108 | { |
GAT27 | 0:18ebb7348150 | 109 | char tx[1]; |
GAT27 | 0:18ebb7348150 | 110 | char rx[1]; |
GAT27 | 0:18ebb7348150 | 111 | |
GAT27 | 1:28beeb2f209b | 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 | 1:28beeb2f209b | 116 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 117 | |
GAT27 | 1:28beeb2f209b | 118 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 119 | i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data); |
GAT27 | 0:18ebb7348150 | 120 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 121 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 122 | |
GAT27 | 0:18ebb7348150 | 123 | return rx[0]; |
GAT27 | 0:18ebb7348150 | 124 | } |
GAT27 | 0:18ebb7348150 | 125 | |
GAT27 | 1:28beeb2f209b | 126 | void SI1143::write_reg(char address, char num_data) // Write a resigter |
GAT27 | 1:28beeb2f209b | 127 | { |
GAT27 | 0:18ebb7348150 | 128 | char tx[2]; |
GAT27 | 5:3fadc61598bc | 129 | |
GAT27 | 5:3fadc61598bc | 130 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 131 | tx[0] = address; |
GAT27 | 0:18ebb7348150 | 132 | tx[1] = num_data; |
GAT27 | 0:18ebb7348150 | 133 | i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2); |
GAT27 | 0:18ebb7348150 | 134 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 135 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 136 | } |
GAT27 | 0:18ebb7348150 | 137 | |
GAT27 | 5:3fadc61598bc | 138 | void SI1143::bias(int ready, int repeat) |
GAT27 | 0:18ebb7348150 | 139 | { |
GAT27 | 5:3fadc61598bc | 140 | wait(ready); |
dmcohen24 | 8:70933bf445af | 141 | //bias1 = get_ps1(repeat); |
dmcohen24 | 8:70933bf445af | 142 | bias1 = 0; |
GAT27 | 5:3fadc61598bc | 143 | bias2 = get_ps2(repeat); |
GAT27 | 5:3fadc61598bc | 144 | bias3 = get_ps3(repeat); |
GAT27 | 0:18ebb7348150 | 145 | } |
GAT27 | 0:18ebb7348150 | 146 | |
dmcohen24 | 9:31d4e955ee92 | 147 | int SI1143::get_ps1(int repeat) // Read the data for the first LED (Used in forced mode) |
GAT27 | 0:18ebb7348150 | 148 | { |
GAT27 | 5:3fadc61598bc | 149 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 150 | |
GAT27 | 0:18ebb7348150 | 151 | command(PSALS_FORCE); |
GAT27 | 0:18ebb7348150 | 152 | |
GAT27 | 5:3fadc61598bc | 153 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 154 | { |
GAT27 | 5:3fadc61598bc | 155 | LowB = read_reg(PS1_DATA0,1); |
GAT27 | 5:3fadc61598bc | 156 | HighB = read_reg(PS1_DATA1,1); |
GAT27 | 5:3fadc61598bc | 157 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 158 | } |
GAT27 | 5:3fadc61598bc | 159 | PS1 = stack / repeat; |
GAT27 | 0:18ebb7348150 | 160 | |
GAT27 | 0:18ebb7348150 | 161 | if(PS1 > bias1) |
GAT27 | 0:18ebb7348150 | 162 | PS1 = PS1 - bias1; |
GAT27 | 0:18ebb7348150 | 163 | else |
GAT27 | 0:18ebb7348150 | 164 | PS1 = 0; |
GAT27 | 5:3fadc61598bc | 165 | |
GAT27 | 5:3fadc61598bc | 166 | return PS1; |
GAT27 | 5:3fadc61598bc | 167 | } |
GAT27 | 5:3fadc61598bc | 168 | |
dmcohen24 | 9:31d4e955ee92 | 169 | void SI1143::start_ps_auto() |
dmcohen24 | 9:31d4e955ee92 | 170 | { |
dmcohen24 | 9:31d4e955ee92 | 171 | command(PS_AUTO); |
dmcohen24 | 9:31d4e955ee92 | 172 | } |
dmcohen24 | 9:31d4e955ee92 | 173 | |
dmcohen24 | 9:31d4e955ee92 | 174 | int SI1143::read_ps1() // Read the data for the first LED (Used in autonomous mode) |
dmcohen24 | 9:31d4e955ee92 | 175 | { |
dmcohen24 | 9:31d4e955ee92 | 176 | |
dmcohen24 | 9:31d4e955ee92 | 177 | |
dmcohen24 | 9:31d4e955ee92 | 178 | LowB = read_reg(PS1_DATA0,1); |
dmcohen24 | 9:31d4e955ee92 | 179 | HighB = read_reg(PS1_DATA1,1); |
dmcohen24 | 9:31d4e955ee92 | 180 | PS1 = (HighB * 256) + LowB; |
dmcohen24 | 9:31d4e955ee92 | 181 | |
dmcohen24 | 9:31d4e955ee92 | 182 | if(PS1 > bias1) |
dmcohen24 | 9:31d4e955ee92 | 183 | PS1 = PS1 - bias1; |
dmcohen24 | 9:31d4e955ee92 | 184 | else |
dmcohen24 | 9:31d4e955ee92 | 185 | PS1 = 0; |
dmcohen24 | 9:31d4e955ee92 | 186 | |
dmcohen24 | 9:31d4e955ee92 | 187 | return PS1; |
dmcohen24 | 9:31d4e955ee92 | 188 | } |
dmcohen24 | 9:31d4e955ee92 | 189 | |
GAT27 | 5:3fadc61598bc | 190 | int SI1143::get_ps2(int repeat) // Read the data for the second LED |
GAT27 | 5:3fadc61598bc | 191 | { |
GAT27 | 5:3fadc61598bc | 192 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 193 | |
GAT27 | 5:3fadc61598bc | 194 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 195 | |
GAT27 | 5:3fadc61598bc | 196 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 197 | { |
GAT27 | 5:3fadc61598bc | 198 | LowB = read_reg(PS2_DATA0,1); |
GAT27 | 5:3fadc61598bc | 199 | HighB = read_reg(PS2_DATA1,1); |
GAT27 | 5:3fadc61598bc | 200 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 201 | } |
GAT27 | 5:3fadc61598bc | 202 | PS2 = stack / repeat; |
GAT27 | 5:3fadc61598bc | 203 | |
GAT27 | 0:18ebb7348150 | 204 | if(PS2 > bias2) |
GAT27 | 0:18ebb7348150 | 205 | PS2 = PS2 - bias2; |
GAT27 | 0:18ebb7348150 | 206 | else |
GAT27 | 0:18ebb7348150 | 207 | PS2 = 0; |
GAT27 | 5:3fadc61598bc | 208 | |
GAT27 | 5:3fadc61598bc | 209 | return PS2; |
GAT27 | 5:3fadc61598bc | 210 | } |
GAT27 | 5:3fadc61598bc | 211 | |
GAT27 | 5:3fadc61598bc | 212 | int SI1143::get_ps3(int repeat) // Read the data for the third LED |
GAT27 | 5:3fadc61598bc | 213 | { |
GAT27 | 5:3fadc61598bc | 214 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 215 | |
GAT27 | 5:3fadc61598bc | 216 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 217 | |
GAT27 | 5:3fadc61598bc | 218 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 219 | { |
GAT27 | 5:3fadc61598bc | 220 | LowB = read_reg(PS3_DATA0,1); |
GAT27 | 5:3fadc61598bc | 221 | HighB = read_reg(PS3_DATA1,1); |
GAT27 | 5:3fadc61598bc | 222 | stack = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 223 | } |
GAT27 | 5:3fadc61598bc | 224 | PS3 = stack / repeat; |
GAT27 | 5:3fadc61598bc | 225 | |
GAT27 | 0:18ebb7348150 | 226 | if(PS3 > bias3) |
GAT27 | 0:18ebb7348150 | 227 | PS3 = PS3 - bias3; |
GAT27 | 0:18ebb7348150 | 228 | else |
GAT27 | 0:18ebb7348150 | 229 | PS3 = 0; |
GAT27 | 0:18ebb7348150 | 230 | |
GAT27 | 5:3fadc61598bc | 231 | return PS3; |
GAT27 | 0:18ebb7348150 | 232 | } |
GAT27 | 2:21381f11a5af | 233 | |
GAT27 | 5:3fadc61598bc | 234 | int SI1143::get_vis(int repeat) // Read the data for ambient light |
GAT27 | 5:3fadc61598bc | 235 | { |
GAT27 | 5:3fadc61598bc | 236 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 237 | |
GAT27 | 5:3fadc61598bc | 238 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 239 | |
GAT27 | 5:3fadc61598bc | 240 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 241 | { |
GAT27 | 5:3fadc61598bc | 242 | LowB = read_reg(ALS_VIS_DATA0,1); |
GAT27 | 5:3fadc61598bc | 243 | HighB = read_reg(ALS_VIS_DATA1,1); |
GAT27 | 5:3fadc61598bc | 244 | VIS = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 245 | } |
GAT27 | 5:3fadc61598bc | 246 | VIS = stack / repeat; |
GAT27 | 5:3fadc61598bc | 247 | |
GAT27 | 5:3fadc61598bc | 248 | return VIS; |
GAT27 | 5:3fadc61598bc | 249 | } |
GAT27 | 4:af8f820733e0 | 250 | |
GAT27 | 5:3fadc61598bc | 251 | int SI1143::get_ir(int repeat) // Read the data for infrared light |
GAT27 | 5:3fadc61598bc | 252 | { |
GAT27 | 5:3fadc61598bc | 253 | int stack = 0; |
GAT27 | 5:3fadc61598bc | 254 | |
GAT27 | 5:3fadc61598bc | 255 | command(PSALS_FORCE); |
GAT27 | 5:3fadc61598bc | 256 | |
GAT27 | 5:3fadc61598bc | 257 | for(int r=repeat; r>0; r=r-1) |
GAT27 | 5:3fadc61598bc | 258 | { |
GAT27 | 5:3fadc61598bc | 259 | LowB = read_reg(ALS_IR_DATA0,1); |
GAT27 | 5:3fadc61598bc | 260 | HighB = read_reg(ALS_IR_DATA1,1); |
GAT27 | 5:3fadc61598bc | 261 | IR = stack + (HighB * 256) + LowB; |
GAT27 | 5:3fadc61598bc | 262 | } |
GAT27 | 5:3fadc61598bc | 263 | IR = stack / repeat; |
GAT27 | 5:3fadc61598bc | 264 | |
GAT27 | 5:3fadc61598bc | 265 | return IR; |
GAT27 | 5:3fadc61598bc | 266 | } |
dmcohen24 | 9:31d4e955ee92 | 267 | |
dmcohen24 | 9:31d4e955ee92 | 268 | void SI1143::clear_int() // Clear Interrupt register when an interrupt occurs. |
dmcohen24 | 9:31d4e955ee92 | 269 | { |
dmcohen24 | 9:31d4e955ee92 | 270 | write_reg(IRQ_STATUS, 0x3F); // Writes one to every interrupt status, thereby clearing them |
dmcohen24 | 9:31d4e955ee92 | 271 | } |