Code: Select all
println("---Shift Light Twin Alert---")
sxCan=0
sxId=0
tickRate=30
sxBright=0 --0=automatic
L=7
--rpm0,rpm4=4000,6800
rpm0,rpm4=2000,4800
inc0=(rpm4-rpm0)/(L+1)
inc4=inc0/L
rpm1,rpm2,rpm3=rpm0+3*inc0,rpm0+5*inc0,rpm0+7*inc0
function sxShiftBar(rpm)
if rpm<rpm0 then
sxSetLed(0,L,0,0,0,0)
elseif rpm<rpm1 then
n=math.floor((rpm-rpm0)/inc0+1)
sxSetLed(0,n,0,255,0,0) --green
sxSetLed(n,L-n,0,0,0,0)
elseif rpm<rpm2 then
n=math.floor((rpm-rpm1)/inc0+1)
sxSetLed(0,3,0,255,0,0) --green
sxSetLed(3,n,255,255,0,0) --yellow
sxSetLed(n+3,L-n-3,0,0,0,0)
elseif rpm<rpm3 then
n=math.floor((rpm-rpm2)/inc0+1)
sxSetLed(0,3,0,255,0,0) --green
sxSetLed(3,2,255,255,0,0) --yellow
sxSetLed(5,n,255,0,0,0) --red
sxSetLed(n+5,L-n-5,0,0,0,0)
elseif rpm<rpm4 then
n=math.floor((rpm-rpm3)/inc4+1)
sxSetLed(0,n,255,255,255,0) --white
sxSetLed(n,L-n,255,0,0,0)
else
sxSetLed(0,L,255,0,0,5) --red flash
end
end
st,stend=0,7000/30
--Virtual Channel ID's
IDopr = addChannel("Pres-rpm",25,0,0,30,"psi")
--IDkph = addChannel("KPH",25,0,0,300,"kph")
--IDcnr = addChannel("CnR",25,0,0,300,"m")
--IDcmb = addChannel("AccComb",25,0,0,3,"g")
function sxOnUpdate()
if st<stend then
--Startup Sequence
if st<stend/2 then
ut=st/stend*2.2
else
ut=(stend-st)/stend*2.2
end
sxShiftBar(rpm4*ut)
sxUpdateAlert(0, 150*ut)
sxUpdateAlert(1, 50*ut)
st=st+1
else
rpm=getChannel("RPM")
if rpm==nil then rpm=100 end
sxShiftBar(rpm)
oilp=getAnalog(0)
sxUpdateAlert(1, oilp) --lhs alert
sxUpdateAlert(0, getChannel("OilTemp")) --rhs Alert
--update Virtual Channels
if rpm>0 then setChannel(IDopr,oilp/rpm*1000) end
--setChannel(IDkph,getGpsSpeed()*1.60934)
--setChannel(IDcnr,getGpsSpeed()^2/getImu(0)*0.020378)
--setChannel(IDcmb,math.sqrt(getImu(0)^2+getImu(1)^2))
end
end
function sxOnInit()
--configure first alert (right LED) as engine temp
sxSetAlertThresh(0,0, 0, 70,240,240,0) --cyan
sxSetAlertThresh(0,1, 60, 0,255, 0,0) --l.green
sxSetAlertThresh(0,2, 95,255,165, 0,0) --yellow
sxSetAlertThresh(0,3,115,255, 0, 0,0) --red
sxSetAlertThresh(0,4,120,255, 0, 0,5) --flash
--configure second alert (left LED) as oil pressure
sxSetAlertThresh(1,0, 0,255, 0, 0,5) --red flash
sxSetAlertThresh(1,1,15,255,165, 0,0) --yellow
sxSetAlertThresh(1,2,20, 0,255, 0,0) --green
sxSetAlertThresh(1,3,60,230,140, 50,0) --orange
end
function sxOnBut(b)
println('button: ' ..b)
end
---ShiftX2 functions---
function sxSetLed(i,l,r,g,b,f)
sxTx(10,{i,l,r,g,b,f})
end
function sxSetAlertThresh(id,tid,th,r,g,b,f)
sxTx(21,{id,tid,spl(th),sph(th),r,g,b,f})
end
function setBaseConfig(bright)
sxTx(3,{bright})
end
function sxSetAlert(id,r,g,b,f)
sxTx(20,{id,r,g,b,f})
end
function sxUpdateAlert(id,v)
if v~=nil then sxTx(22,{id,spl(v),sph(v)}) end
end
function sxInit()
println('config shiftX2')
setBaseConfig(sxBright)
if sxOnInit~=nil then sxOnInit() end
end
function sxChkCan()
id,ext,data=rxCAN(sxCan,0)
if id==sxCanId then sxInit() end
if id==sxCanId+60 and sxOnBut~=nil then sxOnBut(data[1]) end
end
function sxProcess()
sxChkCan()
if sxOnUpdate~=nil then sxOnUpdate() end
end
function sxTx(offset, data)
txCAN(sxCan, sxCanId + offset, 1, data)
sleep(10)
end
function spl(v) return bit.band(v,0xFF) end
function sph(v) return bit.rshift(bit.band(v,0xFF00),8) end
---End Shiftx2 functions---
function onTick()
sxProcess()
collectgarbage()
end
sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)
setTickRate(tickRate)
sxInit()