diff --git a/firmware/config/engines/bmw_n73.cpp b/firmware/config/engines/bmw_n73.cpp index 053bf6ec68..88231f0834 100644 --- a/firmware/config/engines/bmw_n73.cpp +++ b/firmware/config/engines/bmw_n73.cpp @@ -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 diff --git a/unit_tests/tests/lua/test_lua_e65.cpp b/unit_tests/tests/lua/test_lua_e65.cpp index 0ff1023f9c..48fc7d3b60 100644 --- a/unit_tests/tests/lua/test_lua_e65.cpp +++ b/unit_tests/tests/lua/test_lua_e65.cpp @@ -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