Brightness control fixes (#1)

* refactor light value controls method

* fix brightness debug output

* partial fixes for brightness control

* try to fix white brightness

* fix payload debug logging

* fix debug output logging

* fix light_off message

* fix brightness
This commit is contained in:
Dennis George 2025-02-10 09:04:00 -06:00 committed by GitHub
commit d20f78ddfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 106 additions and 57 deletions

View file

@ -2,6 +2,7 @@
#include "esphome/core/log.h"
#include "fastcon_light.h"
#include "fastcon_controller.h"
#include "utils.h"
namespace esphome
{
@ -28,7 +29,7 @@ namespace esphome
light::LightTraits FastconLight::get_traits()
{
auto traits = light::LightTraits();
traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::WHITE, light::ColorMode::BRIGHTNESS});
traits.set_supported_color_modes({light::ColorMode::RGB, light::ColorMode::WHITE, light::ColorMode::BRIGHTNESS, light::ColorMode::COLD_WARM_WHITE});
traits.set_min_mireds(153);
traits.set_max_mireds(500);
return traits;
@ -36,40 +37,31 @@ namespace esphome
void FastconLight::write_state(light::LightState *state)
{
float red = 0.0f, green = 0.0f, blue = 0.0f;
// Get the light data bits from the state
auto light_data = this->controller_->get_light_data(state);
// Get the light state values
float brightness = state->current_values.get_brightness() * 127.0f; // Scale to 0-127
bool is_on = state->current_values.is_on();
auto color_mode = state->current_values.get_color_mode();
if (!is_on)
// Debug output - print the light state values
bool is_on = (light_data[0] & 0x80) != 0;
float brightness = ((light_data[0] & 0x7F) / 127.0f) * 100.0f;
if (light_data.size() == 1)
{
brightness = 0.0f;
ESP_LOGD(TAG, "Writing state: light_id=%d, on=%d, brightness=%.1f%%", light_id_, is_on, brightness);
}
else
{
auto r = light_data[2];
auto g = light_data[3];
auto b = light_data[1];
auto warm = light_data[4];
auto cold = light_data[5];
ESP_LOGD(TAG, "Writing state: light_id=%d, on=%d, brightness=%.1f%%, rgb=(%d,%d,%d), warm=%d, cold=%d", light_id_, is_on, brightness, r, g, b, warm, cold);
}
if (color_mode == light::ColorMode::RGB)
{
state->current_values_as_rgb(&red, &green, &blue);
}
// Convert to protocol values
auto r = static_cast<uint8_t>(red * 255.0f);
auto g = static_cast<uint8_t>(green * 255.0f);
auto b = static_cast<uint8_t>(blue * 255.0f);
ESP_LOGD(TAG, "Writing state: light_id=%d, on=%d, brightness=%.1f%%, rgb=(%d,%d,%d)", light_id_, is_on, (brightness / 127.0f) * 100.0f, r, g, b);
// Get the advertisement data
auto adv_data = this->controller_->get_advertisement(this->light_id_, is_on, brightness, red, green, blue);
// Generate the advertisement payload
auto adv_data = this->controller_->single_control(this->light_id_, light_data);
// Debug output - print payload as hex
char hex_str[adv_data.size() * 2 + 1]; // Each byte needs 2 chars + null terminator
for (size_t i = 0; i < adv_data.size(); i++)
{
sprintf(hex_str + (i * 2), "%02X", adv_data[i]);
}
hex_str[adv_data.size() * 2] = '\0'; // Ensure null termination
auto hex_str = vector_to_hex_string(adv_data).data();
ESP_LOGD(TAG, "Advertisement Payload (%d bytes): %s", adv_data.size(), hex_str);
// Send the advertisement