rusefi/firmware/controllers/lua/examples/vag-headlight.txt

109 lines
1.9 KiB
Plaintext
Raw Normal View History

2023-10-10 12:54:04 -07:00
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
2024-02-24 17:18:24 -08:00
ID_ATLAS_413_19D = 413
ID_ATLAS_981_3D5 = 981
2023-10-10 12:54:04 -07:00
byte413_3 = 0
byte981_3 = 0
function handle()
if byte413_3 == 0x18 then
print("marking lights")
setPwmDuty(0, 1)
2023-10-10 20:38:49 -07:00
setPwmDuty(1, 0)
2023-10-10 12:54:04 -07:00
setPwmDuty(2, 0)
2023-10-10 20:38:49 -07:00
setPwmDuty(3, 0)
2023-10-10 12:54:04 -07:00
elseif byte981_3 == 0x42 then
setPwmDuty(0, 0)
2023-10-10 20:38:49 -07:00
setPwmDuty(1, 1)
setPwmDuty(2, 0)
setPwmDuty(3, 0)
2023-10-10 12:54:04 -07:00
print(" *** fog lights *** ")
elseif byte981_3 == 0x41 then
print(" *** high beams *** ")
2023-10-10 20:38:49 -07:00
setPwmDuty(0, 0)
2023-10-10 12:54:04 -07:00
setPwmDuty(1, 0)
setPwmDuty(2, 1)
2023-10-10 20:38:49 -07:00
setPwmDuty(3, 0)
2023-10-10 12:54:04 -07:00
elseif byte981_3 == 0x40 then
setPwmDuty(0, 0)
2023-10-10 20:38:49 -07:00
setPwmDuty(1, 0)
2023-10-10 12:54:04 -07:00
setPwmDuty(2, 0)
2023-10-10 20:38:49 -07:00
setPwmDuty(3, 1)
2023-10-10 12:54:04 -07:00
print(" *** low beams *** ")
else
setPwmDuty(0, 0)
setPwmDuty(1, 0)
setPwmDuty(2, 0)
2023-10-10 20:38:49 -07:00
setPwmDuty(3, 0)
2023-10-10 12:54:04 -07:00
print(" *** OFF ")
end
end
2024-02-24 17:18:24 -08:00
function onHeadlight(bus, id, dlc, data)
2023-10-10 12:54:04 -07:00
print('Received ' ..arrayToString(data))
canTimer : reset()
2024-02-24 17:18:24 -08:00
if id == ID_ATLAS_413_19D then
2023-10-10 12:54:04 -07:00
byte413_3 = data[3]
2024-02-24 17:18:24 -08:00
elseif id == ID_ATLAS_981_3D5 then
2023-10-10 12:54:04 -07:00
byte981_3 = data[3]
end
handle()
end
2024-02-24 17:18:24 -08:00
-- Mqb?
canRxAdd(ID_ATLAS_413_19D, onHeadlight)
canRxAdd(ID_ATLAS_981_3D5, onHeadlight)
2023-10-10 12:54:04 -07:00
startPwm(0, 10, 0)
startPwm(1, 10, 0)
startPwm(2, 10, 0)
2023-10-10 20:38:49 -07:00
startPwm(3, 10, 0)
2023-10-10 12:54:04 -07:00
2023-12-12 21:32:04 -08:00
canTimer = Timer.new()
canTimer : reset()
commTimer = Timer.new()
commTimer : reset()
2023-10-10 12:54:04 -07:00
canDelay = 1
commDelay = 20
function onTick()
if getOutput("isUsbConnected") == 1 then
commTimer : reset()
end
if (canTimer : getElapsedSeconds() > canDelay) and (commTimer : getElapsedSeconds() > commDelay) then
2023-10-10 12:54:04 -07:00
mcu_standby()
end
handle()
2023-10-10 12:54:04 -07:00
end