rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
controllers
lua
lua_lib.h
Go to the documentation of this file.
1
/**
2
* file lua_lib.h
3
* if you like any of those you would have to copy paste into your script manually - those
4
* are NOT part of the default anything automatically
5
* please remove slash from the end of each line
6
*/
7
8
#define ARRAY_EQUALS "function equals(data1, data2) \
9
\
10
local index = 1 \
11
if data1 == nil then \
12
return -666 \
13
end \
14
while data1[index] ~= nil do \
15
if math.floor(data1[index]) ~= math.floor(data2[index]) then \
16
return -1 - index \
17
end \
18
index = index + 1 \
19
end \
20
if nil ~= data2[index] then \
21
return -1 - index \
22
end \
23
return 0 \
24
end \
25
"
26
27
#define LUA_POW " \
28
function pow(x, power) \
29
local result = x \
30
for i = 2, power, 1 \
31
do \
32
result = result * x \
33
end \
34
return result \
35
end \
36
"
37
38
#define PRINT_ARRAY "hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \"A\", \"B\", \"C\", \"D\", \"E\", \"F\" } \
39
\
40
function toHexString(num) \
41
if num == 0 then \
42
return '0' \
43
end \
44
\
45
local result = \"\" \
46
while num > 0 do \
47
local n = num % 16 \
48
result = hexstr[n + 1] ..result \
49
num = math.floor(num / 16) \
50
end \
51
return result \
52
end \
53
\
54
function arrayToString(arr) \
55
local str = \"\" \
56
local index = 1 \
57
while arr[index] ~= nil do \
58
str = str..\" \"..toHexString(math.floor(arr[index])) \
59
index = index + 1\
60
end \
61
return str \
62
end \
63
\
64
\
65
"
66
67
// LSB (Least Significant Byte comes first) "Intel"
68
// see also getTwoBytesLsb
69
#define TWO_BYTES_LSB "function getTwoBytesLSB(data, offset, factor)\
70
return (data[offset + 2] * 256 + data[offset + 1]) * factor \n\
71
end\n\
72
\
73
"
74
75
// Little-endian System, "Intel"
76
#define SET_TWO_BYTES_LSB " function setTwoBytesLsb(data, offset, value) \
77
value = math.floor(value)\
78
data[offset + 2] = value >> 8\
79
data[offset + 1] = value & 0xff\
80
end \
81
"
82
83
// MOTOROLA order, MSB (Most Significant Byte/Big Endian) comes first.
84
// see also getTwoBytesMsb
85
#define TWO_BYTES_MSB "function getTwoBytesMSB(data, offset, factor) \
86
return (data[offset + 1] * 256 + data[offset + 2]) * factor \
87
end \
88
"
89
90
// see also CanTxMessage#setShortValueMsb
91
#define SET_TWO_BYTES_MSB " function setTwoBytesMsb(data, offset, value) \
92
value = math.floor(value) \
93
data[offset + 1] = value >> 8 \
94
data[offset + 2] = value & 0xff \
95
end\
96
"
97
98
// one day we shall get Preprocessor macros with C++11 raw string literals
99
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55971
100
// for when you want "I want bitWidth number of bits starting at bitIndex in data array
101
#define GET_BIT_RANGE_LSB " \
102
function getBitRange(data, bitIndex, bitWidth) \n\
103
local byteIndex = bitIndex >> 3 \n\
104
local shift = bitIndex - byteIndex * 8 \n\
105
local value = data[1 + byteIndex] \n\
106
if (shift + bitWidth > 8) then \n\
107
value = value + data[2 + byteIndex] * 256 \
108
end \n\
109
local mask = (1 << bitWidth) - 1 \n\
110
return (value >> shift) & mask \n\
111
end \n\
112
"
113
114
// Motorola big-endian
115
#define GET_BIT_RANGE_MSB " \
116
function getBitRangeMsb(data, bitIndex, bitWidth) \n\
117
local byteIndex = bitIndex >> 3 \n\
118
local shift = bitIndex - byteIndex * 8 \n\
119
local value = data[1 + byteIndex] \n\
120
if (shift + bitWidth > 8) then \n\
121
value = value + data[0 + byteIndex] * 256 \
122
end \n\
123
local mask = (1 << bitWidth) - 1 \n\
124
return (value >> shift) & mask \n\
125
end \n\
126
"
127
128
#define SET_BIT_RANGE_LSB " \
129
function setBitRange(data, totalBitIndex, bitWidth, value) \
130
local byteIndex = totalBitIndex >> 3 \
131
local bitInByteIndex = totalBitIndex - byteIndex * 8 \
132
if (bitInByteIndex + bitWidth > 8) then \
133
local bitsToHandleNow = 8 - bitInByteIndex \
134
setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
135
bitWidth = bitsToHandleNow \
136
end \
137
local mask = (1 << bitWidth) - 1 \
138
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) \
139
local maskedValue = value & mask \
140
local shiftedValue = maskedValue << bitInByteIndex \
141
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue \
142
end \n\
143
"
144
145
#define SET_BIT_RANGE_MSB " \
146
function setBitRangeMsb(data, totalBitIndex, bitWidth, value) \
147
local byteIndex = totalBitIndex >> 3 \
148
local bitInByteIndex = totalBitIndex - byteIndex * 8 \
149
if (bitInByteIndex + bitWidth > 8) then \
150
local bitsToHandleNow = 8 - bitInByteIndex \
151
setBitRangeMsb(data, (byteIndex - 1) * 8, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
152
bitWidth = bitsToHandleNow \
153
end \
154
local mask = (1 << bitWidth) - 1 \
155
data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) \
156
local maskedValue = value & mask \
157
local shiftedValue = maskedValue << bitInByteIndex \
158
data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue \
159
end \n\
160
"
161
162
#define HYUNDAI_SUM_NIBBLES "\
163
function hyundaiSumNibbles(data, seed) \n\
164
local sum = seed \n\
165
for i = 1, 7, 1 \n\
166
do \n\
167
local b = data[i] \n\
168
sum = sum + (b % 16) + math.floor(b / 16) \
169
end \
170
return (16 - sum) % 16 \
171
end\
172
"
173
174
// XOR of the array, skipping target index
175
#define VAG_CHECKSUM " \
176
function xorChecksum(data, targetIndex) \
177
local index = 1 \
178
local result = 0 \
179
while data[index] ~= nil do \
180
if index ~= targetIndex then \
181
result = result ~ data[index] \
182
end \
183
index = index + 1 \
184
end \
185
data[targetIndex] = result \
186
return result \
187
end \
188
"
Generated on Sat Sep 27 2025 00:10:06 for rusEFI by
1.9.8