|
| 1 | +/* |
| 2 | +The MIT License(MIT) |
| 3 | +
|
| 4 | +Cayenne Arduino Client Library |
| 5 | +Copyright © 2016 myDevices |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | +documentation files(the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, |
| 10 | +and to permit persons to whom the Software is furnished to do so, subject to the following conditions : |
| 11 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 12 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
| 14 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 15 | +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | +
|
| 17 | +This class adapted from Adafruit VCNL400 example sketch https://github.com/adafruit/VCNL4000/blob/master/vcnl4000.pde |
| 18 | +*/ |
| 19 | + |
| 20 | +#ifndef _CAYENNEVCNL4000_h |
| 21 | +#define _CAYENNEVCNL4000_h |
| 22 | + |
| 23 | +#include <Wire.h> |
| 24 | +#include <Arduino.h> |
| 25 | + |
| 26 | +// commands and constants |
| 27 | +#define VCNL4000_ADDRESS 0x13 |
| 28 | +#define VCNL4000_COMMAND 0x80 |
| 29 | +#define VCNL4000_PRODUCTID 0x81 |
| 30 | +#define VCNL4000_IRLED 0x83 |
| 31 | +#define VCNL4000_AMBIENTPARAMETER 0x84 |
| 32 | +#define VCNL4000_AMBIENTDATA 0x85 |
| 33 | +#define VCNL4000_PROXIMITYDATA 0x87 |
| 34 | +#define VCNL4000_SIGNALFREQ 0x89 |
| 35 | +#define VCNL4000_PROXINITYADJUST 0x8A |
| 36 | + |
| 37 | +#define VCNL4000_3M125 0 |
| 38 | +#define VCNL4000_1M5625 1 |
| 39 | +#define VCNL4000_781K25 2 |
| 40 | +#define VCNL4000_390K625 3 |
| 41 | + |
| 42 | +#define VCNL4000_MEASUREAMBIENT 0x10 |
| 43 | +#define VCNL4000_MEASUREPROXIMITY 0x08 |
| 44 | +#define VCNL4000_AMBIENTREADY 0x40 |
| 45 | +#define VCNL4000_PROXIMITYREADY 0x20 |
| 46 | + |
| 47 | +#define CALIBRATION_CYCLES 5 |
| 48 | +#define MEASUREMENT_CYCLES 10 |
| 49 | +#define THRESHOLD 25 |
| 50 | +#define NO_PROXIMITY -1 |
| 51 | +#define TIMEOUT 2000 |
| 52 | + |
| 53 | +class VCNL4000 |
| 54 | +{ |
| 55 | +public: |
| 56 | + bool begin() { |
| 57 | + Wire.begin(); |
| 58 | + uint8_t rev = read8(VCNL4000_PRODUCTID); |
| 59 | + if ((rev & 0xF0) != 0x10) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + write8(VCNL4000_IRLED, 20); // Set IR current |
| 63 | + write8(VCNL4000_SIGNALFREQ, VCNL4000_781K25); // Set proximity signal frequency |
| 64 | + write8(VCNL4000_PROXINITYADJUST, 0x81); // Set proximity timing |
| 65 | + calibrate(); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + float getLux() { |
| 70 | + return (readAmbient() + 3) * 0.25; |
| 71 | + } |
| 72 | + |
| 73 | + int getMillimeters() { |
| 74 | + unsigned int success = 0; |
| 75 | + unsigned int fail = 0; |
| 76 | + unsigned int proximitySum = 0; |
| 77 | + while ((fail < MEASUREMENT_CYCLES) && (success < MEASUREMENT_CYCLES)) { |
| 78 | + uint16_t realCount = readProximity() - _offset; |
| 79 | + if (realCount > _threshold) { |
| 80 | + success++; |
| 81 | + proximitySum += realCount; |
| 82 | + } |
| 83 | + else { |
| 84 | + fail++; |
| 85 | + } |
| 86 | + } |
| 87 | + if (fail == MEASUREMENT_CYCLES) { |
| 88 | + return NO_PROXIMITY; |
| 89 | + } |
| 90 | + else { |
| 91 | + return calculateMillimeters(proximitySum / MEASUREMENT_CYCLES); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + uint16_t readProximity() { |
| 96 | + write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY); |
| 97 | + while (1) { |
| 98 | + uint8_t result = read8(VCNL4000_COMMAND); |
| 99 | + if (result & VCNL4000_PROXIMITYREADY) { |
| 100 | + return read16(VCNL4000_PROXIMITYDATA); |
| 101 | + } |
| 102 | + delay(1); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + uint16_t readAmbient() { |
| 107 | + write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT); |
| 108 | + while (1) { |
| 109 | + uint8_t result = read8(VCNL4000_COMMAND); |
| 110 | + if (result & VCNL4000_AMBIENTREADY) { |
| 111 | + return read16(VCNL4000_AMBIENTDATA); |
| 112 | + } |
| 113 | + delay(1); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +private: |
| 118 | + void calibrate() { |
| 119 | + for (int i = 0; i < CALIBRATION_CYCLES; ++i) { |
| 120 | + _offset += readProximity(); |
| 121 | + } |
| 122 | + _offset = _offset / CALIBRATION_CYCLES; |
| 123 | + } |
| 124 | + |
| 125 | + uint16_t calculateMillimeters(uint16_t proximityCounts) { |
| 126 | + //According to chip spec the proximity counts are strong non-linear with distance and cannot be calculated |
| 127 | + //with a direct formula. From experience found on web this chip is generally not suited for really exact |
| 128 | + //distance calculations. This is a rough distance estimation lookup table for now. Maybe someone can |
| 129 | + //provide a more exact approximation in the future. |
| 130 | + unsigned int estimatedDistance = 100; |
| 131 | + if (proximityCounts >= 10000) { |
| 132 | + estimatedDistance = 0; |
| 133 | + } |
| 134 | + else if (proximityCounts >= 3000) { |
| 135 | + estimatedDistance = 5; |
| 136 | + } |
| 137 | + else if (proximityCounts >= 900) { |
| 138 | + estimatedDistance = 10; |
| 139 | + } |
| 140 | + else if (proximityCounts >= 300) { |
| 141 | + estimatedDistance = 20; |
| 142 | + } |
| 143 | + else if (proximityCounts >= 150) { |
| 144 | + estimatedDistance = 30; |
| 145 | + } |
| 146 | + else if (proximityCounts >= 75) { |
| 147 | + estimatedDistance = 40; |
| 148 | + } |
| 149 | + else if (proximityCounts >= 50) { |
| 150 | + estimatedDistance = 50; |
| 151 | + } |
| 152 | + else if (proximityCounts >= 25) { |
| 153 | + estimatedDistance = 70; |
| 154 | + } |
| 155 | + return estimatedDistance; |
| 156 | + } |
| 157 | + |
| 158 | + // Read 1 byte from the VCNL4000 at 'address' |
| 159 | + uint8_t read8(uint8_t address) |
| 160 | + { |
| 161 | + uint8_t data; |
| 162 | + |
| 163 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 164 | +#if ARDUINO >= 100 |
| 165 | + Wire.write(address); |
| 166 | +#else |
| 167 | + Wire.send(address); |
| 168 | +#endif |
| 169 | + Wire.endTransmission(); |
| 170 | + |
| 171 | + delayMicroseconds(170); // delay required |
| 172 | + |
| 173 | + Wire.requestFrom(VCNL4000_ADDRESS, 1); |
| 174 | + unsigned int start = millis(); |
| 175 | + while (!Wire.available() && (millis() - start < TIMEOUT)); |
| 176 | + |
| 177 | +#if ARDUINO >= 100 |
| 178 | + return Wire.read(); |
| 179 | +#else |
| 180 | + return Wire.receive(); |
| 181 | +#endif |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + // Read 2 byte from the VCNL4000 at 'address' |
| 186 | + uint16_t read16(uint8_t address) |
| 187 | + { |
| 188 | + uint16_t data; |
| 189 | + |
| 190 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 191 | +#if ARDUINO >= 100 |
| 192 | + Wire.write(address); |
| 193 | +#else |
| 194 | + Wire.send(address); |
| 195 | +#endif |
| 196 | + Wire.endTransmission(); |
| 197 | + |
| 198 | + Wire.requestFrom(VCNL4000_ADDRESS, 2); |
| 199 | + unsigned int start = millis(); |
| 200 | + while (!Wire.available() && (millis() - start < TIMEOUT)); |
| 201 | +#if ARDUINO >= 100 |
| 202 | + data = Wire.read(); |
| 203 | + data <<= 8; |
| 204 | + start = millis(); |
| 205 | + while (!Wire.available() && (millis() - start < TIMEOUT)); |
| 206 | + data |= Wire.read(); |
| 207 | +#else |
| 208 | + data = Wire.receive(); |
| 209 | + data <<= 8; |
| 210 | + start = millis(); |
| 211 | + while (!Wire.available() && (millis() - start < TIMEOUT)); |
| 212 | + data |= Wire.receive(); |
| 213 | +#endif |
| 214 | + |
| 215 | + return data; |
| 216 | + } |
| 217 | + |
| 218 | + // write 1 byte |
| 219 | + void write8(uint8_t address, uint8_t data) |
| 220 | + { |
| 221 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 222 | +#if ARDUINO >= 100 |
| 223 | + Wire.write(address); |
| 224 | + Wire.write(data); |
| 225 | +#else |
| 226 | + Wire.send(address); |
| 227 | + Wire.send(data); |
| 228 | +#endif |
| 229 | + Wire.endTransmission(); |
| 230 | + } |
| 231 | + |
| 232 | + uint16_t _offset; |
| 233 | + uint16_t _threshold; |
| 234 | +}; |
| 235 | + |
| 236 | +#endif |
0 commit comments