Advertisement
Tanoro

ComputerCraft MFSU Monitor

Feb 19th, 2014
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. -- pastebin get 0X93VE9M mfsumon
  2. -- http://www.computercraft.info/forums2/index.php?/topic/16109-mfsu-monitor/
  3.  
  4. --[[
  5. * Pad the left side of a string x number of times
  6. ]]
  7. local function padLeft(str, w)
  8. return string.rep(" ", w - #str) .. str
  9. end
  10.  
  11. --[[
  12. * Locate the nearest
  13. ]]
  14. local function findPeripheral(_type)
  15. for _,name in pairs(peripheral.getNames()) do
  16. if peripheral.getType(name) == _type then
  17. return peripheral.wrap(name)
  18. end
  19. end
  20. end
  21.  
  22. -- Locate the nearest monitor and power sources
  23. local m = findPeripheral("monitor")
  24.  
  25. -- Error catches
  26. if not m then
  27. error("Cannot find monitor attached to this computer", 0)
  28. end
  29.  
  30. m.setTextScale(0.5)
  31.  
  32. local w, h = m.getSize()
  33. local totalCap = 0
  34. local totalStore = 0
  35. local percent = 0
  36. local mfsu
  37.  
  38. os.startTimer(1)
  39.  
  40. while true do
  41. -- Start getting numbers
  42.  
  43. totalCap = 0
  44. totalStore = 0
  45. percent = 0
  46.  
  47. for k,v in pairs(peripheral.getNames()) do
  48. if peripheral.getType(v) == "mfsu" then
  49. -- Increment the storage and capacity
  50. mfsu = peripheral.wrap(v)
  51. totalCap = totalCap + mfsu.getEUCapacity()
  52. totalStore = totalStore + mfsu.getEUStored()
  53. end
  54. end
  55.  
  56. -- Calculate a percentage
  57. percent = math.ceil(totalStore / totalCap) * 100
  58.  
  59.  
  60. m.clear()
  61. m.setCursorPos(1, 2)
  62. m.setTextColour(colours.orange)
  63. m.write("Avail EU: ")
  64. m.setTextColour(colours.white)
  65. m.write(tostring(percent) .. "%")
  66.  
  67.  
  68. -- If available power is 75% of more, turn redstone on
  69. if percent > 75 then
  70. redstone.setOutput("right", false)
  71. m.setCursorPos(1, 4)
  72. m.setTextColour(colours.orange)
  73. m.write("Redstone: ")
  74. m.setTextColour(colours.white)
  75. m.write("Off")
  76. end
  77.  
  78. -- If power falls below 15%, turn redstone off
  79. if percent < 15 then
  80. redstone.setOutput("right", true)
  81. m.setCursorPos(1, 4)
  82. m.setTextColour(colours.orange)
  83. m.write("Redstone: ")
  84. m.setTextColour(colours.white)
  85. m.write("On")
  86. end
  87.  
  88. os.pullEvent("timer")
  89. os.startTimer(1)
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement