Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = peripheral.find "modem"
- local o = peripheral.find "monitor"
- local p = peripheral.find "printer"
- local log_channel, job_channel = 404, 1337
- o.setCursorPos(1, 1)
- o.clear()
- o.setTextScale(0.5)
- m.open(job_channel)
- local function log(msg)
- m.transmit(log_channel, job_channel, msg)
- local last_term = term.redirect(o)
- if type(msg) == "string" then print(msg) end
- term.redirect(last_term)
- end
- local function make_printer_terminal(printer)
- local function stub() end
- local title = ""
- local printerTerminal = {
- getCursorPos = printer.getCursorPos,
- getSize = printer.getPageSize,
- write = printer.write,
- isColor = function() return false end,
- isColour = function() return false end,
- blit = stub,
- clear = stub,
- clearLine = stub,
- setCursorBlink = stub,
- setTextColor = stub,
- setBackgroundColor = stub,
- getBackgroundColor = function() return colors.white end,
- getTextColor = function() return colors.black end
- }
- function printerTerminal.setCursorPos(x, y)
- local w, h = printer.getPageSize()
- if y > (h + 1) then printerTerminal.scroll() y = y - h end
- printer.setCursorPos(x, y)
- end
- function printerTerminal.makePage()
- while not printer.newPage() do
- if printer.getPaperLevel() < 1 then
- log "Add more paper."
- end
- if printer.getInkLevel() < 1 then
- log "Add more ink."
- end
- log "Hit any key on print server when done."
- os.pullEvent "key"
- end
- printer.setPageTitle(title)
- end
- function printerTerminal.finishPage()
- log "Clear output tray."
- while not printer.endPage() do
- sleep(1)
- end
- end
- function printerTerminal.scroll()
- printerTerminal.finishPage()
- printerTerminal.makePage()
- end
- function printerTerminal.setPageTitle(t)
- title = t
- end
- return printerTerminal
- end
- local queue = {}
- local function do_printy_stuff()
- local pterm = make_printer_terminal(p)
- while true do
- local job
- repeat
- job = table.remove(queue, 1)
- sleep(1)
- until job ~= nil
- job.title = job.title or "Untitled"
- term.redirect(pterm)
- log { type = "job_running" }
- pterm.makePage()
- log(string.format("Printing %d chars for job %s.", #job.text, job.title))
- pterm.setPageTitle(job.title)
- print(job.text)
- pterm.finishPage()
- log "Printing complete."
- log { type = "job_complete" }
- end
- end
- local function respond_to_commands()
- while true do
- local _, _, chan, reply_chan, message = os.pullEvent "modem_message"
- if chan == job_channel and reply_chan == log_channel and type(message) == "table" and message.text and message.title then
- log { type = "job_starting" }
- table.insert(queue, message)
- else
- print "Ignoring invalid message."
- end
- end
- end
- local function handle_terminate()
- os.pullEventRaw "terminate"
- log "Print job manually terminated."
- log { type = "job_complete" }
- end
- parallel.waitForAny(respond_to_commands, do_printy_stuff, handle_terminate)
Add Comment
Please, Sign In to add comment