Add encode_uint32 method (#1427)

This commit is contained in:
Dan Jackson 2021-01-09 20:53:12 -08:00 committed by GitHub
parent 699696e8d1
commit 5df398ec31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 9 deletions

View File

@ -62,8 +62,7 @@ void ProtoMessage::decode(const uint8_t *buffer, size_t length) {
error = true; error = true;
break; break;
} }
uint32_t val = (uint32_t(buffer[i]) << 0) | (uint32_t(buffer[i + 1]) << 8) | (uint32_t(buffer[i + 2]) << 16) | uint32_t val = encode_uint32(buffer[i + 3], buffer[i + 2], buffer[i + 1], buffer[i]);
(uint32_t(buffer[i + 3]) << 24);
if (!this->decode_32bit(field_id, Proto32Bit(val))) { if (!this->decode_32bit(field_id, Proto32Bit(val))) {
ESP_LOGV(TAG, "Cannot decode 32-bit field %u with value %u!", field_id, val); ESP_LOGV(TAG, "Cannot decode 32-bit field %u with value %u!", field_id, val);
} }

View File

@ -41,7 +41,7 @@ void MAX31855Sensor::read_data_() {
this->read_array(data, 4); this->read_array(data, 4);
this->disable(); 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 // Verify we got data
if (mem != 0xFFFFFFFF) { if (mem != 0xFFFFFFFF) {

View File

@ -46,8 +46,7 @@ void rdm6300::RDM6300Component::loop() {
} else { } else {
// Valid data // Valid data
this->status_clear_warning(); this->status_clear_warning();
const uint32_t result = (uint32_t(this->buffer_[1]) << 24) | (uint32_t(this->buffer_[2]) << 16) | const uint32_t result = encode_uint32(this->buffer_[1], this->buffer_[2], this->buffer_[3], this->buffer_[4]);
(uint32_t(this->buffer_[3]) << 8) | this->buffer_[4];
bool report = result != last_id_; bool report = result != last_id_;
for (auto *card : this->cards_) { for (auto *card : this->cards_) {
if (card->process(result)) { if (card->process(result)) {

View File

@ -281,8 +281,7 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) {
case TuyaDatapointType::INTEGER: case TuyaDatapointType::INTEGER:
if (data_len != 4) if (data_len != 4)
return; return;
datapoint.value_uint = datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]);
(uint32_t(data[0]) << 24) | (uint32_t(data[1]) << 16) | (uint32_t(data[2]) << 8) | (uint32_t(data[3]) << 0);
break; break;
case TuyaDatapointType::ENUM: case TuyaDatapointType::ENUM:
if (data_len != 1) if (data_len != 1)

View File

@ -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 // idle time since last motion, 4 byte, 32-bit unsigned integer, 1 min
else if ((value_type == 0x17) && (value_length == 4)) { else if ((value_type == 0x17) && (value_length == 4)) {
const uint32_t idle_time = const uint32_t idle_time = encode_uint32(data[3], data[2], data[1], data[0]);
uint32_t(data[0]) | (uint32_t(data[1]) << 8) | (uint32_t(data[2]) << 16) | (uint32_t(data[2]) << 24);
result.idle_time = idle_time / 60.0f; result.idle_time = idle_time / 60.0f;
result.has_motion = (idle_time) ? false : true; result.has_motion = (idle_time) ? false : true;
} else { } else {

View File

@ -299,6 +299,10 @@ std::array<uint8_t, 2> decode_uint16(uint16_t value) {
return {msb, lsb}; 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) { std::string hexencode(const uint8_t *data, uint32_t len) {
char buf[20]; char buf[20];
std::string res; std::string res;

View File

@ -132,6 +132,8 @@ uint32_t reverse_bits_32(uint32_t x);
uint16_t encode_uint16(uint8_t msb, uint8_t lsb); 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. /// Decode a 16-bit unsigned integer into an array of two values: most significant byte, least significant byte.
std::array<uint8_t, 2> decode_uint16(uint16_t value); std::array<uint8_t, 2> 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. * An interrupt helper class.