Advertisement
Guest User

VCNL40x0.cpp

a guest
Mar 9th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.84 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. #include "VCNL40x0.h"
  15.  
  16. VCNL40x0::VCNL40x0() {
  17. }
  18.  
  19.  
  20. /**************************************************************************/
  21. /*!
  22.     @brief  Setups the HW
  23. */
  24. /**************************************************************************/
  25. boolean Adafruit_VCNL4010::begin(uint8_t addr) {
  26.   _addr = addr;
  27.   Wire.begin();
  28.  
  29.   uint8_t rev = read8(VCNL4010_PRODUCTID);
  30.   //Serial.println(rev, HEX);
  31.   if ((rev & 0xF0) != 0x20) {
  32.     return false;
  33.   }
  34. }
  35.  
  36.  
  37. VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
  38.  _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
  39.  _send[1] = Command;
  40.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  41.  return VCNL40x0_ERROR_OK;
  42. }
  43.  
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
  46.  _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
  47.  _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
  48.  _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
  49.  *Command = (unsigned char)(_receive[0]);
  50.  return VCNL40x0_ERROR_OK;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
  54.  _send[0] = REGISTER_ID; // VCNL40x0 product ID revision register
  55.  _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
  56.  _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1); // Read 1 byte on I2C
  57.  *ID = (unsigned char)(_receive[0]);
  58.  return VCNL40x0_ERROR_OK;
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
  62.  _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
  63.  _send[1] = Current;
  64.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  65.  return VCNL40x0_ERROR_OK;
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
  69.  _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register
  70.  _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
  71.  _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
  72.  *Current = (unsigned char)(_receive[0]);
  73.  return VCNL40x0_ERROR_OK;
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
  77.  _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
  78.  _send[1] = ProximityRate;
  79.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  80.  return VCNL40x0_ERROR_OK;
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
  84.  _send[0] = REGISTER_AMBI_PARAMETER; // VCNL40x0 Ambient light configuration
  85.  _send[1] = AmbiConfiguration;
  86.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  87.  return VCNL40x0_ERROR_OK;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
  91.  _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
  92.  _send[1] = InterruptControl;
  93.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  94.  return VCNL40x0_ERROR_OK;
  95. }
  96.  
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
  99.  _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
  100.  _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
  101.  _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
  102.  *InterruptControl = (unsigned char)(_receive[0]);
  103.  return VCNL40x0_ERROR_OK;
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
  107.  _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
  108.  _send[1] = InterruptStatus;
  109.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  110.  return VCNL40x0_ERROR_OK;
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
  114.  _send[0] = REGISTER_PROX_TIMING; // VCNL40x0 Modulator Timing Adjustment register
  115.  _send[1] = ModulatorTimingAdjustment;
  116.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  117.  return VCNL40x0_ERROR_OK;
  118. }
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
  121.  _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
  122.  _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
  123.  _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
  124.  *InterruptStatus = (unsigned char)(_receive[0]);
  125.  return VCNL40x0_ERROR_OK;
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
  129.  _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
  130.  _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
  131.  _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
  132.  *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
  133.  return VCNL40x0_ERROR_OK;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
  137.  _send[0] = REGISTER_AMBI_VALUE; // VCNL40x0 Ambient Light Value register
  138.  _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
  139.  _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
  140.  *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
  141.  return VCNL40x0_ERROR_OK;
  142. }
  143.  
  144. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  145. VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
  146.  unsigned char LoByte=0, HiByte=0;
  147.  
  148.  LoByte = (unsigned char)(LowThreshold & 0x00ff);
  149.  HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
  150.  
  151.  _send[0] = REGISTER_INTERRUPT_LOW_THRES; // VCNL40x0 Low Threshold Register, Hi Byte
  152.  _send[1] = HiByte;
  153.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  154.  
  155.  _send[0] = REGISTER_INTERRUPT_LOW_THRES+1; // VCNL40x0 Low Threshold Register, Lo Byte
  156.  _send[1] = LoByte;
  157.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  158.  return VCNL40x0_ERROR_OK;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
  162.  unsigned char LoByte=0, HiByte=0;
  163.  
  164.  LoByte = (unsigned char)(HighThreshold & 0x00ff);
  165.  HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
  166.  
  167.  _send[0] = REGISTER_INTERRUPT_HIGH_THRES; // VCNL40x0 High Threshold Register, Hi Byte
  168.  _send[1] = HiByte;
  169.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  170.  
  171.  _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1; // VCNL40x0 High Threshold Register, Lo Byte
  172.  _send[1] = LoByte;
  173.  _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
  174.  return VCNL40x0_ERROR_OK;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  178. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  179. VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
  180.  unsigned char Command=0;
  181.  // enable prox value on demand
  182.  SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
  183.  // wait on prox data ready bit
  184.  do {
  185.  ReadCommandRegister (&Command); // read command register
  186.  } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
  187.  ReadProxiValue (ProxiValue); // read prox value
  188.  SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
  189.  
  190.  return VCNL40x0_ERROR_OK;
  191. }
  192. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  193. VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
  194.  unsigned char Command=0;
  195.  // enable ambi value on demand
  196.  SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
  197.  // wait on ambi data ready bit
  198.  do {
  199.  ReadCommandRegister (&Command); // read command register
  200.  } while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
  201.  ReadAmbiValue (AmbiValue); // read ambi value
  202.  SetCommandRegister (COMMAND_ALL_DISABLE); // stop ambi value on demand
  203.  return VCNL40x0_ERROR_OK;
  204. }
  205.  
  206.  
  207. /**************************************************************************/
  208. /*!
  209.     @brief  I2C low level interfacing
  210. */
  211. /**************************************************************************/
  212.  
  213.  
  214. // Read 1 byte from the VCNL40x0 at 'address'
  215. uint8_t VCNL40x0::read8(uint8_t address)
  216. {
  217.   uint8_t data;
  218.  
  219.   Wire.beginTransmission(_i2caddr);
  220. #if ARDUINO >= 100
  221.   Wire.write(address);
  222. #else
  223.   Wire.send(address);
  224. #endif
  225.   Wire.endTransmission();
  226.  
  227.   delayMicroseconds(170);  // delay required
  228.  
  229.   Wire.requestFrom(_i2caddr, (uint8_t)1);
  230.   while(!Wire.available());
  231.  
  232. #if ARDUINO >= 100
  233.   return Wire.read();
  234. #else
  235.   return Wire.receive();
  236. #endif
  237. }
  238.  
  239.  
  240. // Read 2 byte from the VCNL40x0 at 'address'
  241. uint16_t VCNL40x0::read16(uint8_t address)
  242. {
  243.   uint16_t data;
  244.  
  245.   Wire.beginTransmission(_i2caddr);
  246. #if ARDUINO >= 100
  247.   Wire.write(address);
  248. #else
  249.   Wire.send(address);
  250. #endif
  251.   Wire.endTransmission();
  252.  
  253.   Wire.requestFrom(_i2caddr, (uint8_t)2);
  254.   while(!Wire.available());
  255. #if ARDUINO >= 100
  256.   data = Wire.read();
  257.   data <<= 8;
  258.   while(!Wire.available());
  259.   data |= Wire.read();
  260. #else
  261.   data = Wire.receive();
  262.   data <<= 8;
  263.   while(!Wire.available());
  264.   data |= Wire.receive();
  265. #endif
  266.  
  267.   return data;
  268. }
  269.  
  270. // write 1 byte
  271. void VCNL40x0::write8(uint8_t address, uint8_t data)
  272. {
  273.   Wire.beginTransmission(_i2caddr);
  274. #if ARDUINO >= 100
  275.   Wire.write(address);
  276.   Wire.write(data);  
  277. #else
  278.   Wire.send(address);
  279.   Wire.send(data);  
  280. #endif
  281.   Wire.endTransmission();
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement