2021-12-24 13:56:17 -08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "rusefi_lua.h"
|
2022-06-23 13:02:09 -07:00
|
|
|
#include "lua_lib.h"
|
2021-12-24 13:56:17 -08:00
|
|
|
|
2022-02-22 12:33:25 -08:00
|
|
|
#define BMW_CHECKSUM " function bmwChecksum(canID, data, offset, length) \
|
|
|
|
checksum = canID \
|
|
|
|
for i = offset, offset + length - 1,1 \
|
|
|
|
do \
|
|
|
|
checksum = checksum + data[i] \
|
|
|
|
end \
|
|
|
|
checksum = (checksum >> 8) + (checksum & 0xff) \
|
|
|
|
return checksum \
|
|
|
|
end "
|
|
|
|
|
2021-12-24 14:35:27 -08:00
|
|
|
// https://github.com/HeinrichG-V12/E65_ReverseEngineering/blob/main/docs/0x3B4.md
|
2021-12-24 13:56:17 -08:00
|
|
|
TEST(LuaE65, Battery) {
|
2022-07-28 09:08:35 -07:00
|
|
|
const char* realdata = TWO_BYTES_LSB R"(
|
2021-12-24 13:56:17 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0xdc, 0x03, 0x00, 0x53, 0xFE, 0xD3, 0x04, 0x00}
|
2022-07-28 09:08:35 -07:00
|
|
|
return getTwoBytesLSB(data, 0, 0.0147)
|
2021-12-24 14:35:27 -08:00
|
|
|
end)";
|
2021-12-24 13:56:17 -08:00
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 14.5236);
|
2021-12-24 14:35:27 -08:00
|
|
|
}
|
2021-12-24 13:56:17 -08:00
|
|
|
|
2022-07-28 09:08:35 -07:00
|
|
|
TEST(LuaE65, testTwoBytes) {
|
|
|
|
const char* realdata = TWO_BYTES_LSB R"(
|
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0xdc, 0x03}
|
|
|
|
return getTwoBytesLSB(data, 0, 1)
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x03dc);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:50:17 -08:00
|
|
|
// https://github.com/HeinrichG-V12/E65_ReverseEngineering/blob/main/docs/0x0A8.md
|
|
|
|
TEST(LuaE65, extractTorqueFromA8) {
|
2022-07-28 09:08:35 -07:00
|
|
|
const char* realdata = TWO_BYTES_LSB R"(
|
2021-12-28 21:50:17 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = { 0x42, 0x89, 0x10, 0x80, 0x10, 0x0F, 0x00, 0x60 }
|
2022-07-28 09:08:35 -07:00
|
|
|
return 0.5 * (getTwoBytesLSB(data, 1, 1) >> 4)
|
2021-12-28 21:50:17 -08:00
|
|
|
end
|
|
|
|
)";
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x108 / 2);
|
|
|
|
}
|
|
|
|
|
2021-12-24 14:35:27 -08:00
|
|
|
// http://loopybunny.co.uk/CarPC/can/0AA.html
|
|
|
|
TEST(LuaE65, Rpm) {
|
2022-07-28 09:08:35 -07:00
|
|
|
const char* realdata = TWO_BYTES_LSB R"(
|
2021-12-24 14:35:27 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0x5F, 0x59, 0xFF, 0x00, 0x34, 0x0D, 0x80, 0x99}
|
2022-07-28 09:08:35 -07:00
|
|
|
return getTwoBytesLSB(data, 4, 0.25)
|
2021-12-24 14:35:27 -08:00
|
|
|
end)";
|
2021-12-24 13:56:17 -08:00
|
|
|
|
2021-12-24 14:35:27 -08:00
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 845);
|
2021-12-24 13:56:17 -08:00
|
|
|
}
|
2021-12-26 11:48:00 -08:00
|
|
|
|
2022-02-19 17:38:08 -08:00
|
|
|
TEST(LuaE65, gear) {
|
2022-07-28 09:08:35 -07:00
|
|
|
const char* realdata = R"(
|
2022-02-19 17:38:08 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0x58}
|
|
|
|
return data[1] & 0xF
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 8);
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:13:32 -08:00
|
|
|
TEST(LuaE65, repackAA) {
|
2022-07-28 09:08:35 -07:00
|
|
|
const char* realdata = ARRAY_EQUALS R"(
|
2022-02-22 20:13:32 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
rpm = 673.75
|
|
|
|
pedal = 50
|
|
|
|
data = {0x58, 12, 14}
|
|
|
|
expected = {0x58, 12, 14}
|
|
|
|
return equals(data, expected)
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0);
|
|
|
|
}
|
|
|
|
|
2022-02-21 08:26:57 -08:00
|
|
|
TEST(LuaE65, gearTorque) {
|
2022-08-25 07:45:26 -07:00
|
|
|
const char* realdata = GET_BIT_RANGE_LSB R"(
|
2022-02-21 08:26:57 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0x9F, 0x01, 0x32, 0x20, 0x23, 0x30, 0xFF, 0x43}
|
|
|
|
return getBitRange(data, 12, 12)
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 800);
|
|
|
|
}
|
|
|
|
|
2022-06-20 18:35:58 -07:00
|
|
|
TEST(LuaE65, gearTorque2) {
|
2022-08-25 07:45:26 -07:00
|
|
|
const char* realdata = GET_BIT_RANGE_LSB R"(
|
2022-06-20 18:35:58 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-20 18:51:08 -07:00
|
|
|
TEST(LuaE65, gearTorque3) {
|
2022-08-25 07:45:26 -07:00
|
|
|
const char* realdata = GET_BIT_RANGE_LSB R"(
|
2022-06-20 18:51:08 -07:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
data = {0x9F, 0xDF, 0x32, 0x20, 0x23, 0x30, 0xFF, 0x43}
|
|
|
|
return getBitRange(data, 0, 16)
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0xDF9F);
|
|
|
|
}
|
|
|
|
|
2022-02-21 08:26:57 -08:00
|
|
|
|
2021-12-26 11:48:00 -08:00
|
|
|
TEST(LuaE65, sumChecksum) {
|
|
|
|
// checksum is first byte
|
|
|
|
// id = A8, packet:
|
|
|
|
// 12 AD 05 A0 05 0F 00 02
|
|
|
|
|
2022-02-22 12:33:25 -08:00
|
|
|
const char* realdata = BMW_CHECKSUM R"(
|
2021-12-27 18:09:28 -08:00
|
|
|
|
2021-12-26 11:48:00 -08:00
|
|
|
function testFunc()
|
2021-12-27 18:09:28 -08:00
|
|
|
canID = 0xA8
|
2021-12-26 11:48:00 -08:00
|
|
|
data = { 0xAD, 0x05, 0xA0, 0x05, 0x0F, 0x00, 0x02 }
|
2021-12-27 18:09:28 -08:00
|
|
|
checksum = bmwChecksum(canID, data, 1, 7)
|
2021-12-26 11:48:00 -08:00
|
|
|
return checksum;
|
|
|
|
end)";
|
|
|
|
|
|
|
|
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x12);
|
|
|
|
}
|