set up to autonomously sample with one sensor

Fork of SI1142 by Dave Cohen

Committer:
mfillinois
Date:
Thu Jun 12 20:56:59 2014 +0000
Revision:
11:f90ed8f6f38b
Parent:
10:a89555c4f62a
Experimenting with sample rate 06/12/2014

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
dmcohen24 10:a89555c4f62a 165 // Interrupt checks
dmcohen24 10:a89555c4f62a 166
dmcohen24 10:a89555c4f62a 167 #define INT_PS1 0x01
dmcohen24 10:a89555c4f62a 168 #define INT_PS2 0x02
dmcohen24 10:a89555c4f62a 169 #define INT_PS12 0x03
dmcohen24 10:a89555c4f62a 170
GAT27 3:cb3e8160f18e 171 /**
GAT27 3:cb3e8160f18e 172 * Parallax SI1143 Gesture Sensor.
GAT27 3:cb3e8160f18e 173 */
GAT27 0:18ebb7348150 174 class SI1143
GAT27 0:18ebb7348150 175 {
GAT27 0:18ebb7348150 176 public:
GAT27 0:18ebb7348150 177
GAT27 3:cb3e8160f18e 178 /**
GAT27 3:cb3e8160f18e 179 * Constructor.
GAT27 3:cb3e8160f18e 180 *
GAT27 3:cb3e8160f18e 181 * @param sda mbed pin to use for SDA line of I2C interface.
GAT27 3:cb3e8160f18e 182 * @param scl mbed pin to use for SCL line of I2C interface.
GAT27 3:cb3e8160f18e 183 */
dmcohen24 10:a89555c4f62a 184 SI1143(I2C* comm, PinName vcc, PinName enable, char offset);
dmcohen24 10:a89555c4f62a 185
dmcohen24 10:a89555c4f62a 186 char adrs;
dmcohen24 10:a89555c4f62a 187
dmcohen24 10:a89555c4f62a 188 DigitalOut* en;
dmcohen24 10:a89555c4f62a 189
dmcohen24 10:a89555c4f62a 190 DigitalOut* on;
dmcohen24 10:a89555c4f62a 191
GAT27 0:18ebb7348150 192
GAT27 3:cb3e8160f18e 193 /**
GAT27 5:3fadc61598bc 194 * Restarts the device.
GAT27 3:cb3e8160f18e 195 */
dmcohen24 10:a89555c4f62a 196 void restart(char offset);
GAT27 0:18ebb7348150 197
GAT27 3:cb3e8160f18e 198 /**
GAT27 3:cb3e8160f18e 199 * Creates a baseline for sampling measurements.
GAT27 4:af8f820733e0 200 * Should be done early in your code and after a reset.
GAT27 3:cb3e8160f18e 201 */
GAT27 5:3fadc61598bc 202 void bias(int ready, int repeat);
GAT27 5:3fadc61598bc 203
GAT27 5:3fadc61598bc 204 /**
GAT27 5:3fadc61598bc 205 * Takes a number of samples from the proximity of led1 and returns a raw output.
GAT27 5:3fadc61598bc 206 *
GAT27 5:3fadc61598bc 207 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 208 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 209 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 5:3fadc61598bc 210 */
GAT27 5:3fadc61598bc 211 int get_ps1(int repeat);
GAT27 5:3fadc61598bc 212
dmcohen24 9:31d4e955ee92 213 void start_ps_auto();
dmcohen24 9:31d4e955ee92 214
dmcohen24 9:31d4e955ee92 215 int read_ps1();
dmcohen24 9:31d4e955ee92 216
GAT27 5:3fadc61598bc 217 /**
GAT27 5:3fadc61598bc 218 * Takes a number of samples from the proximity of led2 and returns a raw output.
GAT27 5:3fadc61598bc 219 *
GAT27 5:3fadc61598bc 220 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 221 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 222 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 5:3fadc61598bc 223 */
GAT27 5:3fadc61598bc 224 int get_ps2(int repeat);
GAT27 0:18ebb7348150 225
dmcohen24 10:a89555c4f62a 226 int read_ps2();
dmcohen24 10:a89555c4f62a 227
GAT27 4:af8f820733e0 228 /**
GAT27 5:3fadc61598bc 229 * Takes a number of samples from the proximity of led3 and returns a raw output.
GAT27 5:3fadc61598bc 230 *
GAT27 5:3fadc61598bc 231 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 232 * @return In forced conversion output mode, will display a raw output of the average sample
GAT27 5:3fadc61598bc 233 * minus any baseline, where as the greater the value, the closer the object is to the device.
GAT27 4:af8f820733e0 234 */
GAT27 5:3fadc61598bc 235 int get_ps3(int repeat);
GAT27 5:3fadc61598bc 236
GAT27 5:3fadc61598bc 237 /**
GAT27 5:3fadc61598bc 238 * Takes a number of samples for ambient light on device and returns a raw output.
GAT27 5:3fadc61598bc 239 *
GAT27 5:3fadc61598bc 240 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 241 * @return In forced conversion output mode, will display a raw output of the average sample.
GAT27 5:3fadc61598bc 242 */
GAT27 5:3fadc61598bc 243 int get_vis(int repeat);
GAT27 5:3fadc61598bc 244
GAT27 5:3fadc61598bc 245 /**
GAT27 5:3fadc61598bc 246 * Takes a number of samples for infrared light on device and returns a raw output.
GAT27 5:3fadc61598bc 247 *
GAT27 5:3fadc61598bc 248 * @param repeat Tells how many samples to get from the device. Each sample takes 4 ms.
GAT27 5:3fadc61598bc 249 * @return In forced conversion output mode, will display a raw output of the average sample.
GAT27 5:3fadc61598bc 250 */
GAT27 5:3fadc61598bc 251 int get_ir(int repeat);
GAT27 4:af8f820733e0 252
dmcohen24 9:31d4e955ee92 253 void clear_int();
dmcohen24 9:31d4e955ee92 254
dmcohen24 10:a89555c4f62a 255 char read_PSint();
dmcohen24 10:a89555c4f62a 256
GAT27 0:18ebb7348150 257 private:
GAT27 0:18ebb7348150 258
GAT27 0:18ebb7348150 259 I2C* i2c_;
GAT27 0:18ebb7348150 260 int bias1,bias2,bias3,PS1,PS2,PS3,VIS,IR;
GAT27 0:18ebb7348150 261 char LowB,HighB;
GAT27 0:18ebb7348150 262
GAT27 3:cb3e8160f18e 263 /**
GAT27 3:cb3e8160f18e 264 * Wait for the device to respond, then send it a specific command.
GAT27 3:cb3e8160f18e 265 */
GAT27 0:18ebb7348150 266 void command(char cmd);
GAT27 0:18ebb7348150 267
GAT27 3:cb3e8160f18e 268 /**
GAT27 3:cb3e8160f18e 269 * Read a register from the device.
GAT27 3:cb3e8160f18e 270 */
GAT27 3:cb3e8160f18e 271 char read_reg(/*unsigned*/ char address, int num_data);
GAT27 0:18ebb7348150 272
GAT27 3:cb3e8160f18e 273 /**
GAT27 3:cb3e8160f18e 274 * Write to a register on the device.
GAT27 3:cb3e8160f18e 275 */
GAT27 3:cb3e8160f18e 276 void write_reg(char address, char num_data);
GAT27 0:18ebb7348150 277
GAT27 0:18ebb7348150 278 };
GAT27 0:18ebb7348150 279
GAT27 0:18ebb7348150 280 #endif