Guest User

Untitled

a guest
Dec 1st, 2017
174
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. local event = require("event")
  9. local thread = require("thread")
  10.  
  11. -- [[ Constants ]]
  12.  
  13. local gpu = component.gpu
  14. local screen = component.screen
  15.  
  16. local resolutions = {
  17.   { 50, 16 },
  18.   { 80, 25 },
  19.   { 160, 50 },
  20. }
  21.  
  22. -- [[ Variables ]]
  23.  
  24. -- [[ Local functions ]]
  25.  
  26. local function calculateOptimalResolution()
  27.   local monitorWidth, monitorHeight = screen.getAspectRatio()
  28.   local ar = monitorWidth / monitorHeight
  29.  
  30.   local screenWidth, screenHeight = monitor.getResolutionFromTier(monitor.getScreenTier())
  31.  
  32.   local gpuMaxWidth, gpuMaxHeight = monitor.getResolutionFromTier(monitor.getGPUTier())
  33.   local maxWidth = math.min(screenWidth, gpuMaxWidth)
  34.   local maxHeight = math.min(screenHeight, gpuMaxHeight)
  35.  
  36.   if ar == 1.5 then
  37.     return maxWidth, maxHeight
  38.   end
  39.  
  40.   local height = maxHeight
  41.   local width = ar * 2 * height
  42.  
  43.   if width > maxWidth then
  44.     local perc = width / maxWidth
  45.     height = height * (1 / perc)
  46.     width = ar * 2 * height
  47.   end
  48.  
  49.   return width, height
  50. end
  51.  
  52. -- Works for gpu and screen
  53. local function getTier(device)
  54.   local info = computer.getDeviceInfo()[device.address]
  55.  
  56.   -- width is the bit depth
  57.   if info.width == "1" then
  58.     return 1
  59.   elseif info.width == "4" then
  60.     return 2
  61.   elseif info.width == "8" then
  62.     return 3
  63.   end
  64. end
  65.  
  66. -- [[ Exported functions ]]
  67.  
  68. monitor.getGPUTier = function()
  69.   return getTier(gpu)
  70. end
  71.  
  72. monitor.getScreenTier = function()
  73.   return getTier(screen)
  74. end
  75.  
  76. monitor.getResolutionFromTier = function(tier)
  77.   return resolutions[tier][1], resolutions[tier][2]
  78. end
  79.  
  80. monitor.fill = function()
  81.   local w,h = calculateOptimalResolution()
  82.   print(w, h)
  83.   gpu.setResolution(calculateOptimalResolution())
  84. end
  85.  
  86. monitor.test = function()
  87.   local listener = function(_, _, type)
  88.     if type == "screen" then
  89.       monitor.fill()
  90.       require("term").clear()
  91.       gpu.setBackground(0xFFFFFF)
  92.       local w,h = gpu.getResolution()
  93.       gpu.set(1,1,(" "):rep(w))
  94.       gpu.set(1,1,(" "):rep(h), true)
  95.       gpu.set(w,1,(" "):rep(h), true)
  96.       gpu.set(1,h,(" "):rep(w))
  97.       gpu.setBackground(0x000000)
  98.       print(w, h)
  99.     end
  100.   end
  101.  
  102.   event.listen("component_added", listener)
  103.   event.listen("component_removed", listener)
  104.   -- allows us to press ctrl+c to exit cleanly
  105.   event.pull("interrupted")
  106.   event.ignore("component_added", listener)
  107.   event.ignore("component_removed", listener)
  108. end
  109.  
  110. -- [[ Exporting ]]
  111.  
  112. return monitor
  113.  
  114. -- [[ EOF ]]
Add Comment
Please, Sign In to add comment