اطلاعیه

Collapse
No announcement yet.

ماژول تشخیص رنگ TCS34725?

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    ماژول تشخیص رنگ TCS34725?

    سلام
    با راه اندازی ماژول مذکور مشکلی نیست
    ولی در مورد مسائل دیگه ای در رابطه با ماژول سوال دارم
    خب در دیتا شیت گفته مثلا اگر شما رنگ سبز داشته باشید که منشا آن یک ال ای دی است مقداری روی آبی و قرمز تاثیر می گذارد که البته مقدارش قابل توجه است برای آبی
    مثلا اگر 1000 واحد سبز داشته باشیم مقدار آبی 500 واحد تغییر می کنه و مقدار قرمز 100 واحد این اعداد تقریبی است ولی شبیه به واقعیت
    یا اگر نور آبی بفرستید روی سبز تغییر زیادی می گذارد و روی قرمز کم تر
    و قرمز خالی هم روی سبز و آبی تاثیر کم تری به نسبت دارد
    مشکل این جاست که چه طور میشه مقدار واقعی رنگ ها رو تشخیص داد وقتی این کانال ها روی هم تاثیر می گذارند ؟؟؟؟
    اللهم صل علی محمد و ال محمد و عجل فرجهم
    پیامبر اکرم(ص):زکات علم نشر آن است.
    در کشور هاي غربي انتقال تجربيات و دانش به افراد مبتدي يک پيشرفت محسوب شده و به آن مديريت دانش مي گويند. ولي متاسفانه اين فرهنگ هنوز در کشور ايران رايج نشده است !!!

    #2
    پاسخ : ماژول تشخیص رنگ TCS34725?

    سلام
    من برای این قطعه دو مدل برنامه دیدم که با هم فرق هایی دارند و نتیجه خروجیشون یکی نمیشه کی میدونه کدم درسته
    این کد اول هست
    /*!
    * @file Adafruit_TCS34725.cpp
    *
    * [MENTION=77848]main[/MENTION]page Driver for the TCS34725 digital color sensors.
    *
    * @section intro_sec Introduction
    *
    * Adafruit invests time and resources providing this open source code,
    * please support Adafruit and open-source hardware by purchasing
    * products from Adafruit!
    *
    * @section author Author
    *
    * KTOWN (Adafruit Industries)
    *
    * @section license License
    *
    * BSD (see license.txt)
    *
    * @section HISTORY
    *
    * v1.0 - First release
    */
    #ifdef __AVR
    #include <avr/pgmspace.h>
    #elif defined(ESP8266)
    #include <pgmspace.h>
    #endif
    #include <math.h>
    #include <stdlib.h>

    #include "Adafruit_TCS34725.h"

    /*!
    * @brief Implements missing powf function
    * [MENTION=17545]para[/MENTION]m x
    * Base number
    * [MENTION=17545]para[/MENTION]m y
    * Exponent
    * @return x raised to the power of y
    */
    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
    * [MENTION=17545]para[/MENTION]m reg
    * [MENTION=17545]para[/MENTION]m value
    */
    void Adafruit_TCS34725::write8(uint8_t reg, uint32_t value) {
    _wire->beginTransmission(_i2caddr);
    #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
    * [MENTION=17545]para[/MENTION]m reg
    * @return value
    */
    uint8_t Adafruit_TCS34725::read8(uint8_t reg) {
    _wire->beginTransmission(_i2caddr);
    #if ARDUINO >= 100
    _wire->write(TCS34725_COMMAND_BIT | reg);
    #else
    _wire->send(TCS34725_COMMAND_BIT | reg);
    #endif
    _wire->endTransmission();

    _wire->requestFrom(_i2caddr, (uint8_t)1);
    #if ARDUINO >= 100
    return _wire->read();
    #else
    return _wire->receive();
    #endif
    }

    /*!
    * @brief Reads a 16 bit values over I2C
    * [MENTION=17545]para[/MENTION]m reg
    * @return value
    */
    uint16_t Adafruit_TCS34725::read16(uint8_t reg) {
    uint16_t x;
    uint16_t t;

    _wire->beginTransmission(_i2caddr);
    #if ARDUINO >= 100
    _wire->write(TCS34725_COMMAND_BIT | reg);
    #else
    _wire->send(TCS34725_COMMAND_BIT | reg);
    #endif
    _wire->endTransmission();

    _wire->requestFrom(_i2caddr, (uint8_t)2);
    #if ARDUINO >= 100
    t = _wire->read();
    x = _wire->read();
    #else
    t = _wire->receive();
    x = _wire->receive();
    #endif
    x <<= 8;
    x |= t;
    return x;
    }

    /*!
    * @brief Enables the device
    */
    void Adafruit_TCS34725::enable() {
    write8(TCS34725_ENABLE, TCS34725_ENABLE_PON);
    delay(3);
    write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
    /* Set a delay for the integration time.
    This is only necessary in the case where enabling and then
    immediately trying to read values back. This is because setting
    AEN triggers an automatic integration, so if a read RGBC is
    performed too quickly, the data is not yet valid and all 0's are
    returned */
    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 Disables the device (putting it in lower power sleep mode)
    */
    void Adafruit_TCS34725::disable() {
    /* 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));
    }

    /*!
    * @brief Constructor
    * [MENTION=17545]para[/MENTION]m it
    * Integration Time
    * [MENTION=17545]para[/MENTION]m gain
    * Gain
    */
    Adafruit_TCS34725::Adafruit_TCS34725(tcs34725IntegrationTime_t it,
    tcs34725Gain_t gain) {
    _tcs34725Initialised = false;
    _tcs34725IntegrationTime = it;
    _tcs34725Gain = gain;
    }

    /*!
    * @brief Initializes I2C and configures the sensor
    * [MENTION=17545]para[/MENTION]m addr
    * i2c address
    * @return True if initialization was successful, otherwise false.
    */
    boolean Adafruit_TCS34725::begin(uint8_t addr) {
    _i2caddr = addr;
    _wire = &Wire;

    return init();
    }

    /*!
    * @brief Initializes I2C and configures the sensor
    * [MENTION=17545]para[/MENTION]m addr
    * i2c address
    * [MENTION=17545]para[/MENTION]m *theWire
    * The Wire object
    * @return True if initialization was successful, otherwise false.
    */
    boolean Adafruit_TCS34725::begin(uint8_t addr, TwoWire *theWire) {
    _i2caddr = addr;
    _wire = theWire;

    return init();
    }

    /*!
    * @brief Initializes I2C and configures the sensor
    * @return True if initialization was successful, otherwise false.
    */
    boolean Adafruit_TCS34725::begin() {
    _i2caddr = TCS34725_ADDRESS;
    _wire = &Wire;

    return init();
    }

    /*!
    * @brief Part of begin
    * @return True if initialization was successful, otherwise false.
    */
    boolean Adafruit_TCS34725::init() {
    _wire->begin();

    /* Make sure we're actually connected */
    uint8_t x = read8(TCS34725_ID);
    if ((x != 0x44) && (x != 0x10)) {
    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;
    }

    /*!
    * @brief Sets the integration time for the TC34725
    * [MENTION=17545]para[/MENTION]m it
    * Integration Time
    */
    void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it) {
    if (!_tcs34725Initialised)
    begin();

    /* Update the timing register */
    write8(TCS34725_ATIME, it);

    /* Update value placeholders */
    _tcs34725IntegrationTime = it;
    }

    /*!
    * @brief Adjusts the gain on the TCS34725
    * [MENTION=17545]para[/MENTION]m gain
    * Gain (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
    * [MENTION=17545]para[/MENTION]m *r
    * Red value
    * [MENTION=17545]para[/MENTION]m *g
    * Green value
    * [MENTION=17545]para[/MENTION]m *b
    * Blue value
    * [MENTION=17545]para[/MENTION]m *c
    * Clear channel value
    */
    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 Reads the raw red, green, blue and clear channel values in
    * one-shot mode (e.g., wakes from sleep, takes measurement, enters
    * sleep)
    * [MENTION=17545]para[/MENTION]m *r
    * Red value
    * [MENTION=17545]para[/MENTION]m *g
    * Green value
    * [MENTION=17545]para[/MENTION]m *b
    * Blue value
    * [MENTION=17545]para[/MENTION]m *c
    * Clear channel value
    */
    void Adafruit_TCS34725::getRawDataOneShot(uint16_t *r, uint16_t *g, uint16_t *b,
    uint16_t *c) {
    if (!_tcs34725Initialised)
    begin();

    enable();
    getRawData(r, g, b, c);
    disable();
    }

    /*!
    * @brief Read the RGB color detected by the sensor.
    * [MENTION=17545]para[/MENTION]m *r
    * Red value normalized to 0-255
    * [MENTION=17545]para[/MENTION]m *g
    * Green value normalized to 0-255
    * [MENTION=17545]para[/MENTION]m *b
    * Blue value normalized to 0-255
    */
    void Adafruit_TCS34725::getRGB(float *r, float *g, float *b) {
    uint16_t red, green, blue, clear;
    getRawData(&red, &green, &blue, &clear);
    uint32_t sum = clear;

    // Avoid divide by zero errors ... if clear = 0 return black
    if (clear == 0) {
    *r = *g = *b = 0;
    return;
    }

    *r = (float)red / sum * 255.0;
    *g = (float)green / sum * 255.0;
    *b = (float)blue / sum * 255.0;
    }

    /*!
    * @brief Converts the raw R/G/B values to color temperature in degrees Kelvin
    * [MENTION=17545]para[/MENTION]m r
    * Red value
    * [MENTION=17545]para[/MENTION]m g
    * Green value
    * [MENTION=17545]para[/MENTION]m b
    * Blue value
    * @return 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;

    if (r == 0 && g == 0 && b == 0) {
    return 0;
    }

    /* 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 using the algorithm described in DN40 from Taos (now AMS).
    * [MENTION=17545]para[/MENTION]m r
    * Red value
    * [MENTION=17545]para[/MENTION]m g
    * Green value
    * [MENTION=17545]para[/MENTION]m b
    * Blue value
    * [MENTION=17545]para[/MENTION]m c
    * Clear channel value
    * @return Color temperature in degrees Kelvin
    */
    uint16_t Adafruit_TCS34725::calculateColorTemperature_dn40(uint16_t r,
    uint16_t g,
    uint16_t b,
    uint16_t c) {
    uint16_t r2, b2; /* RGB values minus IR component */
    uint16_t sat; /* Digital saturation level */
    uint16_t ir; /* Inferred IR content */

    if (c == 0) {
    return 0;
    }

    /* Analog/Digital saturation:
    *
    * (a) As light becomes brighter, the clear channel will tend to
    * saturate first since R+G+B is approximately equal to C.
    * (b) The TCS34725 accumulates 1024 counts per 2.4ms of integration
    * time, up to a maximum values of 65535. This means analog
    * saturation can occur up to an integration time of 153.6ms
    * (64*2.4ms=153.6ms).
    * (c) If the integration time is > 153.6ms, digital saturation will
    * occur before analog saturation. Digital saturation occurs when
    * the count reaches 65535.
    */
    if ((256 - _tcs34725IntegrationTime) > 63) {
    /* Track digital saturation */
    sat = 65535;
    } else {
    /* Track analog saturation */
    sat = 1024 * (256 - _tcs34725IntegrationTime);
    }

    /* Ripple rejection:
    *
    * (a) An integration time of 50ms or multiples of 50ms are required to
    * reject both 50Hz and 60Hz ripple.
    * (b) If an integration time faster than 50ms is required, you may need
    * to average a number of samples over a 50ms period to reject ripple
    * from fluorescent and incandescent light sources.
    *
    * Ripple saturation notes:
    *
    * (a) If there is ripple in the received signal, the value read from C
    * will be less than the max, but still have some effects of being
    * saturated. This means that you can be below the 'sat' value, but
    * still be saturating. At integration times >150ms this can be
    * ignored, but <= 150ms you should calculate the 75% saturation
    * level to avoid this problem.
    */
    if ((256 - _tcs34725IntegrationTime) <= 63) {
    /* Adjust sat to 75% to avoid analog saturation if atime < 153.6ms */
    sat -= sat / 4;
    }

    /* Check for saturation and mark the sample as invalid if true */
    if (c >= sat) {
    return 0;
    }

    /* AMS RGB sensors have no IR channel, so the IR content must be */
    /* calculated indirectly. */
    ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;

    /* Remove the IR component from the raw RGB values */
    r2 = r - ir;
    b2 = b - ir;

    if (r2 == 0) {
    return 0;
    }

    /* A simple method of measuring color temp is to use the ratio of blue */
    /* to red light, taking IR cancellation into account. */
    uint16_t cct = (3810 * (uint32_t)b2) / /** Color temp coefficient. */
    (uint32_t)r2 +
    1391; /** Color temp offset. */

    return cct;
    }

    /*!
    * @brief Converts the raw R/G/B values to lux
    * [MENTION=17545]para[/MENTION]m r
    * Red value
    * [MENTION=17545]para[/MENTION]m g
    * Green value
    * [MENTION=17545]para[/MENTION]m b
    * Blue value
    * @return Lux value
    */
    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;
    }

    /*!
    * @brief Sets inerrupt for TCS34725
    * [MENTION=17545]para[/MENTION]m i
    * Interrupt (True/False)
    */
    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);
    }

    /*!
    * @brief Clears inerrupt for TCS34725
    */
    void Adafruit_TCS34725::clearInterrupt() {
    _wire->beginTransmission(_i2caddr);
    #if ARDUINO >= 100
    _wire->write(TCS34725_COMMAND_BIT | 0x66);
    #else
    _wire->send(TCS34725_COMMAND_BIT | 0x66);
    #endif
    _wire->endTransmission();
    }

    /*!
    * @brief Sets inerrupt limits
    * [MENTION=17545]para[/MENTION]m low
    * Low limit
    * [MENTION=17545]para[/MENTION]m high
    * High limit
    */
    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);
    }


    این هم کد دوم
    #pragma once
    #ifndef TCS34725_H
    #define TCS34725_H

    #include <Arduino.h>
    #ifdef TEENSYDUINO
    #include <i2c_t3.h>
    #else
    #include <Wire.h>
    #endif

    template <typename WireType>
    class TCS34725_
    {
    static constexpr uint8_t I2C_ADDR {0x29};
    static constexpr uint8_t ID_REG_PART_NUMBER {0x44};
    static constexpr uint8_t COMMAND_BIT {0x80};

    static constexpr float INTEGRATION_CYCLES_MIN {1.f};
    static constexpr float INTEGRATION_CYCLES_MAX {256.f};
    static constexpr float INTEGRATION_TIME_MS_MIN {2.4f};
    static constexpr float INTEGRATION_TIME_MS_MAX {INTEGRATION_TIME_MS_MIN * INTEGRATION_CYCLES_MAX};

    public:

    enum class Reg : uint8_t
    {
    ENABLE = 0x00,
    ATIME = 0x01,
    WTIME = 0x03,
    AILTL = 0x04,
    AILTH = 0x05,
    AIHTL = 0x06,
    AIHTH = 0x07,
    PERS = 0x0C,
    CONFIG = 0x0D,
    CONTROL = 0x0F,
    ID = 0x12,
    STATUS = 0x13,
    CDATAL = 0x14,
    CDATAH = 0x15,
    RDATAL = 0x16,
    RDATAH = 0x17,
    GDATAL = 0x18,
    GDATAH = 0x19,
    BDATAL = 0x1A,
    BDATAH = 0x1B,
    };

    enum class Mask : uint8_t
    {
    ENABLE_AIEN = 0x10,
    ENABLE_WEN = 0x08,
    ENABLE_AEN = 0x02,
    ENABLE_PON = 0x01,
    STATUS_AINT = 0x10,
    STATUS_AVALID = 0x01
    };

    enum class Gain : uint8_t { X01, X04, X16, X60 };

    struct Color { float r, g, b; };
    struct RawData { uint16_t r, g, b, c; };


    bool attach(WireType& w = Wire)
    {
    wire = &w;
    uint8_t x = read8(Reg::ID);
    if (x != ID_REG_PART_NUMBER) return false;

    power(true);
    interrupt(true); // use to detect availability (available())
    persistence(0x00); // every RGBC cycle generates an interrupt

    return true;
    }

    void power(bool b)
    {
    if (b)
    {
    write8(Reg::ENABLE, (uint8_t)Mask::ENABLE_PON);
    delay(3); // 2.4 ms must pass after PON is asserted before an RGBC can be initiated
    write8(Reg::ENABLE, (uint8_t)Mask::ENABLE_PON | (uint8_t)Mask::ENABLE_AEN);
    }
    else
    {
    uint8_t val = read8(Reg::ENABLE);
    write8(Reg::ENABLE, val & ~((uint8_t)Mask::ENABLE_PON | (uint8_t)Mask::ENABLE_AEN));
    }
    }

    void enableColorTempAndLuxCalculation(bool b) { b_ct_lux_calc = b; }

    void integrationTime(float ms) // 2.4 - 614.4 ms
    {
    if (ms < INTEGRATION_TIME_MS_MIN) ms = INTEGRATION_TIME_MS_MIN;
    if (ms > INTEGRATION_TIME_MS_MAX) ms = INTEGRATION_TIME_MS_MAX;
    uint8_t data = (uint8_t)(256.f - ms / INTEGRATION_TIME_MS_MIN);
    write8(Reg::ATIME, data);
    atime = data;
    integration_time = ms;
    }

    void gain(Gain g)
    {
    write8(Reg::CONTROL, (uint8_t)g);
    switch (g)
    {
    case Gain::X01: gain_value = 1.f; break;
    case Gain::X04: gain_value = 4.f; break;
    case Gain::X16: gain_value = 16.f; break;
    case Gain::X60: gain_value = 60.f; break;
    default: gain_value = 1.f; break;
    }
    }

    void scale(float s) { scaling = s; }

    // The Glass Attenuation (FA) factor used to compensate for lower light
    // levels at the device due to the possible presence of glass. The GA is
    // the inverse of the glass transmissivity (T), so GA = 1/T. A transmissivity
    // of 50% gives GA = 1 / 0.50 = 2. If no glass is present, use GA = 1.
    // See Application Note: DN40-Rev 1.0 – Lux and CCT Calculations using
    // ams Color Sensors for more details.
    void glassAttenuation(float v) { if (v < 1.f) v = 1.f; glass_attenuation = v; }

    void persistence(uint16_t data) { write8(Reg::PERS, data); }

    bool available()
    {
    bool b = read8(Reg::STATUS) & (uint8_t)Mask::STATUS_AINT;
    if (b)
    {
    update();
    if (b_ct_lux_calc) calcTemperatureAndLuxDN40();
    clearInterrupt();
    }
    return b;
    }

    const Color& color() const { return clr; }
    const RawData& raw() const { return raw_data; }
    float lux() const { return lx; }
    float colorTemperature() const { return color_temp; }

    void interrupt(bool b)
    {
    uint8_t r = read8(Reg::ENABLE);
    if (b) r |= (uint8_t)Mask::ENABLE_AIEN;
    else r &= ~(uint8_t)Mask::ENABLE_AIEN;
    write8(Reg::ENABLE, r);
    }

    void clearInterrupt()
    {
    wire->beginTransmission(I2C_ADDR);
    wire->write(COMMAND_BIT | 0x66);
    wire->endTransmission();
    }

    void write8(Reg reg, uint8_t value)
    {
    wire->beginTransmission(I2C_ADDR);
    wire->write(COMMAND_BIT | (uint8_t)reg);
    wire->write(value);
    wire->endTransmission();
    }

    uint8_t read8(Reg reg)
    {
    wire->beginTransmission(I2C_ADDR);
    wire->write(COMMAND_BIT | (uint8_t)reg);
    wire->endTransmission();
    wire->requestFrom(I2C_ADDR, (uint8_t)1);
    return wire->read();
    }

    uint16_t read16(Reg reg)
    {
    uint16_t x;
    uint16_t t;

    wire->beginTransmission(I2C_ADDR);
    wire->write(COMMAND_BIT | (uint8_t)reg);
    wire->endTransmission();

    wire->requestFrom(I2C_ADDR, (uint8_t)2);
    t = wire->read();
    x = wire->read();
    x <<= 8;
    x |= t;
    return x;
    }

    private:

    void update()
    {
    raw_data.r = read16(Reg::RDATAL);
    raw_data.g = read16(Reg::GDATAL);
    raw_data.b = read16(Reg::BDATAL);
    raw_data.c = read16(Reg::CDATAL);

    if (raw_data.c == 0) clr.r = clr.g = clr.b = 0;
    else
    {
    clr.r = pow((float)raw_data.r / (float)raw_data.c, scaling) * 255.f;
    clr.g = pow((float)raw_data.g / (float)raw_data.c, scaling) * 255.f;
    clr.b = pow((float)raw_data.b / (float)raw_data.c, scaling) * 255.f;
    if (clr.r > 255.f) clr.r = 255.f;
    if (clr.g > 255.f) clr.g = 255.f;
    if (clr.b > 255.f) clr.b = 255.f;
    }
    }

    // https://github.com/adafruit/Adafruit_CircuitPython_TCS34725/blob/master/adafruit_tcs34725.py
    void calcTemperatureAndLuxDN40()
    {
    // Device specific values (DN40 Table 1 in Appendix I)
    const float GA = glass_attenuation; // Glass Attenuation Factor
    static const float DF = 310.f; // Device Factor
    static const float R_Coef = 0.136f; //
    static const float G_Coef = 1.f; // used in lux computation
    static const float B_Coef = -0.444f; //
    static const float CT_Coef = 3810.f; // Color Temperature Coefficient
    static const float CT_Offset = 1391.f; // Color Temperatuer Offset

    // Analog/Digital saturation (DN40 3.5)
    float saturation = (256 - atime > 63) ? 65535 : 1024 * (256 - atime);

    // Ripple saturation (DN40 3.7)
    if (integration_time < 150)
    saturation -= saturation / 4;

    // Check for saturation and mark the sample as invalid if true
    if (raw_data.c >= saturation)
    return;

    // IR Rejection (DN40 3.1)
    float sum = raw_data.r + raw_data.g + raw_data.b;
    float c = raw_data.c;
    float ir = (sum > c) ? ((sum - c) / 2.f) : 0.f;
    float r2 = raw_data.r - ir;
    float g2 = raw_data.g - ir;
    float b2 = raw_data.b - ir;

    // Lux Calculation (DN40 3.2)
    float g1 = R_Coef * r2 + G_Coef * g2 + B_Coef * b2;
    float cpl = (integration_time * gain_value) / (GA * DF);
    lx = g1 / cpl;

    // CT Calculations (DN40 3.4)
    color_temp = CT_Coef * b2 / r2 + CT_Offset;
    }


    WireType* wire;
    float scaling {2.5f};

    // for lux & temperature
    bool b_ct_lux_calc {true};
    float lx;
    float color_temp;
    RawData raw_data;
    Color clr;
    float gain_value {1.f};
    uint8_t atime {0xFF};
    float integration_time {2.4f}; // [ms]
    float glass_attenuation {1.f};
    };

    #ifdef TEENSYDUINO
    using TCS34725 = TCS34725_<i2c_t3>;
    #else
    using TCS34725 = TCS34725_<TwoWire>;
    #endif

    #endif // TCS34725_H

    اللهم صل علی محمد و ال محمد و عجل فرجهم
    پیامبر اکرم(ص):زکات علم نشر آن است.
    در کشور هاي غربي انتقال تجربيات و دانش به افراد مبتدي يک پيشرفت محسوب شده و به آن مديريت دانش مي گويند. ولي متاسفانه اين فرهنگ هنوز در کشور ايران رايج نشده است !!!

    دیدگاه

    لطفا صبر کنید...
    X