Jyzarc

ModemRS peripheral

Jan 8th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local MODEM_RS_CHANNEL = 127
  2. local ID = os.getComputerID()
  3. local modems = {}
  4. local side,channel,message
  5. local data = {}
  6. local waitForRS = false
  7. local event
  8. local eventSide
  9.  
  10. local function startup()
  11.     term.clear()
  12.     term.setCursorPos(1,1)
  13.     print("RS BOT "..ID)
  14.     for i = 1,6 do
  15.         if peripheral.getType(rs.getSides()[i]) == "modem" then
  16.             modems[#modems+1] = peripheral.wrap(rs.getSides()[i])
  17.         end
  18.     end
  19.     for i = 1,#modems do
  20.         modems[i].open(MODEM_RS_CHANNEL)
  21. end
  22.  
  23. local function send(command)
  24.     for i = 1,#modems do
  25.         peripheral.call(side,"transmit",MODEM_RS_CHANNEL,ID,command)
  26.     end
  27. end
  28.  
  29. startup()
  30.  
  31. while true do
  32.     event,side,channel,_,message = os.pullEvent("modem_message")
  33.     _ = nil
  34.     if event == "redstone" then
  35.         if waitingForRS then
  36.             side = eventSide
  37.             send("gotEvent")
  38.         end
  39.     elseif event == "modem_message" then
  40.         data = textutils.unserialize(message)
  41.         if channel == MODEM_RS_CHANNEL
  42.         and data[4] == ID then
  43.             if data[1] == "setOutput" then
  44.                 rs.setOutput(data[3],data[2])
  45.             elseif data[1] == "setAnalogOutput" then
  46.                 rs.setAnalogOutput(data[3],data[2])
  47.             elseif data[1] == "setBundledOutput" then
  48.                 rs.setBundledOutput(data[3],data[2])
  49.             elseif data[1] == "getInput" then
  50.                 send(rs.getInput(data[3]))
  51.             elseif data[1] == "getAnalogInput" then
  52.                 send(rs.getAnalogInput(data[3]))
  53.             elseif data[1] == "getBundledInput" then
  54.                 send(rs.getBundledInput(data[3]))
  55.             elseif data[1] == "getOutput" then
  56.                 send(rs.getOutput(data[3]))
  57.             elseif data[1] == "getAnalogOutput" then
  58.                 send(rs.getAnalogOutput(data[3]))
  59.             elseif data[1] == "getBundledOutput" then
  60.                 send(rs.getBundledOutput(data[3]))
  61.             elseif data[1] == "testBundledInput" then
  62.                 send(rs.testBundledOutput(data[3],data[2]))
  63.             end
  64.         end
  65.         if channel == MODEM_RS_CHANNEL then
  66.             if data[1] == "event true" then
  67.                 eventSide = side
  68.                 waitForRS = true
  69.             elseif data[1] == "event false" then
  70.                 waitForRS = false
  71.             end
  72.         end
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment