hilburn

Analog bitwise encoding

Jul 18th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local bitwise = {0={false,false,false,false},1={false,false,false,true},2={false,false,true,false},3={false,false,true,true},4={false,true,false,false},5={false,true,false,true},6={false,true,true,false},7={false,true,true,true},8={true,false,false,false},9={true,false,false,true},10={true,false,true,false},11={true,false,true,true},12={true,true,false,false},13={true,true,false,true},14={true,true,true,false},15={true,true,true,true},} --#yeah this is ugly as sin, but I couldn't be bothered to program a bitwise comparator right now
  2. local currentout = {false, false, false, false}
  3.  
  4. local function getRSBundleInput(side)
  5.     local input={}
  6.     for i, j in ipairs(bitwise[rs.getAnalogInput(side)]) do
  7.         if j then input[#input+1]=i end
  8.     end
  9.     return input
  10. end
  11.  
  12. local function setRSBundleOutput(side, value)
  13.     value=value % 15 --#ignores colours bigger than light blue as they are not supported
  14.     for i,j in ipairs(bitwise[value]) do
  15.         if j~= currentout.i then --#only if the output is changing
  16.             local output=i
  17.             if j then output=output+8 end  --#low outputs are set as 1-4, high outputs are 9-12
  18.             rs.setAnalogOutput(side, output)
  19.             currentout.i=j
  20.             sleep(0.05)
  21.         end
  22.     end
  23. end
  24.  
  25. --usage
  26.  
  27. local inputs = getRSBundleInput("left") --#returns eg {1,3,4} if white, purple and lightblue are high
  28. setRSBundleOutput("right", colours.white+colours.orange) --#sets white and orange high, purple and lightblue low
Advertisement
Add Comment
Please, Sign In to add comment