Advertisement
zurbo

cc-rednetRBC-API-WIP

Apr 4th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. local sSide
  2.  
  3. function open(side)
  4.     sSide = side
  5. end
  6.  
  7. function close(side)
  8.         sSide = nil
  9. end
  10.  
  11. local function send2Chars(char1,char2)
  12.     local chars = bit.bor(bit.blshift(char1,7),char2)
  13.     redstone.setBundledOutput(sSide, chars)
  14. end
  15.  
  16. local function recv2Chars()
  17.     local recvd = redstone.getBundledInput(sSide)
  18.     local char2 = bit.band(127,rcvd)
  19.     local char1 = bit.band(127,bit.brshift(rcvd,7))
  20.     return char1, char2
  21. end
  22.  
  23. function receive(timeout)
  24.     if timeout==nil then
  25.         redstone.setBundledOutput(sSide, colors.red)
  26.         local cond = os.pullEvent("redstone")
  27.         local val = "redstone"
  28.     else
  29.         redstone.setBundledOutput(sSide, colors.red)
  30.         local cond = parallel.waitForAny(function() os.pullEvent("redstone"); end, function() os.sleep(timeout); end)
  31.         local val = 1
  32.     end
  33.     if cond==val then
  34.         local i,j,data
  35.         local charTbl={}
  36.         while true do
  37.             redstone.setBundledOutput(sSide, 0)
  38.             charTbl[i], charTbl[i+1] = rcv2chars()
  39.             if redstone.testBundledInput(sSide, colors.black) then
  40.                 for i=0, table.getn(charTbl), 1 do
  41.                     data[i]=string.char(charTbl[i])
  42.                 end
  43.                 return data.concat()
  44.             else
  45.                 i = i+2
  46.                 redstone.setBundledOutput(sSide, colors.red)
  47.                 os.sleep(0.05) 
  48.             end            
  49.         end
  50.     else
  51.         return "requestTimedOut"
  52.     end
  53. end
  54.  
  55. function send(data)
  56.     local i, charTbl
  57.     local len = #data
  58.     for i = 1, len, 1 do
  59.         charTbl[i]=string.byte(data,i)
  60.     end
  61.     for i = 0, len, 2 do
  62.         while redstone.testBundledInput(sSide, colors.red)==false do
  63.             os.pullEvent("redstone")
  64.         end
  65.         send2Chars(charTbl[i],charTbl[i+1])
  66.         while redstone.testBundledInput(sSide, colors.red)==true do
  67.             os.pullEvent("redstone")
  68.         end
  69.     end
  70.     redstone.setBundledOutput(sSide, bit.bxor(redstone.getBundledOutput(sSide), colors.black))         
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement