VCNL4100 High Sensitivity Long Distance Proximity and Ambient Light Sensor with I2C Interface

Dependents:   test_VCNL4100 testSensor

Files at this revision

API Documentation at this revision

Comitter:
Rhyme
Date:
Mon May 08 07:16:46 2017 +0000
Commit message:
First compilable version / no test yet

Changed in this revision

VCNL4100.cpp Show annotated file Show diff for this revision Revisions of this file
VCNL4100.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fbf1a72181fc VCNL4100.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VCNL4100.cpp	Mon May 08 07:16:46 2017 +0000
@@ -0,0 +1,300 @@
+#include "mbed.h"
+#include "VCNL4100.h"
+
+/* I2C protocol
+ *
+ * I2C bus timing for Sendign Word Command Format 
+ * <s> <8bit: Slave Address>W <sa> <8bit: command code> <sa>
+ * <8bit: Data Byte Low> <sa> <8bit: Data Byte High> <sa> <p>
+ *
+ * I2C bus timing for Receiving Word Command Format
+ * <s> <8bit: Slave Address>W <sa> <8bit: Command Code> <sa>
+ * <s> <8bit: Slave Address>R <sa> <8bit: Data Byte Low> <ma>
+ * <8bit: Data Byte High> <ma> <p>
+ * 
+ * <s> Start by Master
+ * <p> Stop by Master
+ * <ma> ACK by Master
+ * <sa> ACK by Slave (VCNL4100)
+ */
+ 
+/* Internal Registers */
+#define REG_ALS_CONF   0x00
+#define REG_ALS_THDH_L 0x01
+#define REG_ALS_THDL_L 0x02
+#define REG_PS_CONF1   0x03
+#define REG_PS_CONF3   0x04
+#define REG_PS_THDL    0x06
+#define REG_PS_DATA    0x08
+#define REG_ASL_DATA   0x09
+#define REG_INT_FLAG   0x0B
+
+VCNL4100::VCNL4100(PinName sda, PinName scl, int addr) :
+    m_i2c(sda, scl), m_addr(addr<<1) 
+{
+    // activate the peripheral
+}
+
+
+VCNL4100::~VCNL4100()
+{
+}
+
+void VCNL4100::readRegs(int addr, uint8_t * data, int len) {
+    char t[1] = {addr};
+    m_i2c.write(m_addr, t, 1, true);
+    m_i2c.read(m_addr, (char *)data, len);
+}
+
+void VCNL4100::writeRegs(uint8_t * data, int len) {
+    m_i2c.write(m_addr, (char *)data, len);
+}
+
+uint8_t  VCNL4100::getAlsConf(void) 
+{
+    uint8_t data[2] ;
+    readRegs(REG_ALS_CONF, data, 2) ;
+    return(data[0]) ;
+}
+
+void     VCNL4100::setAlsConf(uint8_t conf) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_ALS_CONF ;
+    data[1] = conf ;
+    data[2] = 0x00 ;
+    writeRegs(data, 3) ;
+}
+
+uint16_t VCNL4100::getAlsThdh(void) 
+{
+    uint16_t thdh ;
+    uint8_t data[2] ;
+    readRegs(REG_ALS_THDH_L, data, 2) ;
+    thdh = (data[1] << 8) | data[0] ;
+    return( thdh ) ;
+}
+
+void     VCNL4100::setAlsThdh(uint16_t thdh) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_ALS_THDH_L ;
+    data[1] = thdh & 0xFF ;
+    data[2] = (thdh >> 8) & 0xFF ;
+    writeRegs(data, 3) ;
+}
+
+uint16_t VCNL4100::getAlsThdl(void) 
+{
+    uint16_t thdl ;
+    uint8_t data[2] ;
+    readRegs(REG_ALS_THDL_L, data, 2) ;
+    thdl = (data[1] << 8) | data[0] ;
+    return( thdl ) ;
+}
+
+void     VCNL4100::setAlsThdl(uint16_t thdl) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_ALS_THDL_L ;
+    data[1] = thdl & 0xFF ;
+    data[2] = (thdl >> 8) & 0xFF ;
+    writeRegs(data, 3) ;    
+}
+
+uint16_t VCNL4100::getPsConf12(void) 
+{
+    uint16_t psconf12 ;
+    uint8_t data[2] ;
+    readRegs(REG_PS_CONF1, data, 2) ;
+    psconf12 = (data[1] << 8) | data[0] ;
+    return( psconf12 ) ;
+}
+
+void     VCNL4100::setPsConf12(uint16_t conf12) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_PS_CONF1 ;
+    data[1] = conf12 & 0xFF ;
+    data[2] = (conf12 >> 8) & 0xFF ;
+    writeRegs(data, 3) ;
+}
+
+uint8_t  VCNL4100::getPsConf1(void) 
+{
+    uint16_t tmp ;
+    uint8_t psconf1 ;
+    tmp = getPsConf12() ;
+    psconf1 = tmp & 0xFF ;
+    return( psconf1 ) ;
+}
+
+void     VCNL4100::setPsConf1(uint8_t conf1) 
+{
+    uint16_t tmp ;
+    uint8_t data[3] ;
+    data[0] = REG_PS_CONF1 ;
+    tmp = getPsConf12() ;
+    data[1] = conf1 ;
+    data[2] = (tmp >> 8) & 0xFF ; // conf2
+    writeRegs(data, 3) ;
+}
+
+uint8_t  VCNL4100::getPsConf2(void) 
+{
+    uint16_t tmp ;
+    uint8_t psconf2 ;
+    tmp = getPsConf12() ;
+    psconf2 = (tmp >> 8) & 0xFF ;
+    return( psconf2 ) ;
+}
+
+void     VCNL4100::setPsConf2(uint8_t conf2) 
+{
+    uint16_t tmp ;
+    uint8_t data[3] ;
+    data[0] = REG_PS_CONF1 ;
+    tmp = getPsConf12() ;
+    data[1] = tmp & 0xFF ;
+    data[2] = conf2 ;
+    writeRegs(data, 3) ;
+}
+
+uint16_t VCNL4100::getPsConf3Spo(void) 
+{
+    uint16_t conf3spo ;
+    uint8_t data[2] ; /* data[0] = PS_CONF3 ; data[1] = SPO ; */
+    readRegs(REG_PS_CONF3, data, 2) ;
+    conf3spo = (data[1] << 8) | data[0] ;
+    return( conf3spo ) ;
+}
+
+void     VCNL4100::setPsConf3Spo(uint16_t conf3spo) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_PS_CONF3 ;
+    data[1] = conf3spo & 0xFF ; /* PS_CONF3 */
+    data[2] = (conf3spo >> 8) & 0xFF ; /* PS_SPO */
+    writeRegs(data, 3) ;
+}
+
+uint8_t  VCNL4100::getPsConf3(void) 
+{
+    uint16_t conf3spo ;
+    uint8_t conf3 ;
+    conf3spo = getPsConf3Spo() ;
+    conf3 = conf3spo & 0xFF ;
+    return( conf3 ) ;
+}
+
+void     VCNL4100::setPsConf3(uint8_t conf3)
+{
+    uint16_t conf3spo ;
+    uint8_t data[3] ;
+    conf3spo = getPsConf3Spo() ;
+    data[0] = REG_PS_CONF3 ;
+    data[1] = conf3 ;
+    data[2] = (conf3spo >> 8) & 0xFF ; /* spo */
+    writeRegs(data, 3) ;
+}
+
+uint8_t  VCNL4100::getSpo(void) 
+{
+    uint16_t conf3spo ;
+    uint8_t spo ;
+    conf3spo = getPsConf3Spo() ;
+    spo = (conf3spo >> 8) & 0xFF ;
+    return( spo ) ;
+}
+
+void     VCNL4100::setSpo(uint8_t spo) 
+{
+    uint16_t conf3spo ;
+    uint8_t data[3] ;
+    conf3spo = getPsConf3Spo() ;
+    data[0] = REG_PS_CONF3 ;
+    data[1] = conf3spo & 0xFF ; /* PS_CONF3 */
+    data[2] = spo ; /* PS_SPO */
+    writeRegs(data, 3) ;
+}
+
+uint16_t VCNL4100::getPsThd(void) 
+{
+    uint16_t psthd ;
+    uint8_t data[2] ;
+    readRegs(REG_PS_THDL, data, 2) ;
+    psthd = (data[1] << 8) | data[0] ;
+    return( psthd ) ;
+}
+
+void     VCNL4100::setPsThd(uint16_t psthd) 
+{
+    uint8_t data[3] ;
+    data[0] = REG_PS_THDL ;
+    data[1] = psthd & 0xFF ;
+    data[2] = (psthd >> 8) & 0xFF ;
+    writeRegs(data, 3) ;
+}
+
+uint8_t VCNL4100::getPsThdl(void)
+{
+    uint16_t thd16 ;
+    uint8_t thdl ;
+    thd16 = getPsThd() ;
+    thdl = thd16 & 0xFF ;
+    return( thdl ) ;
+}
+
+void VCNL4100::setPsThdl(uint8_t thdl)
+{
+    uint16_t thd16 ;
+    uint8_t data[3] ;
+    thd16 = getPsThd() ;
+    data[0] = REG_PS_THDL ;
+    data[1] = thdl ;
+    data[2] = (thd16 >> 8) & 0xFF ;
+    writeRegs(data, 3) ;
+}
+
+uint8_t VCNL4100::getPsThdh(void)
+{
+    uint16_t thd16 ;
+    uint8_t thdh ;
+    thd16 = getPsThd() ;
+    thdh = (thd16 >> 8) & 0xFF ;
+    return( thdh ) ;
+}
+
+void VCNL4100::setPsThdh(uint8_t thdh)
+{
+    uint16_t thd16 ;
+    uint8_t data[3] ;
+    thd16 = getPsThd() ;
+    data[0] = REG_PS_THDL ;
+    data[1] = thd16 & 0xFF ;
+    data[2] = thdh ;
+    writeRegs(data, 3) ;
+}
+
+uint8_t  VCNL4100::getPsData(void) 
+{
+    uint8_t data[2] ;
+    readRegs(REG_PS_DATA, data, 2) ;
+    return(data[0]) ;
+}
+
+uint16_t VCNL4100::getAlsData(void) 
+{
+    uint16_t alsdata ;
+    uint8_t data[2] ;
+    readRegs(REG_ASL_DATA, data, 2) ;
+    alsdata = (data[1] << 8) | data[0] ;
+    return( alsdata ) ;
+}
+
+uint8_t  VCNL4100::getIntFlag(void) 
+{
+    uint8_t data[2] ;
+    readRegs(REG_INT_FLAG, data, 2) ;
+    return( data[1] ) ;
+}
diff -r 000000000000 -r fbf1a72181fc VCNL4100.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VCNL4100.h	Mon May 08 07:16:46 2017 +0000
@@ -0,0 +1,326 @@
+#ifndef _VCNL4100_H_
+#define _VCNL4100_H_
+/**
+ * Vishay VCNL4100
+ * High Sensitivity Long Distance Proximity and
+ * Ambient Light Sensor with I2C Interface
+ */
+ /*
+ * @code
+* @endcode
+*/
+class VCNL4100
+{
+public:
+  /**
+  * VCNL4100 constructor
+  *
+  * @param sda SDA pin
+  * @param sdl SCL pin
+  * @param addr addr of the I2C peripheral
+  */
+  VCNL4100(PinName sda, PinName scl, int addr);
+
+  /**
+  * VCNL4100 destructor
+  */
+  ~VCNL4100();
+  
+/**
+ * get ALS configuration 
+ * @param none
+ * @return uint8_t ALS CONF
+ *
+ * @note bit[7:6] ALS_IT ASL integration time setting,
+ * @note longer integration time has higher sensitivity 
+ * @note 00=80ms, 01=160ms, 10=320ms, 11=640ms
+ * @note -
+ * @note bit[5:4] (reserved)
+ * @note -
+ * @note bit[3:2] ALS interrupt persistence setting
+ * @note 00=1, 01=2, 10=4, 11=8
+ * @note -
+ * @note bit[1] ALS_INT_EN 0=ALS interrupt disable, 1=ALS interrupt enable
+ * @note -
+ * @note bit[0] ALS_SD 0=ALS power on, 1=ALS Shut Down
+ */
+uint8_t  getAlsConf(void) ;
+
+/**
+ * set ALS configuration
+ * @param uint8_t conf, value to assign
+ * @returns none
+ */
+void     setAlsConf(uint8_t conf) ;
+
+/**
+ * Get ALS high interrupt threshold 
+ * @param none
+ * @returns uint16_t ALS high interrupt threshold
+ */
+uint16_t getAlsThdh(void) ;
+
+/**
+ * Set ALS high interrupt threshold
+ * @param uint16_t thdh, value to assign
+ * @returns none
+ */
+void     setAlsThdh(uint16_t thdh) ;
+
+/**
+ * Get ALS low interrupt threshold
+ * @param none
+ * @returns uint16_t ALS low interrupt threshold
+ */
+uint16_t getAlsThdl(void) ;
+
+/**
+ * Set ALS low interrupt threshold
+ * @param uint16_t thdl, value to assign
+ * @returns none
+ */
+void     setAlsThdl(uint16_t thdl) ;
+
+/**
+ * Get PS CONF1 and PS CONF2
+ * @param none
+ * @returns uint16_t PS_CONF2 as MSB, PS_CONF1 as LSB
+ */
+uint16_t getPsConf12(void) ;
+
+/**
+ * Set PS CONF1 and PS CONF2
+ * @param uint16_t PS_CONF2 as MSB, PS_CONF1 as LSB
+ * @returns none
+ */
+void     setPsConf12(uint16_t conf12) ;
+
+/**
+ * get PS CONF1
+ * @param none
+ * @returns Register value of PS_CONF1
+ *
+ * @note bit[7:6] PS_Duty PS IRED on/off duty ratio setting
+ * @note 00=1/5120, 01=1/640, 10=1/80, 11=1/20
+ * @note -
+ * @note bit[5:4] PS_IT PS integration time setting
+ * @note 00=1T, 01=1.3T, 10=1.6T, 11=2T
+ * @note -
+ * @note bit[3:2] PS_PERS PS interrupt persistence setting
+ * @note 00=1, 01=2, 10=3, 11=4
+ * @note -
+ * @note bit[1] (reserved)
+ * @note -
+ * @note bit[0] PS_SD 0=PS power on, 1=PS shut down
+ */
+uint8_t  getPsConf1(void) ;
+
+/**
+ * set PS_CONF1
+ *
+ * @param uint8_t conf1 value to set
+ * @returns none
+ */
+void     setPsConf1(uint8_t conf1) ;
+
+/**
+ * get PS_CONF2
+ *
+ * @note bit[7:6] PS_ITB PS IT bank setting
+ * @note 00=1/2T, 01=1T, 10=2T, 11=4T
+ * @note -
+ * @note bit[5:4] PS_GAIN
+ * @note 00=1/4, 01=1/2, 10=1, 11=2
+ * @note -
+ * @note bit[3] (reserved)
+ * @note -
+ * @note bit[2] PS_SP_INT_EN 
+ * @note 0 = disable INT function for PS enter/leave sunlight protection mode
+ * @note 1 = issue INT while PS enter/leave sunlight protection mode.
+ * @note  While PS enter sunlight protection mode, the PS output will keep 0xFF
+ * @note -
+ * @note bit[1] (reserved)
+ * @note -
+ * @note bit[0] PS_INT_EN
+ * @note 0 = PS INT function disable, 1 = PS INT function enable
+ */
+uint8_t  getPsConf2(void) ;
+
+/**
+ * set PS_CONF2
+ *
+ * @param uint8_t conf2 value to set
+ * @returns none
+ */
+void     setPsConf2(uint8_t conf2) ;
+
+/**
+ * get PS_CONF3 and PS_SPO
+ *
+ * @param none
+ * @returns uint16_t PS_CONF3 as LSB, PS_SPO as MSB
+ */
+uint16_t getPsConf3Spo(void) ;
+
+/**
+ * set PS_CONF3 and PS_SPO
+ * 
+ * @param uint16_t con3spo PS_CONF3 as LSB, PS_SPO as MSB
+ * @returns none
+ */
+void     setPsConf3Spo(uint16_t conf3spo) ;
+
+/**
+ * get PS_CONF3
+ *
+ * @param none
+ * @returns uint8_t PS_CONF3
+ *
+ * @note bit[7:6] PS_AV 
+ * @note 00=1/2, 01=1/4, 10=1/8, 11=1/16
+ * @note -
+ * @note bit[5] PS_AV_EN 
+ * @note 0= PS average function disable, 1= PS average function enable
+ * @note -
+ * @note bit[4] (reserved)
+ * @note -
+ * @note bit[3] PS_AF
+ * @note 0= active force mode disable (normal mode), 1= active force mode enable
+ * @note -
+ * @note bit[2] PS_TRIG
+ * @note 0= no PS active force mode trigger, 1= trigger one time cycle
+ * @note VCNL4100 output once cycle data every time host writes in "1" to sensor.
+ * @note The state returns to "0" automatically.
+ * @note -
+ * @note bit[1] PS_MPULSE 0= disable, 1= enable
+ * @note PS multi pulse mode setting; PS multi puse number set by PS_AV[1:0]
+ * @note -
+ * @note bit[0] (reserved)
+ */
+uint8_t  getPsConf3(void) ;
+
+/**
+ * set PS_CONF3
+ *
+ * @param uint8_t conf3 value to set
+ * @returns none
+ */
+void     setPsConf3(uint8_t conf3) ;
+
+/**
+ * get PS_SPO
+ *
+ * @param none
+ * @returns uint8_t PS_SPO
+ *
+ * @note Set initial value to "0xA0" or "0x20"
+ * @note bit[7:0] PS_SPO
+ * @note Set initial value = 0xA0 (PS_OUT = 0xFF while PS into sunlight protection)
+ * @note Set initial value = 0x20 (PS_OUT = 0x00 while PS into sunlight protection)
+ *
+ */
+uint8_t  getSpo(void) ;
+
+/**
+ * set PS_SPO
+ * 
+ * @param uint8_t spo value to set (0xA0 or 0x20)
+ * @returns none
+ */
+void     setSpo(uint8_t spo) ;
+
+/**
+ * get PS_THDL as LSB and PS_THDH as MSB
+ *
+ * @param none
+ * @return uint16_t PS_THDL as LSB, PS_THDH as MSB
+ */
+uint16_t getPsThd(void) ;
+
+/**
+ * set PS_THDL as LSB and PS_THDH as MSB
+ *
+ * @param uint16_t PS_THDL as LSB, PS_THDH as MSB
+ * @returns none
+ */
+void     setPsThd(uint16_t psthd) ;
+
+/**
+ * get PS_THDL
+ * 
+ * @param none
+ * @returns uint8_t PS_THDL
+ *
+ * @note bit[7:0] PS_THDL 0x00 to 0xFF, PS low interrupt threshold setting
+ */
+uint8_t getPsThdl(void) ;
+
+/**
+ * set PS_THDL
+ *
+ * @param uint8_t thdl value to set
+ * @returns none
+ */
+void setPsThdl(uint8_t thdl) ;
+
+/**
+ * get PS_THDH
+ *
+ * @param none
+ * @returns uint8_t PS_THDH
+ *
+ * @note bit[7:0] PS_THDH 0x00 to 0xFF, PS high interrupt threshold setting
+ */
+ uint8_t getPsThdh(void) ;
+ 
+ /**
+  * set PS_THDH
+  *
+  * @param uint8_t thdh value to set
+  * @returns none
+  */
+void setPsThdh(uint8_t thdh) ;
+
+/**
+ * get PS_DATA
+ *
+ * @param none
+ * @returns uint8_t PS_DATA
+ *
+ * @note bit[7:0] 0x00 to 0xFF, PS output data
+ */
+uint8_t  getPsData(void) ;
+
+/**
+ * get ALS output data
+ *
+ * @param none
+ * @returns uint16_t ALS output data
+ */
+uint16_t getAlsData(void) ;
+
+/**
+ * get INT_FLAG
+ *
+ * @param none
+ * @returns uint8_t INT_FLAG
+ *
+ * @note bit[7] PS_SPF_LEAVE, PS leaving protection mode
+ * @note bit[6] PS_SPF_ENTER, PS entering protection mode
+ * @note bit[5] ALS_IF_L, ALS crossing low THD INT trigger event
+ * @note bit[4] ALS_IF_H, ALS crossing high THD INT trigger event
+ * @note bit[3:2] (reserved)
+ * @note bit[1] PS_IF_CLOSE, PS rise above PS_THDH INT trigger event
+ * @note bit[0] PS_IF_AWAY, PS drop below PS_THDL INT trigger event
+ */
+uint8_t  getIntFlag(void) ;
+    
+private:
+  I2C m_i2c;
+  int m_addr;
+  void readRegs(int addr, uint8_t * data, int len);
+  void writeRegs(uint8_t * data, int len);
+
+};
+
+#endif /* _VCNL4100_H_ */
\ No newline at end of file