Adafruit_TSL2561_U.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**************************************************************************/
  2. /*!
  3. @file Adafruit_TSL2561.h
  4. @author K. Townsend (Adafruit Industries)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, Adafruit Industries
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**************************************************************************/
  31. #ifndef _TSL2561_H_
  32. #define _TSL2561_H_
  33. #if ARDUINO >= 100
  34. #include <Arduino.h>
  35. #else
  36. #include <WProgram.h>
  37. #endif
  38. #include <Adafruit_Sensor.h>
  39. #ifdef __AVR_ATtiny85__
  40. #include "TinyWireM.h"
  41. #define Wire TinyWireM
  42. #else
  43. #include <Wire.h>
  44. #endif
  45. #define TSL2561_VISIBLE 2 // channel 0 - channel 1
  46. #define TSL2561_INFRARED 1 // channel 1
  47. #define TSL2561_FULLSPECTRUM 0 // channel 0
  48. // I2C address options
  49. #define TSL2561_ADDR_LOW (0x29)
  50. #define TSL2561_ADDR_FLOAT (0x39) // Default address (pin left floating)
  51. #define TSL2561_ADDR_HIGH (0x49)
  52. // Lux calculations differ slightly for CS package
  53. //#define TSL2561_PACKAGE_CS
  54. #define TSL2561_PACKAGE_T_FN_CL
  55. #define TSL2561_COMMAND_BIT (0x80) // Must be 1
  56. #define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear)
  57. #define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte)
  58. #define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write
  59. #define TSL2561_CONTROL_POWERON (0x03)
  60. #define TSL2561_CONTROL_POWEROFF (0x00)
  61. #define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14
  62. #define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9
  63. #define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10
  64. #define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
  65. #define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
  66. // T, FN and CL package values
  67. #define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE
  68. #define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE
  69. #define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE
  70. #define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE
  71. #define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE
  72. #define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE
  73. #define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE
  74. #define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE
  75. #define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE
  76. #define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE
  77. #define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE
  78. #define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE
  79. #define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE
  80. #define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE
  81. #define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE
  82. #define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE
  83. #define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE
  84. #define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE
  85. #define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE
  86. #define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE
  87. #define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE
  88. #define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE
  89. #define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE
  90. #define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE
  91. // CS package values
  92. #define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE
  93. #define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE
  94. #define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE
  95. #define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE
  96. #define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE
  97. #define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE
  98. #define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE
  99. #define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE
  100. #define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE
  101. #define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE
  102. #define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE
  103. #define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE
  104. #define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE
  105. #define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE
  106. #define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE
  107. #define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE
  108. #define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE
  109. #define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE
  110. #define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE
  111. #define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE
  112. #define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE
  113. #define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE
  114. #define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE
  115. #define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
  116. // Auto-gain thresholds
  117. #define TSL2561_AGC_THI_13MS (4850) // Max value at Ti 13ms = 5047
  118. #define TSL2561_AGC_TLO_13MS (100)
  119. #define TSL2561_AGC_THI_101MS (36000) // Max value at Ti 101ms = 37177
  120. #define TSL2561_AGC_TLO_101MS (200)
  121. #define TSL2561_AGC_THI_402MS (63000) // Max value at Ti 402ms = 65535
  122. #define TSL2561_AGC_TLO_402MS (500)
  123. // Clipping thresholds
  124. #define TSL2561_CLIPPING_13MS (4900)
  125. #define TSL2561_CLIPPING_101MS (37000)
  126. #define TSL2561_CLIPPING_402MS (65000)
  127. enum
  128. {
  129. TSL2561_REGISTER_CONTROL = 0x00,
  130. TSL2561_REGISTER_TIMING = 0x01,
  131. TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02,
  132. TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03,
  133. TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04,
  134. TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05,
  135. TSL2561_REGISTER_INTERRUPT = 0x06,
  136. TSL2561_REGISTER_CRC = 0x08,
  137. TSL2561_REGISTER_ID = 0x0A,
  138. TSL2561_REGISTER_CHAN0_LOW = 0x0C,
  139. TSL2561_REGISTER_CHAN0_HIGH = 0x0D,
  140. TSL2561_REGISTER_CHAN1_LOW = 0x0E,
  141. TSL2561_REGISTER_CHAN1_HIGH = 0x0F
  142. };
  143. typedef enum
  144. {
  145. TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms
  146. TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms
  147. TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms
  148. }
  149. tsl2561IntegrationTime_t;
  150. typedef enum
  151. {
  152. TSL2561_GAIN_1X = 0x00, // No gain
  153. TSL2561_GAIN_16X = 0x10, // 16x gain
  154. }
  155. tsl2561Gain_t;
  156. class Adafruit_TSL2561_Unified : public Adafruit_Sensor {
  157. public:
  158. Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1);
  159. boolean begin(void);
  160. /* TSL2561 Functions */
  161. void enableAutoRange(bool enable);
  162. void setIntegrationTime(tsl2561IntegrationTime_t time);
  163. void setGain(tsl2561Gain_t gain);
  164. void getLuminosity (uint16_t *broadband, uint16_t *ir);
  165. uint32_t calculateLux(uint16_t broadband, uint16_t ir);
  166. /* Unified Sensor API Functions */
  167. bool getEvent(sensors_event_t*);
  168. void getSensor(sensor_t*);
  169. private:
  170. int8_t _addr;
  171. boolean _tsl2561Initialised;
  172. boolean _tsl2561AutoGain;
  173. tsl2561IntegrationTime_t _tsl2561IntegrationTime;
  174. tsl2561Gain_t _tsl2561Gain;
  175. int32_t _tsl2561SensorID;
  176. void enable (void);
  177. void disable (void);
  178. void write8 (uint8_t reg, uint32_t value);
  179. uint8_t read8 (uint8_t reg);
  180. uint16_t read16 (uint8_t reg);
  181. void getData (uint16_t *broadband, uint16_t *ir);
  182. };
  183. #endif