Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local computer = require("computer")
- local os = require("os")
- local term = require("term")
- local modem = component.modem
- local CONTROLLER_PORT = 1
- local WORKER_PORT = 2
- modem.open(1)
- local function tickTime()
- return os.time() * (1000/60/60)
- end
- local function handle_ping(address)
- modem.send(address, WORKER_PORT)
- end
- local noteQueue = {}
- local function handle_note(pitch, duration, time)
- table.insert(noteQueue, {pitch = pitch, duration = duration, time = time})
- end
- local function process_queue()
- if #noteQueue == 0 then return end
- local currentTime = tickTime()
- while #noteQueue > 0 and noteQueue[1].time <= currentTime do
- local note = table.remove(noteQueue, 1)
- computer.beep(note.pitch, note.duration)
- end
- end
- term.clear()
- print("Running worker...")
- while true do
- local id, _, remoteAddress, port, _, protocol, data1, data2, data3 = event.pullMultiple("modem_message", "interrupted")
- if id == "interrupted" then
- print("Interrupted. Exiting...")
- break
- end
- if port == CONTROLLER_PORT then
- if protocol == "ping" then
- handle_ping(remoteAddress)
- elseif protocol == "note" then
- handle_note(data1, data2, data3)
- end
- end
- process_queue()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement