Digital_Light_TSL2561.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. TSL2561 library V1.0
  3. 2010 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Original Author: zhangkun
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include <Digital_Light_TSL2561.h>
  18. #include <Arduino.h>
  19. #include <Wire.h>
  20. int TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
  21. {
  22. int value;
  23. Wire.beginTransmission(deviceAddress);
  24. Wire.write(address); // register to read
  25. Wire.endTransmission();
  26. Wire.requestFrom(deviceAddress, 1); // read a byte
  27. while(!Wire.available());
  28. value = Wire.read();
  29. delay(100);
  30. return value;
  31. }
  32. void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, int val)
  33. {
  34. Wire.beginTransmission(deviceAddress); // start transmission to device
  35. Wire.write(address); // send register address
  36. Wire.write(val); // send value to write
  37. Wire.endTransmission(); // end transmission
  38. delay(100);
  39. }
  40. void TSL2561_CalculateLux::getLux(void)
  41. {
  42. CH0_LOW=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL0L);
  43. CH0_HIGH=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL0H);
  44. //read two bytes from registers 0x0E and 0x0F
  45. CH1_LOW=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL1L);
  46. CH1_HIGH=readRegister(TSL2561_ADDRESS,TSL2561_CHANNAL1H);
  47. channel0=CH0_HIGH*256+CH0_LOW;
  48. channel1=CH1_HIGH*256+CH1_LOW;
  49. }
  50. void TSL2561_CalculateLux::init()
  51. {
  52. writeRegister(TSL2561_ADDRESS,TSL2561_CONTROL,0x03); // POWER UP
  53. writeRegister(TSL2561_ADDRESS,TSL2561_TIMING,0x11); //High Gain (16x), integration time of 101ms
  54. writeRegister(TSL2561_ADDRESS,TSL2561_INTERRUPT,0x00);
  55. }
  56. unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
  57. {
  58. switch (tInt)
  59. {
  60. case 0: // 13.7 msec
  61. chScale = CHSCALE_TINT0;
  62. break;
  63. case 1: // 101 msec
  64. chScale = CHSCALE_TINT1;
  65. break;
  66. default: // assume no scaling
  67. chScale = (1 << CH_SCALE);
  68. break;
  69. }
  70. if (!iGain) chScale = chScale << 4; // scale 1X to 16X
  71. // scale the channel values
  72. channel0 = (channel0 * chScale) >> CH_SCALE;
  73. channel1 = (channel1 * chScale) >> CH_SCALE;
  74. ratio1 = 0;
  75. if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
  76. // round the ratio value
  77. unsigned long ratio = (ratio1 + 1) >> 1;
  78. switch (iType)
  79. {
  80. case 0: // T package
  81. if ((ratio >= 0) && (ratio <= K1T))
  82. {b=B1T; m=M1T;}
  83. else if (ratio <= K2T)
  84. {b=B2T; m=M2T;}
  85. else if (ratio <= K3T)
  86. {b=B3T; m=M3T;}
  87. else if (ratio <= K4T)
  88. {b=B4T; m=M4T;}
  89. else if (ratio <= K5T)
  90. {b=B5T; m=M5T;}
  91. else if (ratio <= K6T)
  92. {b=B6T; m=M6T;}
  93. else if (ratio <= K7T)
  94. {b=B7T; m=M7T;}
  95. else if (ratio > K8T)
  96. {b=B8T; m=M8T;}
  97. break;
  98. case 1:// CS package
  99. if ((ratio >= 0) && (ratio <= K1C))
  100. {b=B1C; m=M1C;}
  101. else if (ratio <= K2C)
  102. {b=B2C; m=M2C;}
  103. else if (ratio <= K3C)
  104. {b=B3C; m=M3C;}
  105. else if (ratio <= K4C)
  106. {b=B4C; m=M4C;}
  107. else if (ratio <= K5C)
  108. {b=B5C; m=M5C;}
  109. else if (ratio <= K6C)
  110. {b=B6C; m=M6C;}
  111. else if (ratio <= K7C)
  112. {b=B7C; m=M7C;}
  113. }
  114. temp=((channel0*b)-(channel1*m));
  115. if(temp<0) temp=0;
  116. temp+=(1<<LUX_SCALE-1);
  117. // strip off fractional portion
  118. lux=temp>>LUX_SCALE;
  119. return (lux);
  120. }
  121. TSL2561_CalculateLux TSL2561;