Advertisement
kurruk

Monitor Program

Feb 20th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. p = peripheral.wrap("back")
  2. rednet.open("right")
  3. p.setTextScale(1.5)
  4. p.setBackgroundColor(colors.black)
  5. button={}
  6.  
  7. function split(str, delim, maxNb)
  8.  
  9.   if string.find(str, delim) == nil then
  10.     return { str }
  11.   end
  12.  
  13.   if maxNb == nil or maxNb < 1 then
  14.     maxNb = 0
  15.   end
  16.  
  17.   local result = {}
  18.   local pat = "(.-)" ..delim.. "()"
  19.   local nb = 0
  20.   local lastPos
  21.  
  22.   for part, pos in string.gmatch(str, pat) do
  23.     nb = nb + 1
  24.     result[nb] = part
  25.     if nb == maxNb then
  26.       break
  27.     end
  28.   end
  29.  
  30.   return result
  31.  
  32. end
  33.  
  34. function setTable(name, func, xmin, xmax, ymin, ymax)
  35.    button[name] = {}
  36.    button[name]["func"] = func
  37.    button[name]["active"] = false
  38.    button[name]["xmin"] = xmin
  39.    button[name]["ymin"] = ymin
  40.    button[name]["xmax"] = xmax
  41.    button[name]["ymax"] = ymax
  42. end
  43.  
  44. function cobble()
  45.   p.clear()
  46.   sleep(5)
  47.   setTable("1", send, 14, 21, 5, 9)
  48.   screenNum()
  49.   sleep(0.5)
  50.   function2()
  51. end
  52.  
  53. function send()
  54.     print(thing)
  55.     run = 1
  56. end
  57.  
  58. function fillNum(text, color, bData)
  59.   p.setBackgroundColor(colors.lime)
  60.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  61.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  62.   for j = bData["ymin"], bData["ymax"] do
  63.     p.setCursorPos(bData["xmin"], bData["ymin"])
  64.     if j == yspot then
  65.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  66.         if k == xspot then
  67.           p.write(text)
  68.         else
  69.           p.write(" ")
  70.         end
  71.       end
  72.     else
  73.       for i = bData["xmin"], bData["xmax"] do
  74.          p.write(" ")
  75.       end
  76.     end
  77.   end
  78.   p.setBackgroundColor(colors.black)
  79. end
  80.  
  81. function screenNum()
  82.   local currColor
  83.   for name,data in pairs(button) do
  84.     currColor = colors.lime
  85.     fillNum(name, currColor, data)
  86.   end
  87.   run = 0
  88. end
  89.  
  90. function checkxy(x, y)
  91.    for name, data in pairs(button) do
  92.       if y >=data["ymin"] and  y <= data["ymax"] then
  93.          if x >=data["xmin"] and x <= data["xmax"] then
  94.             data["func"]()
  95.          end
  96.       end
  97.    end
  98. end
  99.  
  100. function fill(text, color, bData)
  101.     p.setCursorPos(bData["xmin"], bData["ymin"])
  102.     p.write(text)
  103. end
  104.  
  105. function screen()
  106.    for name,data in pairs(button) do
  107.       fill(name, currColor, data)
  108.    end
  109. end
  110.  
  111. function1 = function()
  112.  
  113.   line = 2
  114.   item = 89
  115.   p.clear()
  116.   for i=1, 10 do
  117.  
  118.     rednet.send(item, "update")
  119.  
  120.     id, message = rednet.receive(0.5)
  121.  
  122.     if message == nil then
  123.       return
  124.     else
  125.       length = string.len(message) + 3
  126.       setTable(message, cobble, 3, length, line, line)
  127.       mess = {}
  128.       mess = split(message, ":")
  129.       thing = tostring(mess[1])
  130.       line = line + 1
  131.       item = item + 1
  132.       screen()
  133.     end
  134.     sleep(10)
  135.   end
  136.  
  137. end
  138.  
  139. function2 = function()
  140.  
  141. event, side, x, y = os.pullEvent("monitor_touch")
  142. print(x.." "..y)
  143. checkxy(x,y)
  144.  
  145. end
  146.  
  147.  
  148. while true do
  149.  
  150.     parallel.waitForAny(function2,function1)
  151.  
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement