Digital_Light_TSL2561.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 :
  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. int TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
  35. {
  36. int 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, int 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. channel0=CH0_HIGH*256+CH0_LOW;
  62. channel1=CH1_HIGH*256+CH1_LOW;
  63. }
  64. void TSL2561_CalculateLux::init()
  65. {
  66. writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
  67. writeRegister(TSL2561_Address,TSL2561_Timing,0x11); //High Gain (16x), integration time of 101ms
  68. writeRegister(TSL2561_Address,TSL2561_Interrupt,0x00);
  69. writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
  70. }
  71. unsigned long TSL2561_CalculateLux::readVisibleLux()
  72. {
  73. writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
  74. delay(102);
  75. getLux();
  76. writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
  77. return calculateLux(1, 1, 1);
  78. }
  79. unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
  80. {
  81. switch (tInt)
  82. {
  83. case 0: // 13.7 msec
  84. chScale = CHSCALE_TINT0;
  85. break;
  86. case 1: // 101 msec
  87. chScale = CHSCALE_TINT1;
  88. break;
  89. default: // assume no scaling
  90. chScale = (1 << CH_SCALE);
  91. break;
  92. }
  93. if (!iGain) chScale = chScale << 4; // scale 1X to 16X
  94. // scale the channel values
  95. channel0 = (channel0 * chScale) >> CH_SCALE;
  96. channel1 = (channel1 * chScale) >> CH_SCALE;
  97. ratio1 = 0;
  98. if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
  99. // round the ratio value
  100. unsigned long ratio = (ratio1 + 1) >> 1;
  101. switch (iType)
  102. {
  103. case 0: // T package
  104. if ((ratio >= 0) && (ratio <= K1T))
  105. {b=B1T; m=M1T;}
  106. else if (ratio <= K2T)
  107. {b=B2T; m=M2T;}
  108. else if (ratio <= K3T)
  109. {b=B3T; m=M3T;}
  110. else if (ratio <= K4T)
  111. {b=B4T; m=M4T;}
  112. else if (ratio <= K5T)
  113. {b=B5T; m=M5T;}
  114. else if (ratio <= K6T)
  115. {b=B6T; m=M6T;}
  116. else if (ratio <= K7T)
  117. {b=B7T; m=M7T;}
  118. else if (ratio > K8T)
  119. {b=B8T; m=M8T;}
  120. break;
  121. case 1:// CS package
  122. if ((ratio >= 0) && (ratio <= K1C))
  123. {b=B1C; m=M1C;}
  124. else if (ratio <= K2C)
  125. {b=B2C; m=M2C;}
  126. else if (ratio <= K3C)
  127. {b=B3C; m=M3C;}
  128. else if (ratio <= K4C)
  129. {b=B4C; m=M4C;}
  130. else if (ratio <= K5C)
  131. {b=B5C; m=M5C;}
  132. else if (ratio <= K6C)
  133. {b=B6C; m=M6C;}
  134. else if (ratio <= K7C)
  135. {b=B7C; m=M7C;}
  136. }
  137. temp=((channel0*b)-(channel1*m));
  138. if(temp<0) temp=0;
  139. temp+=(1<<LUX_SCALE-1);
  140. // strip off fractional portion
  141. lux=temp>>LUX_SCALE;
  142. return (lux);
  143. }
  144. TSL2561_CalculateLux TSL2561;