Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This is the classroom monitor setup routine
- --written by Matt Kelliher
- --
- --First: we assume there are 4 equal-sized
- --monitors in the room connected to the network
- --we'll find them on the network,
- --give them names based on color, and
- --wrap them with a variable
- local Rmon, Gmon, Bmon, Pmon = peripheral.find("monitor", function(name, object) return object.isColor() end)
- --We'll also get the dimensions of the monitors
- --and store them (we'll assume all monitors are
- --the same number of columns and rows)
- local monRows, monColumns
- --A useful function for centering text
- --on a monitor from the ComputerCraft forums
- function centerPrint(text, y, mon)
- local w, h = mon.getSize() or term.getSize()
- if mon then
- mon.setCursorPos( math.ceil( w/2 - #text/2 + 1), y )
- mon.write( text)
- else
- mon.setCursorPos( math.ceil( w/2 - #text/2 ), y )
- term.write( text )
- end
- end
- --A function that wraps text on monitor
- --and calls the center function
- function printCenterWrap( mon, text, size )
- --size is an optional parameter,
- --we default to 0.5
- if size == nil then size = 0.5 end
- if size > 0 then mon.setTextScale(size) end
- local MaxCols, MaxRows = mon.getSize()
- local stBuffer = text
- local rowCount = 1
- print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows)
- while string.len(stBuffer) > 0 and rowCount <= MaxRows do
- --print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")")
- --find if there's a space character
- local space = string.find(stBuffer, " ")
- if space == nil or space >= MaxCols then
- --print("found no space before EOL")
- --This is the easiest case: we didn't
- --find a space before the End-of-Line MaxCols
- --so just print a substring up to EOL
- mon.setCursorPos(1,rowCount) --center later
- --print("SetCursorPos(1,"..rowCount..")")
- local wl = string.sub(stBuffer,1,MaxCols)
- mon.write(wl) --center this later
- --print("Wrote:"..wl)
- if (MaxCols + 1) > (string.len(stBuffer)) then
- --we're don't processing
- stBuffer = ""
- --print("Set Buffer to EOL")
- else
- stBuffer = string.sub(stBuffer, MaxCols + 1)
- --print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
- end
- else
- print("found space at: "..space)
- local sentence = ""
- local nextspace
- --loop to find the next space in the buffer
- local word = string.sub(stBuffer,1,space)
- print("Word: "..word)
- repeat
- sentence = sentence..word
- print("Sentence: "..sentence.." ("..string.len(sentence)..")")
- --test that we don't go past the end
- if (string.len(word)+1) > string.len(stBuffer) then
- stBuffer = ""
- else
- stBuffer = string.sub(stBuffer,string.len(word)+1)
- end
- nextspace = string.find(stBuffer, " ")
- if nextspace == nil then
- print("nextspacce: nil")
- else
- print("nextspace: "..nextspace)
- end
- word = string.sub(stBuffer,1,nextspace)
- print("nextword: "..word)
- until nextspace == nil or (string.len(sentence)+nextspace) >= MaxCols
- mon.setCursorPos(1, rowCount)
- print("SetCursorPos(1, "..rowCount..")")
- if nextspace == nil then
- mon.write(sentence..word)
- print("Wrote:"..sentence..word)
- else
- mon.write(sentence)
- print("Wrote:"..sentence)
- end
- end --of finding a space
- --increment row counter
- rowCount = rowCount + 1
- end --while for each row
- end
- --Have a Function to setup monitors where you
- --Input/Parameter the monitor object/variable
- --and the colors.color you want it to be
- function setupMonitor(mon, xcolor)
- mon.clear()
- mon.setTextScale(0.5)
- mon.setCursorPos(1,1)
- mon.setBackgroundColor(xcolor)
- mon.clear()
- mon.setCursorPos(1,1)
- --mon.write("Welcome to Beyond Minecraft 101!")
- --centerPrint("Welcome to Beyond Minecraft 101!", 1, mon)
- --printCenter(mon,"Welcome to Beyond Minecraft 101!")
- printCenterWrap(mon, "Welcome to Beyond Minecraft 101!")
- end
- --This is our main control function
- --to run the program
- function main()
- --Determine which of the 4 monitors we have
- --on our network and set them up accordingly
- if Rmon then
- setupMonitor(Rmon, colors.red)
- --now assign the size of the first monitor
- --to be the size of all monitors
- monCols, monRows = Rmon.getSize()
- --print("Connected Red Monitor, size: "..monCols.." columns "..monRows.." rows")
- end
- if Gmon then
- --setupMonitor(Gmon, colors.green)
- --print("Connected Green Monitor")
- end
- if Bmon then
- --setupMonitor(Bmon, colors.blue)
- --print("Connected Blue Monitor")
- end
- if Pmon then
- --setupMonitor(Pmon, colors.purple)
- --print("Connected Purple Monitor")
- end
- end
- --Now kick-off our main program:
- main()
Advertisement
Add Comment
Please, Sign In to add comment