Adafruit TCS34725 Library
Adafruit_TCS34725.h
Go to the documentation of this file.
1 
33 #ifndef _TCS34725_H_
34 #define _TCS34725_H_
35 
36 #if ARDUINO >= 100
37 #include <Arduino.h>
38 #else
39 #include <WProgram.h>
40 #endif
41 
42 #include <Adafruit_I2CDevice.h>
43 
44 #define TCS34725_ADDRESS (0x29)
45 #define TCS34725_COMMAND_BIT (0x80)
46 #define TCS34725_ENABLE (0x00)
47 #define TCS34725_ENABLE_AIEN (0x10)
48 #define TCS34725_ENABLE_WEN \
49  (0x08)
50 #define TCS34725_ENABLE_AEN \
51  (0x02)
52 #define TCS34725_ENABLE_PON \
53  (0x01)
55 #define TCS34725_ATIME (0x01)
56 #define TCS34725_WTIME \
57  (0x03)
58 #define TCS34725_WTIME_2_4MS (0xFF)
59 #define TCS34725_WTIME_204MS (0xAB)
60 #define TCS34725_WTIME_614MS (0x00)
61 #define TCS34725_AILTL \
62  (0x04)
63 #define TCS34725_AILTH \
64  (0x05)
65 #define TCS34725_AIHTL \
66  (0x06)
67 #define TCS34725_AIHTH \
68  (0x07)
69 #define TCS34725_PERS \
70  (0x0C)
72 #define TCS34725_PERS_NONE \
73  (0b0000)
74 #define TCS34725_PERS_1_CYCLE \
75  (0b0001)
77 #define TCS34725_PERS_2_CYCLE \
78  (0b0010)
80 #define TCS34725_PERS_3_CYCLE \
81  (0b0011)
83 #define TCS34725_PERS_5_CYCLE \
84  (0b0100)
86 #define TCS34725_PERS_10_CYCLE \
87  (0b0101)
89 #define TCS34725_PERS_15_CYCLE \
90  (0b0110)
92 #define TCS34725_PERS_20_CYCLE \
93  (0b0111)
95 #define TCS34725_PERS_25_CYCLE \
96  (0b1000)
98 #define TCS34725_PERS_30_CYCLE \
99  (0b1001)
101 #define TCS34725_PERS_35_CYCLE \
102  (0b1010)
104 #define TCS34725_PERS_40_CYCLE \
105  (0b1011)
107 #define TCS34725_PERS_45_CYCLE \
108  (0b1100)
110 #define TCS34725_PERS_50_CYCLE \
111  (0b1101)
113 #define TCS34725_PERS_55_CYCLE \
114  (0b1110)
116 #define TCS34725_PERS_60_CYCLE \
117  (0b1111)
119 #define TCS34725_CONFIG (0x0D)
120 #define TCS34725_CONFIG_WLONG \
121  (0x02)
123 #define TCS34725_CONTROL (0x0F)
124 #define TCS34725_ID \
125  (0x12)
126 #define TCS34725_STATUS (0x13)
127 #define TCS34725_STATUS_AINT (0x10)
128 #define TCS34725_STATUS_AVALID \
129  (0x01)
131 #define TCS34725_CDATAL (0x14)
132 #define TCS34725_CDATAH (0x15)
133 #define TCS34725_RDATAL (0x16)
134 #define TCS34725_RDATAH (0x17)
135 #define TCS34725_GDATAL (0x18)
136 #define TCS34725_GDATAH (0x19)
137 #define TCS34725_BDATAL (0x1A)
138 #define TCS34725_BDATAH (0x1B)
141 /*
142  * 60-Hz period: 16.67ms, 50-Hz period: 20ms
143  * 100ms is evenly divisible by 50Hz periods and by 60Hz periods
144  */
145 #define TCS34725_INTEGRATIONTIME_2_4MS \
146  (0xFF)
147 #define TCS34725_INTEGRATIONTIME_24MS \
148  (0xF6)
149 #define TCS34725_INTEGRATIONTIME_50MS \
150  (0xEB)
151 #define TCS34725_INTEGRATIONTIME_60MS \
152  (0xE7)
153 #define TCS34725_INTEGRATIONTIME_101MS \
154  (0xD6)
155 #define TCS34725_INTEGRATIONTIME_120MS \
156  (0xCE)
157 #define TCS34725_INTEGRATIONTIME_154MS \
158  (0xC0)
159 #define TCS34725_INTEGRATIONTIME_180MS \
160  (0xB5)
161 #define TCS34725_INTEGRATIONTIME_199MS \
162  (0xAD)
163 #define TCS34725_INTEGRATIONTIME_240MS \
164  (0x9C)
165 #define TCS34725_INTEGRATIONTIME_300MS \
166  (0x83)
167 #define TCS34725_INTEGRATIONTIME_360MS \
168  (0x6A)
169 #define TCS34725_INTEGRATIONTIME_401MS \
170  (0x59)
171 #define TCS34725_INTEGRATIONTIME_420MS \
172  (0x51)
173 #define TCS34725_INTEGRATIONTIME_480MS \
174  (0x38)
175 #define TCS34725_INTEGRATIONTIME_499MS \
176  (0x30)
177 #define TCS34725_INTEGRATIONTIME_540MS \
178  (0x1F)
179 #define TCS34725_INTEGRATIONTIME_600MS \
180  (0x06)
181 #define TCS34725_INTEGRATIONTIME_614MS \
182  (0x00)
185 typedef enum {
187  TCS34725_GAIN_4X = 0x01,
189  TCS34725_GAIN_60X = 0x03
191 
197 public:
201  boolean begin(uint8_t addr = TCS34725_ADDRESS, TwoWire *theWire = &Wire);
202  boolean init();
203 
204  void setIntegrationTime(uint8_t it);
206  void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
207  void getRGB(float *r, float *g, float *b);
208  void getRawDataOneShot(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
209  uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
210  uint16_t calculateColorTemperature_dn40(uint16_t r, uint16_t g, uint16_t b,
211  uint16_t c);
212  uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
213  void write8(uint8_t reg, uint8_t value);
214  uint8_t read8(uint8_t reg);
215  uint16_t read16(uint8_t reg);
216  void setInterrupt(boolean flag);
217  void clearInterrupt();
218  void setIntLimits(uint16_t l, uint16_t h);
219  void enable();
220  void disable();
221 
222 private:
223  Adafruit_I2CDevice *i2c_dev = NULL;
224  boolean _tcs34725Initialised;
225  tcs34725Gain_t _tcs34725Gain;
226  uint8_t _tcs34725IntegrationTime;
227 };
228 
229 #endif
uint16_t read16(uint8_t reg)
Reads a 16 bit values over I2C.
Definition: Adafruit_TCS34725.cpp:72
void getRGB(float *r, float *g, float *b)
Read the RGB color detected by the sensor.
Definition: Adafruit_TCS34725.cpp:249
uint8_t read8(uint8_t reg)
Reads an 8 bit value over I2C.
Definition: Adafruit_TCS34725.cpp:61
Adafruit_TCS34725(uint8_t=TCS34725_INTEGRATIONTIME_2_4MS, tcs34725Gain_t=TCS34725_GAIN_1X)
Constructor.
Definition: Adafruit_TCS34725.cpp:112
void setInterrupt(boolean flag)
Sets interrupt for TCS34725.
Definition: Adafruit_TCS34725.cpp:427
uint16_t calculateColorTemperature_dn40(uint16_t r, uint16_t g, uint16_t b, uint16_t c)
Converts the raw R/G/B values to color temperature in degrees Kelvin using the algorithm described in...
Definition: Adafruit_TCS34725.cpp:322
uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b)
Converts the raw R/G/B values to lux.
Definition: Adafruit_TCS34725.cpp:412
tcs34725Gain_t
Definition: Adafruit_TCS34725.h:204
void clearInterrupt()
Clears inerrupt for TCS34725.
Definition: Adafruit_TCS34725.cpp:440
void write8(uint8_t reg, uint8_t value)
Writes a register and an 8 bit value over I2C.
Definition: Adafruit_TCS34725.cpp:51
void setGain(tcs34725Gain_t gain)
Adjusts the gain on the TCS34725.
Definition: Adafruit_TCS34725.cpp:180
void disable()
Disables the device (putting it in lower power sleep mode)
Definition: Adafruit_TCS34725.cpp:98
uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
Converts the raw R/G/B values to color temperature in degrees Kelvin.
Definition: Adafruit_TCS34725.cpp:275
void getRawDataOneShot(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
Reads the raw red, green, blue and clear channel values in one-shot mode (e.g., wakes from sleep...
Definition: Adafruit_TCS34725.cpp:230
#define TCS34725_ADDRESS
Definition: Adafruit_TCS34725.h:44
void enable()
Enables the device.
Definition: Adafruit_TCS34725.cpp:81
Definition: Adafruit_TCS34725.h:207
boolean begin(uint8_t addr=TCS34725_ADDRESS, TwoWire *theWire=&Wire)
Initializes I2C and configures the sensor.
Definition: Adafruit_TCS34725.cpp:126
Definition: Adafruit_TCS34725.h:208
Class that stores state and functions for interacting with TCS34725 Color Sensor. ...
Definition: Adafruit_TCS34725.h:215
boolean init()
Part of begin.
Definition: Adafruit_TCS34725.cpp:138
Definition: Adafruit_TCS34725.h:205
void setIntLimits(uint16_t l, uint16_t h)
Sets inerrupt limits.
Definition: Adafruit_TCS34725.cpp:452
Definition: Adafruit_TCS34725.h:206
void setIntegrationTime(uint8_t it)
Sets the integration time for the TC34725.
Definition: Adafruit_TCS34725.cpp:164
#define TCS34725_INTEGRATIONTIME_2_4MS
Definition: Adafruit_TCS34725.h:164
void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
Reads the raw red, green, blue and clear channel values.
Definition: Adafruit_TCS34725.cpp:202