Library for using SI1141 sensor

Fork of SI1143 by Guillermo Torijano

Committer:
GAT27
Date:
Mon Oct 21 20:12:56 2013 +0000
Revision:
5:3fadc61598bc
Parent:
4:af8f820733e0
Child:
6:50b60d59d568
Child:
9:31d4e955ee92
Slight modification for faster reads.

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef SI1143_h
GAT27 0:18ebb7348150 34 #define SI1143_h
GAT27 0:18ebb7348150 35
GAT27 3:cb3e8160f18e 36 /**
GAT27 3:cb3e8160f18e 37 * Includes
GAT27 3:cb3e8160f18e 38 */
GAT27 3:cb3e8160f18e 39
GAT27 0:18ebb7348150 40 #include "mbed.h"
GAT27 0:18ebb7348150 41
GAT27 3:cb3e8160f18e 42 /**
GAT27 3:cb3e8160f18e 43 * Defines
GAT27 3:cb3e8160f18e 44 */
GAT27 3:cb3e8160f18e 45
GAT27 1:28beeb2f209b 46 #define IR_ADDRESS 0x5A
GAT27 1:28beeb2f209b 47 #define HW_KEY_VAL0 0x17 //Value to write into the HW Key register
GAT27 1:28beeb2f209b 48
GAT27 1:28beeb2f209b 49 // Register Addresses
GAT27 0:18ebb7348150 50
GAT27 1:28beeb2f209b 51 #define PART_ID 0x00
GAT27 1:28beeb2f209b 52 #define REV_ID 0x01
GAT27 1:28beeb2f209b 53 #define SEQ_ID 0x02 //Si114x-A11 (MAJOR_SEQ=1, MINOR_SEQ=1)
GAT27 1:28beeb2f209b 54 #define INT_CFG 0x03
GAT27 1:28beeb2f209b 55 #define IRQ_ENABLE 0x04
GAT27 1:28beeb2f209b 56 #define IRQ_MODE1 0x05
GAT27 1:28beeb2f209b 57 #define IRQ_MODE2 0x06
GAT27 1:28beeb2f209b 58 #define HW_KEY 0x07
GAT27 0:18ebb7348150 59
GAT27 1:28beeb2f209b 60 #define MEAS_RATE 0x08
GAT27 1:28beeb2f209b 61 #define ALS_RATE 0x09
GAT27 1:28beeb2f209b 62 #define PS_RATE 0x0A
GAT27 0:18ebb7348150 63
GAT27 1:28beeb2f209b 64 #define ALS_LOW_TH0 0x0B
GAT27 1:28beeb2f209b 65 #define ALS_LOW_TH1 0x0C
GAT27 1:28beeb2f209b 66 #define ALS_HI_TH0 0x0D
GAT27 1:28beeb2f209b 67 #define ALS_HI_TH1 0x0E
GAT27 0:18ebb7348150 68
GAT27 1:28beeb2f209b 69 #define PS_LED21 0x0F
GAT27 1:28beeb2f209b 70 #define PS_LED3 0x10
GAT27 0:18ebb7348150 71
GAT27 1:28beeb2f209b 72 #define PS1_TH0 0x11
GAT27 1:28beeb2f209b 73 #define PS1_TH1 0x12
GAT27 1:28beeb2f209b 74 #define PS2_TH0 0x13
GAT27 1:28beeb2f209b 75 #define PS2_TH1 0x14
GAT27 1:28beeb2f209b 76 #define PS3_TH0 0x15
GAT27 1:28beeb2f209b 77
GAT27 1:28beeb2f209b 78 #define PS3_TH1 0x16
GAT27 1:28beeb2f209b 79 #define PARAM_WR 0x17
GAT27 1:28beeb2f209b 80 #define COMMAND 0x18
GAT27 0:18ebb7348150 81
GAT27 1:28beeb2f209b 82 #define RESPONSE 0x20
GAT27 1:28beeb2f209b 83 #define IRQ_STATUS 0x21
GAT27 0:18ebb7348150 84
GAT27 1:28beeb2f209b 85 #define ALS_VIS_DATA0 0x22
GAT27 1:28beeb2f209b 86 #define ALS_VIS_DATA1 0x23
GAT27 1:28beeb2f209b 87 #define ALS_IR_DATA0 0x24
GAT27 1:28beeb2f209b 88 #define ALS_IR_DATA1 0x25
GAT27 0:18ebb7348150 89
GAT27 1:28beeb2f209b 90 #define PS1_DATA0 0x26
GAT27 1:28beeb2f209b 91 #define PS1_DATA1 0x27
GAT27 1:28beeb2f209b 92 #define PS2_DATA0 0x28
GAT27 1:28beeb2f209b 93 #define PS2_DATA1 0x29
GAT27 1:28beeb2f209b 94 #define PS3_DATA0 0x2A
GAT27 1:28beeb2f209b 95 #define PS3_DATA1 0x2B
GAT27 0:18ebb7348150 96
GAT27 0:18ebb7348150 97
GAT27 1:28beeb2f209b 98 #define AUX_DATA0 0x2C
GAT27 1:28beeb2f209b 99 #define AUX_DATA1 0x2D
GAT27 0:18ebb7348150 100
GAT27 1:28beeb2f209b 101 #define PARAM_RD 0x2E
GAT27 1:28beeb2f209b 102 #define CHIP_STAT 0x30
GAT27 1:28beeb2f209b 103 #define ANA_IN_KEY 0x3B
GAT27 0:18ebb7348150 104
GAT27 0:18ebb7348150 105 // Command Register Values
GAT27 0:18ebb7348150 106
GAT27 1:28beeb2f209b 107 #define PARAM_QUERY 0x80 //Value is ORed with Parameter Offset
GAT27 1:28beeb2f209b 108 #define PARAM_SET 0xA0 //Value is ORed with Parameter Offset
GAT27 1:28beeb2f209b 109 #define PARAM_AND 0xC0 //Value is ORed with Parameter Offset
GAT27 1:28beeb2f209b 110 #define PARAM_OR 0xE0 //Value is ORed with Parameter Offset
GAT27 1:28beeb2f209b 111 #define NOP 0x00
GAT27 1:28beeb2f209b 112 #define RESET 0x01
GAT27 1:28beeb2f209b 113 #define BUSADDR 0x02
GAT27 1:28beeb2f209b 114 #define PS_FORCE 0x05
GAT27 1:28beeb2f209b 115 #define ALS_FORCE 0x06
GAT27 1:28beeb2f209b 116 #define PSALS_FORCE 0x07
GAT27 1:28beeb2f209b 117 #define PS_PAUSE 0x09
GAT27 1:28beeb2f209b 118 #define ALS_PAUSE 0x0A
GAT27 1:28beeb2f209b 119 #define PSALS_PAUSE 0x0B
GAT27 1:28beeb2f209b 120 #define PS_AUTO 0x0D
GAT27 1:28beeb2f209b 121 #define ALS_AUTO 0x0E
GAT27 1:28beeb2f209b 122 #define PSALS_AUTO 0x0F
GAT27 0:18ebb7348150 123
GAT27 1:28beeb2f209b 124 // Ram Addresses
GAT27 0:18ebb7348150 125
GAT27 1:28beeb2f209b 126 #define I2C_ADDR 0x00
GAT27 1:28beeb2f209b 127 #define CHLIST 0x01
GAT27 1:28beeb2f209b 128 #define PSLED12_SELECT 0x02
GAT27 1:28beeb2f209b 129 #define PSLED3_SELECT 0x03
GAT27 1:28beeb2f209b 130 #define FILTER_EN 0x04
GAT27 1:28beeb2f209b 131 #define PS_ENCODING 0x05
GAT27 1:28beeb2f209b 132 #define ALS_ENCODING 0x06
GAT27 1:28beeb2f209b 133 #define PS1_ADCMUX 0x07
GAT27 1:28beeb2f209b 134 #define PS2_ADCMUX 0x08
GAT27 1:28beeb2f209b 135 #define PS3_ADCMUX 0x09
GAT27 1:28beeb2f209b 136 #define PS_ADC_COUNTER 0x0A
GAT27 1:28beeb2f209b 137 #define PS_ADC_GAIN 0x0B
GAT27 1:28beeb2f209b 138 #define PS_ADC_MISC 0x0C
GAT27 1:28beeb2f209b 139 #define ALS1_ADCMUX 0x0D
GAT27 1:28beeb2f209b 140 #define ALS2_ADCMUX 0x0E
GAT27 1:28beeb2f209b 141 #define ALS3_ADCMUX 0x0F
GAT27 1:28beeb2f209b 142 #define ALS_VIS_ADC_COUNTER 0x10
GAT27 1:28beeb2f209b 143 #define ALS_VIS_ADC_GAIN 0x11
GAT27 1:28beeb2f209b 144 #define ALS_VIS_ADC_MISC 0x12
GAT27 1:28beeb2f209b 145 #define ALS_HYST 0x16
GAT27 1:28beeb2f209b 146 #define PS_HYST 0x17
GAT27 1:28beeb2f209b 147 #define PS_HISTORY 0x18
GAT27 1:28beeb2f209b 148 #define ALS_HISTORY 0x19
GAT27 1:28beeb2f209b 149 #define ADC_OFFSET 0x1A
GAT27 1:28beeb2f209b 150 #define SLEEP_CTRL 0x1B
GAT27 1:28beeb2f209b 151 #define LED_REC 0x1C
GAT27 1:28beeb2f209b 152 #define ALS_IR_ADC_COUNTER 0x1D
GAT27 1:28beeb2f209b 153 #define ALS_IR_ADC_GAIN 0x1E
GAT27 1:28beeb2f209b 154 #define ALS_IR_ADC_MISC 0x1F
GAT27 0:18ebb7348150 155
GAT27 1:28beeb2f209b 156 // Measurement Channel List
GAT27 3:cb3e8160f18e 157
GAT27 1:28beeb2f209b 158 #define PS1_TASK 0x01
GAT27 1:28beeb2f209b 159 #define PS2_TASK 0x02
GAT27 1:28beeb2f209b 160 #define PS3_TASK 0x04
GAT27 1:28beeb2f209b 161 #define ALS_VIS_TASK 0x10
GAT27 1:28beeb2f209b 162 #define ALS_IR_TASK 0x20
GAT27 1:28beeb2f209b 163 #define AUX_TASK 0x40
GAT27 0:18ebb7348150 164
GAT27 3:cb3e8160f18e 165 /**
GAT27 3:cb3e8160f18e 166 * Parallax SI1143 Gesture Sensor.
GAT27 3:cb3e8160f18e 167 */
GAT27 0:18ebb7348150 168 class SI1143
GAT27 0:18ebb7348150 169 {
GAT27 0:18ebb7348150 170 public:
GAT27 0:18ebb7348150 171
GAT27 3:cb3e8160f18e 172 /**
GAT27 3:cb3e8160f18e 173 * Constructor.
GAT27 3:cb3e8160f18e 174 *
GAT27 3:cb3e8160f18e 175 * @param sda mbed pin to use for SDA line of I2C interface.
GAT27 3:cb3e8160f18e 176 * @param scl mbed pin to use for SCL line of I2C interface.
GAT27 3:cb3e8160f18e 177 */
GAT27 0:18ebb7348150 178 SI1143(PinName sda, PinName scl);
GAT27 0:18ebb7348150 179
GAT27 3:cb3e8160f18e 180 /**
GAT27 5:3fadc61598bc 181 * Restarts the device.
GAT27 3:cb3e8160f18e 182 */
GAT27 5:3fadc61598bc 183 void restart(void);
GAT27 0:18ebb7348150 184
GAT27 3:cb3e8160f18e 185 /**
GAT27 3:cb3e8160f18e 186 * Creates a baseline for sampling measurements.
GAT27 4:af8f820733e0 187 * Should be done early in your code and after a reset.
GAT27 3:cb3e8160f18e 188 */
GAT27 5:3fadc61598bc 189 void bias(int ready, int repeat);
GAT27 5:3fadc61598bc 190
GAT27 5:3fadc61598bc 191 /**
GAT27 5:3fadc61598bc 192 * Takes a number of samples from the proximity of led1 and returns a raw output.
GAT27 5:3fadc61598bc 193 *
GAT27 5:3fadc61598bc 194 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 195 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 196 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 5:3fadc61598bc 197 */
GAT27 5:3fadc61598bc 198 int get_ps1(int repeat);
GAT27 5:3fadc61598bc 199
GAT27 5:3fadc61598bc 200 /**
GAT27 5:3fadc61598bc 201 * Takes a number of samples from the proximity of led2 and returns a raw output.
GAT27 5:3fadc61598bc 202 *
GAT27 5:3fadc61598bc 203 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 204 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 205 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 5:3fadc61598bc 206 */
GAT27 5:3fadc61598bc 207 int get_ps2(int repeat);
GAT27 0:18ebb7348150 208
GAT27 4:af8f820733e0 209 /**
GAT27 5:3fadc61598bc 210 * Takes a number of samples from the proximity of led3 and returns a raw output.
GAT27 5:3fadc61598bc 211 *
GAT27 5:3fadc61598bc 212 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 213 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 214 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 4:af8f820733e0 215 */
GAT27 5:3fadc61598bc 216 int get_ps3(int repeat);
GAT27 5:3fadc61598bc 217
GAT27 5:3fadc61598bc 218 /**
GAT27 5:3fadc61598bc 219 * Takes a number of samples for ambient light on device and returns a raw output.
GAT27 5:3fadc61598bc 220 *
GAT27 5:3fadc61598bc 221 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 222 * @return In forced conversion output mode, will display a raw output of the average sample.
GAT27 5:3fadc61598bc 223 */
GAT27 5:3fadc61598bc 224 int get_vis(int repeat);
GAT27 5:3fadc61598bc 225
GAT27 5:3fadc61598bc 226 /**
GAT27 5:3fadc61598bc 227 * Takes a number of samples for infrared light on device and returns a raw output.
GAT27 5:3fadc61598bc 228 *
GAT27 5:3fadc61598bc 229 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 230 * @return In forced conversion output mode, will display a raw output of the average sample.
GAT27 5:3fadc61598bc 231 */
GAT27 5:3fadc61598bc 232 int get_ir(int repeat);
GAT27 4:af8f820733e0 233
GAT27 0:18ebb7348150 234 private:
GAT27 0:18ebb7348150 235
GAT27 0:18ebb7348150 236 I2C* i2c_;
GAT27 0:18ebb7348150 237 int bias1,bias2,bias3,PS1,PS2,PS3,VIS,IR;
GAT27 0:18ebb7348150 238 char LowB,HighB;
GAT27 0:18ebb7348150 239
GAT27 3:cb3e8160f18e 240 /**
GAT27 3:cb3e8160f18e 241 * Wait for the device to respond, then send it a specific command.
GAT27 3:cb3e8160f18e 242 */
GAT27 0:18ebb7348150 243 void command(char cmd);
GAT27 0:18ebb7348150 244
GAT27 3:cb3e8160f18e 245 /**
GAT27 3:cb3e8160f18e 246 * Read a register from the device.
GAT27 3:cb3e8160f18e 247 */
GAT27 3:cb3e8160f18e 248 char read_reg(/*unsigned*/ char address, int num_data);
GAT27 0:18ebb7348150 249
GAT27 3:cb3e8160f18e 250 /**
GAT27 3:cb3e8160f18e 251 * Write to a register on the device.
GAT27 3:cb3e8160f18e 252 */
GAT27 3:cb3e8160f18e 253 void write_reg(char address, char num_data);
GAT27 0:18ebb7348150 254
GAT27 0:18ebb7348150 255 };
GAT27 0:18ebb7348150 256
GAT27 0:18ebb7348150 257 #endif