Guest User

VCNL40x0.h

a guest
Mar 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. #if ARDUINO >= 100
  2.  #include "Arduino.h"
  3. #else
  4.  #include "WProgram.h"
  5. #endif
  6.  
  7. #ifdef __AVR_ATtiny85__
  8.  #include "TinyWireM.h"
  9.  #define Wire TinyWireM
  10. #else
  11.  #include <Wire.h>
  12. #endif
  13.  
  14.  
  15. // Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020/3020
  16. // The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
  17. #define VCNL40x0_ADDRESS (0x26) // 001 0011 shifted left 1 bit = 0x26
  18. // registers
  19. #define REGISTER_COMMAND (0x80)
  20. #define REGISTER_ID (0x81)
  21. #define REGISTER_PROX_RATE (0x82)
  22. #define REGISTER_PROX_CURRENT (0x83)
  23. #define REGISTER_AMBI_PARAMETER (0x84)
  24. #define REGISTER_AMBI_VALUE (0x85)
  25. #define REGISTER_PROX_VALUE (0x87)
  26. #define REGISTER_INTERRUPT_CONTROL (0x89)
  27. #define REGISTER_INTERRUPT_LOW_THRES (0x8a)
  28. #define REGISTER_INTERRUPT_HIGH_THRES (0x8c)
  29. #define REGISTER_INTERRUPT_STATUS (0x8e)
  30. #define REGISTER_PROX_TIMING (0xf9)
  31. #define REGISTER_AMBI_IR_LIGHT_LEVEL (0x90) // This register is not intended to be use by customer
  32. // Bits in Command register (0x80)
  33. #define COMMAND_ALL_DISABLE (0x00)
  34. #define COMMAND_SELFTIMED_MODE_ENABLE (0x01)
  35. #define COMMAND_PROX_ENABLE (0x02)
  36. #define COMMAND_AMBI_ENABLE (0x04)
  37. #define COMMAND_PROX_ON_DEMAND (0x08)
  38. #define COMMAND_AMBI_ON_DEMAND (0x10)
  39. #define COMMAND_MASK_PROX_DATA_READY (0x20)
  40. #define COMMAND_MASK_AMBI_DATA_READY (0x40)
  41. #define COMMAND_MASK_LOCK (0x80)
  42.  
  43. // Bits in Product ID Revision Register (0x81)
  44. #define PRODUCT_MASK_REVISION_ID (0x0f)
  45. #define PRODUCT_MASK_PRODUCT_ID (0xf0)
  46. // Bits in Prox Measurement Rate register (0x82)
  47. #define PROX_MEASUREMENT_RATE_2 (0x00) // DEFAULT
  48. #define PROX_MEASUREMENT_RATE_4 (0x01)
  49. #define PROX_MEASUREMENT_RATE_8 (0x02)
  50. #define PROX_MEASUREMENT_RATE_16 (0x03)
  51. #define PROX_MEASUREMENT_RATE_31 (0x04)
  52. #define PROX_MEASUREMENT_RATE_62 (0x05)
  53. #define PROX_MEASUREMENT_RATE_125 (0x06)
  54. #define PROX_MEASUREMENT_RATE_250 (0x07)
  55. #define PROX_MASK_MEASUREMENT_RATE (0x07)
  56. // Bits in Proximity LED current setting (0x83)
  57. #define PROX_MASK_LED_CURRENT (0x3f) // DEFAULT = 2
  58. #define PROX_MASK_FUSE_PROG_ID (0xc0)
  59. // Bits in Ambient Light Parameter register (0x84)
  60. #define AMBI_PARA_AVERAGE_1 (0x00)
  61. #define AMBI_PARA_AVERAGE_2 (0x01)
  62. #define AMBI_PARA_AVERAGE_4 (0x02)
  63. #define AMBI_PARA_AVERAGE_8 (0x03)
  64. #define AMBI_PARA_AVERAGE_16 (0x04)
  65. #define AMBI_PARA_AVERAGE_32 (0x05) // DEFAULT
  66. #define AMBI_PARA_AVERAGE_64 (0x06)
  67. #define AMBI_PARA_AVERAGE_128 (0x07)
  68. #define AMBI_MASK_PARA_AVERAGE (0x07)
  69. #define AMBI_PARA_AUTO_OFFSET_ENABLE (0x08) // DEFAULT enable
  70. #define AMBI_MASK_PARA_AUTO_OFFSET (0x08)
  71. #define AMBI_PARA_MEAS_RATE_1 (0x00)
  72. #define AMBI_PARA_MEAS_RATE_2 (0x10) // DEFAULT
  73. #define AMBI_PARA_MEAS_RATE_3 (0x20)
  74. #define AMBI_PARA_MEAS_RATE_4 (0x30)
  75. #define AMBI_PARA_MEAS_RATE_5 (0x40)
  76. #define AMBI_PARA_MEAS_RATE_6 (0x50)
  77. #define AMBI_PARA_MEAS_RATE_8 (0x60)
  78. #define AMBI_PARA_MEAS_RATE_10 (0x70)
  79. #define AMBI_MASK_PARA_MEAS_RATE (0x70)
  80. #define AMBI_PARA_CONT_CONV_ENABLE (0x80)
  81. #define AMBI_MASK_PARA_CONT_CONV (0x80) // DEFAULT disable
  82. // Bits in Interrupt Control Register (x89)
  83. #define INTERRUPT_THRES_SEL_PROX (0x00)
  84. #define INTERRUPT_THRES_SEL_ALS (0x01)
  85. #define INTERRUPT_THRES_ENABLE (0x02)
  86. #define INTERRUPT_ALS_READY_ENABLE (0x04)
  87. #define INTERRUPT_PROX_READY_ENABLE (0x08)
  88. #define INTERRUPT_COUNT_EXCEED_1 (0x00) // DEFAULT
  89. #define INTERRUPT_COUNT_EXCEED_2 (0x20)
  90. #define INTERRUPT_COUNT_EXCEED_4 (0x40)
  91. #define INTERRUPT_COUNT_EXCEED_8 (0x60)
  92. #define INTERRUPT_COUNT_EXCEED_16 (0x80)
  93. #define INTERRUPT_COUNT_EXCEED_32 (0xa0)
  94. #define INTERRUPT_COUNT_EXCEED_64 (0xc0)
  95. #define INTERRUPT_COUNT_EXCEED_128 (0xe0)
  96. #define INTERRUPT_MASK_COUNT_EXCEED (0xe0)
  97. // Bits in Interrupt Status Register (x8e)
  98. #define INTERRUPT_STATUS_THRES_HI (0x01)
  99. #define INTERRUPT_STATUS_THRES_LO (0x02)
  100. #define INTERRUPT_STATUS_ALS_READY (0x04)
  101. #define INTERRUPT_STATUS_PROX_READY (0x08)
  102. #define INTERRUPT_MASK_STATUS_THRES_HI (0x01)
  103. #define INTERRUPT_MASK_THRES_LO (0x02)
  104. #define INTERRUPT_MASK_ALS_READY (0x04)
  105. #define INTERRUPT_MASK_PROX_READY (0x08)
  106.  
  107. typedef enum {
  108.  VCNL40x0_ERROR_OK = 0, // Everything executed normally
  109.  VCNL40x0_ERROR_I2CINIT, // Unable to initialize I2C
  110.  VCNL40x0_ERROR_I2CBUSY, // I2C already in use
  111.  VCNL40x0_ERROR_LAST
  112. } VCNL40x0Error_e;
  113.  
  114. class VCNL40x0 {
  115. public:
  116. // public functions
  117.  
  118.  VCNL40x0();
  119.  
  120.  boolean begin(uint8_t a = VCNL40x0_ADDRESS);  
  121.  
  122.  
  123.  VCNL40x0Error_e Init (void);
  124.  
  125.  VCNL40x0Error_e SetCommandRegister (unsigned char Command);
  126.  VCNL40x0Error_e SetCurrent (unsigned char CurrentValue);
  127.  VCNL40x0Error_e SetProximityRate (unsigned char ProximityRate);
  128.  VCNL40x0Error_e SetAmbiConfiguration (unsigned char AmbiConfiguration);
  129.  VCNL40x0Error_e SetLowThreshold (unsigned int LowThreshold);
  130.  VCNL40x0Error_e SetHighThreshold (unsigned int HighThreshold);
  131.  VCNL40x0Error_e SetInterruptControl (unsigned char InterruptControl);
  132.  VCNL40x0Error_e SetInterruptStatus (unsigned char InterruptStatus);
  133.  VCNL40x0Error_e SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
  134.  VCNL40x0Error_e ReadID (unsigned char *ID);
  135.  VCNL40x0Error_e ReadCurrent (unsigned char *CurrentValue);
  136.  VCNL40x0Error_e ReadCommandRegister (unsigned char *Command);
  137.  VCNL40x0Error_e ReadProxiValue (unsigned int *ProxiValue);
  138.  VCNL40x0Error_e ReadAmbiValue (unsigned int *AmbiValue);
  139.  VCNL40x0Error_e ReadInterruptStatus (unsigned char *InterruptStatus);
  140.  VCNL40x0Error_e ReadInterruptControl (unsigned char *InterruptControl);
  141.  
  142.  VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
  143.  VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
  144. private:
  145.  //I2C _i2c;
  146.  void write8(uint8_t address, uint8_t data);
  147.  uint16_t read16(uint8_t address);
  148.  uint8_t read8(uint8_t address);
  149.  uint8_t _addr;
  150.  
  151.  
  152.  
  153.  char _send[3];
  154.  char _receive[2];
  155.  
  156. };
Add Comment
Please, Sign In to add comment