Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sSide
- function open(side)
- sSide = side
- end
- function close(side)
- sSide = nil
- end
- local function send2Chars(char1,char2)
- local chars = bit.bor(bit.blshift(char1,7),char2)
- redstone.setBundledOutput(sSide, chars)
- end
- local function recv2Chars()
- local recvd = redstone.getBundledInput(sSide)
- local char2 = bit.band(127,rcvd)
- local char1 = bit.band(127,bit.brshift(rcvd,7))
- return char1, char2
- end
- function receive(timeout)
- if timeout==nil then
- redstone.setBundledOutput(sSide, colors.red)
- local cond = os.pullEvent("redstone")
- local val = "redstone"
- else
- redstone.setBundledOutput(sSide, colors.red)
- local cond = parallel.waitForAny(function() os.pullEvent("redstone"); end, function() os.sleep(timeout); end)
- local val = 1
- end
- if cond==val then
- local i,j,data
- local charTbl={}
- while true do
- redstone.setBundledOutput(sSide, 0)
- charTbl[i], charTbl[i+1] = rcv2chars()
- if redstone.testBundledInput(sSide, colors.black) then
- for i=0, table.getn(charTbl), 1 do
- data[i]=string.char(charTbl[i])
- end
- return data.concat()
- else
- i = i+2
- redstone.setBundledOutput(sSide, colors.red)
- os.sleep(0.05)
- end
- end
- else
- return "requestTimedOut"
- end
- end
- function send(data)
- local i, charTbl
- local len = #data
- for i = 1, len, 1 do
- charTbl[i]=string.byte(data,i)
- end
- for i = 0, len, 2 do
- while redstone.testBundledInput(sSide, colors.red)==false do
- os.pullEvent("redstone")
- end
- send2Chars(charTbl[i],charTbl[i+1])
- while redstone.testBundledInput(sSide, colors.red)==true do
- os.pullEvent("redstone")
- end
- end
- redstone.setBundledOutput(sSide, bit.bxor(redstone.getBundledOutput(sSide), colors.black))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement