View difference between Paste ID: TVht54Zb and nLZRUxZH
SHOW: | | - or go back to the newest paste.
1
--This is the classroom monitor setup routine
2
--written by Matt Kelliher
3
--
4
-- Useage: <program_name> [-d] [-A|-R|-G|-B|-P] <text>
5
-- If you would like to see debugging comments on the terminal then pass in the -d
6
-- option when you run this program before your text
7
local tArgs = { ... }
8
assert( #tArgs >= 1, "Useage: <program_name> [-d] <text>" )
9
10
--Parse the flags:
11
local param1 = tArgs[1]
12
local param2 --optional
13
local param3 --optional
14
if #tArgs >= 2 then param2 = tArgs[2] end
15
if #tArgs >= 3 then param3 = tArgs[3] end
16
17
--Now figure out if the debug flag was set
18
local debug = false
19
local GLOBALtext = ""
20
if param1 == "-d" then 
21
  debug = true
22
  assert( #tArgs >= 2, "Useage: <program_name> [-d] <text>" )
23
  GLOBALtext = param2
24
else
25
  GLOBALtext = param1
26
end
27
28
29
--First: we assume there are 4 equal-sized
30
--monitors in the room connected to the network
31
--we'll find them on the network,
32
--give them names based on color, and
33
--wrap them with a variable
34
local Rmon, Gmon, Bmon, Pmon = peripheral.find("monitor", function(name, object) return object.isColor() end)
35
36
--We'll also get the dimensions of the monitors
37
--and store them (we'll assume all monitors are 
38
--the same number of columns and rows)
39
local monRows, monColumns
40
41
--A useful function for centering text
42
--on a monitor from the ComputerCraft forums
43-
  print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows)
43+
44
  local w, h = mon.getSize() or term.getSize()
45-
    print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")")
45+
46-
    t=read() --pause to debug
46+
47
    mon.write( text)
48
  else
