GROVE i2c color sensor Library. based on https://github.com/Seeed-Studio/Grove_I2C_Color_Sensor_TCS3472

Dependents:   PROJ 2PA2S 2PA2S_v2 2PA2S-interrupteur

Committer:
sgrsn
Date:
Fri Aug 31 05:32:03 2018 +0000
Revision:
0:1bf53b314b01
First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgrsn 0:1bf53b314b01 1 /**************************************************************************/
sgrsn 0:1bf53b314b01 2 /*!
sgrsn 0:1bf53b314b01 3 @file Adafruit_TCS34725.h
sgrsn 0:1bf53b314b01 4 @author KTOWN (Adafruit Industries)
sgrsn 0:1bf53b314b01 5
sgrsn 0:1bf53b314b01 6 @section LICENSE
sgrsn 0:1bf53b314b01 7
sgrsn 0:1bf53b314b01 8 Software License Agreement (BSD License)
sgrsn 0:1bf53b314b01 9
sgrsn 0:1bf53b314b01 10 Copyright (c) 2013, Adafruit Industries
sgrsn 0:1bf53b314b01 11 All rights reserved.
sgrsn 0:1bf53b314b01 12
sgrsn 0:1bf53b314b01 13 Redistribution and use in source and binary forms, with or without
sgrsn 0:1bf53b314b01 14 modification, are permitted provided that the following conditions are met:
sgrsn 0:1bf53b314b01 15 1. Redistributions of source code must retain the above copyright
sgrsn 0:1bf53b314b01 16 notice, this list of conditions and the following disclaimer.
sgrsn 0:1bf53b314b01 17 2. Redistributions in binary form must reproduce the above copyright
sgrsn 0:1bf53b314b01 18 notice, this list of conditions and the following disclaimer in the
sgrsn 0:1bf53b314b01 19 documentation and/or other materials provided with the distribution.
sgrsn 0:1bf53b314b01 20 3. Neither the name of the copyright holders nor the
sgrsn 0:1bf53b314b01 21 names of its contributors may be used to endorse or promote products
sgrsn 0:1bf53b314b01 22 derived from this software without specific prior written permission.
sgrsn 0:1bf53b314b01 23
sgrsn 0:1bf53b314b01 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
sgrsn 0:1bf53b314b01 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sgrsn 0:1bf53b314b01 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sgrsn 0:1bf53b314b01 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
sgrsn 0:1bf53b314b01 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sgrsn 0:1bf53b314b01 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sgrsn 0:1bf53b314b01 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
sgrsn 0:1bf53b314b01 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
sgrsn 0:1bf53b314b01 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sgrsn 0:1bf53b314b01 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sgrsn 0:1bf53b314b01 34 */
sgrsn 0:1bf53b314b01 35 /**************************************************************************/
sgrsn 0:1bf53b314b01 36 #ifndef Adafruit_TCS34725_H_
sgrsn 0:1bf53b314b01 37 #define Adafruit_TCS34725_H_
sgrsn 0:1bf53b314b01 38
sgrsn 0:1bf53b314b01 39 #include "mbed.h"
sgrsn 0:1bf53b314b01 40
sgrsn 0:1bf53b314b01 41 #define TCS34725_ADDRESS (0x29<<1)
sgrsn 0:1bf53b314b01 42
sgrsn 0:1bf53b314b01 43 #define TCS34725_COMMAND_BIT (0x80)
sgrsn 0:1bf53b314b01 44
sgrsn 0:1bf53b314b01 45 #define TCS34725_ENABLE (0x00)
sgrsn 0:1bf53b314b01 46 #define TCS34725_ENABLE_AIEN (0x10) /* RGBC Interrupt Enable */
sgrsn 0:1bf53b314b01 47 #define TCS34725_ENABLE_WEN (0x08) /* Wait enable - Writing 1 activates the wait timer */
sgrsn 0:1bf53b314b01 48 #define TCS34725_ENABLE_AEN (0x02) /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
sgrsn 0:1bf53b314b01 49 #define TCS34725_ENABLE_PON (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
sgrsn 0:1bf53b314b01 50 #define TCS34725_ATIME (0x01) /* Integration time */
sgrsn 0:1bf53b314b01 51 #define TCS34725_WTIME (0x03) /* Wait time (if TCS34725_ENABLE_WEN is asserted) */
sgrsn 0:1bf53b314b01 52 #define TCS34725_WTIME_2_4MS (0xFF) /* WLONG0 = 2.4ms WLONG1 = 0.029s */
sgrsn 0:1bf53b314b01 53 #define TCS34725_WTIME_204MS (0xAB) /* WLONG0 = 204ms WLONG1 = 2.45s */
sgrsn 0:1bf53b314b01 54 #define TCS34725_WTIME_614MS (0x00) /* WLONG0 = 614ms WLONG1 = 7.4s */
sgrsn 0:1bf53b314b01 55 #define TCS34725_AILTL (0x04) /* Clear channel lower interrupt threshold */
sgrsn 0:1bf53b314b01 56 #define TCS34725_AILTH (0x05)
sgrsn 0:1bf53b314b01 57 #define TCS34725_AIHTL (0x06) /* Clear channel upper interrupt threshold */
sgrsn 0:1bf53b314b01 58 #define TCS34725_AIHTH (0x07)
sgrsn 0:1bf53b314b01 59 #define TCS34725_PERS (0x0C) /* Persistence register - basic SW filtering mechanism for interrupts */
sgrsn 0:1bf53b314b01 60 #define TCS34725_PERS_NONE (0b0000) /* Every RGBC cycle generates an interrupt */
sgrsn 0:1bf53b314b01 61 #define TCS34725_PERS_1_CYCLE (0b0001) /* 1 clean channel value outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 62 #define TCS34725_PERS_2_CYCLE (0b0010) /* 2 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 63 #define TCS34725_PERS_3_CYCLE (0b0011) /* 3 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 64 #define TCS34725_PERS_5_CYCLE (0b0100) /* 5 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 65 #define TCS34725_PERS_10_CYCLE (0b0101) /* 10 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 66 #define TCS34725_PERS_15_CYCLE (0b0110) /* 15 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 67 #define TCS34725_PERS_20_CYCLE (0b0111) /* 20 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 68 #define TCS34725_PERS_25_CYCLE (0b1000) /* 25 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 69 #define TCS34725_PERS_30_CYCLE (0b1001) /* 30 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 70 #define TCS34725_PERS_35_CYCLE (0b1010) /* 35 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 71 #define TCS34725_PERS_40_CYCLE (0b1011) /* 40 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 72 #define TCS34725_PERS_45_CYCLE (0b1100) /* 45 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 73 #define TCS34725_PERS_50_CYCLE (0b1101) /* 50 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 74 #define TCS34725_PERS_55_CYCLE (0b1110) /* 55 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 75 #define TCS34725_PERS_60_CYCLE (0b1111) /* 60 clean channel values outside threshold range generates an interrupt */
sgrsn 0:1bf53b314b01 76 #define TCS34725_CONFIG (0x0D)
sgrsn 0:1bf53b314b01 77 #define TCS34725_CONFIG_WLONG (0x02) /* Choose between short and long (12x) wait times via TCS34725_WTIME */
sgrsn 0:1bf53b314b01 78 #define TCS34725_CONTROL (0x0F) /* Set the gain level for the sensor */
sgrsn 0:1bf53b314b01 79 #define TCS34725_ID (0x12) /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
sgrsn 0:1bf53b314b01 80 #define TCS34725_STATUS (0x13)
sgrsn 0:1bf53b314b01 81 #define TCS34725_STATUS_AINT (0x10) /* RGBC Clean channel interrupt */
sgrsn 0:1bf53b314b01 82 #define TCS34725_STATUS_AVALID (0x01) /* Indicates that the RGBC channels have completed an integration cycle */
sgrsn 0:1bf53b314b01 83 #define TCS34725_CDATAL (0x14) /* Clear channel data */
sgrsn 0:1bf53b314b01 84 #define TCS34725_CDATAH (0x15)
sgrsn 0:1bf53b314b01 85 #define TCS34725_RDATAL (0x16) /* Red channel data */
sgrsn 0:1bf53b314b01 86 #define TCS34725_RDATAH (0x17)
sgrsn 0:1bf53b314b01 87 #define TCS34725_GDATAL (0x18) /* Green channel data */
sgrsn 0:1bf53b314b01 88 #define TCS34725_GDATAH (0x19)
sgrsn 0:1bf53b314b01 89 #define TCS34725_BDATAL (0x1A) /* Blue channel data */
sgrsn 0:1bf53b314b01 90 #define TCS34725_BDATAH (0x1B)
sgrsn 0:1bf53b314b01 91
sgrsn 0:1bf53b314b01 92 typedef enum
sgrsn 0:1bf53b314b01 93 {
sgrsn 0:1bf53b314b01 94 TCS34725_INTEGRATIONTIME_2_4MS = 0xFF, /**< 2.4ms - 1 cycle - Max Count: 1024 */
sgrsn 0:1bf53b314b01 95 TCS34725_INTEGRATIONTIME_24MS = 0xF6, /**< 24ms - 10 cycles - Max Count: 10240 */
sgrsn 0:1bf53b314b01 96 TCS34725_INTEGRATIONTIME_50MS = 0xEB, /**< 50ms - 20 cycles - Max Count: 20480 */
sgrsn 0:1bf53b314b01 97 TCS34725_INTEGRATIONTIME_101MS = 0xD5, /**< 101ms - 42 cycles - Max Count: 43008 */
sgrsn 0:1bf53b314b01 98 TCS34725_INTEGRATIONTIME_154MS = 0xC0, /**< 154ms - 64 cycles - Max Count: 65535 */
sgrsn 0:1bf53b314b01 99 TCS34725_INTEGRATIONTIME_700MS = 0x00 /**< 700ms - 256 cycles - Max Count: 65535 */
sgrsn 0:1bf53b314b01 100 }
sgrsn 0:1bf53b314b01 101 tcs34725IntegrationTime_t;
sgrsn 0:1bf53b314b01 102
sgrsn 0:1bf53b314b01 103 typedef enum
sgrsn 0:1bf53b314b01 104 {
sgrsn 0:1bf53b314b01 105 TCS34725_GAIN_1X = 0x00, /**< No gain */
sgrsn 0:1bf53b314b01 106 TCS34725_GAIN_4X = 0x01, /**< 2x gain */
sgrsn 0:1bf53b314b01 107 TCS34725_GAIN_16X = 0x02, /**< 16x gain */
sgrsn 0:1bf53b314b01 108 TCS34725_GAIN_60X = 0x03 /**< 60x gain */
sgrsn 0:1bf53b314b01 109 }
sgrsn 0:1bf53b314b01 110 tcs34725Gain_t;
sgrsn 0:1bf53b314b01 111
sgrsn 0:1bf53b314b01 112 class Adafruit_TCS34725 {
sgrsn 0:1bf53b314b01 113 public:
sgrsn 0:1bf53b314b01 114 Adafruit_TCS34725(I2C *i2c, tcs34725IntegrationTime_t = TCS34725_INTEGRATIONTIME_2_4MS, tcs34725Gain_t = TCS34725_GAIN_1X);
sgrsn 0:1bf53b314b01 115
sgrsn 0:1bf53b314b01 116 bool begin(void);
sgrsn 0:1bf53b314b01 117 void setIntegrationTime(tcs34725IntegrationTime_t it);
sgrsn 0:1bf53b314b01 118 void setGain(tcs34725Gain_t gain);
sgrsn 0:1bf53b314b01 119 void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
sgrsn 0:1bf53b314b01 120 uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
sgrsn 0:1bf53b314b01 121 uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
sgrsn 0:1bf53b314b01 122 void write8 (uint8_t reg, uint32_t value);
sgrsn 0:1bf53b314b01 123 uint8_t read8 (uint8_t reg);
sgrsn 0:1bf53b314b01 124 uint16_t read16 (uint8_t reg);
sgrsn 0:1bf53b314b01 125 void setInterrupt(bool flag);
sgrsn 0:1bf53b314b01 126 void clearInterrupt(void);
sgrsn 0:1bf53b314b01 127 void setIntLimits(uint16_t l, uint16_t h);
sgrsn 0:1bf53b314b01 128 void enable(void);
sgrsn 0:1bf53b314b01 129
sgrsn 0:1bf53b314b01 130 private:
sgrsn 0:1bf53b314b01 131 bool _tcs34725Initialised;
sgrsn 0:1bf53b314b01 132 tcs34725Gain_t _tcs34725Gain;
sgrsn 0:1bf53b314b01 133 tcs34725IntegrationTime_t _tcs34725IntegrationTime;
sgrsn 0:1bf53b314b01 134
sgrsn 0:1bf53b314b01 135 I2C *_i2c;
sgrsn 0:1bf53b314b01 136
sgrsn 0:1bf53b314b01 137 void disable(void);
sgrsn 0:1bf53b314b01 138 };
sgrsn 0:1bf53b314b01 139
sgrsn 0:1bf53b314b01 140 #endif