Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if ARDUINO >= 100
- #include "Arduino.h"
- #else
- #include "WProgram.h"
- #endif
- #ifdef __AVR_ATtiny85__
- #include "TinyWireM.h"
- #define Wire TinyWireM
- #else
- #include <Wire.h>
- #endif
- #include "VCNL40x0.h"
- VCNL40x0::VCNL40x0() {
- }
- /**************************************************************************/
- /*!
- @brief Setups the HW
- */
- /**************************************************************************/
- boolean Adafruit_VCNL4010::begin(uint8_t addr) {
- _addr = addr;
- Wire.begin();
- uint8_t rev = read8(VCNL4010_PRODUCTID);
- //Serial.println(rev, HEX);
- if ((rev & 0xF0) != 0x20) {
- return false;
- }
- }
- VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
- _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
- _send[1] = Command;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
- _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
- _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
- *Command = (unsigned char)(_receive[0]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
- _send[0] = REGISTER_ID; // VCNL40x0 product ID revision register
- _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1); // Read 1 byte on I2C
- *ID = (unsigned char)(_receive[0]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
- _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
- _send[1] = Current;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
- _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register
- _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
- *Current = (unsigned char)(_receive[0]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
- _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
- _send[1] = ProximityRate;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
- _send[0] = REGISTER_AMBI_PARAMETER; // VCNL40x0 Ambient light configuration
- _send[1] = AmbiConfiguration;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
- _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
- _send[1] = InterruptControl;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
- _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
- _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
- *InterruptControl = (unsigned char)(_receive[0]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
- _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
- _send[1] = InterruptStatus;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
- _send[0] = REGISTER_PROX_TIMING; // VCNL40x0 Modulator Timing Adjustment register
- _send[1] = ModulatorTimingAdjustment;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
- _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
- _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
- *InterruptStatus = (unsigned char)(_receive[0]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
- _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
- _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
- *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
- _send[0] = REGISTER_AMBI_VALUE; // VCNL40x0 Ambient Light Value register
- _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
- _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
- *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
- unsigned char LoByte=0, HiByte=0;
- LoByte = (unsigned char)(LowThreshold & 0x00ff);
- HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
- _send[0] = REGISTER_INTERRUPT_LOW_THRES; // VCNL40x0 Low Threshold Register, Hi Byte
- _send[1] = HiByte;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- _send[0] = REGISTER_INTERRUPT_LOW_THRES+1; // VCNL40x0 Low Threshold Register, Lo Byte
- _send[1] = LoByte;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
- unsigned char LoByte=0, HiByte=0;
- LoByte = (unsigned char)(HighThreshold & 0x00ff);
- HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
- _send[0] = REGISTER_INTERRUPT_HIGH_THRES; // VCNL40x0 High Threshold Register, Hi Byte
- _send[1] = HiByte;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1; // VCNL40x0 High Threshold Register, Lo Byte
- _send[1] = LoByte;
- _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
- unsigned char Command=0;
- // enable prox value on demand
- SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
- // wait on prox data ready bit
- do {
- ReadCommandRegister (&Command); // read command register
- } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
- ReadProxiValue (ProxiValue); // read prox value
- SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
- return VCNL40x0_ERROR_OK;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
- unsigned char Command=0;
- // enable ambi value on demand
- SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
- // wait on ambi data ready bit
- do {
- ReadCommandRegister (&Command); // read command register
- } while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
- ReadAmbiValue (AmbiValue); // read ambi value
- SetCommandRegister (COMMAND_ALL_DISABLE); // stop ambi value on demand
- return VCNL40x0_ERROR_OK;
- }
- /**************************************************************************/
- /*!
- @brief I2C low level interfacing
- */
- /**************************************************************************/
- // Read 1 byte from the VCNL40x0 at 'address'
- uint8_t VCNL40x0::read8(uint8_t address)
- {
- uint8_t data;
- Wire.beginTransmission(_i2caddr);
- #if ARDUINO >= 100
- Wire.write(address);
- #else
- Wire.send(address);
- #endif
- Wire.endTransmission();
- delayMicroseconds(170); // delay required
- Wire.requestFrom(_i2caddr, (uint8_t)1);
- while(!Wire.available());
- #if ARDUINO >= 100
- return Wire.read();
- #else
- return Wire.receive();
- #endif
- }
- // Read 2 byte from the VCNL40x0 at 'address'
- uint16_t VCNL40x0::read16(uint8_t address)
- {
- uint16_t data;
- Wire.beginTransmission(_i2caddr);
- #if ARDUINO >= 100
- Wire.write(address);
- #else
- Wire.send(address);
- #endif
- Wire.endTransmission();
- Wire.requestFrom(_i2caddr, (uint8_t)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 VCNL40x0::write8(uint8_t address, uint8_t data)
- {
- Wire.beginTransmission(_i2caddr);
- #if ARDUINO >= 100
- Wire.write(address);
- Wire.write(data);
- #else
- Wire.send(address);
- Wire.send(data);
- #endif
- Wire.endTransmission();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement