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