tcs34725: optimize fetch time with burst read for RGB and clear values (#7494)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@RubenKelevra 2024-10-01 00:00:40 +02:00 committed by GitHub
parent 507d27e84a
commit 01e03b76a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include <algorithm>
#include "esphome/core/helpers.h"
namespace esphome {
namespace tcs34725 {
@ -14,10 +15,7 @@ static const uint8_t TCS34725_REGISTER_ID = TCS34725_COMMAND_BIT | 0x12;
static const uint8_t TCS34725_REGISTER_ATIME = TCS34725_COMMAND_BIT | 0x01;
static const uint8_t TCS34725_REGISTER_CONTROL = TCS34725_COMMAND_BIT | 0x0F;
static const uint8_t TCS34725_REGISTER_ENABLE = TCS34725_COMMAND_BIT | 0x00;
static const uint8_t TCS34725_REGISTER_CDATAL = TCS34725_COMMAND_BIT | 0x14;
static const uint8_t TCS34725_REGISTER_RDATAL = TCS34725_COMMAND_BIT | 0x16;
static const uint8_t TCS34725_REGISTER_GDATAL = TCS34725_COMMAND_BIT | 0x18;
static const uint8_t TCS34725_REGISTER_BDATAL = TCS34725_COMMAND_BIT | 0x1A;
static const uint8_t TCS34725_REGISTER_CRGBDATAL = TCS34725_COMMAND_BIT | 0x14;
void TCS34725Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up TCS34725...");
@ -181,27 +179,21 @@ void TCS34725Component::calculate_temperature_and_lux_(uint16_t r, uint16_t g, u
}
void TCS34725Component::update() {
uint16_t raw_c;
uint16_t raw_r;
uint16_t raw_g;
uint16_t raw_b;
uint8_t data[8]; // Buffer to hold the 8 bytes (2 bytes for each of the 4 channels)
if (this->read_data_register_(TCS34725_REGISTER_CDATAL, raw_c) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
if (this->read_data_register_(TCS34725_REGISTER_RDATAL, raw_r) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
if (this->read_data_register_(TCS34725_REGISTER_GDATAL, raw_g) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
if (this->read_data_register_(TCS34725_REGISTER_BDATAL, raw_b) != i2c::ERROR_OK) {
// Perform burst
if (this->read_register(TCS34725_REGISTER_CRGBDATAL, data, 8) != i2c::ERROR_OK) {
this->status_set_warning();
ESP_LOGW(TAG, "Error reading TCS34725 sensor data");
return;
}
// Extract the data
uint16_t raw_c = encode_uint16(data[1], data[0]); // Clear channel
uint16_t raw_r = encode_uint16(data[3], data[2]); // Red channel
uint16_t raw_g = encode_uint16(data[5], data[4]); // Green channel
uint16_t raw_b = encode_uint16(data[7], data[6]); // Blue channel
ESP_LOGV(TAG, "Raw values clear=%d red=%d green=%d blue=%d", raw_c, raw_r, raw_g, raw_b);
float channel_c;