Setting up Warning and Critical Lights on your ShiftX2

Q&A For ShiftX2, RGB sequential shift light <a href="https://wiki.autosportlabs.com/ShiftX2">Installation and Operation Guide</a>
Post Reply
jpf11
Posts: 57
Joined: Wed Oct 23, 2013 9:42 pm
Location: United States

Setting up Warning and Critical Lights on your ShiftX2

Post by jpf11 »

While many of you have some experience coding I imagine there are at least a few that don't. For those folks I thought this, arguably simple, code snippet would be useful. The script gives an example of how you can use the side lights on your ShiftX2 to alert you to Warning or Critical alerts based on any of your inputs to your RaceCapture device.

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&#40;"RightLED",50,0,0,1&#41;
function setRightWarning&#40;&#41;
  oilpressure = getAnalog&#40;0&#41;
  oiltemp = getAnalog&#40;1&#41;
  egt = getAnalog&#40;2&#41;
  watertemp = getAnalog&#40;3&#41;
  fuellevel = getAnalog&#40;4&#41;  
  boost = getAnalog&#40;7&#41;
  turborpm = getChannel&#40;TurboRPMID&#41;

  if oilpressure < oilpressurecrit or oiltemp > oiltempcrit or egt > egtcrit or watertemp > watertempcrit or boost > boostcrit or turborpm > turborpmcrit or fuellevel < fuellevelcrit then
    setChannel&#40;rightLEDID, 2&#41;
  elseif oilpressure < oilpressurewarn or oiltemp > oiltempwarn or egt > egtwarn or watertemp > watertempwarn or boost > boostwarn or turborpm > turborpmwarn or fuellevel < fuellevelwarn then
    setChannel&#40;rightLEDID, 1&#41;
  else
    setChannel&#40;rightLEDID, 0&#41;
  end
end

function onTick&#40;&#41;
  setRightWarning&#40;&#41;
  sxProcess&#40;&#41;
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 &#40;if ADR1 jumper is cut&#41;
sxId=0

--how often ShiftX2 is updated
tickRate=50

--Brightness, 0-100. 0=automatic brightness
sxBright=0

function sxOnUpdate&#40;&#41;
  --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&#40;getTimerRpm&#40;0&#41;&#41;
  
  --update rightLED
  sxUpdateAlert&#40;0,getChannel&#40;rightLEDID&#41;&#41;

end

function sxOnInit&#40;&#41;
  --config shift light
  sxCfgLinearGraph&#40;0,0,0,8000&#41; --left to right graph, linear style, 0 - 8000 RPM range

  sxSetLinearThresh&#40;0,0,3000,0,255,0,0&#41; --green at 3000 RPM
  sxSetLinearThresh&#40;1,0,6500,255,255,0,0&#41; --yellow at 6500 RPM
  sxSetLinearThresh&#40;2,0,7500,255,0,0,10&#41; --red+flash at 7500 RPM
  
&#91;b&#93;  --configure second alert &#40;right LED&#41; as rightLED
  sxSetAlertThresh&#40;0,0,1,255,255,0,0&#41; --yellow warning at 1
  sxSetAlertThresh&#40;0,1,2,255,0,0,10&#41; -- red flash at 2&#91;/b&#93;

end

function sxOnBut&#40;b&#41;
  --called if the button state changes
  println&#40;'button&#58; ' ..b&#41;
end

---ShiftX2 functions

function sxSetLed&#40;i,l,r,g,b,f&#41;
  sxTx&#40;10,&#123;i,l,r,g,b,f&#125;&#41;
end

function sxSetLinearThresh&#40;id,s,th,r,g,b,f&#41;
  sxTx&#40;41,&#123;id,s,spl&#40;th&#41;,sph&#40;th&#41;,r,g,b,f&#125;&#41;
end

function sxSetAlertThresh&#40;id,tid,th,r,g,b,f&#41;
  sxTx&#40;21,&#123;id,tid,spl&#40;th&#41;,sph&#40;th&#41;,r,g,b,f&#125;&#41;
end

function setBaseConfig&#40;bright&#41;
  sxTx&#40;3,&#123;bright&#125;&#41;
end

function sxSetAlert&#40;id,r,g,b,f&#41;
  sxTx&#40;20,&#123;id,r,g,b,f&#125;&#41;
end

function sxUpdateAlert&#40;id,v&#41;
  if v~=nil then sxTx&#40;22,&#123;id,spl&#40;v&#41;,sph&#40;v&#41;&#125;&#41; end
end

function sxCfgLinearGraph&#40;rs,ls,lr,hr&#41; 
  sxTx&#40;40,&#123;rs,ls,spl&#40;lr&#41;,sph&#40;lr&#41;,spl&#40;hr&#41;,sph&#40;hr&#41;&#125;&#41;
end

function sxUpdateLinearGraph&#40;v&#41;
  if v ~= nil then sxTx&#40;42,&#123;spl&#40;v&#41;,sph&#40;v&#41;&#125;&#41; end
end

function sxInit&#40;&#41;
  println&#40;'config shiftX2'&#41;
  setBaseConfig&#40;sxBright&#41;
  if sxOnInit~=nil then sxOnInit&#40;&#41; end
end

function sxChkCan&#40;&#41;
  id,ext,data=rxCAN&#40;sxCan,0&#41;
  if id==sxCanId then sxInit&#40;&#41; end
  if id==sxCanId+60 and sxOnBut~=nil then sxOnBut&#40;data&#91;1&#93;&#41; end
end

function sxProcess&#40;&#41;
  sxChkCan&#40;&#41;
  if sxOnUpdate~=nil then sxOnUpdate&#40;&#41; end
end

function sxTx&#40;offset, data&#41;
  txCAN&#40;sxCan, sxCanId + offset, 1, data&#41;
  sleep&#40;10&#41;
end

function spl&#40;v&#41; return bit.band&#40;v,0xFF&#41; end
function sph&#40;v&#41; return bit.rshift&#40;bit.band&#40;v,0xFF00&#41;,8&#41; end


sxCanId = 0xE3600 + &#40;256 * sxId&#41;
println&#40;'shiftx2 base id ' ..sxCanId&#41;

setTickRate&#40;tickRate&#41;
sxInit&#40;&#41;

brentp
Site Admin
Posts: 6282
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

This is great! Now a sticky.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

jpf11
Posts: 57
Joined: Wed Oct 23, 2013 9:42 pm
Location: United States

Post by jpf11 »

I thought also about making a timer virtual channel so I could keep warnings and criticals lit for a set minimum time once they trigger. Basically a decay timer. If anyone is interested in that let me know, it's pretty easy to write up.

Post Reply