AskSin++
Tsl2561.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __SENSORS_TSL2561_h__
7 #define __SENSORS_TSL2561_h__
8 
9 #include <Sensors.h>
10 #include <Wire.h>
11 #include <TSL2561.h>
12 
13 namespace as {
14 
15 // https://github.com/adafruit/TSL2561-Arduino-Library
16 template <int ADDRESS=TSL2561_ADDR_LOW>
17 class Tsl2561 : public Brightness {
18  ::TSL2561 _tsl;
19  bool _x16;
20 public:
21  Tsl2561 () : _tsl(ADDRESS), _x16(true) {}
22  void init () {
23  if( (_present = _tsl.begin()) == true ) {
24  _tsl.setGain(TSL2561_GAIN_16X);
25  _tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS);
26  DPRINT("TSL2561 found at 0x");DHEXLN((uint8_t)ADDRESS);
27  }
28  else {
29  DPRINTLN("No TSL2561 present");
30  }
31  }
32  void measure (__attribute__((unused)) bool async=false) {
33  if( present() == true ) {
34  uint16_t b = _tsl.getLuminosity(TSL2561_VISIBLE);
35  if( b > 63000 && _x16 == true ) {
36  _x16 = false;
37  _tsl.setGain(TSL2561_GAIN_0X);
38  b = _tsl.getLuminosity(TSL2561_VISIBLE);
39  }
40  else if ( b < 500 && _x16 == false ) {
41  _x16 = true;
42  _tsl.setGain(TSL2561_GAIN_16X);
43  b = _tsl.getLuminosity(TSL2561_VISIBLE);
44  }
45  DPRINT("Brightness: ");DDECLN(b);
46  _brightness = b;
47  }
48  }
49 };
50 
51 }
52 
53 #endif
as::Brightness
Definition: Sensors.h:21
as::Tsl2561
Definition: Tsl2561.h:17