Advertisement
TwoThe

nanoLib

Feb 1st, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   modem.open(PORT)
  18.   class.send("setResponsePort", PORT)
  19. end
  20.  
  21. function class.listen(callback)
  22.   if (not modem.isOpen(PORT)) then
  23.     modem.open(PORT)
  24.   end
  25.   event.listen("modem_message", function(_, _, _, _, _, header, ...)
  26.     if (header == CODE_NANOMACHINES) then
  27.       callback(...)
  28.     end
  29.   end)
  30. end
  31.  
  32. function class.getPort()
  33.   return PORT
  34. end
  35.  
  36. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement