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