/
Makerblog_TSL45315.cpp
100 lines (82 loc) · 2.66 KB
/
Makerblog_TSL45315.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************
This is a library for the TSL45315 Lux sensor breakout board by Watterott
These sensors use I2C to communicate, 2 pins are required to interface
Written by Adi Dax/Makerblog.at
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Arduino.h>
#include "Makerblog_TSL45315.h"
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
/**************************************************************************/
/*!
Constructor
*/
/**************************************************************************/
Makerblog_TSL45315::Makerblog_TSL45315(uint8_t resolution)
{
_resolution = resolution;
_timerfactor = 0;
if (resolution == uint8_t(TSL45315_TIME_M1)) {
_timerfactor = 1;
}
if (resolution == uint8_t(TSL45315_TIME_M2)) {
_timerfactor = 2;
}
if (resolution == uint8_t(TSL45315_TIME_M4)) {
_timerfactor = 4;
}
}
/*========================================================================*/
/* PUBLIC FUNCTIONS */
/*========================================================================*/
boolean Makerblog_TSL45315::begin(void)
{
Wire.begin();
Wire.beginTransmission(TSL45315_I2C_ADDR);
Wire.write(0x80|TSL45315_REG_ID);
Wire.endTransmission();
Wire.requestFrom(TSL45315_I2C_ADDR, 1);
while(Wire.available())
{
unsigned char c = Wire.read();
c = c & 0xF0;
if (c != 0xA0) {
return false;
}
}
Wire.beginTransmission(TSL45315_I2C_ADDR);
Wire.write(0x80|TSL45315_REG_CONTROL);
Wire.write(0x03);
Wire.endTransmission();
Wire.beginTransmission(TSL45315_I2C_ADDR);
Wire.write(0x80|TSL45315_REG_CONFIG);
Wire.write(_resolution);
Wire.endTransmission();
return true;
}
uint32_t Makerblog_TSL45315::readLux(void)
{
uint32_t lux;
Wire.beginTransmission(TSL45315_I2C_ADDR);
Wire.write(0x80|TSL45315_REG_DATALOW);
Wire.endTransmission();
Wire.requestFrom(TSL45315_I2C_ADDR, 2);
_low = Wire.read();
_high = Wire.read();
while(Wire.available()){
Wire.read();
}
lux = (_high<<8) | _low;
lux = lux * _timerfactor;
return lux;
}
boolean Makerblog_TSL45315::powerDown(void)
{
Wire.beginTransmission(TSL45315_I2C_ADDR);
Wire.write(0x80|TSL45315_REG_CONTROL);
Wire.write(0x00);
Wire.endTransmission();
return true;
}