1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-01 08:18:16 +00:00

Code style optimizations

This commit is contained in:
Georgi Filipov 2024-12-24 03:25:18 +02:00
parent b8f0b42694
commit 9d5c2b344f

View File

@ -46,10 +46,10 @@ void SamsungClimateIR::send_() {
data->space(SAMSUNG_AIRCON1_HDR_SPACE); data->space(SAMSUNG_AIRCON1_HDR_SPACE);
} }
uint8_t sendByte = protocol_.raw[i]; uint8_t send_byte = protocol_.raw[i];
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
if (sendByte & 0x01) { if (send_byte & 0x01) {
data->mark(SAMSUNG_AIRCON1_BIT_MARK); data->mark(SAMSUNG_AIRCON1_BIT_MARK);
data->space(SAMSUNG_AIRCON1_ONE_SPACE); data->space(SAMSUNG_AIRCON1_ONE_SPACE);
} else { } else {
@ -57,7 +57,7 @@ void SamsungClimateIR::send_() {
data->space(SAMSUNG_AIRCON1_ZERO_SPACE); data->space(SAMSUNG_AIRCON1_ZERO_SPACE);
} }
sendByte >>= 1; send_byte >>= 1;
} }
} }
@ -124,15 +124,15 @@ void SamsungClimateIR::set_temp_(const uint8_t temp) {
/// Change the AC power state. /// Change the AC power state.
/// @param[in] on true, the AC is on. false, the AC is off. /// @param[in] on true, the AC is on. false, the AC is off.
void SamsungClimateIR::set_and_send_power_state_(const bool on) { void SamsungClimateIR::set_and_send_power_state_(const bool on) {
static const uint8_t kOn[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, static const uint8_t K_ON[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0,
0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x01, 0xE2, 0xFE, 0x71, 0x80, 0x11, 0xF0}; 0x01, 0xE2, 0xFE, 0x71, 0x80, 0x11, 0xF0};
static const uint8_t kOff[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0xB2, 0x0F, 0x00, 0x00, 0x00, 0xC0, static const uint8_t K_OFF[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0xB2, 0x0F, 0x00, 0x00, 0x00, 0xC0,
0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0xFF, 0x71, 0x80, 0x11, 0xC0}; 0x01, 0x02, 0xFF, 0x71, 0x80, 0x11, 0xC0};
std::memcpy(protocol_.raw, on ? kOn : kOff, K_SAMSUNG_AC_EXTENDED_STATE_LENGTH); std::memcpy(protocol_.raw, on ? K_ON : K_OFF, K_SAMSUNG_AC_EXTENDED_STATE_LENGTH);
send_(); send_();
@ -177,7 +177,7 @@ uint8_t SamsungClimateIR::calc_section_checksum(const uint8_t *section) {
} }
/// Update the checksum_ for the internal state. /// Update the checksum_ for the internal state.
void SamsungClimateIR::checksum_(void) { void SamsungClimateIR::checksum_() {
uint8_t sectionsum = calc_section_checksum(protocol_.raw); uint8_t sectionsum = calc_section_checksum(protocol_.raw);
protocol_.Sum1Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE); protocol_.Sum1Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE);
protocol_.Sum1Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE); protocol_.Sum1Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE);
@ -199,16 +199,19 @@ uint16_t SamsungClimateIR::count_bits(const uint8_t *const start, const uint16_t
const uint16_t init) { const uint16_t init) {
uint16_t count = init; uint16_t count = init;
for (uint16_t offset = 0; offset < length; offset++) for (uint16_t offset = 0; offset < length; offset++) {
for (uint8_t currentbyte = *(start + offset); currentbyte; currentbyte >>= 1) for (uint8_t currentbyte = *(start + offset); currentbyte; currentbyte >>= 1) {
if (currentbyte & 1) if (currentbyte & 1)
count++; count++;
}
}
if (ones || length == 0) if (ones || length == 0) {
return count; return count;
else } else {
return (length * 8) - count; return (length * 8) - count;
} }
}
/// Count the number of bits of a certain type in an Integer. /// Count the number of bits of a certain type in an Integer.
/// @param[in] data The value you want bits counted for. Starting from the LSB. /// @param[in] data The value you want bits counted for. Starting from the LSB.
@ -218,16 +221,18 @@ uint16_t SamsungClimateIR::count_bits(const uint8_t *const start, const uint16_t
/// @return The nr. of bits found of the given type found in the Integer. /// @return The nr. of bits found of the given type found in the Integer.
uint16_t SamsungClimateIR::count_bits(const uint64_t data, const uint8_t length, const bool ones, const uint16_t init) { uint16_t SamsungClimateIR::count_bits(const uint64_t data, const uint8_t length, const bool ones, const uint16_t init) {
uint16_t count = init; uint16_t count = init;
uint8_t bitsSoFar = length; uint8_t bits_so_far = length;
for (uint64_t remainder = data; remainder && bitsSoFar; remainder >>= 1, bitsSoFar--) for (uint64_t remainder = data; remainder && bits_so_far; remainder >>= 1, bits_so_far--) {
if (remainder & 1) if (remainder & 1)
count++; count++;
}
if (ones || length == 0) if (ones || length == 0) {
return count; return count;
else } else {
return length - count; return length - count;
} }
}
} // namespace climate_ir_samsung } // namespace climate_ir_samsung
} // namespace esphome } // namespace esphome