Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Creates a monitor class with these methods:
- -- Finds the position of the monitor if directly connected
- -- Finds the monitor if connected by modem and network cable
- -- Get the monitor size
- -- Get/Set monitor text scale. Size is re-calculated
- -- Get Advanced (Colour)/Normal (Monochrome) status
- -- Write to monitor at specific location, text colour and back colour
- -- Divide monitor into a grid for calculating monitor_touch locations, eg 4x2
- -- Read monitor_touch from either grid coordinates or actual, eg 40x12 cols/rows or 4x2 grid
- function clsMonitor()
- clsMonitor = {}
- clsMonitor.__index = clsMonitor
- local self = setmetatable({}, clsMonitor)
- self.monitor = {}
- self.x = 0
- self.y = 0
- self.gridX = 1
- self.gridY = 1
- self.width = 0
- self.height = 0
- self.textColour = colors.white
- self.backColour = colors.black
- self.textScale = 1
- self.isColour = false
- self.gridWidth = 1
- self.gridHeight = 1
- self.side = ""
- local strSides = {"front","back","top", "bottom","right","left","monitor_0"}
- local bFound = false
- for i = 1, 7 do
- if peripheral.getType(strSides[i]) == 'monitor' then
- self.side = strSides[i]
- bFound = true
- break
- end
- end
- if bFound then
- self.monitor = peripheral.wrap(self.side)
- self.width, self.height = self.monitor.getSize()
- if self.monitor.isColor() then
- self.isColour = true
- end
- print("monitor found: "..self.side..", cols="..self.width..", rows="..self.height)
- end
- function clsMonitor.setTextScale(self, iScale)
- self.monitor.setTextScale(iScale)
- self.width, self.height = self.monitor.getSize()
- print("monitor textScale set: "..iScale..", cols="..self.width..", rows="..self.height)
- end
- function clsMonitor.getTextScale(self)
- return self.monitor.getTextScale()
- end
- function clsMonitor.clearMonitor(self, fColour, bColour)
- self.monitor.clear()
- self.monitor.setCursorPos(1,1)
- self.textColour = colors.white --white
- self.backColour = colors.black --black
- end
- function clsMonitor.write(self, x, y, text, fColour, bColour)
- fColour = fColour or self.textColour
- bColour = bColour or self.backColour
- if self.isColour then
- self.monitor.setTextColor(fColour)
- self.monitor.setBackgroundColor(bColour)
- else
- if fColour > 0 then
- self.textColour = 1
- self.backColour = 0
- else
- self.textColour = 0
- self.backColour = 1
- end
- end
- self.monitor.setCursorPos(1,1)
- self.monitor.write(text)
- self.monitor.setTextColor(self.textColour)
- self.monitor.setBackgroundColor(self.backColour)
- end
- function clsMonitor.getTouch(self)
- local touch = false
- while not touch do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" then
- touch = true
- self.x = x
- self.y = y
- end
- end
- return self.x, self.y
- end
- function clsMonitor.getGridRef(self)
- local touch = false
- while not touch do
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" then
- -- debug
- -- print(tostring(event)..", x="..tostring(x)..", y="..tostring(y))
- touch = true
- for i = self.gridWidth - 1, 0, -1 do
- if x >= (self.width / self.gridWidth) * i then --eg 4x2 39/4 =10
- self.gridX = i + 1
- break
- end
- end
- for i = self.gridHeight - 1, 0 , -1 do
- if y >= (self.height / self.gridHeight) * i then --eg 4x2 39/4 =10
- self.gridY = i + 1
- break
- end
- end
- end
- sleep(0.1)
- end
- return self.gridX, self.gridY
- end
- function clsMonitor.setGrid(self, width, height)
- self.gridWidth = width
- self.gridHeight = height
- print("Monitor grid set to "..width.."x"..height)
- end
- function clsMonitor.drawGrid(self)
- -- write spaces of alternating background colours over the monitor
- local backcolour = colors.white
- -- get the number of columns of each cell
- local numSpaces = math.ceil(self.width / self.gridWidth)
- -- Get the number of rows of each cell
- local numRows = math.ceil(self.height / self.gridHeight)
- -- Create a string of spaces the width of each cell
- local spaceBar = ""
- for i = 1, numSpaces do
- spaceBar = spaceBar.." "
- end
- -- Draw each cell as a series of rows of spaces using alternate black/white colours
- for i = 0, self.gridWidth - 1 do -- run 4x in 4x2 grid
- if i == 0 or i % 2 == 0 then
- backcolour = colors.white
- else
- backcolour = colors.black
- end
- for j = 0, self.gridHeight - 1 do -- run 2x in 4x2 grid
- for square = 1, numRows do
- self.monitor.setCursorPos(i * (self.width / self.gridWidth) + 1, j * numRows + square)
- self.monitor.setBackgroundColor(backcolour)
- self.monitor.write(spaceBar)
- end
- -- change background for next square
- if backcolour == colors.white then
- backcolour = colors.black
- else
- backcolour = colors.white
- end
- end
- end
- end
- return self
- end
- function main()
- -- Example for demonstration/testing purposes
- -- Build a monitor 4 blocks wide x 2 blocks high
- mon = clsMonitor() -- create monitor object
- mon:clearMonitor(0,1) -- clear monitor and set text/background colours
- mon:setGrid(4,3) -- set grid size eg 4 rows x 2 cols (does not have to be same as physical size)
- mon:drawGrid() -- Draw demo grid of 4x2
- while true do -- test routine to output coordinates of the grid touched
- local x, y = mon:getGridRef()
- -- mon:write(cursorPos X, cursorPos Y, Text, text colour, background colour)
- mon:write(1, 1, "x="..x..", y="..y, 2, 1)-- write to monitor at 1,1, using orange text on white background
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement