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