Digital_Light_ISL29035.cpp 5.6 KB

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