bit math is hard :(

This commit is contained in:
rusefillc 2022-06-20 21:35:58 -04:00
parent 282e79290d
commit 2ae59f6bd5
2 changed files with 13 additions and 2 deletions

View File

@ -29,7 +29,7 @@ function getBitRange(data, bitIndex, bitWidth)
byteIndex = bitIndex >> 3
shift = bitIndex - byteIndex * 8
value = data[1 + byteIndex]
if (shift + bitIndex > 8) then
if (shift + bitWidth > 8) then
value = value + data[2 + byteIndex] * 256
end
mask = (1 << bitWidth) - 1

View File

@ -35,7 +35,7 @@ end \
byteIndex = bitIndex >> 3 \
shift = bitIndex - byteIndex * 8 \
value = data[1 + byteIndex] \
if (shift + bitIndex > 8) then \
if (shift + bitWidth > 8) then \
value = value + data[2 + byteIndex] * 256 \
end \
mask = (1 << bitWidth) - 1 \
@ -114,6 +114,17 @@ TEST(LuaE65, gearTorque) {
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 800);
}
TEST(LuaE65, gearTorque2) {
const char* realdata = GET_BIT_RANGE R"(
function testFunc()
data = {0x9F, 0x01, 0x32, 0x20, 0x23, 0x30, 0xFF, 0x43}
return getBitRange(data, 0, 16)
end)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x019F);
}
TEST(LuaE65, sumChecksum) {
// checksum is first byte