Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = {}
- -- [[ Requires ]]
- local computer = require("computer")
- local component = require("component")
- -- Used for monitor.test, you can comment these out if you don't use it
- local event = require("event")
- local thread = require("thread")
- -- [[ Constants ]]
- local gpu = component.gpu
- local screen = component.screen
- -- [[ Variables ]]
- -- [[ Local functions ]]
- local function calculateOptimalResolution()
- local monitorWidth, monitorHeight = screen.getAspectRatio()
- local ar = monitorWidth / monitorHeight
- local maxWidth, maxHeight = gpu.maxResolution()
- if ar == 1.5 then
- return maxWidth, maxHeight
- end
- local height = maxHeight
- local width = ar * 2 * height
- if width > maxWidth then
- local perc = width / maxWidth
- height = height * (1 / perc)
- width = ar * 2 * height
- end
- return width, height
- end
- -- [[ Exported functions ]]
- monitor.fill = function()
- local w, h = calculateOptimalResolution()
- gpu.setResolution(calculateOptimalResolution())
- return w, h
- end
- monitor.test = function()
- local listener = function(_, _, type)
- if type == "screen" then
- local w, h = monitor.fill()
- require("term").clear()
- gpu.setBackground(0xFFFFFF)
- gpu.set(1,1,(" "):rep(w))
- gpu.set(1,1,(" "):rep(h), true)
- gpu.set(w,1,(" "):rep(h), true)
- gpu.set(1,h,(" "):rep(w))
- gpu.setBackground(0x000000)
- print(w, h)
- end
- end
- event.listen("component_added", listener)
- event.listen("component_removed", listener)
- -- allows us to press ctrl+c to exit cleanly
- event.pull("interrupted")
- event.ignore("component_added", listener)
- event.ignore("component_removed", listener)
- end
- -- [[ Exporting ]]
- return monitor
- -- [[ EOF ]]
Advertisement
Add Comment
Please, Sign In to add comment