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