Guest User

Untitled

a guest
Dec 2nd, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local monitor = {}
  2.  
  3. -- [[ Requires ]]
  4.  
  5. local computer = require("computer")
  6. local component = require("component")
  7.  
  8. -- Used for monitor.test, you can comment these out if you don't use it
  9. local event = require("event")
  10. local thread = require("thread")
  11.  
  12. -- [[ Constants ]]
  13.  
  14. local gpu = component.gpu
  15. local screen = component.screen
  16.  
  17. -- [[ Variables ]]
  18.  
  19. -- [[ Local functions ]]
  20.  
  21. local function calculateOptimalResolution()
  22.   local monitorWidth, monitorHeight = screen.getAspectRatio()
  23.   local ar = monitorWidth / monitorHeight
  24.  
  25.   local maxWidth, maxHeight = gpu.maxResolution()
  26.  
  27.   if ar == 1.5 then
  28.     return maxWidth, maxHeight
  29.   end
  30.  
  31.   local height = maxHeight
  32.   local width = ar * 2 * height
  33.  
  34.   if width > maxWidth then
  35.     local perc = width / maxWidth
  36.     height = height * (1 / perc)
  37.     width = ar * 2 * height
  38.   end
  39.  
  40.   return width, height
  41. end
  42.  
  43. -- [[ Exported functions ]]
  44.  
  45. monitor.fill = function()
  46.   local w, h = calculateOptimalResolution()
  47.   gpu.setResolution(calculateOptimalResolution())
  48.  
  49.   return w, h
  50. end
  51.  
  52. monitor.test = function()
  53.   local listener = function(_, _, type)
  54.     if type == "screen" then
  55.       local w, h = monitor.fill()
  56.       require("term").clear()
  57.       gpu.setBackground(0xFFFFFF)
  58.       gpu.set(1,1,(" "):rep(w))
  59.       gpu.set(1,1,(" "):rep(h), true)
  60.       gpu.set(w,1,(" "):rep(h), true)
  61.       gpu.set(1,h,(" "):rep(w))
  62.       gpu.setBackground(0x000000)
  63.       print(w, h)
  64.     end
  65.   end
  66.  
  67.   event.listen("component_added", listener)
  68.   event.listen("component_removed", listener)
  69.   -- allows us to press ctrl+c to exit cleanly
  70.   event.pull("interrupted")
  71.   event.ignore("component_added", listener)
  72.   event.ignore("component_removed", listener)
  73. end
  74.  
  75. -- [[ Exporting ]]
  76.  
  77. return monitor
  78.  
  79. -- [[ EOF ]]
Advertisement
Add Comment
Please, Sign In to add comment