Basically what it does here is:
1) Creates a code block of static variables for clearly setting warning and critical thresholds for LEDs (I place this section in the top of my code so it's easy to find if I want to modify thresholds).
2) Sets up a virtual channel "RightLED" that has a value of 0 for off, 1 for Warnings, and 2 for Criticals.
3) Creates a function "setRightWarning" for checking all thresholds and setting the channel RightLED to its proper value (see the really long if statement). Have to know which inputs are what here. It polls all the inputs we care about, storing their values temporarily, and compares those values to the thresholds we set earlier. Criticals override Warnings (i.e. if there's any critical the channel value will be 2 regardless of how many warnings there are).
4) Adds the function call of "setRightWarning" to the onTick function (which is the main loops for all scripts) so that all thresholds are checked every tick.
5) Sets the ShiftX2 to monitor the RightLED channel, and show on the right LED solid Yellow for Warnings and flash Red for Criticals.
Here's the code:
Code: Select all
oilpressurewarn = 20
oilpressurecrit = 15
oiltempwarn = 305
oiltempcrit = 330
egtwarn = 1650
egtcrit = 1700
watertempwarn = 210
watertempcrit = 225
boostwarn = 30
boostcrit = 32
turborpmwarn = 125000
turborpmcrit = 140000
fuellevelwarn = 10
fuellevelcrit = 5
-- Sets the ShiftX2 right warning light's channel
rightLEDID = addChannel("RightLED",50,0,0,1)
function setRightWarning()
oilpressure = getAnalog(0)
oiltemp = getAnalog(1)
egt = getAnalog(2)
watertemp = getAnalog(3)
fuellevel = getAnalog(4)
boost = getAnalog(7)
turborpm = getChannel(TurboRPMID)
if oilpressure < oilpressurecrit or oiltemp > oiltempcrit or egt > egtcrit or watertemp > watertempcrit or boost > boostcrit or turborpm > turborpmcrit or fuellevel < fuellevelcrit then
setChannel(rightLEDID, 2)
elseif oilpressure < oilpressurewarn or oiltemp > oiltempwarn or egt > egtwarn or watertemp > watertempwarn or boost > boostwarn or turborpm > turborpmwarn or fuellevel < fuellevelwarn then
setChannel(rightLEDID, 1)
else
setChannel(rightLEDID, 0)
end
end
function onTick()
setRightWarning()
sxProcess()
end
-- Insert all the ShiftX2 code here
-- What CAN bus ShiftX2 is connected to. 0=CAN1, 1=CAN2
sxCan = 0
-- 0=first ShiftX2 on bus, 1=second ShiftX2 (if ADR1 jumper is cut)
sxId=0
--how often ShiftX2 is updated
tickRate=50
--Brightness, 0-100. 0=automatic brightness
sxBright=0
function sxOnUpdate()
--add your code to update ShiftX2 alerts or linear graph during run time.
--Runs continuously based on tickRate.
--uncomment the below for Direct RPM on input 0
sxUpdateLinearGraph(getTimerRpm(0))
--update rightLED
sxUpdateAlert(0,getChannel(rightLEDID))
end
function sxOnInit()
--config shift light
sxCfgLinearGraph(0,0,0,8000) --left to right graph, linear style, 0 - 8000 RPM range
sxSetLinearThresh(0,0,3000,0,255,0,0) --green at 3000 RPM
sxSetLinearThresh(1,0,6500,255,255,0,0) --yellow at 6500 RPM
sxSetLinearThresh(2,0,7500,255,0,0,10) --red+flash at 7500 RPM
[b] --configure second alert (right LED) as rightLED
sxSetAlertThresh(0,0,1,255,255,0,0) --yellow warning at 1
sxSetAlertThresh(0,1,2,255,0,0,10) -- red flash at 2[/b]
end
function sxOnBut(b)
--called if the button state changes
println('button: ' ..b)
end
---ShiftX2 functions
function sxSetLed(i,l,r,g,b,f)
sxTx(10,{i,l,r,g,b,f})
end
function sxSetLinearThresh(id,s,th,r,g,b,f)
sxTx(41,{id,s,spl(th),sph(th),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 sxCfgLinearGraph(rs,ls,lr,hr)
sxTx(40,{rs,ls,spl(lr),sph(lr),spl(hr),sph(hr)})
end
function sxUpdateLinearGraph(v)
if v ~= nil then sxTx(42,{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
sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)
setTickRate(tickRate)
sxInit()