Digital_Light_TSL2561.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Digital_Light_TSL2561.cpp
  3. A library for TSL2561
  4. Copyright (c) 2012 seeed technology inc.
  5. Website : www.seeed.cc
  6. Author : zhangkun
  7. Create Time:
  8. Change Log : Jack Shao, Nov 2014, bug fix and update for user experience
  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_TSL2561.h>
  27. #include <Arduino.h>
  28. #include <Wire.h>
  29. TSL2561_CalculateLux::TSL2561_CalculateLux(uint32_t read_timeout) : read_timeout(read_timeout) {
  30. }
  31. uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address) {
  32. uint8_t value;
  33. Wire.beginTransmission(deviceAddress);
  34. Wire.write(address); // register to read
  35. Wire.endTransmission();
  36. Wire.requestFrom(deviceAddress, 1); // read a byte
  37. uint32_t readStart = micros();
  38. while (!Wire.available()) {
  39. if (read_timeout != TSL2561_NO_READ_TIMEOUT && micros() - readStart >= read_timeout) {
  40. hadTimeout = true;
  41. break;
  42. }
  43. }
  44. value = Wire.read();
  45. //delay(100);
  46. return value;
  47. }
  48. void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val) {
  49. Wire.beginTransmission(deviceAddress); // start transmission to device
  50. Wire.write(address); // send register address
  51. Wire.write(val); // send value to write
  52. Wire.endTransmission(); // end transmission
  53. //delay(100);
  54. }
  55. void TSL2561_CalculateLux::getLux(void) {
  56. CH0_LOW = readRegister(TSL2561_Address, TSL2561_Channal0L);
  57. CH0_HIGH = readRegister(TSL2561_Address, TSL2561_Channal0H);
  58. //read two bytes from registers 0x0E and 0x0F
  59. CH1_LOW = readRegister(TSL2561_Address, TSL2561_Channal1L);
  60. CH1_HIGH = readRegister(TSL2561_Address, TSL2561_Channal1H);
  61. ch0 = (CH0_HIGH << 8) | CH0_LOW;
  62. ch1 = (CH1_HIGH << 8) | CH1_LOW;
  63. }
  64. void TSL2561_CalculateLux::init() {
  65. writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  66. writeRegister(TSL2561_Address, TSL2561_Timing, 0x00); //No High Gain (1x), integration time of 13ms
  67. writeRegister(TSL2561_Address, TSL2561_Interrupt, 0x00);
  68. writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  69. }
  70. uint16_t TSL2561_CalculateLux::readIRLuminosity() { // read Infrared channel value only, not convert to lux.
  71. writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  72. delay(14);
  73. getLux();
  74. writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  75. if (ch1 == 0) {
  76. return 0;
  77. }
  78. if (ch0 / ch1 < 2 && ch0 > 4900) {
  79. return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  80. }
  81. return ch1;
  82. }
  83. uint16_t TSL2561_CalculateLux::readFSpecLuminosity() { //read Full Spectrum channel value only, not convert to lux.
  84. writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  85. delay(14);
  86. getLux();
  87. writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  88. if (ch1 == 0) {
  89. return 0;
  90. }
  91. if (ch0 / ch1 < 2 && ch0 > 4900) {
  92. return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  93. }
  94. return ch0;
  95. }
  96. signed long TSL2561_CalculateLux::readVisibleLux() {
  97. writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  98. delay(14);
  99. getLux();
  100. writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  101. if (ch1 == 0) {
  102. return 0;
  103. }
  104. if (ch0 / ch1 < 2 && ch0 > 4900) {
  105. return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  106. }
  107. return calculateLux(0, 0, 0); //T package, no gain, 13ms
  108. }
  109. unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt, int iType) {
  110. switch (tInt) {
  111. case 0: // 13.7 msec
  112. chScale = CHSCALE_TINT0;
  113. break;
  114. case 1: // 101 msec
  115. chScale = CHSCALE_TINT1;
  116. break;
  117. default: // assume no scaling
  118. chScale = (1 << CH_SCALE);
  119. break;
  120. }
  121. if (!iGain) {
  122. chScale = chScale << 4; // scale 1X to 16X
  123. }
  124. // scale the channel values
  125. channel0 = (ch0 * chScale) >> CH_SCALE;
  126. channel1 = (ch1 * chScale) >> CH_SCALE;
  127. ratio1 = 0;
  128. if (channel0 != 0) {
  129. ratio1 = (channel1 << (RATIO_SCALE + 1)) / channel0;
  130. }
  131. // round the ratio value
  132. unsigned long ratio = (ratio1 + 1) >> 1;
  133. switch (iType) {
  134. case 0: // T package
  135. if ((ratio >= 0) && (ratio <= K1T)) {
  136. b = B1T;
  137. m = M1T;
  138. } else if (ratio <= K2T) {
  139. b = B2T;
  140. m = M2T;
  141. } else if (ratio <= K3T) {
  142. b = B3T;
  143. m = M3T;
  144. } else if (ratio <= K4T) {
  145. b = B4T;
  146. m = M4T;
  147. } else if (ratio <= K5T) {
  148. b = B5T;
  149. m = M5T;
  150. } else if (ratio <= K6T) {
  151. b = B6T;
  152. m = M6T;
  153. } else if (ratio <= K7T) {
  154. b = B7T;
  155. m = M7T;
  156. } else if (ratio > K8T) {
  157. b = B8T;
  158. m = M8T;
  159. }
  160. break;
  161. case 1:// CS package
  162. if ((ratio >= 0) && (ratio <= K1C)) {
  163. b = B1C;
  164. m = M1C;
  165. } else if (ratio <= K2C) {
  166. b = B2C;
  167. m = M2C;
  168. } else if (ratio <= K3C) {
  169. b = B3C;
  170. m = M3C;
  171. } else if (ratio <= K4C) {
  172. b = B4C;
  173. m = M4C;
  174. } else if (ratio <= K5C) {
  175. b = B5C;
  176. m = M5C;
  177. } else if (ratio <= K6C) {
  178. b = B6C;
  179. m = M6C;
  180. } else if (ratio <= K7C) {
  181. b = B7C;
  182. m = M7C;
  183. }
  184. }
  185. temp = ((channel0 * b) - (channel1 * m));
  186. if (temp < 0) {
  187. temp = 0;
  188. }
  189. temp += (1 << (LUX_SCALE - 1));
  190. // strip off fractional portion
  191. lux = temp >> LUX_SCALE;
  192. return (lux);
  193. }
  194. bool TSL2561_CalculateLux::checkHadTimeout(){
  195. bool timeout = hadTimeout;
  196. hadTimeout = false;
  197. return timeout;
  198. }
  199. TSL2561_CalculateLux TSL2561;