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