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
|
|
|
|
|
|
|
|
ID_413_19D = 413
|
|
|
|
ID_981_3D5 = 981
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
function onMqbHeadlight(bus, id, dlc, data)
|
|
|
|
print('Received ' ..arrayToString(data))
|
|
|
|
|
|
|
|
canTimer : reset()
|
|
|
|
|
|
|
|
if id == ID_413_19D then
|
|
|
|
byte413_3 = data[3]
|
|
|
|
elseif id == ID_981_3D5 then
|
|
|
|
byte981_3 = data[3]
|
|
|
|
end
|
|
|
|
handle()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
canRxAdd(ID_413_19D, onMqbHeadlight)
|
|
|
|
canRxAdd(ID_981_3D5, onMqbHeadlight)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2023-10-17 19:15:36 -07:00
|
|
|
commTimer = Timer.new()
|
|
|
|
commTimer : reset()
|
|
|
|
|
2023-10-10 12:54:04 -07:00
|
|
|
canDelay = 1
|
|
|
|
commDelay = 20
|
|
|
|
|
|
|
|
function onTick()
|
2023-10-17 19:15:36 -07:00
|
|
|
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
|
2023-10-17 19:15:36 -07:00
|
|
|
handle()
|
2023-10-10 12:54:04 -07:00
|
|
|
end
|