Add encode_uint32 method (#1427)

This commit is contained in:
Dan Jackson 2021-01-09 20:53:12 -08:00 committed by Jesse Hills
parent dcd3d2084d
commit c67539cf5b
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
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;
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);
}

View file

@ -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) {

View file

@ -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)) {

View file

@ -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)

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
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 {

View file

@ -299,6 +299,10 @@ std::array<uint8_t, 2> 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;

View file

@ -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<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.