Guest User

nanomachines.lua

a guest
Feb 1st, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.62 KB | None | 0 0
  1. local event = require("event")
  2. local modem = require("component").modem
  3.  
  4. local class = {}
  5.  
  6. local CODE_NANOMACHINES = "nanomachines"
  7.  
  8. local PORT = 1
  9.  
  10. function class.send(...)
  11.   modem.broadcast(1, CODE_NANOMACHINES, ...)
  12. end
  13.  
  14. function class.init(port)
  15.   modem.close(PORT)
  16.   PORT = port
  17.   class.send("setResponsePort", PORT)
  18. end
  19.  
  20. function class.listen(callback)
  21.   if (not modem.isOpen(PORT)) then
  22.     modem.open(PORT)
  23.   end
  24.   event.listen("modem_message", function(_, _, _, _, _, header, ...)
  25.     if (header == CODE_NANOMACHINES) then
  26.       callback(...)
  27.     end
  28.   end)
  29. end
  30.  
  31. function class.getPort()
  32.   return PORT
  33. end
  34.  
  35. return class
Advertisement
Add Comment
Please, Sign In to add comment