Digital_Light_TSL2561.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Digital_Light_TSL2561.h
  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 :
  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. #ifndef Digital_Light_TSL2561_H
  27. #define Digital_Light_TSL2561_H
  28. #include <Arduino.h>
  29. #define TSL2561_Control 0x80
  30. #define TSL2561_Timing 0x81
  31. #define TSL2561_Interrupt 0x86
  32. #define TSL2561_Channal0L 0x8C
  33. #define TSL2561_Channal0H 0x8D
  34. #define TSL2561_Channal1L 0x8E
  35. #define TSL2561_Channal1H 0x8F
  36. #define TSL2561_Address 0x29 //device address
  37. #define LUX_SCALE 14 // scale by 2^14
  38. #define RATIO_SCALE 9 // scale ratio by 2^9
  39. #define CH_SCALE 10 // scale channel values by 2^10
  40. #define CHSCALE_TINT0 0x7517 // 322/11 * 2^CH_SCALE
  41. #define CHSCALE_TINT1 0x0fe7 // 322/81 * 2^CH_SCALE
  42. #define K1T 0x0040 // 0.125 * 2^RATIO_SCALE
  43. #define B1T 0x01f2 // 0.0304 * 2^LUX_SCALE
  44. #define M1T 0x01be // 0.0272 * 2^LUX_SCALE
  45. #define K2T 0x0080 // 0.250 * 2^RATIO_SCA
  46. #define B2T 0x0214 // 0.0325 * 2^LUX_SCALE
  47. #define M2T 0x02d1 // 0.0440 * 2^LUX_SCALE
  48. #define K3T 0x00c0 // 0.375 * 2^RATIO_SCALE
  49. #define B3T 0x023f // 0.0351 * 2^LUX_SCALE
  50. #define M3T 0x037b // 0.0544 * 2^LUX_SCALE
  51. #define K4T 0x0100 // 0.50 * 2^RATIO_SCALE
  52. #define B4T 0x0270 // 0.0381 * 2^LUX_SCALE
  53. #define M4T 0x03fe // 0.0624 * 2^LUX_SCALE
  54. #define K5T 0x0138 // 0.61 * 2^RATIO_SCALE
  55. #define B5T 0x016f // 0.0224 * 2^LUX_SCALE
  56. #define M5T 0x01fc // 0.0310 * 2^LUX_SCALE
  57. #define K6T 0x019a // 0.80 * 2^RATIO_SCALE
  58. #define B6T 0x00d2 // 0.0128 * 2^LUX_SCALE
  59. #define M6T 0x00fb // 0.0153 * 2^LUX_SCALE
  60. #define K7T 0x029a // 1.3 * 2^RATIO_SCALE
  61. #define B7T 0x0018 // 0.00146 * 2^LUX_SCALE
  62. #define M7T 0x0012 // 0.00112 * 2^LUX_SCALE
  63. #define K8T 0x029a // 1.3 * 2^RATIO_SCALE
  64. #define B8T 0x0000 // 0.000 * 2^LUX_SCALE
  65. #define M8T 0x0000 // 0.000 * 2^LUX_SCALE
  66. #define K1C 0x0043 // 0.130 * 2^RATIO_SCALE
  67. #define B1C 0x0204 // 0.0315 * 2^LUX_SCALE
  68. #define M1C 0x01ad // 0.0262 * 2^LUX_SCALE
  69. #define K2C 0x0085 // 0.260 * 2^RATIO_SCALE
  70. #define B2C 0x0228 // 0.0337 * 2^LUX_SCALE
  71. #define M2C 0x02c1 // 0.0430 * 2^LUX_SCALE
  72. #define K3C 0x00c8 // 0.390 * 2^RATIO_SCALE
  73. #define B3C 0x0253 // 0.0363 * 2^LUX_SCALE
  74. #define M3C 0x0363 // 0.0529 * 2^LUX_SCALE
  75. #define K4C 0x010a // 0.520 * 2^RATIO_SCALE
  76. #define B4C 0x0282 // 0.0392 * 2^LUX_SCALE
  77. #define M4C 0x03df // 0.0605 * 2^LUX_SCALE
  78. #define K5C 0x014d // 0.65 * 2^RATIO_SCALE
  79. #define B5C 0x0177 // 0.0229 * 2^LUX_SCALE
  80. #define M5C 0x01dd // 0.0291 * 2^LUX_SCALE
  81. #define K6C 0x019a // 0.80 * 2^RATIO_SCALE
  82. #define B6C 0x0101 // 0.0157 * 2^LUX_SCALE
  83. #define M6C 0x0127 // 0.0180 * 2^LUX_SCALE
  84. #define K7C 0x029a // 1.3 * 2^RATIO_SCALE
  85. #define B7C 0x0037 // 0.00338 * 2^LUX_SCALE
  86. #define M7C 0x002b // 0.00260 * 2^LUX_SCALE
  87. #define K8C 0x029a // 1.3 * 2^RATIO_SCALE
  88. #define B8C 0x0000 // 0.000 * 2^LUX_SCALE
  89. #define M8C 0x0000 // 0.000 * 2^LUX_SCALE
  90. /** No timeout while waiting for a read response from the digital light sensor. */
  91. #define TSL2561_NO_READ_TIMEOUT 0
  92. class TSL2561_CalculateLux {
  93. public:
  94. /**
  95. * Create a new interface to a TSL2561 light sensor.
  96. *
  97. * @param read_timeout The timeout to wait for read responses in microseconds.
  98. */
  99. explicit TSL2561_CalculateLux(uint32_t read_timeout = TSL2561_NO_READ_TIMEOUT);
  100. signed long readVisibleLux();
  101. uint16_t readIRLuminosity();
  102. uint16_t readFSpecLuminosity();
  103. unsigned long calculateLux(unsigned int iGain, unsigned int tInt, int iType);
  104. void getLux(void);
  105. void init(void);
  106. uint8_t readRegister(int deviceAddress, int address);
  107. void writeRegister(int deviceAddress, int address, uint8_t val);
  108. /**
  109. * Check whether there was a read timeout since the last call to this function
  110. * and clear the flag.
  111. *
  112. * @return whether there was a read timeout since the last check.
  113. */
  114. bool checkHadTimeout();
  115. private:
  116. uint8_t CH0_LOW, CH0_HIGH, CH1_LOW, CH1_HIGH;
  117. uint16_t ch0, ch1;
  118. unsigned long chScale;
  119. unsigned long channel1;
  120. unsigned long channel0;
  121. unsigned long ratio1;
  122. unsigned int b;
  123. unsigned int m;
  124. unsigned long temp;
  125. unsigned long lux;
  126. /**
  127. * The configured read timeout in microseconds.
  128. */
  129. uint32_t read_timeout;
  130. /**
  131. * Whether a read timeout occurred since the last time checkHadTimeout was called.
  132. */
  133. bool hadTimeout = false;
  134. };
  135. extern TSL2561_CalculateLux TSL2561;
  136. #endif