Skip to content

Instantly share code, notes, and snippets.

@brentpicasso
Created February 12, 2017 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentpicasso/219dc0c9aa7982267b833045dba9a4b8 to your computer and use it in GitHub Desktop.
Save brentpicasso/219dc0c9aa7982267b833045dba9a4b8 to your computer and use it in GitHub Desktop.
ShiftX2 AFR simulator
slcan = 1
function splitWord(value)
return bit.band(value, 0xFF), bit.rshift(bit.band(value, 0xFF00),8)
end
function sl_cfgAlert(id, threshId, threshold, red, green, blue, flash)
local lowt
local hight
lowt, hight = splitWord(threshold)
txCAN(slcan, 744389 + 12, 1, {id, threshId, lowt, hight, red, green, blue, flash})
sleep(10)
end
function setLinearThreshold(id, segLen, threshold, red, green, blue, flash)
local lowt
local hight
lowt, hight = splitWord(threshold)
txCAN(slcan, 744389 + 15, 1, {id, segLen, lowt, hight, red, green, blue, flash})
sleep(10)
end
function setBaseConfig(bright)
txCAN(slcan, 744389 + 3, 1, {bright})
sleep(10)
end
function setAlert(id, r, g, b, f)
txCAN(slcan, 744389 + 11, 1, {id, r, g, b, f})
sleep(10)
end
function configLinearGraph(lowRange, highRange, renderStyle, linearStyle)
local lowRangeLow
local lowRangeHigh
local highRangeLow
local highRangeHigh
lowRangeLow, lowRangeHigh = splitWord(lowRange)
highRangeLow, highRangeHigh = splitWord(highRange)
txCAN(slcan, 744389 + 14, 1, {renderStyle, linearStyle, lowRangeLow, lowRangeHigh, highRangeLow, highRangeHigh})
sleep(10)
end
function updateLinearGraph(value)
low, high = splitWord(value)
txCAN(slcan, 744389 + 16, 1, {low, high})
end
function sl_updateAlert(alertId, value)
low, high = splitWord(value)
txCAN(slcan, 744389 + 13, 1, {alertId, low, high})
end
function shiftx_init_laptime()
end
function shiftx_init_alerts()
--oil alert
sl_cfgAlert(1, 0, 0, 0, 255, 255, 10)
sl_cfgAlert(1, 1, 10, 0, 0, 255, 0)
sl_cfgAlert(1, 2, 20, 0, 0,0, 0)
println('shiftx config')
--engine temp alert
sl_cfgAlert(0, 0, 0, 0, 0, 0, 0)
sl_cfgAlert(0, 1, 210, 255, 127, 0, 0)
sl_cfgAlert(0, 2, 225, 255, 0, 0, 5)
end
function shiftx_init_afr()
configLinearGraph(0, 20, 0, 0)
setLinearThreshold(0, 0, 0, 255, 255, 0, 0)
setLinearThreshold(1, 0, 9, 0, 255, 0, 0)
setLinearThreshold(2, 0, 14, 255, 127, 0, 0)
setLinearThreshold(3, 0, 17, 255, 0, 0, 10)
end
function check_can_msg()
id, ext, data = rxCAN(slcan,0)
if id == 744389 then
shiftx_init()
println('discovered shiftx, configuring')
end
end
function updateAFR()
local afr = getAnalog(0) / 6
setChannel(afrId, afr)
updateLinearGraph(afr)
end
function shiftx_init()
setBaseConfig(20)
shiftx_init_afr()
end
function onTick()
updateAFR()
check_can_msg()
end
afrId = addChannel("AFR", 10, 1, 0,20)
setTickRate(30)
initCAN(slcan, 500000)
shiftx_init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment