[kp18058]: fix correct config max values for c_current and rgb_current, fix clang tidy complain about the union.

This commit is contained in:
NewoPL 2024-11-03 08:45:22 +01:00
parent 64e27827ea
commit 7f0b97c317
3 changed files with 46 additions and 51 deletions

View file

@ -19,8 +19,8 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(): cv.declare_id(KP18058), cv.GenerateID(): cv.declare_id(KP18058),
cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_CW_CURRENT, default=5): cv.int_range(min=0, max=31), cv.Optional(CONF_CW_CURRENT, default=8): cv.float_range(min=0, max=77.5),
cv.Optional(CONF_RGB_CURRENT, default=5): cv.int_range(min=0, max=31), cv.Optional(CONF_RGB_CURRENT, default=5): cv.float_range(min=0, max=48),
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)

View file

@ -59,13 +59,13 @@ void KP18058::program_led_driver() {
return true; return true;
}; };
// Create the settings union // Create the settings struct
KP18058_Settings settings{}; KP18058_Settings settings{};
settings.address_identification = 1; settings.address_identification = 1;
settings.working_mode = all_channels_zero() ? STANDBY_MODE : RGBCW_MODE; settings.working_mode = all_channels_zero() ? STANDBY_MODE : RGBCW_MODE;
// Set byte address start. valid values are 0 - 13 // Set byte address start. valid values are 0 - 13
// In this message always all bytes are transmited (starting from 0) // In this message always all bytes are transmitted (starting from 0)
settings.start_byte_address = 0x00; settings.start_byte_address = 0x00;
// Set Line Compensation Mechanism // Set Line Compensation Mechanism
@ -78,7 +78,7 @@ void KP18058::program_led_driver() {
settings.max_current_out4_5 = static_cast<uint8_t>(max_cw_current_ / 2.5) & 0x1F; settings.max_current_out4_5 = static_cast<uint8_t>(max_cw_current_ / 2.5) & 0x1F;
settings.max_current_out1_3 = static_cast<uint8_t>(max_rgb_current_ / 2.5) & 0x1F; settings.max_current_out1_3 = static_cast<uint8_t>(max_rgb_current_ / 2.5) & 0x1F;
// set dimming method for RGB channels and chop dimming frequency // Set dimming method for RGB channels and chop dimming frequency
settings.chop_dimming_out1_3 = ANALOG_DIMMING; settings.chop_dimming_out1_3 = ANALOG_DIMMING;
settings.chop_dimming_frequency = CD_FREQUENCY_500HZ; settings.chop_dimming_frequency = CD_FREQUENCY_500HZ;
@ -90,23 +90,24 @@ void KP18058::program_led_driver() {
} }
// Calculate parity bits for each byte // Calculate parity bits for each byte
for (auto &byte : settings.bytes) { uint8_t *settings_bytes = reinterpret_cast<uint8_t *>(&settings);
for (auto &byte : settings_bytes) {
byte |= get_parity_bit(byte); byte |= get_parity_bit(byte);
} }
// Send the I2C message // Send the I2C message
i2c_.start(); i2c_.start();
for (int i = 0; i < sizeof(KP18058_Settings); i++) { for (size_t i = 0; i < sizeof(KP18058_Settings); i++) {
// on error try to repeat the byte transmission I2C_MAX_RETRY times // On error, try to repeat the byte transmission I2C_MAX_RETRY times
bool write_succeeded; bool write_succeeded;
for (int attempt = 0; attempt < I2C_MAX_RETRY; attempt++) { for (int attempt = 0; attempt < I2C_MAX_RETRY; attempt++) {
write_succeeded = i2c_.write_byte(settings.bytes[i]); write_succeeded = i2c_.write_byte(settings_bytes[i]);
if (write_succeeded) if (write_succeeded)
break; break;
} }
// if all tries failed break and stop sending the rest of the frame bytes // If all tries failed, break and stop sending the rest of the frame bytes
if (!write_succeeded) { if (!write_succeeded) {
ESP_LOGE(TAG, "Failed to write byte %02d (0x%02X) after %d attempts", i, settings.bytes[i], I2C_MAX_RETRY); ESP_LOGE(TAG, "Failed to write byte %02d (0x%02X) after %d attempts", i, settings_bytes[i], I2C_MAX_RETRY);
i2c_ready_ = false; i2c_ready_ = false;
break; break;
} }

View file

@ -92,11 +92,9 @@ enum CDFrequency : uint8_t {
#pragma pack(push, 1) #pragma pack(push, 1)
/** /**
* @brief Union representing the structure of the I2C message for configuring the KP18058 LED driver settings. * @brief the structure of the I2C message for configuring the KP18058 LED driver settings.
*/ */
typedef union { struct KP18058_Settings {
// Access the settings as a structure
struct {
// Byte 0 // Byte 0
uint8_t byte0_parity_bit : 1; uint8_t byte0_parity_bit : 1;
uint8_t start_byte_address : 4; uint8_t start_byte_address : 4;
@ -132,10 +130,6 @@ typedef union {
} channels[5]; } channels[5];
}; };
// Access the settings as a byte array
uint8_t bytes[14];
} KP18058_Settings;
#pragma pack(pop) #pragma pack(pop)
// Ensures that KP18058_Settings size is exactly 14 bytes during compilation // Ensures that KP18058_Settings size is exactly 14 bytes during compilation