rusefi/firmware/controllers/lua/lua_lib.h

186 lines
5.0 KiB
C
Raw Normal View History

2022-08-25 07:45:26 -07:00
/**
* file lua_lib.h
2022-11-22 12:20:33 -08:00
* if you like any of those you would have to copy paste into your script manually - those
* are NOT part of the default anything automatically
* please remove slash from the end of each line
2022-08-25 07:45:26 -07:00
*/
2022-06-23 13:02:09 -07:00
#define ARRAY_EQUALS "function equals(data1, data2) \
\
local index = 1 \
2022-09-06 12:12:43 -07:00
if data1 == nil then \
return -666 \
end \
2022-06-23 13:02:09 -07:00
while data1[index] ~= nil do \
2022-08-26 09:07:13 -07:00
if math.floor(data1[index]) ~= math.floor(data2[index]) then \
2022-06-23 13:02:09 -07:00
return -1 - index \
end \
index = index + 1 \
end \
if nil ~= data2[index] then \
return -1 - index \
end \
return 0 \
end \
"
2022-09-05 17:02:33 -07:00
#define LUA_POW " \
function pow(x, power) \
local result = x \
for i = 2, power, 1 \
do \
result = result * x \
end \
return result \
end \
"
2022-08-25 21:04:47 -07:00
#define PRINT_ARRAY "hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \"A\", \"B\", \"C\", \"D\", \"E\", \"F\" } \
\
2022-08-25 21:14:52 -07:00
function toHexString(num) \
if num == 0 then \
return '0' \
end \
\
local result = \"\" \
while num > 0 do \
local n = num % 16 \
result = hexstr[n + 1] ..result \
num = math.floor(num / 16) \
end \
return result \
end \
\
2022-08-25 21:16:36 -07:00
function arrayToString(arr) \
local str = \"\" \
local index = 1 \
while arr[index] ~= nil do \
2022-08-26 09:07:13 -07:00
str = str..\" \"..toHexString(math.floor(arr[index])) \
2023-07-09 16:33:58 -07:00
index = index + 1\
2023-07-09 18:29:34 -07:00
end \
return str \
end \
\
2022-08-25 21:14:52 -07:00
\
2022-08-25 21:04:47 -07:00
"
// LSB (Least Significant Byte comes first) "Intel"
2023-07-09 16:33:58 -07:00
#define TWO_BYTES_LSB "function getTwoBytesLSB(data, offset, factor)\
2023-07-30 21:36:20 -07:00
return (data[offset + 2] * 256 + data[offset + 1]) * factor \n\
2023-07-30 21:47:06 -07:00
end\n\
2023-07-30 21:36:20 -07:00
\
2023-07-09 16:33:58 -07:00
"
2022-06-23 13:02:09 -07:00
// Little-endian System, "Intel"
2023-07-09 16:33:58 -07:00
#define SET_TWO_BYTES_LSB " function setTwoBytesLsb(data, offset, value) \
value = math.floor(value)\
data[offset + 2] = value >> 8\
data[offset + 1] = value & 0xff\
2022-06-23 13:02:09 -07:00
end \
2023-07-09 16:33:58 -07:00
"
// MOTOROLA order, MSB (Most Significant Byte/Big Endian) comes first.
#define TWO_BYTES_MSB "function getTwoBytesMSB(data, offset, factor) \
return (data[offset + 1] * 256 + data[offset + 2]) * factor \
2022-09-13 21:03:38 -07:00
end \
"
#define SET_TWO_BYTES_MSB " function setTwoBytesMsb(data, offset, value) \
value = math.floor(value) \
data[offset + 1] = value >> 8 \
data[offset + 2] = value & 0xff \
2023-07-09 16:33:58 -07:00
end\
"
2022-08-25 07:45:26 -07:00
// one day we shall get Preprocessor macros with C++11 raw string literals
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55971
2022-11-22 11:00:06 -08:00
// for when you want "I want bitWidth number of bits starting at bitIndex in data array
2022-08-25 07:45:26 -07:00
#define GET_BIT_RANGE_LSB " \
2023-07-30 21:36:20 -07:00
function getBitRange(data, bitIndex, bitWidth) \n\
2024-02-12 20:15:56 -08:00
local byteIndex = bitIndex >> 3 \n\
local shift = bitIndex - byteIndex * 8 \n\
local value = data[1 + byteIndex] \n\
2023-07-30 21:36:20 -07:00
if (shift + bitWidth > 8) then \n\
2022-08-25 07:45:26 -07:00
value = value + data[2 + byteIndex] * 256 \
2023-07-30 21:36:20 -07:00
end \n\
2024-02-12 20:15:56 -08:00
local mask = (1 << bitWidth) - 1 \n\
2023-07-30 21:36:20 -07:00
return (value >> shift) & mask \n\
end \n\
2022-08-25 07:45:26 -07:00
"
2024-02-15 13:30:56 -08:00
2024-02-16 12:21:13 -08:00
// Motorola big-endian
2023-11-14 21:28:43 -08:00
#define GET_BIT_RANGE_MSB " \
function getBitRangeMsb(data, bitIndex, bitWidth) \n\
2024-02-12 20:15:56 -08:00
local byteIndex = bitIndex >> 3 \n\
local shift = bitIndex - byteIndex * 8 \n\
local value = data[1 + byteIndex] \n\
2023-11-14 21:28:43 -08:00
if (shift + bitWidth > 8) then \n\
value = value + data[0 + byteIndex] * 256 \
end \n\
2024-02-12 20:15:56 -08:00
local mask = (1 << bitWidth) - 1 \n\
2023-11-14 21:28:43 -08:00
return (value >> shift) & mask \n\
end \n\
"
2022-09-06 12:12:43 -07:00
#define SET_BIT_RANGE_LSB " \
function setBitRange(data, totalBitIndex, bitWidth, value) \
local byteIndex = totalBitIndex >> 3 \
local bitInByteIndex = totalBitIndex - byteIndex * 8 \
if (bitInByteIndex + bitWidth > 8) then \
2024-02-12 20:15:56 -08:00
local bitsToHandleNow = 8 - bitInByteIndex \
2024-02-15 13:14:54 -08:00
setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
bitWidth = bitsToHandleNow \
end \
local mask = (1 << bitWidth) - 1 \
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) \
local maskedValue = value & mask \
local shiftedValue = maskedValue << bitInByteIndex \
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue \
end \n\
"
#define SET_BIT_RANGE_MSB " \
function setBitRangeMsb(data, totalBitIndex, bitWidth, value) \
local byteIndex = totalBitIndex >> 3 \
local bitInByteIndex = totalBitIndex - byteIndex * 8 \
if (bitInByteIndex + bitWidth > 8) then \
local bitsToHandleNow = 8 - bitInByteIndex \
2024-02-15 13:41:04 -08:00
setBitRangeMsb(data, totalBitIndex - bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
2022-09-07 12:36:50 -07:00
bitWidth = bitsToHandleNow \
2022-09-06 12:12:43 -07:00
end \
2024-02-12 20:15:56 -08:00
local mask = (1 << bitWidth) - 1 \
2022-09-06 12:12:43 -07:00
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) \
2024-02-12 20:15:56 -08:00
local maskedValue = value & mask \
local shiftedValue = maskedValue << bitInByteIndex \
2022-09-06 12:12:43 -07:00
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue \
2023-07-30 21:36:20 -07:00
end \n\
2022-09-06 12:12:43 -07:00
"
2023-07-07 20:25:13 -07:00
#define HYUNDAI_SUM_NIBBLES "\
2023-07-30 21:36:20 -07:00
function hyundaiSumNibbles(data, seed) \n\
2023-07-07 21:35:21 -07:00
local sum = seed \n\
for i = 1, 7, 1 \n\
do \n\
2024-02-12 20:15:56 -08:00
local b = data[i] \n\
2023-07-07 20:25:13 -07:00
sum = sum + (b % 16) + math.floor(b / 16) \
end \
return (16 - sum) % 16 \
2023-07-09 16:33:58 -07:00
end\
2023-07-07 20:25:13 -07:00
"
2022-08-25 07:45:26 -07:00
2023-09-21 17:01:18 -07:00
// XOR of the array, skipping target index
#define VAG_CHECKSUM " \
function xorChecksum(data, targetIndex) \
local index = 1 \
local result = 0 \
while data[index] ~= nil do \
if index ~= targetIndex then \
result = result ~ data[index] \
end \
index = index + 1 \
end \
data[targetIndex] = result \
return result \
end \
"