VAG Lua progress

This commit is contained in:
Andrey 2022-06-23 16:02:09 -04:00
parent 8f7a3aa6ba
commit edd4dcb004
3 changed files with 48 additions and 19 deletions

View File

@ -0,0 +1,23 @@
#define ARRAY_EQUALS "function equals(data1, data2) \
\
local index = 1 \
while data1[index] ~= nil do \
if data1[index] ~= data1[index] then \
return -1 - index \
end \
index = index + 1 \
end \
if nil ~= data2[index] then \
return -1 - index \
end \
return 0 \
end \
"
#define SET_TWO_BYTES " function setTwoBytes(data, offset, value) \
value = math.floor(value) \
data[offset + 2] = value >> 8 \
data[offset + 1] = value & 0xff \
end \
"

View File

@ -1,5 +1,6 @@
#include "pch.h" #include "pch.h"
#include "rusefi_lua.h" #include "rusefi_lua.h"
#include "lua_lib.h"
#define BMW_CHECKSUM " function bmwChecksum(canID, data, offset, length) \ #define BMW_CHECKSUM " function bmwChecksum(canID, data, offset, length) \
checksum = canID \ checksum = canID \
@ -11,25 +12,10 @@
return checksum \ return checksum \
end " end "
#define TWO_BYTES "function twoBytes(data, offset, factor) \ #define TWO_BYTES "function getTwoBytes(data, offset, factor) \
return (data[offset + 2] * 256 + data[offset + 1]) * factor \ return (data[offset + 2] * 256 + data[offset + 1]) * factor \
end" end"
#define ARRAY_EQUALS "function equals(data1, data2) \
\
local index = 1 \
while data1[index] ~= nil do \
if data1[index] ~= data1[index] then \
return -1 - index \
end \
index = index + 1 \
end \
if nil ~= data2[index] then \
return -1 - index \
end \
return 0 \
end \
"
#define GET_BIT_RANGE "function getBitRange(data, bitIndex, bitWidth) \ #define GET_BIT_RANGE "function getBitRange(data, bitIndex, bitWidth) \
byteIndex = bitIndex >> 3 \ byteIndex = bitIndex >> 3 \
@ -48,7 +34,7 @@ TEST(LuaE65, Battery) {
function testFunc() function testFunc()
data = {0xdc, 0x03, 0x00, 0x53, 0xFE, 0xD3, 0x04, 0x00} data = {0xdc, 0x03, 0x00, 0x53, 0xFE, 0xD3, 0x04, 0x00}
return twoBytes(data, 0, 0.0147) return getTwoBytes(data, 0, 0.0147)
end)"; end)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 14.5236); EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 14.5236);
@ -60,7 +46,7 @@ TEST(LuaE65, extractTorqueFromA8) {
function testFunc() function testFunc()
data = { 0x42, 0x89, 0x10, 0x80, 0x10, 0x0F, 0x00, 0x60 } data = { 0x42, 0x89, 0x10, 0x80, 0x10, 0x0F, 0x00, 0x60 }
return 0.5 * (twoBytes(data, 1, 1) >> 4) return 0.5 * (getTwoBytes(data, 1, 1) >> 4)
end end
)"; )";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x108 / 2); EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x108 / 2);
@ -72,7 +58,7 @@ TEST(LuaE65, Rpm) {
function testFunc() function testFunc()
data = {0x5F, 0x59, 0xFF, 0x00, 0x34, 0x0D, 0x80, 0x99} data = {0x5F, 0x59, 0xFF, 0x00, 0x34, 0x0D, 0x80, 0x99}
return twoBytes(data, 4, 0.25) return getTwoBytes(data, 4, 0.25)
end)"; end)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 845); EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 845);

View File

@ -1,5 +1,6 @@
#include "pch.h" #include "pch.h"
#include "rusefi_lua.h" #include "rusefi_lua.h"
#include "lua_lib.h"
#define VAG_CHECKSUM "function xorChecksum(data) \ #define VAG_CHECKSUM "function xorChecksum(data) \
return data[1] ~ data[2] ~ data[3] ~ data[4] ~ data[5] ~ data[6] ~ data[7] \ return data[1] ~ data[2] ~ data[3] ~ data[4] ~ data[5] ~ data[6] ~ data[7] \
@ -17,3 +18,22 @@ TEST(LuaVag, Checksum) {
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x60); EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x60);
} }
TEST(LuaVag, packMotor1) {
const char* realdata = ARRAY_EQUALS SET_TWO_BYTES R"(
function testFunc()
rpm = 1207.1
data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
setTwoBytes(data, 2, 4 * rpm)
expected = { 0x00, 0x00, 0xDF, 0x12, 0x00, 0x00, 0x00, 0x00 }
-- print(data)
return equals(data, expected)
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0);
}