This library demonstrates how to use the sensors on the QW Shield.

Dependents:   QW-TEMP_GPS-NMEA QW-Motiondetection QW-Closet-detection

Committer:
quicksand
Date:
Wed May 18 14:44:57 2016 +0000
Revision:
3:1b27ad5eb94a
Parent:
0:4b56d28cc7e9
Renamed main.cpp to main.txt (example usage code) to avoid conflicts and confusion.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quicksand 0:4b56d28cc7e9 1 /*
quicksand 0:4b56d28cc7e9 2 Copyright (c) 2012 Vishay GmbH, www.vishay.com
quicksand 0:4b56d28cc7e9 3 author: DS, version 1.2
quicksand 0:4b56d28cc7e9 4
quicksand 0:4b56d28cc7e9 5 Permission is hereby granted, free of charge, to any person obtaining a copy
quicksand 0:4b56d28cc7e9 6 of this software and associated documentation files (the "Software"), to deal
quicksand 0:4b56d28cc7e9 7 in the Software without restriction, including without limitation the rights
quicksand 0:4b56d28cc7e9 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
quicksand 0:4b56d28cc7e9 9 copies of the Software, and to permit persons to whom the Software is
quicksand 0:4b56d28cc7e9 10 furnished to do so, subject to the following conditions:
quicksand 0:4b56d28cc7e9 11
quicksand 0:4b56d28cc7e9 12 The above copyright notice and this permission notice shall be included in
quicksand 0:4b56d28cc7e9 13 all copies or substantial portions of the Software.
quicksand 0:4b56d28cc7e9 14
quicksand 0:4b56d28cc7e9 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
quicksand 0:4b56d28cc7e9 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
quicksand 0:4b56d28cc7e9 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
quicksand 0:4b56d28cc7e9 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
quicksand 0:4b56d28cc7e9 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
quicksand 0:4b56d28cc7e9 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
quicksand 0:4b56d28cc7e9 21 THE SOFTWARE.
quicksand 0:4b56d28cc7e9 22 */
quicksand 0:4b56d28cc7e9 23
quicksand 0:4b56d28cc7e9 24 #ifndef VCNL40x0_H
quicksand 0:4b56d28cc7e9 25 #define VCNL40x0_H
quicksand 0:4b56d28cc7e9 26
quicksand 0:4b56d28cc7e9 27 #include "mbed.h"
quicksand 0:4b56d28cc7e9 28
quicksand 0:4b56d28cc7e9 29 // Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020
quicksand 0:4b56d28cc7e9 30 // The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
quicksand 0:4b56d28cc7e9 31
quicksand 0:4b56d28cc7e9 32 #define VCNL40x0_ADDRESS (0x26) // 001 0011 shifted left 1 bit = 0x26
quicksand 0:4b56d28cc7e9 33
quicksand 0:4b56d28cc7e9 34 // registers
quicksand 0:4b56d28cc7e9 35 #define REGISTER_COMMAND (0x80)
quicksand 0:4b56d28cc7e9 36 #define REGISTER_ID (0x81)
quicksand 0:4b56d28cc7e9 37 #define REGISTER_PROX_RATE (0x82)
quicksand 0:4b56d28cc7e9 38 #define REGISTER_PROX_CURRENT (0x83)
quicksand 0:4b56d28cc7e9 39 #define REGISTER_AMBI_PARAMETER (0x84)
quicksand 0:4b56d28cc7e9 40 #define REGISTER_AMBI_VALUE (0x85)
quicksand 0:4b56d28cc7e9 41 #define REGISTER_PROX_VALUE (0x87)
quicksand 0:4b56d28cc7e9 42 #define REGISTER_INTERRUPT_CONTROL (0x89)
quicksand 0:4b56d28cc7e9 43 #define REGISTER_INTERRUPT_LOW_THRES (0x8a)
quicksand 0:4b56d28cc7e9 44 #define REGISTER_INTERRUPT_HIGH_THRES (0x8c)
quicksand 0:4b56d28cc7e9 45 #define REGISTER_INTERRUPT_STATUS (0x8e)
quicksand 0:4b56d28cc7e9 46 #define REGISTER_PROX_TIMING (0x8f)
quicksand 0:4b56d28cc7e9 47 #define REGISTER_AMBI_IR_LIGHT_LEVEL (0x90) // This register is not intended to be use by customer
quicksand 0:4b56d28cc7e9 48
quicksand 0:4b56d28cc7e9 49 // Bits in Command register (0x80)
quicksand 0:4b56d28cc7e9 50 #define COMMAND_ALL_DISABLE (0x00)
quicksand 0:4b56d28cc7e9 51 #define COMMAND_SELFTIMED_MODE_ENABLE (0x01)
quicksand 0:4b56d28cc7e9 52 #define COMMAND_PROX_ENABLE (0x02)
quicksand 0:4b56d28cc7e9 53 #define COMMAND_AMBI_ENABLE (0x04)
quicksand 0:4b56d28cc7e9 54 #define COMMAND_PROX_ON_DEMAND (0x08)
quicksand 0:4b56d28cc7e9 55 #define COMMAND_AMBI_ON_DEMAND (0x10)
quicksand 0:4b56d28cc7e9 56 #define COMMAND_MASK_PROX_DATA_READY (0x20)
quicksand 0:4b56d28cc7e9 57 #define COMMAND_MASK_AMBI_DATA_READY (0x40)
quicksand 0:4b56d28cc7e9 58 #define COMMAND_MASK_LOCK (0x80)
quicksand 0:4b56d28cc7e9 59
quicksand 0:4b56d28cc7e9 60 // Bits in Product ID Revision Register (0x81)
quicksand 0:4b56d28cc7e9 61 #define PRODUCT_MASK_REVISION_ID (0x0f)
quicksand 0:4b56d28cc7e9 62 #define PRODUCT_MASK_PRODUCT_ID (0xf0)
quicksand 0:4b56d28cc7e9 63
quicksand 0:4b56d28cc7e9 64 // Bits in Prox Measurement Rate register (0x82)
quicksand 0:4b56d28cc7e9 65 #define PROX_MEASUREMENT_RATE_2 (0x00) // DEFAULT
quicksand 0:4b56d28cc7e9 66 #define PROX_MEASUREMENT_RATE_4 (0x01)
quicksand 0:4b56d28cc7e9 67 #define PROX_MEASUREMENT_RATE_8 (0x02)
quicksand 0:4b56d28cc7e9 68 #define PROX_MEASUREMENT_RATE_16 (0x03)
quicksand 0:4b56d28cc7e9 69 #define PROX_MEASUREMENT_RATE_31 (0x04)
quicksand 0:4b56d28cc7e9 70 #define PROX_MEASUREMENT_RATE_62 (0x05)
quicksand 0:4b56d28cc7e9 71 #define PROX_MEASUREMENT_RATE_125 (0x06)
quicksand 0:4b56d28cc7e9 72 #define PROX_MEASUREMENT_RATE_250 (0x07)
quicksand 0:4b56d28cc7e9 73 #define PROX_MASK_MEASUREMENT_RATE (0x07)
quicksand 0:4b56d28cc7e9 74
quicksand 0:4b56d28cc7e9 75 // Bits in Procimity LED current setting (0x83)
quicksand 0:4b56d28cc7e9 76 #define PROX_MASK_LED_CURRENT (0x3f) // DEFAULT = 2
quicksand 0:4b56d28cc7e9 77 #define PROX_MASK_FUSE_PROG_ID (0xc0)
quicksand 0:4b56d28cc7e9 78
quicksand 0:4b56d28cc7e9 79 // Bits in Ambient Light Parameter register (0x84)
quicksand 0:4b56d28cc7e9 80 #define AMBI_PARA_AVERAGE_1 (0x00)
quicksand 0:4b56d28cc7e9 81 #define AMBI_PARA_AVERAGE_2 (0x01)
quicksand 0:4b56d28cc7e9 82 #define AMBI_PARA_AVERAGE_4 (0x02)
quicksand 0:4b56d28cc7e9 83 #define AMBI_PARA_AVERAGE_8 (0x03)
quicksand 0:4b56d28cc7e9 84 #define AMBI_PARA_AVERAGE_16 (0x04)
quicksand 0:4b56d28cc7e9 85 #define AMBI_PARA_AVERAGE_32 (0x05) // DEFAULT
quicksand 0:4b56d28cc7e9 86 #define AMBI_PARA_AVERAGE_64 (0x06)
quicksand 0:4b56d28cc7e9 87 #define AMBI_PARA_AVERAGE_128 (0x07)
quicksand 0:4b56d28cc7e9 88 #define AMBI_MASK_PARA_AVERAGE (0x07)
quicksand 0:4b56d28cc7e9 89
quicksand 0:4b56d28cc7e9 90 #define AMBI_PARA_AUTO_OFFSET_ENABLE (0x08) // DEFAULT enable
quicksand 0:4b56d28cc7e9 91 #define AMBI_MASK_PARA_AUTO_OFFSET (0x08)
quicksand 0:4b56d28cc7e9 92
quicksand 0:4b56d28cc7e9 93 #define AMBI_PARA_MEAS_RATE_1 (0x00)
quicksand 0:4b56d28cc7e9 94 #define AMBI_PARA_MEAS_RATE_2 (0x10) // DEFAULT
quicksand 0:4b56d28cc7e9 95 #define AMBI_PARA_MEAS_RATE_3 (0x20)
quicksand 0:4b56d28cc7e9 96 #define AMBI_PARA_MEAS_RATE_4 (0x30)
quicksand 0:4b56d28cc7e9 97 #define AMBI_PARA_MEAS_RATE_5 (0x40)
quicksand 0:4b56d28cc7e9 98 #define AMBI_PARA_MEAS_RATE_6 (0x50)
quicksand 0:4b56d28cc7e9 99 #define AMBI_PARA_MEAS_RATE_8 (0x60)
quicksand 0:4b56d28cc7e9 100 #define AMBI_PARA_MEAS_RATE_10 (0x70)
quicksand 0:4b56d28cc7e9 101 #define AMBI_MASK_PARA_MEAS_RATE (0x70)
quicksand 0:4b56d28cc7e9 102
quicksand 0:4b56d28cc7e9 103 #define AMBI_PARA_CONT_CONV_ENABLE (0x80)
quicksand 0:4b56d28cc7e9 104 #define AMBI_MASK_PARA_CONT_CONV (0x80) // DEFAULT disable
quicksand 0:4b56d28cc7e9 105
quicksand 0:4b56d28cc7e9 106 // Bits in Interrupt Control Register (x89)
quicksand 0:4b56d28cc7e9 107 #define INTERRUPT_THRES_SEL_PROX (0x00)
quicksand 0:4b56d28cc7e9 108 #define INTERRUPT_THRES_SEL_ALS (0x01)
quicksand 0:4b56d28cc7e9 109
quicksand 0:4b56d28cc7e9 110 #define INTERRUPT_THRES_ENABLE (0x02)
quicksand 0:4b56d28cc7e9 111
quicksand 0:4b56d28cc7e9 112 #define INTERRUPT_ALS_READY_ENABLE (0x04)
quicksand 0:4b56d28cc7e9 113
quicksand 0:4b56d28cc7e9 114 #define INTERRUPT_PROX_READY_ENABLE (0x08)
quicksand 0:4b56d28cc7e9 115
quicksand 0:4b56d28cc7e9 116 #define INTERRUPT_COUNT_EXCEED_1 (0x00) // DEFAULT
quicksand 0:4b56d28cc7e9 117 #define INTERRUPT_COUNT_EXCEED_2 (0x20)
quicksand 0:4b56d28cc7e9 118 #define INTERRUPT_COUNT_EXCEED_4 (0x40)
quicksand 0:4b56d28cc7e9 119 #define INTERRUPT_COUNT_EXCEED_8 (0x60)
quicksand 0:4b56d28cc7e9 120 #define INTERRUPT_COUNT_EXCEED_16 (0x80)
quicksand 0:4b56d28cc7e9 121 #define INTERRUPT_COUNT_EXCEED_32 (0xa0)
quicksand 0:4b56d28cc7e9 122 #define INTERRUPT_COUNT_EXCEED_64 (0xc0)
quicksand 0:4b56d28cc7e9 123 #define INTERRUPT_COUNT_EXCEED_128 (0xe0)
quicksand 0:4b56d28cc7e9 124 #define INTERRUPT_MASK_COUNT_EXCEED (0xe0)
quicksand 0:4b56d28cc7e9 125
quicksand 0:4b56d28cc7e9 126 // Bits in Interrupt Status Register (x8e)
quicksand 0:4b56d28cc7e9 127 #define INTERRUPT_STATUS_THRES_HI (0x01)
quicksand 0:4b56d28cc7e9 128 #define INTERRUPT_STATUS_THRES_LO (0x02)
quicksand 0:4b56d28cc7e9 129 #define INTERRUPT_STATUS_ALS_READY (0x04)
quicksand 0:4b56d28cc7e9 130 #define INTERRUPT_STATUS_PROX_READY (0x08)
quicksand 0:4b56d28cc7e9 131 #define INTERRUPT_MASK_STATUS_THRES_HI (0x01)
quicksand 0:4b56d28cc7e9 132 #define INTERRUPT_MASK_THRES_LO (0x02)
quicksand 0:4b56d28cc7e9 133 #define INTERRUPT_MASK_ALS_READY (0x04)
quicksand 0:4b56d28cc7e9 134 #define INTERRUPT_MASK_PROX_READY (0x08)
quicksand 0:4b56d28cc7e9 135
quicksand 0:4b56d28cc7e9 136 typedef enum {
quicksand 0:4b56d28cc7e9 137 VCNL40x0_ERROR_OK = 0, // Everything executed normally
quicksand 0:4b56d28cc7e9 138 VCNL40x0_ERROR_I2CINIT, // Unable to initialise I2C
quicksand 0:4b56d28cc7e9 139 VCNL40x0_ERROR_I2CBUSY, // I2C already in use
quicksand 0:4b56d28cc7e9 140 VCNL40x0_ERROR_LAST
quicksand 0:4b56d28cc7e9 141 } VCNL40x0Error_e;
quicksand 0:4b56d28cc7e9 142
quicksand 0:4b56d28cc7e9 143
quicksand 0:4b56d28cc7e9 144 class VCNL40x0 {
quicksand 0:4b56d28cc7e9 145 public:
quicksand 0:4b56d28cc7e9 146 // Creates an instance of the class.
quicksand 0:4b56d28cc7e9 147 // Connect module at I2C address addr using I2C port pins sda and scl.
quicksand 0:4b56d28cc7e9 148
quicksand 0:4b56d28cc7e9 149 VCNL40x0 (PinName sda, PinName scl, unsigned char addr);
quicksand 0:4b56d28cc7e9 150
quicksand 0:4b56d28cc7e9 151 // Destroys instance
quicksand 0:4b56d28cc7e9 152
quicksand 0:4b56d28cc7e9 153 ~VCNL40x0();
quicksand 0:4b56d28cc7e9 154
quicksand 0:4b56d28cc7e9 155 // public functions
quicksand 0:4b56d28cc7e9 156
quicksand 0:4b56d28cc7e9 157 VCNL40x0Error_e Init (void);
quicksand 0:4b56d28cc7e9 158
quicksand 0:4b56d28cc7e9 159 VCNL40x0Error_e SetCommandRegister (unsigned char Command);
quicksand 0:4b56d28cc7e9 160 VCNL40x0Error_e SetCurrent (unsigned char CurrentValue);
quicksand 0:4b56d28cc7e9 161 VCNL40x0Error_e SetProximityRate (unsigned char ProximityRate);
quicksand 0:4b56d28cc7e9 162 VCNL40x0Error_e SetAmbiConfiguration (unsigned char AmbiConfiguration);
quicksand 0:4b56d28cc7e9 163 VCNL40x0Error_e SetLowThreshold (unsigned int LowThreshold);
quicksand 0:4b56d28cc7e9 164 VCNL40x0Error_e SetHighThreshold (unsigned int HighThreshold);
quicksand 0:4b56d28cc7e9 165 VCNL40x0Error_e SetInterruptControl (unsigned char InterruptControl);
quicksand 0:4b56d28cc7e9 166 VCNL40x0Error_e SetInterruptStatus (unsigned char InterruptStatus);
quicksand 0:4b56d28cc7e9 167 VCNL40x0Error_e SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
quicksand 0:4b56d28cc7e9 168
quicksand 0:4b56d28cc7e9 169 VCNL40x0Error_e ReadID (unsigned char *ID);
quicksand 0:4b56d28cc7e9 170 VCNL40x0Error_e ReadCurrent (unsigned char *CurrentValue);
quicksand 0:4b56d28cc7e9 171 VCNL40x0Error_e ReadCommandRegister (unsigned char *Command);
quicksand 0:4b56d28cc7e9 172 VCNL40x0Error_e ReadProxiValue (unsigned int *ProxiValue);
quicksand 0:4b56d28cc7e9 173 VCNL40x0Error_e ReadAmbiValue (unsigned int *AmbiValue);
quicksand 0:4b56d28cc7e9 174 VCNL40x0Error_e ReadInterruptStatus (unsigned char *InterruptStatus);
quicksand 0:4b56d28cc7e9 175 VCNL40x0Error_e ReadInterruptControl (unsigned char *InterruptControl);
quicksand 0:4b56d28cc7e9 176
quicksand 0:4b56d28cc7e9 177 VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
quicksand 0:4b56d28cc7e9 178 VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
quicksand 0:4b56d28cc7e9 179
quicksand 0:4b56d28cc7e9 180 private:
quicksand 0:4b56d28cc7e9 181 I2C _i2c;
quicksand 0:4b56d28cc7e9 182 int _addr;
quicksand 0:4b56d28cc7e9 183 char _send[3];
quicksand 0:4b56d28cc7e9 184 char _receive[2];
quicksand 0:4b56d28cc7e9 185
quicksand 0:4b56d28cc7e9 186 };
quicksand 0:4b56d28cc7e9 187
quicksand 0:4b56d28cc7e9 188 #endif