Adafruit_TSL2591.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**************************************************************************/
  2. /*!
  3. @file Adafruit_TSL2591.cpp
  4. @author KT0WN (adafruit.com)
  5. This is a library for the Adafruit TSL2591 breakout board
  6. This library works with the Adafruit TSL2591 breakout
  7. ----> https://www.adafruit.com/products/1980
  8. Check out the links above for our tutorials and wiring diagrams
  9. These chips use I2C to communicate
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing
  12. products from Adafruit!
  13. @section LICENSE
  14. Software License Agreement (BSD License)
  15. Copyright (c) 2014 Adafruit Industries
  16. All rights reserved.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions are met:
  19. 1. Redistributions of source code must retain the above copyright
  20. notice, this list of conditions and the following disclaimer.
  21. 2. Redistributions in binary form must reproduce the above copyright
  22. notice, this list of conditions and the following disclaimer in the
  23. documentation and/or other materials provided with the distribution.
  24. 3. Neither the name of the copyright holders nor the
  25. names of its contributors may be used to endorse or promote products
  26. derived from this software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  28. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  31. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**************************************************************************/
  39. #if defined(ESP8266)
  40. #include <pgmspace.h>
  41. #else
  42. #include <avr/pgmspace.h>
  43. #endif
  44. #if defined(__AVR__)
  45. #include <util/delay.h>
  46. #endif
  47. #include <stdlib.h>
  48. #include "Adafruit_TSL2591.h"
  49. Adafruit_TSL2591::Adafruit_TSL2591(int32_t sensorID)
  50. {
  51. _initialized = false;
  52. _integration = TSL2591_INTEGRATIONTIME_100MS;
  53. _gain = TSL2591_GAIN_MED;
  54. _sensorID = sensorID;
  55. // we cant do wire initialization till later, because we havent loaded Wire yet
  56. }
  57. boolean Adafruit_TSL2591::begin(void)
  58. {
  59. uint8_t id = read8(0x12);
  60. if (id == 0x50 )
  61. {
  62. Serial.println("Found Adafruit_TSL2591");
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. _initialized = true;
  69. // Set default integration time and gain
  70. setTiming(_integration);
  71. setGain(_gain);
  72. // Note: by default, the device is in power down mode on bootup
  73. disable();
  74. return true;
  75. }
  76. void Adafruit_TSL2591::enable(void)
  77. {
  78. if (!_initialized)
  79. {
  80. if (!begin())
  81. {
  82. return;
  83. }
  84. }
  85. // Enable the device by setting the control bit to 0x01
  86. write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
  87. }
  88. void Adafruit_TSL2591::disable(void)
  89. {
  90. if (!_initialized)
  91. {
  92. if (!begin())
  93. {
  94. return;
  95. }
  96. }
  97. // Disable the device by setting the control bit to 0x00
  98. write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
  99. }
  100. void Adafruit_TSL2591::setGain(tsl2591Gain_t gain)
  101. {
  102. if (!_initialized)
  103. {
  104. if (!begin())
  105. {
  106. return;
  107. }
  108. }
  109. enable();
  110. _gain = gain;
  111. write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);
  112. disable();
  113. }
  114. tsl2591Gain_t Adafruit_TSL2591::getGain()
  115. {
  116. return _gain;
  117. }
  118. void Adafruit_TSL2591::setTiming(tsl2591IntegrationTime_t integration)
  119. {
  120. if (!_initialized)
  121. {
  122. if (!begin())
  123. {
  124. return;
  125. }
  126. }
  127. enable();
  128. _integration = integration;
  129. write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);
  130. disable();
  131. }
  132. tsl2591IntegrationTime_t Adafruit_TSL2591::getTiming()
  133. {
  134. return _integration;
  135. }
  136. uint32_t Adafruit_TSL2591::calculateLux(uint16_t ch0, uint16_t ch1)
  137. {
  138. uint16_t atime, again;
  139. float cpl, lux1, lux2, lux;
  140. uint32_t chan0, chan1;
  141. // Check for overflow conditions first
  142. if ((ch0 == 0xFFFF) | (ch1 == 0xFFFF))
  143. {
  144. // Signal an overflow
  145. return 0;
  146. }
  147. // Note: This algorithm is based on preliminary coefficients
  148. // provided by AMS and may need to be updated in the future
  149. switch (_integration)
  150. {
  151. case TSL2591_INTEGRATIONTIME_100MS :
  152. atime = 100.0F;
  153. break;
  154. case TSL2591_INTEGRATIONTIME_200MS :
  155. atime = 200.0F;
  156. break;
  157. case TSL2591_INTEGRATIONTIME_300MS :
  158. atime = 300.0F;
  159. break;
  160. case TSL2591_INTEGRATIONTIME_400MS :
  161. atime = 400.0F;
  162. break;
  163. case TSL2591_INTEGRATIONTIME_500MS :
  164. atime = 500.0F;
  165. break;
  166. case TSL2591_INTEGRATIONTIME_600MS :
  167. atime = 600.0F;
  168. break;
  169. default: // 100ms
  170. atime = 100.0F;
  171. break;
  172. }
  173. switch (_gain)
  174. {
  175. case TSL2591_GAIN_LOW :
  176. again = 1.0F;
  177. break;
  178. case TSL2591_GAIN_MED :
  179. again = 25.0F;
  180. break;
  181. case TSL2591_GAIN_HIGH :
  182. again = 428.0F;
  183. break;
  184. case TSL2591_GAIN_MAX :
  185. again = 9876.0F;
  186. break;
  187. default:
  188. again = 1.0F;
  189. break;
  190. }
  191. // cpl = (ATIME * AGAIN) / DF
  192. cpl = (atime * again) / TSL2591_LUX_DF;
  193. lux1 = ( (float)ch0 - (TSL2591_LUX_COEFB * (float)ch1) ) / cpl;
  194. lux2 = ( ( TSL2591_LUX_COEFC * (float)ch0 ) - ( TSL2591_LUX_COEFD * (float)ch1 ) ) / cpl;
  195. // The highest value is the approximate lux equivalent
  196. lux = lux1 > lux2 ? lux1 : lux2;
  197. // Signal I2C had no errors
  198. return (uint32_t)lux;
  199. }
  200. uint32_t Adafruit_TSL2591::getFullLuminosity (void)
  201. {
  202. if (!_initialized)
  203. {
  204. if (!begin())
  205. {
  206. return 0;
  207. }
  208. }
  209. // Enable the device
  210. enable();
  211. // Wait x ms for ADC to complete
  212. for (uint8_t d=0; d<=_integration; d++)
  213. {
  214. delay(120);
  215. }
  216. uint32_t x;
  217. x = read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
  218. x <<= 16;
  219. x |= read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
  220. disable();
  221. return x;
  222. }
  223. uint16_t Adafruit_TSL2591::getLuminosity (uint8_t channel)
  224. {
  225. uint32_t x = getFullLuminosity();
  226. if (channel == TSL2591_FULLSPECTRUM)
  227. {
  228. // Reads two byte value from channel 0 (visible + infrared)
  229. return (x & 0xFFFF);
  230. }
  231. else if (channel == TSL2591_INFRARED)
  232. {
  233. // Reads two byte value from channel 1 (infrared)
  234. return (x >> 16);
  235. }
  236. else if (channel == TSL2591_VISIBLE)
  237. {
  238. // Reads all and subtracts out just the visible!
  239. return ( (x & 0xFFFF) - (x >> 16));
  240. }
  241. // unknown channel!
  242. return 0;
  243. }
  244. uint8_t Adafruit_TSL2591::read8(uint8_t reg)
  245. {
  246. Wire.beginTransmission(TSL2591_ADDR);
  247. Wire.write(0x80 | 0x20 | reg); // command bit, normal mode
  248. Wire.endTransmission();
  249. Wire.requestFrom(TSL2591_ADDR, 1);
  250. while (! Wire.available());
  251. return Wire.read();
  252. }
  253. uint16_t Adafruit_TSL2591::read16(uint8_t reg)
  254. {
  255. uint16_t x;
  256. uint16_t t;
  257. Wire.beginTransmission(TSL2591_ADDR);
  258. #if ARDUINO >= 100
  259. Wire.write(reg);
  260. #else
  261. Wire.send(reg);
  262. #endif
  263. Wire.endTransmission();
  264. Wire.requestFrom(TSL2591_ADDR, 2);
  265. #if ARDUINO >= 100
  266. t = Wire.read();
  267. x = Wire.read();
  268. #else
  269. t = Wire.receive();
  270. x = Wire.receive();
  271. #endif
  272. x <<= 8;
  273. x |= t;
  274. return x;
  275. }
  276. void Adafruit_TSL2591::write8 (uint8_t reg, uint8_t value)
  277. {
  278. Wire.beginTransmission(TSL2591_ADDR);
  279. #if ARDUINO >= 100
  280. Wire.write(reg);
  281. Wire.write(value);
  282. #else
  283. Wire.send(reg);
  284. Wire.send(value);
  285. #endif
  286. Wire.endTransmission();
  287. }
  288. /**************************************************************************/
  289. /*!
  290. @brief Gets the most recent sensor event
  291. */
  292. /**************************************************************************/
  293. bool Adafruit_TSL2591::getEvent(sensors_event_t *event)
  294. {
  295. uint16_t ir, full;
  296. uint32_t lum = getFullLuminosity();
  297. /* Early silicon seems to have issues when there is a sudden jump in */
  298. /* light levels. :( To work around this for now sample the sensor 2x */
  299. lum = getFullLuminosity();
  300. ir = lum >> 16;
  301. full = lum & 0xFFFF;
  302. /* Clear the event */
  303. memset(event, 0, sizeof(sensors_event_t));
  304. event->version = sizeof(sensors_event_t);
  305. event->sensor_id = _sensorID;
  306. event->type = SENSOR_TYPE_LIGHT;
  307. event->timestamp = millis();
  308. /* Calculate the actual lux value */
  309. /* 0 = sensor overflow (too much light) */
  310. event->light = calculateLux(full, ir);
  311. return true;
  312. }
  313. /**************************************************************************/
  314. /*!
  315. @brief Gets the sensor_t data
  316. */
  317. /**************************************************************************/
  318. void Adafruit_TSL2591::getSensor(sensor_t *sensor)
  319. {
  320. /* Clear the sensor_t object */
  321. memset(sensor, 0, sizeof(sensor_t));
  322. /* Insert the sensor name in the fixed length char array */
  323. strncpy (sensor->name, "TSL2591", sizeof(sensor->name) - 1);
  324. sensor->name[sizeof(sensor->name)- 1] = 0;
  325. sensor->version = 1;
  326. sensor->sensor_id = _sensorID;
  327. sensor->type = SENSOR_TYPE_LIGHT;
  328. sensor->min_delay = 0;
  329. sensor->max_value = 88000.0;
  330. sensor->min_value = 0.0;
  331. sensor->resolution = 1.0;
  332. }