Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disco lights + 7 segment + button test
sxCan = 1
sxId=0
tickRate=30
sxBright=0
disp = 0
function rndc()
return math.random(0,255)
end
function sxOnUpdate()
for i=0,9,1 do
r = rndc()
g = rndc()
b = rndc()
sxSetLed(i,1,r,g,b,0)
end
--sxSetDisplay(math.random(0,9))
end
function sxOnBut(b, id)
println('button: ' ..b ..' ' ..id)
if b == 0 then return end
if id == 1 then disp = disp + 1 end
if id == 0 then disp = disp - 1 end
sxSetDisplay(disp)
end
function sxOnInit()
print('sx onInit')
end
--ShiftX2 library 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 sxSetBaseConfig(bright)
sxTx(3,{bright})
end
function sxSetAlert(id,r,g,b,f)
sxTx(20,{id,r,g,b,f})
end
function sxSetDisplay(value)
sxTx(50, {0, 48+value})
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)
sxTx(42,{spl(v),sph(v)})
end
function sxInit()
println('config shiftX2')
sxSetBaseConfig(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], data[2]) 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()
sxProcess()
end
sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)
setTickRate(tickRate)
sxInit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.