mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
SML: fix incomplete sign extension for abbreviated transmissions (#5544)
This commit is contained in:
parent
6839de69c1
commit
b0ac729a8e
1 changed files with 8 additions and 17 deletions
|
@ -88,11 +88,6 @@ uint64_t bytes_to_uint(const bytes &buffer) {
|
|||
for (auto const value : buffer) {
|
||||
val = (val << 8) + value;
|
||||
}
|
||||
// Some smart meters send 24 bit signed integers. Sign extend to 64 bit if the
|
||||
// 24 bit value is negative.
|
||||
if (buffer.size() == 3 && buffer[0] & 0x80) {
|
||||
val |= 0xFFFFFFFFFF000000;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
@ -100,19 +95,15 @@ int64_t bytes_to_int(const bytes &buffer) {
|
|||
uint64_t tmp = bytes_to_uint(buffer);
|
||||
int64_t val;
|
||||
|
||||
switch (buffer.size()) {
|
||||
case 1: // int8
|
||||
val = (int8_t) tmp;
|
||||
break;
|
||||
case 2: // int16
|
||||
val = (int16_t) tmp;
|
||||
break;
|
||||
case 4: // int32
|
||||
val = (int32_t) tmp;
|
||||
break;
|
||||
default: // int64
|
||||
val = (int64_t) tmp;
|
||||
// sign extension for abbreviations of leading ones (e.g. 3 byte transmissions, see 6.2.2 of SML protocol definition)
|
||||
// see https://stackoverflow.com/questions/42534749/signed-extension-from-24-bit-to-32-bit-in-c
|
||||
if (buffer.size() < 8) {
|
||||
const int bits = buffer.size() * 8;
|
||||
const uint64_t m = 1u << (bits - 1);
|
||||
tmp = (tmp ^ m) - m;
|
||||
}
|
||||
|
||||
val = (int64_t) tmp;
|
||||
return val;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue