From 0a0ff11d5a3624f0635ed5b3780a4ab904f9ad7e Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 6 Nov 2024 23:11:58 -0500 Subject: [PATCH] motorolaMagicFromDbc(17, 10) only:new API --- firmware/util/efilib.cpp | 16 ++++++++++++++++ firmware/util/efilib.h | 3 +++ unit_tests/tests/lua/test_lua_dbc.cpp | 16 +++------------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index 3641287eb1..46ba29e62c 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -231,3 +231,19 @@ int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth) { int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth) { return getBitRangeCommon(data, bitIndex, bitWidth, -1); } + +int motorolaMagicFromDbc(int b, int length) { + // https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder + // convert from lsb0 bit numbering to msb0 bit numbering (or msb0 to lsb0) + b = b - (b % 8) + 7 - (b % 8); + // convert from lsbit of signal data to msbit of signal data, when bit numbering is msb0 + b = b + length - 1; + // convert from msbit of signal data to lsbit of signal data, when bit numbering is msb0 + b = b - (b % 8) + 7 - (b % 8); + return b; +} + +int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth) { + int b = motorolaMagicFromDbc(bitIndex, bitWidth); + return getBitRangeMsb(data, b, bitWidth); +} diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index dc6f62315c..6f0d94cb06 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -134,3 +134,6 @@ int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth); for instance DBC 8|16@0 */ int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth); + +int motorolaMagicFromDbc(int b, int length); +int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth); diff --git a/unit_tests/tests/lua/test_lua_dbc.cpp b/unit_tests/tests/lua/test_lua_dbc.cpp index 4ca79311ba..44dde5d2af 100644 --- a/unit_tests/tests/lua/test_lua_dbc.cpp +++ b/unit_tests/tests/lua/test_lua_dbc.cpp @@ -2,17 +2,6 @@ #include "rusefi_lua.h" #include "lua_lib.h" -static int motorolaMagicFromDbc(int b, int length) { - // https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder - // convert from lsb0 bit numbering to msb0 bit numbering (or msb0 to lsb0) - b = b - (b % 8) + 7 - (b % 8); - // convert from lsbit of signal data to msbit of signal data, when bit numbering is msb0 - b = b + length - 1; - // convert from msbit of signal data to lsbit of signal data, when bit numbering is msb0 - b = b - (b % 8) + 7 - (b % 8); - return b; -} - TEST(dbcMotorola, testFromDbc) { ASSERT_EQ(24, motorolaMagicFromDbc(27, 4)); ASSERT_EQ(24, motorolaMagicFromDbc(30, 7)); @@ -29,10 +18,11 @@ TEST(dbcMotorola, testFromIntelIntoMotorolaDbc) { TEST(dbcMotorola, accessRange) { uint8_t data[] = { 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00}; - EXPECT_NEAR_M3(getBitRangeMsb(data, motorolaMagicFromDbc(17, 10), 10), 0x03FF); + EXPECT_NEAR_M3(getBitRangeMoto(data, 17, 10), 0x03FF); EXPECT_NEAR_M3(getBitRangeLsb(data, 16, 2), 3); EXPECT_NEAR_M3(getBitRangeLsb(data, 24, 8), 0xFF); - + // todo: setBitRangeMoto(data, 17, 10, 0x03FF); + // todo: assert data is empty now? }