|
| 1 | +#include "Arduino.h" |
| 2 | +#include "Wire.h" |
| 3 | + |
| 4 | +#include "Adafruit_VCNL4000.h" |
| 5 | + |
| 6 | + |
| 7 | +bool Adafruit_VCNL4000::begin() { |
| 8 | + Wire.begin(); |
| 9 | + |
| 10 | + // Check VCNL4000 product ID and fail if not expected value. |
| 11 | + uint8_t rev = read8(VCNL4000_PRODUCTID); |
| 12 | + if ((rev & 0xF0) != 0x10) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + |
| 16 | + // Set IR LED current to 200mA. |
| 17 | + write8(VCNL4000_IRLED, 20); // set to 20 * 10mA = 200mA |
| 18 | + |
| 19 | + // Set proximity adjustment value. |
| 20 | + write8(VCNL4000_PROXINITYADJUST, 0x81); |
| 21 | + |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +uint16_t Adafruit_VCNL4000::readProximity() { |
| 26 | + write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY); |
| 27 | + while (1) { |
| 28 | + uint8_t result = read8(VCNL4000_COMMAND); |
| 29 | + //Serial.print("Ready = 0x"); Serial.println(result, HEX); |
| 30 | + if (result & VCNL4000_PROXIMITYREADY) { |
| 31 | + return read16(VCNL4000_PROXIMITYDATA); |
| 32 | + } |
| 33 | + delay(1); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Read 1 byte from the VCNL4000 at 'address' |
| 38 | +uint8_t Adafruit_VCNL4000::read8(uint8_t address) |
| 39 | +{ |
| 40 | + uint8_t data; |
| 41 | + |
| 42 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 43 | +#if ARDUINO >= 100 |
| 44 | + Wire.write(address); |
| 45 | +#else |
| 46 | + Wire.send(address); |
| 47 | +#endif |
| 48 | + Wire.endTransmission(); |
| 49 | + |
| 50 | + delayMicroseconds(170); // delay required |
| 51 | + |
| 52 | + Wire.requestFrom(VCNL4000_ADDRESS, 1); |
| 53 | + while(!Wire.available()); |
| 54 | + |
| 55 | +#if ARDUINO >= 100 |
| 56 | + return Wire.read(); |
| 57 | +#else |
| 58 | + return Wire.receive(); |
| 59 | +#endif |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +// Read 2 byte from the VCNL4000 at 'address' |
| 64 | +uint16_t Adafruit_VCNL4000::read16(uint8_t address) |
| 65 | +{ |
| 66 | + uint16_t data; |
| 67 | + |
| 68 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 69 | +#if ARDUINO >= 100 |
| 70 | + Wire.write(address); |
| 71 | +#else |
| 72 | + Wire.send(address); |
| 73 | +#endif |
| 74 | + Wire.endTransmission(); |
| 75 | + |
| 76 | + Wire.requestFrom(VCNL4000_ADDRESS, 2); |
| 77 | + while(!Wire.available()); |
| 78 | +#if ARDUINO >= 100 |
| 79 | + data = Wire.read(); |
| 80 | + data <<= 8; |
| 81 | + while(!Wire.available()); |
| 82 | + data |= Wire.read(); |
| 83 | +#else |
| 84 | + data = Wire.receive(); |
| 85 | + data <<= 8; |
| 86 | + while(!Wire.available()); |
| 87 | + data |= Wire.receive(); |
| 88 | +#endif |
| 89 | + |
| 90 | + return data; |
| 91 | +} |
| 92 | + |
| 93 | +// write 1 byte |
| 94 | +void Adafruit_VCNL4000::write8(uint8_t address, uint8_t data) |
| 95 | +{ |
| 96 | + Wire.beginTransmission(VCNL4000_ADDRESS); |
| 97 | +#if ARDUINO >= 100 |
| 98 | + Wire.write(address); |
| 99 | + Wire.write(data); |
| 100 | +#else |
| 101 | + Wire.send(address); |
| 102 | + Wire.send(data); |
| 103 | +#endif |
| 104 | + Wire.endTransmission(); |
| 105 | +} |
0 commit comments