mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 23:18:10 +01:00
Clean-up reverse_bits helpers (#3011)
This commit is contained in:
parent
5e1e543b06
commit
a4931f5d78
4 changed files with 20 additions and 22 deletions
|
@ -9,8 +9,8 @@ static const char *const TAG = "remote.midea";
|
||||||
uint8_t MideaData::calc_cs_() const {
|
uint8_t MideaData::calc_cs_() const {
|
||||||
uint8_t cs = 0;
|
uint8_t cs = 0;
|
||||||
for (const uint8_t *it = this->data(); it != this->data() + OFFSET_CS; ++it)
|
for (const uint8_t *it = this->data(); it != this->data() + OFFSET_CS; ++it)
|
||||||
cs -= reverse_bits_8(*it);
|
cs -= reverse_bits(*it);
|
||||||
return reverse_bits_8(cs);
|
return reverse_bits(cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MideaData::check_compliment(const MideaData &rhs) const {
|
bool MideaData::check_compliment(const MideaData &rhs) const {
|
||||||
|
|
|
@ -35,7 +35,7 @@ void TTP229LSFComponent::loop() {
|
||||||
}
|
}
|
||||||
touched = i2c::i2ctohs(touched);
|
touched = i2c::i2ctohs(touched);
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
touched = reverse_bits_16(touched);
|
touched = reverse_bits(touched);
|
||||||
for (auto *channel : this->channels_) {
|
for (auto *channel : this->channels_) {
|
||||||
channel->process(touched);
|
channel->process(touched);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 fnv1_hash(const std::string &str) {
|
||||||
uint32_t hash = 2166136261UL;
|
uint32_t hash = 2166136261UL;
|
||||||
for (char c : str) {
|
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;
|
return strcasecmp(a.c_str(), b.c_str()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<uint32_t> 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)
|
static int high_freq_num_requests = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void HighFrequencyLoopRequester::start() {
|
void HighFrequencyLoopRequester::start() {
|
||||||
|
|
|
@ -120,10 +120,6 @@ std::string uint64_to_string(uint64_t num);
|
||||||
/// Convert a uint32_t to a hex string
|
/// Convert a uint32_t to a hex string
|
||||||
std::string uint32_to_string(uint32_t num);
|
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)
|
/// 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);
|
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)
|
/// Convert hue (0-360) & saturation/value percentage (0-1) to RGB floats (0-1)
|
||||||
|
@ -343,6 +339,23 @@ inline std::array<uint8_t, sizeof(T)> decode_value(T val) {
|
||||||
return ret;
|
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<uint8_t>(x & 0xFF)) << 8) | reverse_bits(static_cast<uint8_t>((x >> 8) & 0xFF));
|
||||||
|
}
|
||||||
|
/// Reverse the order of 32 bits.
|
||||||
|
inline uint32_t reverse_bits(uint32_t x) {
|
||||||
|
return (reverse_bits(static_cast<uint16_t>(x & 0xFFFF)) << 16) |
|
||||||
|
reverse_bits(static_cast<uint16_t>((x >> 16) & 0xFFFF));
|
||||||
|
}
|
||||||
|
|
||||||
/// Convert a value between host byte order and big endian (most significant byte first) order.
|
/// Convert a value between host byte order and big endian (most significant byte first) order.
|
||||||
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T convert_big_endian(T val) {
|
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T convert_big_endian(T val) {
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
|
Loading…
Reference in a new issue