Advertisement
melzneni

MonitorReceive

Jun 9th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. args={...}
  2. if #args==0 then
  3. error("required arguments: monitorName")
  4. end
  5. mName=args[1]
  6.  
  7. monitor=peripheral.wrap("right")
  8. monitor.setTextScale(1)
  9.  
  10. monitor.clear()
  11.  
  12. function len(txt)
  13. return #txt
  14. end
  15.  
  16. rednet.open("left")
  17.  
  18. texts={}
  19.  
  20. function rewrite()
  21. lines={}
  22. c=0
  23. for i,v in pairs(texts) do
  24. parts=split(v,'[\n]+')
  25. lines[c]=i..":"
  26. c=c+1
  27. for i1,v1 in pairs(parts) do
  28. lines[c]=" "..v1
  29. c=c+1
  30. end
  31.  
  32. end
  33. monitor.clear()
  34. monitor.setCursorPos(1,1)
  35. for i=0,c-1 do
  36. monitor.setCursorPos(1,1+i)
  37. monitor.write(lines[i])
  38. end
  39. end
  40.  
  41. function split(str, pat)
  42. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  43. local fpat = "(.-)" .. pat
  44. local last_end = 1
  45. local s, e, cap = str:find(fpat, 1)
  46. while s do
  47. if s ~= 1 or cap ~= "" then
  48. table.insert(t,cap)
  49. end
  50. last_end = e+1
  51. s, e, cap = str:find(fpat, last_end)
  52. end
  53. if last_end <= #str then
  54. cap = str:sub(last_end)
  55. table.insert(t, cap)
  56. end
  57. return t
  58. end
  59.  
  60. while true do
  61.  
  62. id,msg,distance,protocol=rednet.receive("monitoring")
  63.  
  64. if msg:sub(1,len("textToMonitor:"))=="textToMonitor:" then
  65. msg=msg:sub(len("textToMonitor:")+1)
  66. ind=string.find(msg,":")
  67. monName=msg:sub(1,ind-1)
  68. if monName==mName then
  69. msg=msg:sub(ind+1)
  70. ind=string.find(msg,":")
  71. sName=msg:sub(1,ind-1)
  72. txt=msg:sub(ind+1)
  73. texts[sName]=txt
  74. rewrite()
  75. end
  76. else
  77. print("unknown message: '"..msg.."'")
  78. end
  79. --"textToMonitor:"..mName..":EnergyStored:"..storage.getEnergyStored().."/"..storage.getMaxEnergyStored()
  80.  
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement