VCNL4010  v1.1.0
Arduino Library for VCNL4010
VCNL4010.h
Go to the documentation of this file.
1 // clang-format off
73 // clang-format on
74 #include "Arduino.h" // Arduino data type definitions
75 #include <Wire.h> // Standard I2C "Wire" library
76 #ifndef VCNL4010_h // Guard code definition
77 
78 #define VCNL4010_h // Define the name inside guard code
79 #ifndef _BV
80 #define _BV(bit) (1 << (bit))
81 #endif
82 /********************************************
83 ** Declare all constants used in the class **
84 ********************************************/
85 #ifndef I2C_MODES // I2C related constants
86 
87 #define I2C_MODES // Guard code to prevent multiple declarations
88 const uint32_t I2C_STANDARD_MODE{100000};
89 const uint32_t I2C_FAST_MODE{400000};
90 const uint32_t I2C_FAST_MODE_PLUS_MODE{1000000};
91 const uint32_t I2C_HIGH_SPEED_MODE{3400000};
92 #endif
93 const uint8_t VCNL4010_I2C_ADDRESS{0x13};
94 const uint8_t VCNL4010_PRODUCT_VERSION{0x21};
95 const uint8_t VCNL4010_I2C_MS_DELAY{200};
96 const uint8_t REGISTER_CMD{0x80};
97 const uint8_t REGISTER_PRODUCT{0x81};
98 const uint8_t REGISTER_PROXIMITY_RATE{0x82};
99 const uint8_t REGISTER_LED_CURRENT{0x83};
100 const uint8_t REGISTER_AMBIENT_PARAM{0x84};
101 const uint8_t REGISTER_AMBIENT_LIGHT{0x85};
102 const uint8_t REGISTER_PROXIMITY{0x87};
103 const uint8_t REGISTER_INTERRUPT{0x89};
104 const uint8_t REGISTER_LOW_THRESH_MSB{0x8A};
105 const uint8_t REGISTER_LOW_THRESH_LSB{0x8B};
106 const uint8_t REGISTER_HIGH_THRESH_MSB{0x8C};
107 const uint8_t REGISTER_HIGH_THRESH_LSB{0x8D};
108 const uint8_t REGISTER_INTERRUPT_STATUS{0x8E};
109 const uint8_t REGISTER_PROXIMITY_TIMING{0x8F};
110 const uint8_t BIT_ALS_DATA_RDY{6};
111 const uint8_t BIT_PROX_DATA_RDY{5};
112 const uint8_t BIT_ALS_OD{4};
113 const uint8_t BIT_PROX_OD{3};
114 const uint8_t BIT_ALS_EN{2};
115 const uint8_t BIT_PROX_EN{1};
116 const uint8_t BIT_SELFTIMED_EN{0};
117 
118 class VCNL4010 {
119  public:
124  VCNL4010();
125  ~VCNL4010();
126  bool begin(void); // Overloaded just device address
127  bool begin(const uint8_t deviceAddress); // Overloaded just device address
128  bool begin(const uint32_t &i2CSpeed); // Overloaded just speed
129  bool begin(const uint8_t deviceAddress, // Start I2C communications
130  const uint32_t &i2CSpeed); // specifying both parameters
131  void setProximityHz(const uint8_t Hz = 2); // Set proximity Hz sampling rate
132  void setLEDmA(const uint8_t mA = 20) const; // Set milliamperes used by IR LED
133  void setProximityFreq(const uint8_t value = 0); // Set Frequency value from list
134  uint16_t getAmbientLight() const; // Retrieve ambient light reading
135  uint16_t getProximity() const; // Retrieve proximity reading
136  uint8_t getInterrupt() const; // Retrieve Interrupt bits
137  void clearInterrupt(const uint8_t intVal = 0) const; // Clear Interrupt bits
138  uint8_t readByte(const uint8_t addr) const; // Read a single byte from device
139  uint16_t readWord(const uint8_t addr) const; // Read two bytes from device
140  void writeByte(const uint8_t addr, const uint8_t data) const; // Write a byte to device
141  void setAmbientLight(const uint8_t sample = 2,
142  const uint8_t avg = 32) const; // Set samples and avg
143  void setAmbientContinuous(const bool ContinuousMode = true); // Cont. Ambient sampling on/off
144  void setProximityContinuous(const bool ContinuousMode = true); // Cont. Prox sampling on/off
145  void setInterrupt(const uint8_t count = 1, const bool ProxReady = false,
146  const bool ALSReady = false, const bool ProxThreshold = false,
147  const bool ALSThreshold = false, const uint16_t lowThreshold = 0,
148  const uint16_t highThreshold = UINT16_MAX) const;
149 
150  private:
151  bool _ContinuousAmbient = false; // If mode turned on for Ambient readings
152  bool _ContinuousProximity = false; // If mode turned on for Proximity readings
153  uint8_t _I2Caddress = VCNL4010_I2C_ADDRESS; // Default to standard I2C address
154 }; // of VCNL4010 class definition
155 #endif
void setAmbientContinuous(const bool ContinuousMode=true)
Definition: VCNL4010.cpp:375
const uint8_t VCNL4010_I2C_MS_DELAY
I2C Delay in communications.
Definition: VCNL4010.h:95
const uint8_t BIT_ALS_OD
start On-Demand ALS reading bit
Definition: VCNL4010.h:112
Main class definition.
Definition: VCNL4010.h:118
const uint8_t REGISTER_AMBIENT_PARAM
Register containing ambient set.
Definition: VCNL4010.h:100
const uint32_t I2C_FAST_MODE
Fast mode.
Definition: VCNL4010.h:89
void setInterrupt(const uint8_t count=1, const bool ProxReady=false, const bool ALSReady=false, const bool ProxThreshold=false, const bool ALSThreshold=false, const uint16_t lowThreshold=0, const uint16_t highThreshold=UINT16_MAX) const
Definition: VCNL4010.cpp:327
const uint8_t REGISTER_PROXIMITY_RATE
Register containing sampling rate.
Definition: VCNL4010.h:98
const uint8_t REGISTER_HIGH_THRESH_MSB
MSB of high threshold value.
Definition: VCNL4010.h:106
void setProximityHz(const uint8_t Hz=2)
Definition: VCNL4010.cpp:131
const uint8_t BIT_ALS_EN
enable periodic ALS reading bit
Definition: VCNL4010.h:114
void setProximityContinuous(const bool ContinuousMode=true)
Definition: VCNL4010.cpp:396
const uint8_t REGISTER_CMD
Register containing commands.
Definition: VCNL4010.h:96
const uint8_t VCNL4010_PRODUCT_VERSION
Current product ID.
Definition: VCNL4010.h:94
const uint32_t I2C_FAST_MODE_PLUS_MODE
Really fast mode.
Definition: VCNL4010.h:90
const uint8_t REGISTER_INTERRUPT
Register containing Interrupts.
Definition: VCNL4010.h:103
const uint8_t BIT_PROX_OD
start On-Demand PROX reading bit
Definition: VCNL4010.h:113
const uint8_t BIT_ALS_DATA_RDY
ALS data ready bit.
Definition: VCNL4010.h:110
const uint8_t BIT_PROX_EN
enable periodic PROX reading bit
Definition: VCNL4010.h:115
uint16_t getAmbientLight() const
Definition: VCNL4010.cpp:269
const uint8_t REGISTER_PROXIMITY
Register containing Proximity.
Definition: VCNL4010.h:102
void clearInterrupt(const uint8_t intVal=0) const
Definition: VCNL4010.cpp:312
const uint8_t REGISTER_LOW_THRESH_MSB
MSB of low threshold value.
Definition: VCNL4010.h:104
void setAmbientLight(const uint8_t sample=2, const uint8_t avg=32) const
Definition: VCNL4010.cpp:218
const uint8_t REGISTER_HIGH_THRESH_LSB
LSB of high threshold value.
Definition: VCNL4010.h:107
bool begin(void)
Definition: VCNL4010.cpp:61
uint16_t readWord(const uint8_t addr) const
Definition: VCNL4010.cpp:101
VCNL4010()
Definition: VCNL4010.cpp:8
const uint8_t BIT_SELFTIMED_EN
enable periodic (self-timed) reading bit
Definition: VCNL4010.h:116
const uint8_t REGISTER_LOW_THRESH_LSB
LSB of low threshold value.
Definition: VCNL4010.h:105
uint16_t getProximity() const
Definition: VCNL4010.cpp:285
void writeByte(const uint8_t addr, const uint8_t data) const
Definition: VCNL4010.cpp:119
const uint8_t BIT_PROX_DATA_RDY
PROX data ready bit.
Definition: VCNL4010.h:111
~VCNL4010()
Definition: VCNL4010.cpp:14
const uint8_t REGISTER_PROXIMITY_TIMING
Register containing ProxTiming.
Definition: VCNL4010.h:109
const uint8_t REGISTER_PRODUCT
Register containing product ID.
Definition: VCNL4010.h:97
uint8_t readByte(const uint8_t addr) const
Definition: VCNL4010.cpp:87
const uint8_t REGISTER_INTERRUPT_STATUS
Interrupt status register.
Definition: VCNL4010.h:108
void setLEDmA(const uint8_t mA=20) const
Definition: VCNL4010.cpp:180
const uint8_t REGISTER_LED_CURRENT
Register containing IR LED mA.
Definition: VCNL4010.h:99
uint8_t getInterrupt() const
Definition: VCNL4010.cpp:301
const uint8_t REGISTER_AMBIENT_LIGHT
Register containing ambient data.
Definition: VCNL4010.h:101
const uint8_t VCNL4010_I2C_ADDRESS
Device address, fixed value.
Definition: VCNL4010.h:93
const uint32_t I2C_STANDARD_MODE
Default normal I2C 100KHz speed.
Definition: VCNL4010.h:88
void setProximityFreq(const uint8_t value=0)
Definition: VCNL4010.cpp:189
const uint32_t I2C_HIGH_SPEED_MODE
Turbo mode.
Definition: VCNL4010.h:91