49
    mon.setCursorPos( math.ceil( w/2 - #text/2 ), y )
50
    term.write( text )
51-
      print("found no space before EOL")
51+
52
end
53
54
--A function that wraps text on monitor
55-
      mon.setCursorPos(1,rowCount) --center later
55+
56-
      --print("SetCursorPos(1,"..rowCount..")")
56+
57
  --size is an optional parameter,
58-
      mon.write(wl) --center this later
58+
59-
      --print("Wrote:"..wl)
59+
60
  
61
  if size > 0 then mon.setTextScale(size) end
62
  
63-
        --print("Set Buffer to EOL")
63+
64
  local stBuffer = text
65
  local rowCount = 1
66-
        --print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
66+
67
  if debug then print("MaxCols: "..MaxCols.." MaxRows: "..MaxRows) end
68
  while string.len(stBuffer) > 0 and rowCount <= MaxRows do
69-
      print("found space at: "..space)
69+
    if debug then 
70
      print("LoopStartBuffer: "..stBuffer.." ("..string.len(stBuffer)..")")
71
      t=read() --pause to debug
72
    end
73
74-
      print("Word: "..word)
74+
75-
      repeat
75+
76-
        s=read() --pause to debug
76+
77
    if space == nil or space >= MaxCols then
78-
        print("Sentence: "..sentence.." ("..string.len(sentence)..")")
78+
      if debug then print("found no space before EOL") end
79
      --This is the easiest case: we didn't
80
      --find a space before the End-of-Line MaxCols
81
      --so just print a substring up to EOL
82
      --For Left Justify: mon.setCursorPos(1,rowCount)
83
      if debug then print("SetCursorPos(1,"..rowCount..")") end
84-
          print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
84+
85
      --mon.write(wl) --center this later
86
      centerPrint(wl,rowCount,mon)
87
      if debug then print("Wrote:"..wl) end
88
      if (MaxCols + 1) > (string.len(stBuffer)) then
89-
          print("nextspace: nil")
89+
90
        stBuffer = ""
91-
          print("nextspace: "..nextspace)
91+
        if debug then print("Set Buffer to EOL") end
92
      else
93
        stBuffer = string.sub(stBuffer, MaxCols + 1)
94
        if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
95-
        print("nextword: "..word.." ("..string.len(word)..")")
95+
96
    else
97
      if debug then print("found space at: "..space) end
98
      local sentence = ""
99-
      mon.setCursorPos(1, rowCount)
99+
100-
      print("SetCursorPos(1, "..rowCount..")")
100+
101
      local word = string.sub(stBuffer,1,space)
102-
        mon.write(sentence..word)
102+
      if debug then print("Word: "..word) end
103-
        print("Wrote sen+word:"..sentence..word.." ("..#sentence+#word..")")
103+
      repeat        
104
        sentence = sentence..word
105
        if debug then 
106
          print("Sentence: "..sentence.." ("..string.len(sentence)..")")
107-
            print("Cleared the buffer")
107+
          s=read() --pause to debug
108
        end
109
        --test that we don't go past the end
110-
          print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")")
110+
111
          stBuffer = ""
112
        else
113-
        mon.write(sentence)
113+
114-
        print("Wrote sen:"..sentence.." ("..#sentence..")")
114+
          if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
115
        end
116
        
117
        nextspace = string.find(stBuffer, " ")
118
        if nextspace == nil then
119
          if debug then print("nextspace: nil") end
120
        else
121
          if debug then print("nextspace: "..nextspace) end
122
        end
123
        
124-
--Have a Function to setup monitors where you
124+
125
        if debug then print("nextword: "..word.." ("..string.len(word)..")") end
126
        
127-
function setupMonitor(mon, xcolor)
127+
128
      
129
      --LEFT JUSTIFY: mon.setCursorPos(1, rowCount)
130
      if debug then print("SetCursorPos(1, "..rowCount..")") end
131
      if nextspace == nil then
132
        --mon.write(sentence..word)
133
        centerPrint(sentence..word,rowCount,mon)
134-
  --mon.write("Welcome to Beyond Minecraft 101!")
134+
        if debug then print("Wrote sen+word:"..sentence..word.." ("..#sentence+#word..")") end
135-
  --centerPrint("Welcome to Beyond Minecraft 101!", 1, mon)
135+
136-
  --printCenter(mon,"Welcome to Beyond Minecraft 101!")
136+
137-
  printCenterWrap(mon, "Welcome to Beyond Minecraft 101 Class!", 3)
137+
138
            if debug then print("Cleared the buffer") end
139
        else
140
          stBuffer = string.sub(stBuffer,string.len(word)+1)
141
          if debug then print("Set Buffer to:"..stBuffer.." ("..string.len(stBuffer)..")") end
142
        end
143
      else
144
        --mon.write(sentence)
145
        centerPrint(sentence,rowCount,mon)
146
        if debug then print("Wrote sen:"..sentence.." ("..#sentence..")") end
147-
    setupMonitor(Rmon, colors.red)
147+
148-
    --now assign the size of the first monitor
148+
149-
    --to be the size of all monitors
149+
150-
    monCols, monRows = Rmon.getSize()
150+
151
    --increment row counter
152
    rowCount = rowCount + 1
153-
--[[  
153+
154
  
155-
    setupMonitor(Gmon, colors.green)
155+
156
157
158
--Have a Function to clear monitors where you
159-
    setupMonitor(Bmon, colors.blue)
159+
160
--and the colors.color you want it to be
161
function clearMonitor(mon, xcolor)
162
  mon.clear()
163-
    setupMonitor(Pmon, colors.purple)
163+
164
  mon.setCursorPos(1,1)
165
  mon.setBackgroundColor(xcolor)
166-
--]]
166+
167
  mon.setCursorPos(1,1)
168
  --printCenterWrap(mon, "Welcome to Beyond Minecraft 101 Class!", 3)
169
end
170
171
--This function will display text on all monitiors at once
172
function displayOnAll(text, size)
173
  if size == nil then size = 0.5 end
174
  if Rmon then
175
    clearMonitor(Rmon, colors.red)
176
    printCenterWrap(Rmon, text, size)
177
  end
178
  if Gmon then
179
    clearMonitor(Gmon, colors.green)
180
    printCenterWrap(Gmon, text, size)
181
  end
182
  if Bmon then
183
    clearMonitor(Bmon, colors.blue)
184
    printCenterWrap(Bmon, text, size)
185
  end
186
  if Pmon then
187
    clearMonitor(Pmon, colors.purple)
188
    printCenterWrap(Pmon, text, size)
189
  end
190
end
191
192
--This function sets up the initial settings
193
--for the monitors with their colors
194
--and a blank screen displayed
195
function initMonitors()
196
  --Determine which of the 4 monitors we have
197
  --on our network and set them up accordingly
198
  if Rmon then
199
    clearMonitor(Rmon, colors.red)
200
    print("Connected Red Monitor")
201
  end 
202
  if Gmon then
203
    clearMonitor(Gmon, colors.green)
204
    print("Connected Green Monitor")
205
  end
206
  if Bmon then
207
    clearMonitor(Bmon, colors.blue)
208
    print("Connected Blue Monitor")
209
  end 
210
  if Pmon then
211
    clearMonitor(Pmon, colors.purple)
212
    print("Connected Purple Monitor")
213
  end
214
end
215
216
217
--This is our main control function
218
--to run the program
219
function main()
220
  initMonitors()
221
  if GLOBALtext ~= "test" then
222
    displayOnAll(GLOBALtext, 3)
223
  else
224
    displayOnAll("Welcome to the Beyond Minecraft 101 Class!", 3)
225
  end
226
227
end
228
229
--Now kick-off our main program:
230
main()