Dragon53535

Redstone Communication

May 7th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. function receiveRedstone(timeout)
  2.   local tim
  3.  
  4.   --Set possible timeout
  5.   if timeout then
  6.     tim = os.startTimer(timeout)
  7.   end
  8.   while true do
  9.     local event, timer = os.pullEvent()
  10.     --If we got a redstone event, then we didn't hit our timeout and are ready to receive
  11.     if event == "redstone" then
  12.    
  13.       --Map each line so we know what order we're going to receive in
  14.       local receiveTbl = {}
  15.       for a,v in ipairs(rs.getSides()) do
  16.         receiveTbl[math.floor(rs.getBundledInput(v)/256)] = v
  17.       end
  18.      
  19.       --Default each of our values.
  20.      
  21.       --ReceivedValues is our integers of each line in order
  22.       local receivedValues = {0,0,0,0,0,0}
  23.      
  24.       --OldValue is the 9th bit of the first line, it toggles between 0 and 1 to show we're changing characters
  25.       local oldValue = 0
  26.      
  27.       --Our string to build
  28.       local buildString = ""
  29.      
  30.       --Our ending bool, gets changed to false if the 10th bit of 1st line is true
  31.       local endBits = true
  32.      
  33.       --Loop
  34.       while endBits do
  35.      
  36.         --Grab 9th bit, compare to old
  37.         local curValue = bit.band(receivedValues[1],256)
  38.         if( curValue ~= oldValue) then
  39.        
  40.           --If different, change old to new
  41.           oldValue = curValue
  42.          
  43.           --If the 10th bit in first line is on, this is the final time we're running
  44.           if(bit.band(receivedValues[1],512) ~= 0) then
  45.             endBits = false
  46.           end
  47.          
  48.           --Loop through our received values table
  49.           for a,v in ipairs(receivedValues) do
  50.          
  51.             --If the value isn't 0 (or technically two full null characters)
  52.             if v ~= 0 then
  53.            
  54.               --Tack onto end of buildString
  55.               buildString = buildString .. string.char(v%256)
  56.              
  57.               --#If we're not the first line, tack the second half onto the end as well
  58.               if a ~= 1 then
  59.                 buildString = buildString .. string.char(math.floor(v/256))
  60.               end
  61.             end
  62.           end
  63.         end
  64.        
  65.        
  66.         --Pullevent on redstone
  67.         os.pullEvent("redstone")
  68.        
  69.         --Update values based on redstone change
  70.         for a,v in ipairs(receiveTbl) do
  71.           receivedValues[a] = rs.getBundledInput(v)
  72.         end
  73.      
  74.       end
  75.      
  76.       --We received the 10th bit, return true that we got a message, and the message itself
  77.       return true, buildString
  78.     elseif event == "timer" and timer == tim then
  79.      
  80.       --We timed out, return false
  81.       return false, ""
  82.     end
  83.   end
  84. end
  85.  
  86. function sendRedstone(str)
  87.   --Create our sending table. Just a local table that holds the sides of the computer
  88.   local sendTbl = rs.getSides()
  89.  
  90.   --Convert the characters in the string
  91.   local sending = {}
  92.   for i = 1, #str do
  93.     sending[i] = string.byte(string.sub(str,i,i))
  94.   end
  95.  
  96.  
  97.   --Ping end server and identify each line
  98.   for i = 1, #sendTbl do
  99.     rs.setBundledOutput(sendTbl[i],256*i)
  100.   end
  101.  
  102.   --Reset the lines to get ready for sending
  103.   sleep(.1)
  104.   for i = 1, #sendTbl do
  105.     rs.setBundledOutput(sendTbl[i],0)
  106.   end
  107.   sleep(.1)
  108.  
  109.  
  110.   --Send bytes in order
  111.  
  112.   --Bool is for first line, it's our toggling bit
  113.   local bool = true
  114.  
  115.   --I is the current character to send, curSide is the line to send it through
  116.   local i = 1
  117.   local curSide = 1
  118.  
  119.   --While we have still have more characters to send
  120.   while i <= #sending do
  121.  
  122.     --bytesToSend is our integer representing the 16 bit number to send on the line
  123.     local bytesToSend = 0
  124.    
  125.     --Add just the first character's 0-255 representation, increment our character by 1
  126.     bytesToSend = bytesToSend + sending[i]
  127.     i = i + 1
  128.    
  129.     --If we're on our first line then if our toggle bit needs to be on, then turn it on
  130.     if (curSide == 1) then
  131.       if (bool) then
  132.         bytesToSend = bytesToSend + 256
  133.       end
  134.      
  135.     --Elseif we still have more characters to send
  136.     elseif (i <= #sending) then
  137.       --Add other character bit shifted right by 8 bits and increment i
  138.       bytesToSend = bytesToSend + sending[i] * 256
  139.       i = i + 1
  140.     end
  141.    
  142.     --Set the output to our number
  143.     rs.setBundledOutput(sendTbl[curSide],bytesToSend)
  144.    
  145.     --If we're on side 6, sleep so we can update, set side to 1, and toggle bool
  146.     if curSide == 6 then
  147.       curSide = 1
  148.       sleep(.1)
  149.       bool = not bool
  150.     else
  151.       --Else, increment curSide
  152.       curSide = curSide + 1
  153.     end
  154.   end
  155.  
  156.   --End of write, need to make sure that any unused character lines are cleared.
  157.   while (curSide <= 6) do
  158.     rs.setBundledOutput(sendTbl[curSide],0)
  159.     curSide = curSide + 1
  160.   end
  161.  
  162.   --Add the 10th bit to the first line
  163.   rs.setBundledOutput(sendTbl[1],rs.getBundledOutput(sendTbl[1]) + (2 * 256))
  164.  
  165.   --Sleep then clear lines, we're done.
  166.   sleep(.1)
  167.   for a,v in ipairs(sendTbl) do
  168.     rs.setBundledOutput(v,0)
  169.   end
  170. end
Advertisement
Add Comment
Please, Sign In to add comment