From 5df398ec31f6e710ac87c0fe8d625a767c54c0b9 Mon Sep 17 00:00:00 2001 From: Dan Jackson Date: Sat, 9 Jan 2021 20:53:12 -0800 Subject: [PATCH] Add encode_uint32 method (#1427) --- esphome/components/api/proto.cpp | 3 +-- esphome/components/max31855/max31855.cpp | 2 +- esphome/components/rdm6300/rdm6300.cpp | 3 +-- esphome/components/tuya/tuya.cpp | 3 +-- esphome/components/xiaomi_ble/xiaomi_ble.cpp | 3 +-- esphome/core/helpers.cpp | 4 ++++ esphome/core/helpers.h | 2 ++ 7 files changed, 11 insertions(+), 9 deletions(-) diff --git a/esphome/components/api/proto.cpp b/esphome/components/api/proto.cpp index 3d2f669f54..4bd22af769 100644 --- a/esphome/components/api/proto.cpp +++ b/esphome/components/api/proto.cpp @@ -62,8 +62,7 @@ void ProtoMessage::decode(const uint8_t *buffer, size_t length) { error = true; break; } - uint32_t val = (uint32_t(buffer[i]) << 0) | (uint32_t(buffer[i + 1]) << 8) | (uint32_t(buffer[i + 2]) << 16) | - (uint32_t(buffer[i + 3]) << 24); + uint32_t val = encode_uint32(buffer[i + 3], buffer[i + 2], buffer[i + 1], buffer[i]); if (!this->decode_32bit(field_id, Proto32Bit(val))) { ESP_LOGV(TAG, "Cannot decode 32-bit field %u with value %u!", field_id, val); } diff --git a/esphome/components/max31855/max31855.cpp b/esphome/components/max31855/max31855.cpp index 7a0dc2427c..868cd4f16a 100644 --- a/esphome/components/max31855/max31855.cpp +++ b/esphome/components/max31855/max31855.cpp @@ -41,7 +41,7 @@ void MAX31855Sensor::read_data_() { this->read_array(data, 4); this->disable(); - const uint32_t mem = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3] << 0; + const uint32_t mem = encode_uint32(data[0], data[1], data[2], data[3]); // Verify we got data if (mem != 0xFFFFFFFF) { diff --git a/esphome/components/rdm6300/rdm6300.cpp b/esphome/components/rdm6300/rdm6300.cpp index 6c6f0c0311..2d2ba61824 100644 --- a/esphome/components/rdm6300/rdm6300.cpp +++ b/esphome/components/rdm6300/rdm6300.cpp @@ -46,8 +46,7 @@ void rdm6300::RDM6300Component::loop() { } else { // Valid data this->status_clear_warning(); - const uint32_t result = (uint32_t(this->buffer_[1]) << 24) | (uint32_t(this->buffer_[2]) << 16) | - (uint32_t(this->buffer_[3]) << 8) | this->buffer_[4]; + const uint32_t result = encode_uint32(this->buffer_[1], this->buffer_[2], this->buffer_[3], this->buffer_[4]); bool report = result != last_id_; for (auto *card : this->cards_) { if (card->process(result)) { diff --git a/esphome/components/tuya/tuya.cpp b/esphome/components/tuya/tuya.cpp index feaab9b0ed..2fd4b27937 100644 --- a/esphome/components/tuya/tuya.cpp +++ b/esphome/components/tuya/tuya.cpp @@ -281,8 +281,7 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) { case TuyaDatapointType::INTEGER: if (data_len != 4) return; - datapoint.value_uint = - (uint32_t(data[0]) << 24) | (uint32_t(data[1]) << 16) | (uint32_t(data[2]) << 8) | (uint32_t(data[3]) << 0); + datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]); break; case TuyaDatapointType::ENUM: if (data_len != 1) diff --git a/esphome/components/xiaomi_ble/xiaomi_ble.cpp b/esphome/components/xiaomi_ble/xiaomi_ble.cpp index 202620c06d..75af1e4b7c 100644 --- a/esphome/components/xiaomi_ble/xiaomi_ble.cpp +++ b/esphome/components/xiaomi_ble/xiaomi_ble.cpp @@ -70,8 +70,7 @@ bool parse_xiaomi_value(uint8_t value_type, const uint8_t *data, uint8_t value_l } // idle time since last motion, 4 byte, 32-bit unsigned integer, 1 min else if ((value_type == 0x17) && (value_length == 4)) { - const uint32_t idle_time = - uint32_t(data[0]) | (uint32_t(data[1]) << 8) | (uint32_t(data[2]) << 16) | (uint32_t(data[2]) << 24); + const uint32_t idle_time = encode_uint32(data[3], data[2], data[1], data[0]); result.idle_time = idle_time / 60.0f; result.has_motion = (idle_time) ? false : true; } else { diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 38b80d85fb..f0e0c65f66 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -299,6 +299,10 @@ std::array decode_uint16(uint16_t value) { return {msb, lsb}; } +uint32_t encode_uint32(uint8_t msb, uint8_t byte2, uint8_t byte3, uint8_t lsb) { + return (uint32_t(msb) << 24) | (uint32_t(byte2) << 16) | (uint32_t(byte3) << 8) | uint32_t(lsb); +} + std::string hexencode(const uint8_t *data, uint32_t len) { char buf[20]; std::string res; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 0c660bdc8e..40e53e601e 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -132,6 +132,8 @@ uint32_t reverse_bits_32(uint32_t x); uint16_t encode_uint16(uint8_t msb, uint8_t lsb); /// Decode a 16-bit unsigned integer into an array of two values: most significant byte, least significant byte. std::array decode_uint16(uint16_t value); +/// Encode a 32-bit unsigned integer given four bytes in MSB -> LSB order +uint32_t encode_uint32(uint8_t msb, uint8_t byte2, uint8_t byte3, uint8_t lsb); /*** * An interrupt helper class.