diff --git a/firmware/controllers/lua/lua_lib.h b/firmware/controllers/lua/lua_lib.h new file mode 100644 index 0000000000..bb5d90002b --- /dev/null +++ b/firmware/controllers/lua/lua_lib.h @@ -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 \ + " diff --git a/unit_tests/tests/lua/test_lua_e65.cpp b/unit_tests/tests/lua/test_lua_e65.cpp index 2fdefac6f4..bc01b490d5 100644 --- a/unit_tests/tests/lua/test_lua_e65.cpp +++ b/unit_tests/tests/lua/test_lua_e65.cpp @@ -1,5 +1,6 @@ #include "pch.h" #include "rusefi_lua.h" +#include "lua_lib.h" #define BMW_CHECKSUM " function bmwChecksum(canID, data, offset, length) \ checksum = canID \ @@ -11,25 +12,10 @@ return checksum \ 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 \ 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) \ byteIndex = bitIndex >> 3 \ @@ -48,7 +34,7 @@ TEST(LuaE65, Battery) { function testFunc() data = {0xdc, 0x03, 0x00, 0x53, 0xFE, 0xD3, 0x04, 0x00} - return twoBytes(data, 0, 0.0147) + return getTwoBytes(data, 0, 0.0147) end)"; EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 14.5236); @@ -60,7 +46,7 @@ TEST(LuaE65, extractTorqueFromA8) { function testFunc() 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 )"; EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x108 / 2); @@ -72,7 +58,7 @@ TEST(LuaE65, Rpm) { function testFunc() data = {0x5F, 0x59, 0xFF, 0x00, 0x34, 0x0D, 0x80, 0x99} - return twoBytes(data, 4, 0.25) + return getTwoBytes(data, 4, 0.25) end)"; EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 845); diff --git a/unit_tests/tests/lua/test_lua_vag.cpp b/unit_tests/tests/lua/test_lua_vag.cpp index ec465b6ac3..8e7364a087 100644 --- a/unit_tests/tests/lua/test_lua_vag.cpp +++ b/unit_tests/tests/lua/test_lua_vag.cpp @@ -1,5 +1,6 @@ #include "pch.h" #include "rusefi_lua.h" +#include "lua_lib.h" #define VAG_CHECKSUM "function xorChecksum(data) \ 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); } + +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); +} +