motorolaMagicFromDbc(17, 10)

only:new API
This commit is contained in:
rusefillc 2024-11-06 23:11:58 -05:00
parent 75c0750bdf
commit 0a0ff11d5a
3 changed files with 22 additions and 13 deletions

View File

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

View File

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

View File

@ -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?
}