TCS3200 Arduino Library
Comprehensive Arduino library for TCS3200 Color Sensor.
Loading...
Searching...
No Matches
TCS3200.h
Go to the documentation of this file.
1/*
2 * This file is part of the TCS3200 Color Sensor Arduino library.
3 * Copyright (c) 2023 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
147#ifndef TCS3200_H
148#define TCS3200_H
149
150#include <Arduino.h>
151
152#define TCS3200_COLOR_RED 0x00
153#define TCS3200_COLOR_GREEN 0x01
154#define TCS3200_COLOR_BLUE 0x02
155#define TCS3200_COLOR_CLEAR 0x03
156
157#define TCS3200_PWR_DOWN 0x00
158#define TCS3200_OFREQ_2P 0x01
159#define TCS3200_OFREQ_20P 0x02
160#define TCS3200_OFREQ_100P 0x03
161
167typedef struct _RGBColor {
168 uint8_t red;
169 uint8_t green;
170 uint8_t blue;
172
178typedef struct _HSVColor {
179 float hue;
181 float value;
183
189typedef struct _CMYKColor {
190 float cyan;
191 float magenta;
192 float yellow;
193 float black;
195
201typedef struct _CIE1931 {
202 float x;
203 float y;
204 float z;
206
219class TCS3200 {
220public:
232 TCS3200(uint8_t s0_pin, uint8_t s1_pin, uint8_t s2_pin, uint8_t s3_pin, uint8_t out_pin);
233
239 void begin();
240
248 uint8_t read_red();
249
257 uint8_t read_green();
258
266 uint8_t read_blue();
267
275 uint8_t read_clear();
276
287 void calibrate();
288
300
312
320 void integration_time(unsigned int time);
321
329 unsigned int integration_time();
330
338 void frequency_scaling(int scaling);
339
348
357
366
374 void white_balance(RGBColor white_balance_rgb);
375
385
395
405
421 float get_chroma();
422
431
454 void loop();
455
467 void upper_bound_interrupt(RGBColor threshold, void (*callback)());
468
479 void lower_bound_interrupt(RGBColor threshold, void (*callback)());
480
490
500
514 template <typename T>
515 T nearest_color(T *color_labels, RGBColor *color_values, int size) {
516 T nearest;
517
518 RGBColor readings = this->read_rgb_color();
519 uint16_t min_dist = 0xffff;
520
521 for(int i = 0; i < size; i++) {
522 uint16_t dist = abs(readings.red - color_values[i].red) +
523 abs(readings.green - color_values[i].green) +
524 abs(readings.blue - color_values[i].blue);
525
526 if(dist < min_dist) {
527 min_dist = dist;
528 nearest = color_labels[i];
529 }
530 }
531
532 return nearest;
533 }
534
535private:
536 uint8_t _s0_pin, _s1_pin, _s2_pin, _s3_pin, _out_pin;
537 uint8_t max_r, max_g, max_b;
538 uint8_t min_r, min_g, min_b;
539
540 unsigned int _integration_time;
541 int _frequency_scaling;
542 bool is_calibrated;
543
544 void (*upper_bound_interrupt_callback)();
545 void (*lower_bound_interrupt_callback)();
546
547 RGBColor white_balance_rgb, ub_threshold, lb_threshold;
548
549 void select_filter(uint8_t filter);
550};
551
552#endif
struct _CMYKColor CMYKColor
Structure to represent CMYK color values.
struct _CIE1931 CIE1931Color
Structure to represent CIE 1931 XYZ color values.
struct _HSVColor HSVColor
Structure to represent HSV color values.
struct _RGBColor RGBColor
Structure to represent RGB color values.
Class representing the TCS3200 color sensor.
Definition TCS3200.h:219
void calibrate_light()
Perform light calibration for white balancing.
void calibrate()
Perform definition of the sensor as calibrated.
void integration_time(unsigned int time)
Set the integration time for color sensing.
uint8_t get_rgb_dominant_color()
Get the dominant RGB color channel.
uint8_t read_red()
Read the intensity of the red color channel.
uint8_t read_blue()
Read the intensity of the blue color channel.
void lower_bound_interrupt(RGBColor threshold, void(*callback)())
Enable a lower bound interrupt with a given threshold.
HSVColor read_hsv()
Read the HSV color values from the sensor.
T nearest_color(T *color_labels, RGBColor *color_values, int size)
Find the nearest color label based on the current sensor readings.
Definition TCS3200.h:515
uint8_t read_clear()
Read the intensity of the clear (ambient) color channel.
CIE1931Color read_cie1931()
Read the CIE 1931 XYZ color values from the sensor.
void frequency_scaling(int scaling)
Set the frequency scaling for color sensing.
void loop()
Continuously monitor color intensity values and trigger callbacks for interrupt conditions.
unsigned int integration_time()
Get the current integration time setting.
TCS3200(uint8_t s0_pin, uint8_t s1_pin, uint8_t s2_pin, uint8_t s3_pin, uint8_t out_pin)
Constructor for TCS3200 class.
void clear_lower_bound_interrupt()
Clear the lower bound interrupt.
CMYKColor read_cmyk()
Read the CMYK color values from the sensor.
uint8_t read_green()
Read the intensity of the green color channel.
void white_balance(RGBColor white_balance_rgb)
Set the white balance RGB values.
float get_chroma()
Calculate the chroma of the current color readings.
RGBColor white_balance()
Get the current white balance RGB values.
int frequency_scaling()
Get the current frequency scaling setting.
RGBColor read_rgb_color()
Read the RGB color values from the sensor.
void clear_upper_bound_interrupt()
Clear the upper bound interrupt.
void upper_bound_interrupt(RGBColor threshold, void(*callback)())
Enable an upper bound interrupt with a given threshold.
void begin()
Initialize the TCS3200 sensor and configure pins.
void calibrate_dark()
Perform dark calibration for white balancing.
Structure to represent CIE 1931 XYZ color values.
Definition TCS3200.h:201
float y
Y value.
Definition TCS3200.h:203
float x
X value.
Definition TCS3200.h:202
float z
Z value.
Definition TCS3200.h:204
Structure to represent CMYK color values.
Definition TCS3200.h:189
float black
Black (Key) color intensity (0-1)
Definition TCS3200.h:193
float yellow
Yellow color intensity (0-1)
Definition TCS3200.h:192
float cyan
Cyan color intensity (0-1)
Definition TCS3200.h:190
float magenta
Magenta color intensity (0-1)
Definition TCS3200.h:191
Structure to represent HSV color values.
Definition TCS3200.h:178
float hue
Hue value in degrees (0-360)
Definition TCS3200.h:179
float value
Value (brightness) value (0-1)
Definition TCS3200.h:181
float saturation
Saturation value (0-1)
Definition TCS3200.h:180
Structure to represent RGB color values.
Definition TCS3200.h:167
uint8_t red
Red color intensity (0-255)
Definition TCS3200.h:168
uint8_t blue
Blue color intensity (0-255)
Definition TCS3200.h:170
uint8_t green
Green color intensity (0-255)
Definition TCS3200.h:169