Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = {}
- -- [[ Requires ]]
- local computer = require("computer")
- local component = require("component")
- local event = require("event")
- local thread = require("thread")
- -- [[ Constants ]]
- local gpu = component.gpu
- local screen = component.screen
- local resolutions = {
- { 50, 16 },
- { 80, 25 },
- { 160, 50 },
- }
- -- [[ Variables ]]
- -- [[ Local functions ]]
- local function calculateOptimalResolution()
- local monitorWidth, monitorHeight = screen.getAspectRatio()
- local ar = monitorWidth / monitorHeight
- local screenWidth, screenHeight = monitor.getResolutionFromTier(monitor.getScreenTier())
- local gpuMaxWidth, gpuMaxHeight = monitor.getResolutionFromTier(monitor.getGPUTier())
- local maxWidth = math.min(screenWidth, gpuMaxWidth)
- local maxHeight = math.min(screenHeight, gpuMaxHeight)
- 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
- -- Works for gpu and screen
- local function getTier(device)
- local info = computer.getDeviceInfo()[device.address]
- -- width is the bit depth
- if info.width == "1" then
- return 1
- elseif info.width == "4" then
- return 2
- elseif info.width == "8" then
- return 3
- end
- end
- -- [[ Exported functions ]]
- monitor.getGPUTier = function()
- return getTier(gpu)
- end
- monitor.getScreenTier = function()
- return getTier(screen)
- end
- monitor.getResolutionFromTier = function(tier)
- return resolutions[tier][1], resolutions[tier][2]
- end
- monitor.fill = function()
- local w,h = calculateOptimalResolution()
- print(w, h)
- gpu.setResolution(calculateOptimalResolution())
- end
- monitor.test = function()
- local listener = function(_, _, type)
- if type == "screen" then
- monitor.fill()
- require("term").clear()
- gpu.setBackground(0xFFFFFF)
- local w,h = gpu.getResolution()
- 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 ]]
Add Comment
Please, Sign In to add comment