View difference between Paste ID: 3WKmxJrr and cjziUQ3r
SHOW: | | - or go back to the newest paste.
1-
local event = require("event")
1+
local event = require("event")
2-
local modem = require("component").modem
2+
local modem = require("component").modem
3-
3+
4-
local class = {}
4+
local class = {}
5-
5+
6-
local CODE_NANOMACHINES = "nanomachines"
6+
local CODE_NANOMACHINES = "nanomachines"
7-
7+
8-
local PORT = 1
8+
local PORT = 1
9-
9+
10-
function class.send(...)
10+
function class.send(...)
11-
  modem.broadcast(1, CODE_NANOMACHINES, ...)
11+
  modem.broadcast(1, CODE_NANOMACHINES, ...)
12-
end
12+
end
13-
13+
14-
function class.init(port)
14+
function class.init(port)
15-
  modem.close(PORT)
15+
  modem.close(PORT)
16-
  PORT = port
16+
  PORT = port
17-
  class.send("setResponsePort", PORT)
17+
  modem.open(PORT)
18-
end
18+
  class.send("setResponsePort", PORT)
19-
19+
end
20-
function class.listen(callback)
20+
21-
  if (not modem.isOpen(PORT)) then
21+
function class.listen(callback)
22-
    modem.open(PORT)
22+
  if (not modem.isOpen(PORT)) then
23-
  end
23+
    modem.open(PORT)
24-
  event.listen("modem_message", function(_, _, _, _, _, header, ...) 
24+
  end
25-
    if (header == CODE_NANOMACHINES) then
25+
  event.listen("modem_message", function(_, _, _, _, _, header, ...) 
26-
      callback(...) 
26+
    if (header == CODE_NANOMACHINES) then
27-
    end
27+
      callback(...) 
28-
  end)
28+
    end
29-
end
29+
  end)
30-
30+
end
31-
function class.getPort() 
31+
32-
  return PORT
32+
function class.getPort() 
33-
end
33+
  return PORT
34-
34+
end
35
36
return class