From a4931f5d788d24bfeeefb13fb3ead037cb5eaa6e Mon Sep 17 00:00:00 2001 From: Oxan van Leeuwen Date: Thu, 6 Jan 2022 12:54:58 +0100 Subject: [PATCH] Clean-up reverse_bits helpers (#3011) --- .../components/remote_base/midea_protocol.cpp | 4 ++-- esphome/components/ttp229_lsf/ttp229_lsf.cpp | 2 +- esphome/core/helpers.cpp | 15 ------------- esphome/core/helpers.h | 21 +++++++++++++++---- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/esphome/components/remote_base/midea_protocol.cpp b/esphome/components/remote_base/midea_protocol.cpp index baf64f246f..a19a5b50c1 100644 --- a/esphome/components/remote_base/midea_protocol.cpp +++ b/esphome/components/remote_base/midea_protocol.cpp @@ -9,8 +9,8 @@ static const char *const TAG = "remote.midea"; uint8_t MideaData::calc_cs_() const { uint8_t cs = 0; for (const uint8_t *it = this->data(); it != this->data() + OFFSET_CS; ++it) - cs -= reverse_bits_8(*it); - return reverse_bits_8(cs); + cs -= reverse_bits(*it); + return reverse_bits(cs); } bool MideaData::check_compliment(const MideaData &rhs) const { diff --git a/esphome/components/ttp229_lsf/ttp229_lsf.cpp b/esphome/components/ttp229_lsf/ttp229_lsf.cpp index 21c7b02740..773d51b76e 100644 --- a/esphome/components/ttp229_lsf/ttp229_lsf.cpp +++ b/esphome/components/ttp229_lsf/ttp229_lsf.cpp @@ -35,7 +35,7 @@ void TTP229LSFComponent::loop() { } touched = i2c::i2ctohs(touched); this->status_clear_warning(); - touched = reverse_bits_16(touched); + touched = reverse_bits(touched); for (auto *channel : this->channels_) { channel->process(touched); } diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index da5d0915c2..c70aa2d6dd 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -187,17 +187,6 @@ void delay_microseconds_safe(uint32_t us) { // avoids CPU locks that could trig ; } -uint8_t reverse_bits_8(uint8_t x) { - x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1); - x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2); - x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4); - return x; -} - -uint16_t reverse_bits_16(uint16_t x) { - return uint16_t(reverse_bits_8(x & 0xFF) << 8) | uint16_t(reverse_bits_8(x >> 8)); -} - uint32_t fnv1_hash(const std::string &str) { uint32_t hash = 2166136261UL; for (char c : str) { @@ -210,10 +199,6 @@ bool str_equals_case_insensitive(const std::string &a, const std::string &b) { return strcasecmp(a.c_str(), b.c_str()) == 0; } -template uint32_t reverse_bits(uint32_t x) { - return uint32_t(reverse_bits_16(x & 0xFFFF) << 16) | uint32_t(reverse_bits_16(x >> 16)); -} - static int high_freq_num_requests = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) void HighFrequencyLoopRequester::start() { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 2e6a978012..4699e8071c 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -120,10 +120,6 @@ std::string uint64_to_string(uint64_t num); /// Convert a uint32_t to a hex string std::string uint32_to_string(uint32_t num); -uint8_t reverse_bits_8(uint8_t x); -uint16_t reverse_bits_16(uint16_t x); -uint32_t reverse_bits_32(uint32_t x); - /// Convert RGB floats (0-1) to hue (0-360) & saturation/value percentage (0-1) void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value); /// Convert hue (0-360) & saturation/value percentage (0-1) to RGB floats (0-1) @@ -343,6 +339,23 @@ inline std::array decode_value(T val) { return ret; } +/// Reverse the order of 8 bits. +inline uint8_t reverse_bits(uint8_t x) { + x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1); + x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2); + x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4); + return x; +} +/// Reverse the order of 16 bits. +inline uint16_t reverse_bits(uint16_t x) { + return (reverse_bits(static_cast(x & 0xFF)) << 8) | reverse_bits(static_cast((x >> 8) & 0xFF)); +} +/// Reverse the order of 32 bits. +inline uint32_t reverse_bits(uint32_t x) { + return (reverse_bits(static_cast(x & 0xFFFF)) << 16) | + reverse_bits(static_cast((x >> 16) & 0xFFFF)); +} + /// Convert a value between host byte order and big endian (most significant byte first) order. template::value, int> = 0> constexpr T convert_big_endian(T val) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__