Advertisement
Jyzarc

ModemRS API

Jan 8th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. local message,senderID
  2. local MODEM_RS_CHANNEL = 127
  3. local modems = {}
  4.  
  5. --Local function, this isn't part of the API
  6. local function send(message)
  7.     for i = 1,6 do
  8.         if modems[rs.getSides()[i]] then
  9.             peripheral.call(rs.getSides()[i],"transmit",MODEM_RS_CHANNEL,MODEM_RS_CHANNEL,message)
  10.         end
  11.     end
  12. end
  13.  
  14. --This basically tells the program that this modem can be used for sending and receiving messages.
  15. function addModem(side)
  16.     modems[side] = true
  17.     peripheral.call(side,"open",MODEM_RS_CHANNEL)
  18. end
  19.  
  20. --This does the opposite of the previous method
  21. function removeModem(side)
  22.     modems[side] = false
  23.     peripheral.call(side,"open",MODEM_RS_CHANNEL)
  24. end
  25.  
  26. --This returns true if the modem is "connected"
  27. function isConnected(side)
  28.     if modems[side] then
  29.         return true
  30.     else
  31.         return false
  32.     end
  33. end
  34.  
  35. --This waits for a redstone update to occur on one of the RS bots
  36. function waitForUpdate()
  37.     message = "event true"
  38.     send(message)
  39.     while message ~= "gotEvent" do
  40.         _,_,_,_,message = os.pullEvent("modem_message")
  41.     end
  42.     message = "event false"
  43.     send(message)
  44.     return
  45. end
  46.    
  47.  
  48. --[[
  49. All of these functions work pretty much exactly as the redstone API methods.
  50. The "output" functions require a "state" argument, for setOutput this would be true or false.
  51. For setAnalogOutput it would be a number 0-15, bundled would be a number 0-65535, etc.
  52. They also require a side (the side of the output computer you want to output) and the ID of the output computer.
  53. "Get" methods just require the last two, and they return the state. testBundledOutput requires all 3 arguments.
  54. ]]
  55.  
  56. function setOutput(bool,side,id)
  57.     message = textutils.serialize("setOutput",bool,side,id)
  58.     send(message)
  59. end
  60.  
  61. function setAnalogOutput(num,side,id)
  62.     message = textutils.serialize("setAnalogOutput",num,side,id)
  63.     send(message)
  64. end
  65.  
  66. function setBundledOutput(num,side,id)
  67.     message = textutils.serialize("setBundledOutput",num,side,id)
  68.     send(message)
  69. end
  70.  
  71. function getInput(side,id)
  72.     message = textutils.serialize("getInput",0,side,id)
  73.     send(message)
  74.     while senderID ~= id do
  75.         _,_,_,senderID,message = os.pullEvent("modem_message")
  76.     end
  77.     senderID,_ = nil
  78.     return message
  79. end
  80.  
  81. function getAnalogInput(side,id)
  82.     message = textutils.serialize("getAnalogInput",0,side,id)
  83.     send(message)
  84.     while senderID ~= id do
  85.         _,_,_,senderID,message = os.pullEvent("modem_message")
  86.     end
  87.     senderID,_ = nil
  88.     return message
  89. end
  90.  
  91. function getBundledInput(side,id)
  92.     message = textutils.serialize("getBundledInput",0,side,id)
  93.     send(message)
  94.     while senderID ~= id do
  95.         _,_,_,senderID,message = os.pullEvent("modem_message")
  96.     end
  97.     senderID,_ = nil
  98.     return message
  99. end
  100.  
  101. function getOutput(side,id)
  102.     message = textutils.serialize("getOutput",0,side,id)
  103.     send(message)
  104.     while senderID ~= id do
  105.         _,_,_,senderID,message = os.pullEvent("modem_message")
  106.     end
  107.     senderID,_ = nil
  108.     return message
  109. end
  110.  
  111. function getAnalogOutput(side,id)
  112.     message = textutils.serialize("getAnalogOutput",0,side,id)
  113.     send(message)
  114.     while senderID ~= id do
  115.         _,_,_,senderID,message = os.pullEvent("modem_message")
  116.     end
  117.     senderID,_ = nil
  118.     return message
  119. end
  120.  
  121. function getBundledOutput(side,id)
  122.     message = textutils.serialize("getBundledOutput",0,side,id)
  123.     send(message)
  124.     while senderID ~= id do
  125.         _,_,_,senderID,message = os.pullEvent("modem_message")
  126.     end
  127.     senderID,_ = nil
  128.     return message
  129. end
  130.  
  131. function testBundledInput(num,side,id)
  132.     message = textutils.serialize("testBundledInput",num,side,id)
  133.     send(message)
  134.     while senderID ~= id do
  135.         _,_,_,senderID,message = os.pullEvent("modem_message")
  136.     end
  137.     senderID,_ = nil
  138.     return message
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement