Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function receiveRedstone(timeout)
- local tim
- --Set possible timeout
- if timeout then
- tim = os.startTimer(timeout)
- end
- while true do
- local event, timer = os.pullEvent()
- --If we got a redstone event, then we didn't hit our timeout and are ready to receive
- if event == "redstone" then
- --Map each line so we know what order we're going to receive in
- local receiveTbl = {}
- for a,v in ipairs(rs.getSides()) do
- receiveTbl[math.floor(rs.getBundledInput(v)/256)] = v
- end
- --Default each of our values.
- --ReceivedValues is our integers of each line in order
- local receivedValues = {0,0,0,0,0,0}
- --OldValue is the 9th bit of the first line, it toggles between 0 and 1 to show we're changing characters
- local oldValue = 0
- --Our string to build
- local buildString = ""
- --Our ending bool, gets changed to false if the 10th bit of 1st line is true
- local endBits = true
- --Loop
- while endBits do
- --Grab 9th bit, compare to old
- local curValue = bit.band(receivedValues[1],256)
- if( curValue ~= oldValue) then
- --If different, change old to new
- oldValue = curValue
- --If the 10th bit in first line is on, this is the final time we're running
- if(bit.band(receivedValues[1],512) ~= 0) then
- endBits = false
- end
- --Loop through our received values table
- for a,v in ipairs(receivedValues) do
- --If the value isn't 0 (or technically two full null characters)
- if v ~= 0 then
- --Tack onto end of buildString
- buildString = buildString .. string.char(v%256)
- --#If we're not the first line, tack the second half onto the end as well
- if a ~= 1 then
- buildString = buildString .. string.char(math.floor(v/256))
- end
- end
- end
- end
- --Pullevent on redstone
- os.pullEvent("redstone")
- --Update values based on redstone change
- for a,v in ipairs(receiveTbl) do
- receivedValues[a] = rs.getBundledInput(v)
- end
- end
- --We received the 10th bit, return true that we got a message, and the message itself
- return true, buildString
- elseif event == "timer" and timer == tim then
- --We timed out, return false
- return false, ""
- end
- end
- end
- function sendRedstone(str)
- --Create our sending table. Just a local table that holds the sides of the computer
- local sendTbl = rs.getSides()
- --Convert the characters in the string
- local sending = {}
- for i = 1, #str do
- sending[i] = string.byte(string.sub(str,i,i))
- end
- --Ping end server and identify each line
- for i = 1, #sendTbl do
- rs.setBundledOutput(sendTbl[i],256*i)
- end
- --Reset the lines to get ready for sending
- sleep(.1)
- for i = 1, #sendTbl do
- rs.setBundledOutput(sendTbl[i],0)
- end
- sleep(.1)
- --Send bytes in order
- --Bool is for first line, it's our toggling bit
- local bool = true
- --I is the current character to send, curSide is the line to send it through
- local i = 1
- local curSide = 1
- --While we have still have more characters to send
- while i <= #sending do
- --bytesToSend is our integer representing the 16 bit number to send on the line
- local bytesToSend = 0
- --Add just the first character's 0-255 representation, increment our character by 1
- bytesToSend = bytesToSend + sending[i]
- i = i + 1
- --If we're on our first line then if our toggle bit needs to be on, then turn it on
- if (curSide == 1) then
- if (bool) then
- bytesToSend = bytesToSend + 256
- end
- --Elseif we still have more characters to send
- elseif (i <= #sending) then
- --Add other character bit shifted right by 8 bits and increment i
- bytesToSend = bytesToSend + sending[i] * 256
- i = i + 1
- end
- --Set the output to our number
- rs.setBundledOutput(sendTbl[curSide],bytesToSend)
- --If we're on side 6, sleep so we can update, set side to 1, and toggle bool
- if curSide == 6 then
- curSide = 1
- sleep(.1)
- bool = not bool
- else
- --Else, increment curSide
- curSide = curSide + 1
- end
- end
- --End of write, need to make sure that any unused character lines are cleared.
- while (curSide <= 6) do
- rs.setBundledOutput(sendTbl[curSide],0)
- curSide = curSide + 1
- end
- --Add the 10th bit to the first line
- rs.setBundledOutput(sendTbl[1],rs.getBundledOutput(sendTbl[1]) + (2 * 256))
- --Sleep then clear lines, we're done.
- sleep(.1)
- for a,v in ipairs(sendTbl) do
- rs.setBundledOutput(v,0)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment