rusefi/firmware/controllers/lua/examples/vwtp.txt

141 lines
2.6 KiB
Plaintext

-- this controls onCanRx rate as well!
setTickRate(100)
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
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
function arrayToString(arr)
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..toHexString(arr[index])
index = index + 1
end
return str
end
VWTP_OUT = 0x200
VWTP_IN = 0x201
VWTP_TESTER = 0x300
local ecuId = 0
function onCanHello(bus, id, dlc, data)
-- here we handle 201 packets
print('Got Hello Response ' ..arrayToString(data))
ecuId = data[6] * 256 + data[5]
print('From ECU ' ..ecuId)
txCan(1, ecuId, 0, { 0xA0, 0x0F, 0x8A, 0xFF, 0x32, 0xFF })
end
local sendCounter = 2
local packetCounter = 1
local payLoadIndex = 0
local groupId = 2
function onCanTester(bus, id, dlc, data)
-- here we handle 300 packets
-- print('Got from tester ' ..arrayToString(data))
if data[1] == 0xA3 then
-- print ("Keep-alive")
txCan(1, ecuId, 0, { 0xA1, 0x0F, 0x8A, 0xFF, 0x4A, 0xFF })
-- request group 1
reqFirst = 0x10 + sendCounter
print("Requesting group X again with counter " ..sendCounter)
txCan(1, ecuId, 0, { reqFirst, 0x00, 0x02, 0x21, groupId })
sendCounter = sendCounter + 1
if sendCounter == 16 then
sendCounter = 0
end
return
end
if data[1] == 0xA1 then
print ("Happy 300 packet")
txCan(1, ecuId, 0, { 0x10, 0x00, 0x02, 0x10, 0x89 })
return
end
if data[1] == 0xA8 then
print ("They said Bye-Bye")
return
end
if data[1] == 0x10 and dlc == 5 then
ackPacket = 0xB0 + packetCounter
print ("Sending ACK B1 " ..ackPacket)
txCan(1, ecuId, 0, { ackPacket })
-- request group groupId
txCan(1, ecuId, 0, { 0x11, 0x00, 0x02, 0x21, groupId })
return
end
top4 = math.floor(data[1] / 16)
if top4 == 0xB then
-- print("Got ACK")
return
end
if top4 == 2 or top4 == 1 then
print ("Looks like payload index " ..payLoadIndex ..": " ..arrayToString(data))
payLoadIndex = payLoadIndex + 1
packetCounter = packetCounter + 1
if packetCounter > 15 then
packetCounter = 0
end
if top4 == 1 then
ackPacket = 0xB0 + packetCounter
print ("Sending payload ACK " ..ackPacket)
txCan(1, ecuId, 0, { ackPacket })
payLoadIndex = 0
end
return
end
-- if currentState == STATE_GOT_SECOND_RESPONSE then
-- end
print('Got unexpected ' ..arrayToString(data))
end
canRxAdd(VWTP_IN, onCanHello)
canRxAdd(VWTP_TESTER, onCanTester)
txCan(1, VWTP_OUT, 0, { 0x01, 0xC0, 0x00, 0x10, 0x00, 0x03, 0x01 })
function onTick()
end