Starting library to get the SI1143 Gesture Sensor to work. Currently only works in forced conversion mode.
Dependents: Gesture_Sensor Gesture_Arm_Robot mbed_gesture Gesture_Sensor_Sample ... more
SI1143.cpp@2:21381f11a5af, 2013-10-17 (annotated)
- Committer:
- GAT27
- Date:
- Thu Oct 17 21:03:01 2013 +0000
- Revision:
- 2:21381f11a5af
- Parent:
- 1:28beeb2f209b
- Child:
- 3:cb3e8160f18e
license
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 | 1:28beeb2f209b | 39 | //3.4MHz, as specified by the datasheet. (DO NOT USE) |
GAT27 | 1:28beeb2f209b | 40 | //i2c_->frequency(3400000); |
GAT27 | 0:18ebb7348150 | 41 | |
GAT27 | 0:18ebb7348150 | 42 | command(RESET); |
GAT27 | 0:18ebb7348150 | 43 | wait_ms(30); |
GAT27 | 0:18ebb7348150 | 44 | |
GAT27 | 0:18ebb7348150 | 45 | write_reg(HW_KEY,HW_KEY_VAL0); // Setting up LED Power to full |
GAT27 | 0:18ebb7348150 | 46 | write_reg(PS_LED21,0xAA); |
GAT27 | 0:18ebb7348150 | 47 | write_reg(PS_LED3,0x0A); |
GAT27 | 0:18ebb7348150 | 48 | write_reg(PARAM_WR, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK); |
GAT27 | 0:18ebb7348150 | 49 | |
GAT27 | 0:18ebb7348150 | 50 | command(PARAM_SET + (CHLIST & 0x1F)); |
GAT27 | 0:18ebb7348150 | 51 | |
GAT27 | 0:18ebb7348150 | 52 | write_reg(INT_CFG,0); |
GAT27 | 0:18ebb7348150 | 53 | write_reg(IRQ_ENABLE,0); |
GAT27 | 0:18ebb7348150 | 54 | write_reg(IRQ_MODE1,0); |
GAT27 | 0:18ebb7348150 | 55 | write_reg(IRQ_MODE2,0); |
GAT27 | 0:18ebb7348150 | 56 | } |
GAT27 | 0:18ebb7348150 | 57 | |
GAT27 | 0:18ebb7348150 | 58 | void SI1143::command(char cmd) |
GAT27 | 0:18ebb7348150 | 59 | { |
GAT27 | 0:18ebb7348150 | 60 | int val; |
GAT27 | 0:18ebb7348150 | 61 | |
GAT27 | 0:18ebb7348150 | 62 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 63 | while(val!=0) |
GAT27 | 0:18ebb7348150 | 64 | { |
GAT27 | 0:18ebb7348150 | 65 | write_reg(COMMAND,NOP); |
GAT27 | 0:18ebb7348150 | 66 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 67 | } |
GAT27 | 0:18ebb7348150 | 68 | do{ |
GAT27 | 0:18ebb7348150 | 69 | write_reg(COMMAND,cmd); |
GAT27 | 0:18ebb7348150 | 70 | if(cmd==RESET) break; |
GAT27 | 0:18ebb7348150 | 71 | val = read_reg(RESPONSE,1); |
GAT27 | 0:18ebb7348150 | 72 | }while(val==0); |
GAT27 | 0:18ebb7348150 | 73 | } |
GAT27 | 0:18ebb7348150 | 74 | |
GAT27 | 1:28beeb2f209b | 75 | char SI1143::read_reg(/*unsigned*/ char address, int num_data) // Read a register |
GAT27 | 0:18ebb7348150 | 76 | { |
GAT27 | 0:18ebb7348150 | 77 | char tx[1]; |
GAT27 | 0:18ebb7348150 | 78 | char rx[1]; |
GAT27 | 0:18ebb7348150 | 79 | |
GAT27 | 1:28beeb2f209b | 80 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 81 | tx[0] = address; |
GAT27 | 0:18ebb7348150 | 82 | i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, num_data); |
GAT27 | 0:18ebb7348150 | 83 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 84 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 85 | |
GAT27 | 1:28beeb2f209b | 86 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 87 | i2c_->read((IR_ADDRESS << 1) | 0x01, rx, num_data); |
GAT27 | 0:18ebb7348150 | 88 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 89 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 90 | |
GAT27 | 0:18ebb7348150 | 91 | return rx[0]; |
GAT27 | 0:18ebb7348150 | 92 | } |
GAT27 | 0:18ebb7348150 | 93 | |
GAT27 | 1:28beeb2f209b | 94 | void SI1143::write_reg(char address, char num_data) // Write a resigter |
GAT27 | 1:28beeb2f209b | 95 | { |
GAT27 | 0:18ebb7348150 | 96 | char tx[2]; |
GAT27 | 0:18ebb7348150 | 97 | |
GAT27 | 0:18ebb7348150 | 98 | tx[0] = address; |
GAT27 | 0:18ebb7348150 | 99 | tx[1] = num_data; |
GAT27 | 0:18ebb7348150 | 100 | |
GAT27 | 1:28beeb2f209b | 101 | //i2c_->start(); |
GAT27 | 0:18ebb7348150 | 102 | i2c_->write((IR_ADDRESS << 1) & 0xFE, tx, 2); |
GAT27 | 0:18ebb7348150 | 103 | wait_ms(1); |
GAT27 | 1:28beeb2f209b | 104 | //i2c_->stop(); |
GAT27 | 0:18ebb7348150 | 105 | } |
GAT27 | 0:18ebb7348150 | 106 | |
GAT27 | 0:18ebb7348150 | 107 | void SI1143::bias() |
GAT27 | 0:18ebb7348150 | 108 | { |
GAT27 | 0:18ebb7348150 | 109 | sample(0); |
GAT27 | 0:18ebb7348150 | 110 | bias1 = PS1; |
GAT27 | 0:18ebb7348150 | 111 | bias2 = PS2; |
GAT27 | 0:18ebb7348150 | 112 | bias3 = PS3; |
GAT27 | 0:18ebb7348150 | 113 | } |
GAT27 | 0:18ebb7348150 | 114 | |
GAT27 | 0:18ebb7348150 | 115 | int SI1143::sample(int point) |
GAT27 | 0:18ebb7348150 | 116 | { |
GAT27 | 0:18ebb7348150 | 117 | //int data[5]; |
GAT27 | 0:18ebb7348150 | 118 | command(PSALS_FORCE); |
GAT27 | 0:18ebb7348150 | 119 | |
GAT27 | 1:28beeb2f209b | 120 | LowB = read_reg(ALS_VIS_DATA0,1); // Read the data for ambient light |
GAT27 | 0:18ebb7348150 | 121 | HighB = read_reg(ALS_VIS_DATA1,1); |
GAT27 | 0:18ebb7348150 | 122 | VIS = (HighB * 256) + LowB; |
GAT27 | 0:18ebb7348150 | 123 | |
GAT27 | 1:28beeb2f209b | 124 | LowB = read_reg(ALS_IR_DATA0,1); // Read the data for infrared light |
GAT27 | 0:18ebb7348150 | 125 | HighB = read_reg(ALS_IR_DATA1,1); |
GAT27 | 0:18ebb7348150 | 126 | IR = (HighB * 256) + LowB; |
GAT27 | 0:18ebb7348150 | 127 | |
GAT27 | 0:18ebb7348150 | 128 | LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED |
GAT27 | 0:18ebb7348150 | 129 | HighB = read_reg(PS1_DATA1,1); |
GAT27 | 0:18ebb7348150 | 130 | PS1 = (HighB * 256) + LowB; |
GAT27 | 0:18ebb7348150 | 131 | |
GAT27 | 1:28beeb2f209b | 132 | LowB = read_reg(PS2_DATA0,1); // Read the data for the second LED |
GAT27 | 0:18ebb7348150 | 133 | HighB = read_reg(PS2_DATA1,1); |
GAT27 | 0:18ebb7348150 | 134 | PS2 = (HighB * 256) + LowB; |
GAT27 | 0:18ebb7348150 | 135 | |
GAT27 | 1:28beeb2f209b | 136 | LowB = read_reg(PS3_DATA0,1); // Read the data for the third LED |
GAT27 | 0:18ebb7348150 | 137 | HighB = read_reg(PS3_DATA1,1); |
GAT27 | 0:18ebb7348150 | 138 | PS3 = (HighB * 256) + LowB; |
GAT27 | 0:18ebb7348150 | 139 | |
GAT27 | 0:18ebb7348150 | 140 | if(PS1 > bias1) |
GAT27 | 0:18ebb7348150 | 141 | PS1 = PS1 - bias1; |
GAT27 | 0:18ebb7348150 | 142 | else |
GAT27 | 0:18ebb7348150 | 143 | PS1 = 0; |
GAT27 | 0:18ebb7348150 | 144 | if(PS2 > bias2) |
GAT27 | 0:18ebb7348150 | 145 | PS2 = PS2 - bias2; |
GAT27 | 0:18ebb7348150 | 146 | else |
GAT27 | 0:18ebb7348150 | 147 | PS2 = 0; |
GAT27 | 0:18ebb7348150 | 148 | if(PS3 > bias3) |
GAT27 | 0:18ebb7348150 | 149 | PS3 = PS3 - bias3; |
GAT27 | 0:18ebb7348150 | 150 | else |
GAT27 | 0:18ebb7348150 | 151 | PS3 = 0; |
GAT27 | 0:18ebb7348150 | 152 | |
GAT27 | 0:18ebb7348150 | 153 | switch(point) |
GAT27 | 0:18ebb7348150 | 154 | { |
GAT27 | 0:18ebb7348150 | 155 | case 1: return VIS; |
GAT27 | 0:18ebb7348150 | 156 | case 2: return IR; |
GAT27 | 0:18ebb7348150 | 157 | case 3: return PS1; |
GAT27 | 0:18ebb7348150 | 158 | case 4: return PS2; |
GAT27 | 0:18ebb7348150 | 159 | case 5: return PS3; |
GAT27 | 0:18ebb7348150 | 160 | default: return 0; |
GAT27 | 0:18ebb7348150 | 161 | } |
GAT27 | 0:18ebb7348150 | 162 | //data[0] = VIS; |
GAT27 | 0:18ebb7348150 | 163 | //data[1] = IR; |
GAT27 | 0:18ebb7348150 | 164 | //data[2] = PS1; |
GAT27 | 0:18ebb7348150 | 165 | //data[3] = PS2; |
GAT27 | 0:18ebb7348150 | 166 | //data[4] = PS3; |
GAT27 | 0:18ebb7348150 | 167 | |
GAT27 | 0:18ebb7348150 | 168 | //return data; |
GAT27 | 0:18ebb7348150 | 169 | |
GAT27 | 0:18ebb7348150 | 170 | //return PS1; |
GAT27 | 0:18ebb7348150 | 171 | } |
GAT27 | 2:21381f11a5af | 172 |