Digital_Light_ISL29035.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Digital_Light_ISL29035.cpp
  3. * A library for ISL29035
  4. *
  5. * Copyright (c) 2017 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : Jack
  8. * Create Time:
  9. * Change Log :
  10. *
  11. * The MIT License (MIT)
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #include <Digital_Light_ISL29035.h>
  32. #include <Wire.h>
  33. DigitalLightISL29035::DigitalLightISL29035():
  34. _full_scale_lux_range(DEFAULT_LUX_RANGE_INDEX),
  35. _integration_time(DEFAULT_INTEGRATION_TIME_INDEX)
  36. {
  37. _adc_count_max[0] = 65536;
  38. _adc_count_max[1] = 4096;
  39. _adc_count_max[2] = 256;
  40. _adc_count_max[3] = 16;
  41. _intg_time[0] = INTEGRATION_TIME0;
  42. _intg_time[1] = INTEGRATION_TIME1;
  43. _intg_time[2] = INTEGRATION_TIME2;
  44. _intg_time[4] = INTEGRATION_TIME3;
  45. _ranges[0] = FULL_SCALE_LUX_RANGE0;
  46. _ranges[1] = FULL_SCALE_LUX_RANGE1;
  47. _ranges[2] = FULL_SCALE_LUX_RANGE2;
  48. _ranges[3] = FULL_SCALE_LUX_RANGE3;
  49. }
  50. uint8_t DigitalLightISL29035::readRegister(int device_address, int reg_address)
  51. {
  52. uint8_t value;
  53. Wire.beginTransmission(device_address);
  54. Wire.write(reg_address);
  55. Wire.endTransmission(false);
  56. Wire.requestFrom(device_address, 1);
  57. //while(!Wire.available());
  58. value = Wire.read();
  59. return value;
  60. }
  61. void DigitalLightISL29035::writeRegister(int device_address, int reg_address, uint8_t val)
  62. {
  63. Wire.beginTransmission(device_address);
  64. Wire.write(reg_address);
  65. Wire.write(val);
  66. Wire.endTransmission();
  67. }
  68. int DigitalLightISL29035::init()
  69. {
  70. uint8_t reg = readRegister(ISL29035_I2C_ADDRESS, CHIP_ID);
  71. //Serial.println(reg, HEX);
  72. uint8_t chip_id = (reg >> 3) & 0x7;
  73. if (chip_id != 0x5) return -1;
  74. //clear the BOUT bit
  75. writeRegister(ISL29035_I2C_ADDRESS, CHIP_ID, reg & 0x7f);
  76. //ensure the chip is under stop mode
  77. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_I, 0);
  78. //set the default full scale lux range, and the integration time
  79. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_II, _full_scale_lux_range|(_integration_time<<2));
  80. return 0;
  81. }
  82. int DigitalLightISL29035::setFullScaleLuxRangeIndex(int range_index)
  83. {
  84. if(range_index < 0 || range_index > 3)
  85. return -1;
  86. _full_scale_lux_range = range_index;
  87. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_II, _full_scale_lux_range|(_integration_time<<2));
  88. return 0;
  89. }
  90. int DigitalLightISL29035::setIntegrationTimeIndex(int intg_time_index)
  91. {
  92. if(intg_time_index < 0 || intg_time_index > 3)
  93. return -1;
  94. _integration_time = intg_time_index;
  95. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_II, _full_scale_lux_range|(_integration_time<<2));
  96. return 0;
  97. }
  98. void DigitalLightISL29035::test()
  99. {
  100. #if 0
  101. uint8_t value;
  102. Wire.beginTransmission(ISL29035_I2C_ADDRESS);
  103. Wire.write(0);
  104. Wire.endTransmission();
  105. Wire.requestFrom(ISL29035_I2C_ADDRESS, 8);
  106. while(!Wire.available());
  107. for(int i=0;i<8;i++)
  108. {
  109. Serial.print("reg ");
  110. Serial.print(i);
  111. Serial.print(": 0x");
  112. Serial.println(Wire.read(), HEX);
  113. }
  114. value = readRegister(ISL29035_I2C_ADDRESS, CHIP_ID);
  115. Serial.print("reg 0xf: 0x");
  116. Serial.println(value, HEX);
  117. #endif
  118. }
  119. uint16_t DigitalLightISL29035::readData()
  120. {
  121. uint8_t l,h;
  122. Wire.beginTransmission(ISL29035_I2C_ADDRESS);
  123. Wire.write(DATA_L);
  124. Wire.endTransmission(false);
  125. Wire.requestFrom(ISL29035_I2C_ADDRESS, 2);
  126. while(Wire.available()<2);
  127. l = Wire.read();
  128. h = Wire.read();
  129. return (h << 8) | l;
  130. }
  131. uint16_t DigitalLightISL29035::measure(uint8_t what)
  132. {
  133. //start
  134. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_I, what);
  135. float time = _intg_time[_integration_time];
  136. if(time < 1.0f)
  137. {
  138. delayMicroseconds((int)(time * 1000));
  139. }else
  140. {
  141. delay((int)(time+1.5f));
  142. }
  143. uint16_t data = readData();
  144. //stop
  145. writeRegister(ISL29035_I2C_ADDRESS, COMMAND_I, 0);
  146. return data;
  147. }
  148. uint32_t DigitalLightISL29035::readVisibleLux()
  149. {
  150. uint16_t data = measure(OPMODE_ALS_ONCE);
  151. return ((uint32_t)_ranges[_full_scale_lux_range]) * (uint32_t)data / _adc_count_max[_integration_time];
  152. }
  153. uint32_t DigitalLightISL29035::readIRLux()
  154. {
  155. uint16_t data = measure(OPMODE_IR_ONCE);
  156. return ((uint32_t)_ranges[_full_scale_lux_range]) * (uint32_t)data / _adc_count_max[_integration_time];
  157. }
  158. int32_t DigitalLightISL29035::readEV()
  159. {
  160. uint16_t data1 = measure(OPMODE_ALS_ONCE);
  161. uint16_t data2 = measure(OPMODE_IR_ONCE);
  162. float k = 0.82;
  163. float beta = -11292.86f;
  164. if(_integration_time > 1) beta = 2137.14f;
  165. return (int32_t)(k*data1 + data2/beta);
  166. }
  167. DigitalLightISL29035 ISL29035;