Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Usage: show_img <nfp_url> [monitor_side_or_name]
- -- Example: show_img https://example.com/picture.nfp right
- local args = {...}
- if #args < 1 then
- print("Usage: show_img <nfp_url> [monitor_side_or_name]")
- return
- end
- local url = args[1]
- local monId = args[2] -- optional: side ("right") or peripheral name
- if not http then
- error("HTTP API is disabled. Enable http.enabled=true in the config.")
- end
- -- Find monitor
- local mon
- if monId then
- if peripheral.isPresent(monId) and peripheral.getType(monId) == "monitor" then
- mon = peripheral.wrap(monId)
- else
- error("Monitor not found at '"..monId.."'")
- end
- else
- mon = peripheral.find("monitor")
- if not mon then error("No monitor connected.") end
- end
- -- Download the NFP image to a temp file
- local tmpPath = "/.dl_image.nfp"
- local function download(url, path)
- local h = http.get(url, {["Cache-Control"]="no-cache"})
- if not h then error("HTTP GET failed for URL: "..url) end
- local data = h.readAll()
- h.close()
- local f = fs.open(path, "w")
- f.write(data)
- f.close()
- end
- local function drawNfp(path, monitor)
- local img = paintutils.loadImage(path)
- if not img then error("Could not parse NFP image.") end
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- smallest text makes the “pixels” as fine as possible
- monitor.setTextScale(0.5)
- local old = term.redirect(monitor)
- -- Draw at top-left (1,1). No scaling is possible with NFP.
- paintutils.drawImage(img, 1, 1)
- term.redirect(old)
- end
- -- Main
- download(url, tmpPath)
- local ok, err = pcall(drawNfp, tmpPath, mon)
- if not ok then
- print("Error: "..tostring(err))
- else
- print("Image drawn on monitor.")
- end
- -- Optional: clean up the temp file
- -- pcall(fs.delete, tmpPath)
Advertisement
Add Comment
Please, Sign In to add comment