Advertisement
osmarks

Really Slow Redstone Networking

Dec 19th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- converts a value between 0 and 127 to bits, least significant bits first
  2. local function to_bits(charbyte)
  3.     if charbyte > 127 then error "invalid character" end
  4.     local out = {}
  5.     for i = 0, 6 do
  6.         local bitmask = bit.blshift(1, i)
  7.         local bit = bit.brshift(bit.band(bitmask, charbyte), i)
  8.         table.insert(out, bit)
  9.     end
  10.     return out
  11. end
  12.  
  13. local function from_bits(bit_table)
  14.     local int = 0
  15.     for i = 0, 6 do
  16.         local index = i + 1 -- Lua...
  17.         int = bit.bor(int, bit.blshift(bit_table[index], i))
  18.     end
  19.     return int
  20. end
  21.  
  22. local rx_side = settings.get "rx_side" or "right"
  23. local tx_side = settings.get "tx_side" or "left"
  24.  
  25. local function send(str)
  26.     str = "\127" .. str
  27.     for i = 1, #str do
  28.         local byte = str:byte(i)
  29.         for _, bit in ipairs(to_bits(byte)) do
  30.             rs.setOutput(tx_side, bit == 1)
  31.             sleep(0.1)
  32.         end
  33.     end
  34.     rs.setOutput(tx_side, false)
  35. end
  36.  
  37. local function receive(char_callback)
  38.     local str = ""
  39.     repeat
  40.         os.pullEvent "redstone"
  41.     until rs.getInput(rx_side)
  42.     while true do
  43.         local bits = {}
  44.         for i = 0, 6 do
  45.             if rs.getInput(rx_side) then
  46.                 table.insert(bits, 1)
  47.             else
  48.                 table.insert(bits, 0)
  49.             end
  50.             sleep(0.1)
  51.         end
  52.         local char = string.char(from_bits(bits))
  53.         if char == "\0" then break end
  54.         if char ~= "\127" and char_callback then char_callback(char) end
  55.         str = str .. char
  56.     end
  57.     return str:sub(2)
  58. end
  59.  
  60. local w, h = term.getSize()
  61. local send_window = window.create(term.current(), 1, h, w, 1)
  62. local message_window = window.create(term.current(), 1, 1, w, h - 1)
  63.  
  64. local function exec_in_window(w, f)
  65.     local x, y = term.getCursorPos()
  66.     local last = term.redirect(w)
  67.     f()
  68.     term.redirect(last)
  69.     w.redraw()
  70.     term.setCursorPos(x, y)
  71. end
  72.  
  73. local function write_char(txt)
  74.     exec_in_window(message_window, function()
  75.         write(txt)
  76.     end)
  77. end
  78.  
  79. local function sender()
  80.     term.redirect(send_window)
  81.     term.setBackgroundColor(colors.lightGray)
  82.     term.setTextColor(colors.white)
  83.     term.clear()
  84.     while true do
  85.         local msg = read()
  86.         send(msg)
  87.     end
  88. end
  89.  
  90. local function receiver()
  91.     while true do
  92.         receive(write_char)
  93.         write_char "\n"
  94.     end
  95. end
  96.  
  97. parallel.waitForAll(sender, receiver)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement