shiftx & getChannel
Posted: Wed Jul 19, 2017 12:22 am
Not sure I'm doing this right, trying to use the sample with 3 mapped channels, I verified they are working and showing up in the dashboard but I'm not getting anything out of the shiftx. I did confirm shiftx is working with the disco demo
tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
-- What CAN bus ShiftX2 is connected to. 0=CAN1, 1=CAN2
sxCan = 1
-- 0=first ShiftX2 on bus, 1=second ShiftX2 (if ADR1 jumper is cut)
sxId=0
--Brightness, 0-100. 0=automatic brightness
sxBright=0
--add your virtual channels here
--format addChannel(<name>, <sample rate>, <precision>, <min>, <max>, [units])
tempId = addChannel("EngineTempX", 1, 0, 0, 255, "F")
rpmId = addChannel("RPMX", 10, 0, 0, 10000)
fuelId = addChannel("FuelX", 1, 0, 0, 100, "%")
--Convert C to F
function toF(value)
return value * 1.8 + 32
end
function fuelFilter(value)
--adjust for 7 bit value
value = bit.band(value, 0x7F)
--convert liters to %
return value / 0.48
end
--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 OBDII RPM PID
--sxUpdateLinearGraph(readOBD2(12))
--uncomment the below for Direct RPM from lua
sxUpdateLinearGraph(getChannel("RPMX"))
--update engine temp alert
sxUpdateAlert(0, getChannel("EngineTempX"))
--update Fuel Level
sxUpdateAlert(1, getChannel("FuelX"))
end
function sxOnInit()
--config shift light
sxCfgLinearGraph(0,0,0,7000) --left to right graph, linear style, 0 - 7000 RPM range
sxSetLinearThresh(0,0,500,0,255,0,0) --green at 3000 RPM
sxSetLinearThresh(1,0,5000,255,255,0,0) --yellow at 5000 RPM
sxSetLinearThresh(2,0,6000,255,0,0,10) --red+flash at 6500 RPM
--configure first alert (right LED) as engine temperature (F)
sxSetAlertThresh(0,0,100,255,255,0,0) --yellow warning at 205F
sxSetAlertThresh(0,1,225,255,0,0,10) -- red flash at 225F
--configure second alert (left LED) as fuel (%)
sxSetAlertThresh(1,0,0,255,0,0,10) --red flash below 20 %
sxSetAlertThresh(1,1,25,255,255,0,5) --yellow flash 25-40 PSI
sxSetAlertThresh(1,2,40,0,0,0,0) --above 40, no alert
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
function onTick()
processCAN(CAN_chan)
sxProcess()
end
sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)
----------------------------------------
--customize here for CAN channel mapping
--format is:
--[CAN Id] = function(data) map_chan(<chan_id>, <data>, <CAN offset>, <CAN length>, <multiplier>,
-- <adder>, [filter])
----------------------------------------
CAN_map = {
[809] = function(data) map_chan(tempId, data, 1, 1, 0.75, -48, toF) end,
[790] = function(data) map_chan(rpmId, data, 2, 2, 0.15625, 0, rpmFilter) end,
[1555] = function (data) map_chan(fuelId, data, 2, 1, 1, 0, fuelFilter) end
}
--function onTick()
-- processCAN(CAN_chan)
--end
--===========do not edit below===========
function processCAN(chan)
local msg = 0
repeat
local id, e, data = rxCAN(chan, 0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
msg = msg + 1
until id == nil or msg > 100
end
--Map CAN channel, little endian format
function map_chan(cid, data, offset, len, mult, add, filter)
if offset + len > #data then return end
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)
tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
-- What CAN bus ShiftX2 is connected to. 0=CAN1, 1=CAN2
sxCan = 1
-- 0=first ShiftX2 on bus, 1=second ShiftX2 (if ADR1 jumper is cut)
sxId=0
--Brightness, 0-100. 0=automatic brightness
sxBright=0
--add your virtual channels here
--format addChannel(<name>, <sample rate>, <precision>, <min>, <max>, [units])
tempId = addChannel("EngineTempX", 1, 0, 0, 255, "F")
rpmId = addChannel("RPMX", 10, 0, 0, 10000)
fuelId = addChannel("FuelX", 1, 0, 0, 100, "%")
--Convert C to F
function toF(value)
return value * 1.8 + 32
end
function fuelFilter(value)
--adjust for 7 bit value
value = bit.band(value, 0x7F)
--convert liters to %
return value / 0.48
end
--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 OBDII RPM PID
--sxUpdateLinearGraph(readOBD2(12))
--uncomment the below for Direct RPM from lua
sxUpdateLinearGraph(getChannel("RPMX"))
--update engine temp alert
sxUpdateAlert(0, getChannel("EngineTempX"))
--update Fuel Level
sxUpdateAlert(1, getChannel("FuelX"))
end
function sxOnInit()
--config shift light
sxCfgLinearGraph(0,0,0,7000) --left to right graph, linear style, 0 - 7000 RPM range
sxSetLinearThresh(0,0,500,0,255,0,0) --green at 3000 RPM
sxSetLinearThresh(1,0,5000,255,255,0,0) --yellow at 5000 RPM
sxSetLinearThresh(2,0,6000,255,0,0,10) --red+flash at 6500 RPM
--configure first alert (right LED) as engine temperature (F)
sxSetAlertThresh(0,0,100,255,255,0,0) --yellow warning at 205F
sxSetAlertThresh(0,1,225,255,0,0,10) -- red flash at 225F
--configure second alert (left LED) as fuel (%)
sxSetAlertThresh(1,0,0,255,0,0,10) --red flash below 20 %
sxSetAlertThresh(1,1,25,255,255,0,5) --yellow flash 25-40 PSI
sxSetAlertThresh(1,2,40,0,0,0,0) --above 40, no alert
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
function onTick()
processCAN(CAN_chan)
sxProcess()
end
sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)
----------------------------------------
--customize here for CAN channel mapping
--format is:
--[CAN Id] = function(data) map_chan(<chan_id>, <data>, <CAN offset>, <CAN length>, <multiplier>,
-- <adder>, [filter])
----------------------------------------
CAN_map = {
[809] = function(data) map_chan(tempId, data, 1, 1, 0.75, -48, toF) end,
[790] = function(data) map_chan(rpmId, data, 2, 2, 0.15625, 0, rpmFilter) end,
[1555] = function (data) map_chan(fuelId, data, 2, 1, 1, 0, fuelFilter) end
}
--function onTick()
-- processCAN(CAN_chan)
--end
--===========do not edit below===========
function processCAN(chan)
local msg = 0
repeat
local id, e, data = rxCAN(chan, 0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
msg = msg + 1
until id == nil or msg > 100
end
--Map CAN channel, little endian format
function map_chan(cid, data, offset, len, mult, add, filter)
if offset + len > #data then return end
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)