Create Adafruit_VCNL4000.cpp
This commit is contained in:
parent
d38b0a4a32
commit
5f1943ac1a
1 changed files with 105 additions and 0 deletions
105
Feather_Holiday_Lights/Feather_32u4_Lights/Adafruit_VCNL4000.cpp
Normal file
105
Feather_Holiday_Lights/Feather_32u4_Lights/Adafruit_VCNL4000.cpp
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
#include "Adafruit_VCNL4000.h"
|
||||
|
||||
|
||||
bool Adafruit_VCNL4000::begin() {
|
||||
Wire.begin();
|
||||
|
||||
// Check VCNL4000 product ID and fail if not expected value.
|
||||
uint8_t rev = read8(VCNL4000_PRODUCTID);
|
||||
if ((rev & 0xF0) != 0x10) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set IR LED current to 200mA.
|
||||
write8(VCNL4000_IRLED, 20); // set to 20 * 10mA = 200mA
|
||||
|
||||
// Set proximity adjustment value.
|
||||
write8(VCNL4000_PROXINITYADJUST, 0x81);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t Adafruit_VCNL4000::readProximity() {
|
||||
write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
|
||||
while (1) {
|
||||
uint8_t result = read8(VCNL4000_COMMAND);
|
||||
//Serial.print("Ready = 0x"); Serial.println(result, HEX);
|
||||
if (result & VCNL4000_PROXIMITYREADY) {
|
||||
return read16(VCNL4000_PROXIMITYDATA);
|
||||
}
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Read 1 byte from the VCNL4000 at 'address'
|
||||
uint8_t Adafruit_VCNL4000::read8(uint8_t address)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
Wire.beginTransmission(VCNL4000_ADDRESS);
|
||||
#if ARDUINO >= 100
|
||||
Wire.write(address);
|
||||
#else
|
||||
Wire.send(address);
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
|
||||
delayMicroseconds(170); // delay required
|
||||
|
||||
Wire.requestFrom(VCNL4000_ADDRESS, 1);
|
||||
while(!Wire.available());
|
||||
|
||||
#if ARDUINO >= 100
|
||||
return Wire.read();
|
||||
#else
|
||||
return Wire.receive();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// Read 2 byte from the VCNL4000 at 'address'
|
||||
uint16_t Adafruit_VCNL4000::read16(uint8_t address)
|
||||
{
|
||||
uint16_t data;
|
||||
|
||||
Wire.beginTransmission(VCNL4000_ADDRESS);
|
||||
#if ARDUINO >= 100
|
||||
Wire.write(address);
|
||||
#else
|
||||
Wire.send(address);
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.requestFrom(VCNL4000_ADDRESS, 2);
|
||||
while(!Wire.available());
|
||||
#if ARDUINO >= 100
|
||||
data = Wire.read();
|
||||
data <<= 8;
|
||||
while(!Wire.available());
|
||||
data |= Wire.read();
|
||||
#else
|
||||
data = Wire.receive();
|
||||
data <<= 8;
|
||||
while(!Wire.available());
|
||||
data |= Wire.receive();
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// write 1 byte
|
||||
void Adafruit_VCNL4000::write8(uint8_t address, uint8_t data)
|
||||
{
|
||||
Wire.beginTransmission(VCNL4000_ADDRESS);
|
||||
#if ARDUINO >= 100
|
||||
Wire.write(address);
|
||||
Wire.write(data);
|
||||
#else
|
||||
Wire.send(address);
|
||||
Wire.send(data);
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
}
|
||||
Loading…
Reference in a new issue