Digital_Light_TSL2561.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Digital_Light_TSL2561.cpp
  3. * A library for TSL2561
  4. *
  5. * Copyright (c) 2012 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : zhangkun
  8. * Create Time:
  9. * Change Log : Jack Shao, Nov 2014, bug fix and update for user experience
  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_TSL2561.h>
  32. #include <Arduino.h>
  33. #include <Wire.h>
  34. uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
  35. {
  36. uint8_t value;
  37. Wire.beginTransmission(deviceAddress);
  38. Wire.write(address); // register to read
  39. Wire.endTransmission();
  40. Wire.requestFrom(deviceAddress, 1); // read a byte
  41. while(!Wire.available());
  42. value = Wire.read();
  43. //delay(100);
  44. return value;
  45. }
  46. void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val)
  47. {
  48. Wire.beginTransmission(deviceAddress); // start transmission to device
  49. Wire.write(address); // send register address
  50. Wire.write(val); // send value to write
  51. Wire.endTransmission(); // end transmission
  52. //delay(100);
  53. }
  54. void TSL2561_CalculateLux::getLux(void)
  55. {
  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. {
  66. writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
  67. writeRegister(TSL2561_Address,TSL2561_Timing,0x00); //No High Gain (1x), integration time of 13ms
  68. writeRegister(TSL2561_Address,TSL2561_Interrupt,0x00);
  69. writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
  70. }
  71. signed long TSL2561_CalculateLux::readVisibleLux()
  72. {
  73. writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
  74. delay(14);
  75. getLux();
  76. writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
  77. if(ch1 == 0)
  78. {
  79. return 0;
  80. }
  81. if(ch0/ch1 < 2 && ch0 > 4900)
  82. {
  83. return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  84. }
  85. return calculateLux(0, 0, 0); //T package, no gain, 13ms
  86. }
  87. unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
  88. {
  89. switch (tInt)
  90. {
  91. case 0: // 13.7 msec
  92. chScale = CHSCALE_TINT0;
  93. break;
  94. case 1: // 101 msec
  95. chScale = CHSCALE_TINT1;
  96. break;
  97. default: // assume no scaling
  98. chScale = (1 << CH_SCALE);
  99. break;
  100. }
  101. if (!iGain) chScale = chScale << 4; // scale 1X to 16X
  102. // scale the channel values
  103. channel0 = (ch0 * chScale) >> CH_SCALE;
  104. channel1 = (ch1 * chScale) >> CH_SCALE;
  105. ratio1 = 0;
  106. if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
  107. // round the ratio value
  108. unsigned long ratio = (ratio1 + 1) >> 1;
  109. switch (iType)
  110. {
  111. case 0: // T package
  112. if ((ratio >= 0) && (ratio <= K1T))
  113. {b=B1T; m=M1T;}
  114. else if (ratio <= K2T)
  115. {b=B2T; m=M2T;}
  116. else if (ratio <= K3T)
  117. {b=B3T; m=M3T;}
  118. else if (ratio <= K4T)
  119. {b=B4T; m=M4T;}
  120. else if (ratio <= K5T)
  121. {b=B5T; m=M5T;}
  122. else if (ratio <= K6T)
  123. {b=B6T; m=M6T;}
  124. else if (ratio <= K7T)
  125. {b=B7T; m=M7T;}
  126. else if (ratio > K8T)
  127. {b=B8T; m=M8T;}
  128. break;
  129. case 1:// CS package
  130. if ((ratio >= 0) && (ratio <= K1C))
  131. {b=B1C; m=M1C;}
  132. else if (ratio <= K2C)
  133. {b=B2C; m=M2C;}
  134. else if (ratio <= K3C)
  135. {b=B3C; m=M3C;}
  136. else if (ratio <= K4C)
  137. {b=B4C; m=M4C;}
  138. else if (ratio <= K5C)
  139. {b=B5C; m=M5C;}
  140. else if (ratio <= K6C)
  141. {b=B6C; m=M6C;}
  142. else if (ratio <= K7C)
  143. {b=B7C; m=M7C;}
  144. }
  145. temp=((channel0*b)-(channel1*m));
  146. if(temp<0) temp=0;
  147. temp+=(1<<(LUX_SCALE-1));
  148. // strip off fractional portion
  149. lux=temp>>LUX_SCALE;
  150. return (lux);
  151. }
  152. TSL2561_CalculateLux TSL2561;