-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsl2561.cpp
62 lines (50 loc) · 1.54 KB
/
tsl2561.cpp
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
54
55
56
57
58
59
60
61
#include "tsl2561.h"
#include <TSL2561.h>
#include <vector>
#include <utility>
namespace tsl2561 {
TSL2561 sensor = TSL2561(TSL2561_ADDR_FLOAT);
unsigned long MAX_LUX = 40000;
unsigned long MAX_VALUE = 65535;
bool setup() {
if(!sensor.begin())
{
Serial.println("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
return false;
}
sensor.setGain(TSL2561_GAIN_16X);
sensor.setTiming(TSL2561_INTEGRATIONTIME_402MS);
return true;
}
Measurement _measure() {
Measurement m;
uint32_t lum = sensor.getFullLuminosity();
m.ir = lum >> 16;
m.full = lum & 0xFFFF;
m.visible = m.full - m.ir;
m.lux = sensor.calculateLux(m.full, m.ir);
return m;
}
Measurement measure(std::map<String, String> &environment) {
Measurement m;
std::vector< std::pair<tsl2561Gain_t, tsl2561IntegrationTime_t> > configs = {
{TSL2561_GAIN_16X, TSL2561_INTEGRATIONTIME_402MS},
{TSL2561_GAIN_0X, TSL2561_INTEGRATIONTIME_402MS},
{TSL2561_GAIN_0X, TSL2561_INTEGRATIONTIME_101MS},
{TSL2561_GAIN_0X, TSL2561_INTEGRATIONTIME_13MS}
};
for (auto const &conf : configs) {
sensor.setGain(conf.first);
sensor.setTiming(conf.second);
m = _measure();
if (m.lux < MAX_LUX && m.full < MAX_VALUE && m.ir < MAX_VALUE) {
break;
}
Serial.println("Too bright, lowering gain and timing.");
}
environment["lux"] = String(m.lux);
environment["visible"] = String(m.visible);
environment["ir"] = String(m.ir);
return m;
}
}