forked from adafruit/Adafruit_TCS34725
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_TCS34725.cpp
336 lines (290 loc) · 9.57 KB
/
Adafruit_TCS34725.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**************************************************************************/
/*!
@file Adafruit_TCS34725.cpp
@author KTOWN (Adafruit Industries)
@license BSD (see license.txt)
Driver for the TCS34725 digital color sensors.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <stdlib.h>
#include <math.h>
#include "Adafruit_TCS34725.h"
/*========================================================================*/
/* PRIVATE FUNCTIONS */
/*========================================================================*/
/**************************************************************************/
/*!
@brief Implements missing powf function
*/
/**************************************************************************/
float powf(const float x, const float y)
{
return (float)(pow((double)x, (double)y));
}
/**************************************************************************/
/*!
@brief Writes a register and an 8 bit value over I2C
*/
/**************************************************************************/
void Adafruit_TCS34725::write8 (uint8_t reg, uint32_t value)
{
Wire.beginTransmission(TCS34725_ADDRESS);
#if ARDUINO >= 100
Wire.write(TCS34725_COMMAND_BIT | reg);
Wire.write(value & 0xFF);
#else
Wire.send(TCS34725_COMMAND_BIT | reg);
Wire.send(value & 0xFF);
#endif
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads an 8 bit value over I2C
*/
/**************************************************************************/
uint8_t Adafruit_TCS34725::read8(uint8_t reg)
{
Wire.beginTransmission(TCS34725_ADDRESS);
#if ARDUINO >= 100
Wire.write(TCS34725_COMMAND_BIT | reg);
#else
Wire.send(TCS34725_COMMAND_BIT | reg);
#endif
Wire.endTransmission();
Wire.requestFrom(TCS34725_ADDRESS, 1);
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
/**************************************************************************/
/*!
@brief Reads a 16 bit values over I2C
*/
/**************************************************************************/
uint16_t Adafruit_TCS34725::read16(uint8_t reg)
{
uint16_t x; uint16_t t;
Wire.beginTransmission(TCS34725_ADDRESS);
#if ARDUINO >= 100
Wire.write(TCS34725_COMMAND_BIT | reg);
#else
Wire.send(TCS34725_COMMAND_BIT | reg);
#endif
Wire.endTransmission();
Wire.requestFrom(TCS34725_ADDRESS, 2);
#if ARDUINO >= 100
t = Wire.read();
x = Wire.read();
#else
t = Wire.receive();
x = Wire.receive();
#endif
x <<= 8;
x |= t;
return x;
}
/**************************************************************************/
/*!
Enables the device
*/
/**************************************************************************/
void Adafruit_TCS34725::enable(void)
{
write8(TCS34725_ENABLE, TCS34725_ENABLE_PON);
delay(3);
write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
}
/**************************************************************************/
/*!
Disables the device (putting it in lower power sleep mode)
*/
/**************************************************************************/
void Adafruit_TCS34725::disable(void)
{
/* Turn the device off to save power */
uint8_t reg = 0;
reg = read8(TCS34725_ENABLE);
write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
}
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
/**************************************************************************/
/*!
Constructor
*/
/**************************************************************************/
Adafruit_TCS34725::Adafruit_TCS34725(tcs34725IntegrationTime_t it, tcs34725Gain_t gain)
{
_tcs34725Initialised = false;
_tcs34725IntegrationTime = it;
_tcs34725Gain = gain;
}
/*========================================================================*/
/* PUBLIC FUNCTIONS */
/*========================================================================*/
/**************************************************************************/
/*!
Initializes I2C and configures the sensor (call this function before
doing anything else)
*/
/**************************************************************************/
boolean Adafruit_TCS34725::begin(void)
{
Wire.begin();
/* Make sure we're actually connected */
uint8_t x = read8(TCS34725_ID);
Serial.println(x, HEX);
if (x != 0x44)
{
return false;
}
_tcs34725Initialised = true;
/* Set default integration time and gain */
setIntegrationTime(_tcs34725IntegrationTime);
setGain(_tcs34725Gain);
/* Note: by default, the device is in power down mode on bootup */
enable();
return true;
}
/**************************************************************************/
/*!
Sets the integration time for the TC34725
*/
/**************************************************************************/
void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it)
{
if (!_tcs34725Initialised) begin();
/* Update the timing register */
write8(TCS34725_ATIME, it);
/* Update value placeholders */
_tcs34725IntegrationTime = it;
}
/**************************************************************************/
/*!
Adjusts the gain on the TCS34725 (adjusts the sensitivity to light)
*/
/**************************************************************************/
void Adafruit_TCS34725::setGain(tcs34725Gain_t gain)
{
if (!_tcs34725Initialised) begin();
/* Update the timing register */
write8(TCS34725_CONTROL, gain);
/* Update value placeholders */
_tcs34725Gain = gain;
}
/**************************************************************************/
/*!
@brief Reads the raw red, green, blue and clear channel values
*/
/**************************************************************************/
void Adafruit_TCS34725::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
{
if (!_tcs34725Initialised) begin();
*c = read16(TCS34725_CDATAL);
*r = read16(TCS34725_RDATAL);
*g = read16(TCS34725_GDATAL);
*b = read16(TCS34725_BDATAL);
/* Set a delay for the integration time */
switch (_tcs34725IntegrationTime)
{
case TCS34725_INTEGRATIONTIME_2_4MS:
delay(3);
break;
case TCS34725_INTEGRATIONTIME_24MS:
delay(24);
break;
case TCS34725_INTEGRATIONTIME_50MS:
delay(50);
break;
case TCS34725_INTEGRATIONTIME_101MS:
delay(101);
break;
case TCS34725_INTEGRATIONTIME_154MS:
delay(154);
break;
case TCS34725_INTEGRATIONTIME_700MS:
delay(700);
break;
}
}
/**************************************************************************/
/*!
@brief Converts the raw R/G/B values to color temperature in degrees
Kelvin
*/
/**************************************************************************/
uint16_t Adafruit_TCS34725::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
{
float X, Y, Z; /* RGB to XYZ correlation */
float xc, yc; /* Chromaticity co-ordinates */
float n; /* McCamy's formula */
float cct;
/* 1. Map RGB values to their XYZ counterparts. */
/* Based on 6500K fluorescent, 3000K fluorescent */
/* and 60W incandescent values for a wide range. */
/* Note: Y = Illuminance or lux */
X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
/* 2. Calculate the chromaticity co-ordinates */
xc = (X) / (X + Y + Z);
yc = (Y) / (X + Y + Z);
/* 3. Use McCamy's formula to determine the CCT */
n = (xc - 0.3320F) / (0.1858F - yc);
/* Calculate the final CCT */
cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
/* Return the results in degrees Kelvin */
return (uint16_t)cct;
}
/**************************************************************************/
/*!
@brief Converts the raw R/G/B values to color temperature in degrees
Kelvin
*/
/**************************************************************************/
uint16_t Adafruit_TCS34725::calculateLux(uint16_t r, uint16_t g, uint16_t b)
{
float illuminance;
/* This only uses RGB ... how can we integrate clear or calculate lux */
/* based exclusively on clear since this might be more reliable? */
illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
return (uint16_t)illuminance;
}
void Adafruit_TCS34725::setInterrupt(boolean i) {
uint8_t r = read8(TCS34725_ENABLE);
if (i) {
r |= TCS34725_ENABLE_AIEN;
} else {
r &= ~TCS34725_ENABLE_AIEN;
}
write8(TCS34725_ENABLE, r);
}
void Adafruit_TCS34725::clearInterrupt(void) {
Wire.beginTransmission(TCS34725_ADDRESS);
#if ARDUINO >= 100
Wire.write(0x66);
#else
Wire.send(0x66);
#endif
Wire.endTransmission();
}
void Adafruit_TCS34725::setIntLimits(uint16_t low, uint16_t high) {
write8(0x04, low & 0xFF);
write8(0x05, low >> 8);
write8(0x06, high & 0xFF);
write8(0x07, high >> 8);
}