distantcam

MFSU Monitor

Nov 22nd, 2013
3,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local function padLeft(str, w)
  2.   return string.rep(" ", w - #str) .. str
  3. end
  4.  
  5. local function findPeripheral(_type)
  6.   for _,name in pairs(peripheral.getNames()) do
  7.     if peripheral.getType(name) == _type then
  8.       return peripheral.wrap(name)
  9.     end
  10.   end
  11. end
  12.  
  13. local m = findPeripheral("monitor")
  14. local mfsu = findPeripheral("mfsu")
  15. local batbox = findPeripheral("batbox")
  16.  
  17. if not m then
  18.   error("Cannot find monitor attached to this computer", 0)
  19. end
  20.  
  21. if not mfsu and not batbox then
  22.   error("Cannot find mfsu attached to this computer", 0)
  23. end
  24.  
  25. m.setTextScale(0.5)
  26.  
  27. local w, h = m.getSize()
  28. local total = 0
  29.  
  30. if mfsu then
  31.   total = mfsu.getEUCapacity()
  32. elseif batbox then
  33.   total = batbox.getCapacity()
  34. end
  35. os.startTimer(1)
  36.  
  37. while true do
  38.   local stored = 0
  39.   if mfsu then
  40.     stored = mfsu.getEUStored()
  41.   elseif batbox then
  42.     stored = batbox.getStored()
  43.   end
  44.  
  45.   m.clear()
  46.   m.setCursorPos(1, 2)
  47.   m.setTextColour(colours.orange)
  48.   m.write("Energy:")
  49.   m.setTextColour(colours.white)
  50.   m.write(padLeft(tostring(stored), w - 7))
  51.  
  52.   m.setCursorPos(1, 4)
  53.   m.setTextColour(colours.orange)
  54.   m.write("Total:")
  55.   m.setTextColour(colours.white)
  56.   m.write(padLeft(tostring(total), w - 6))
  57.  
  58.   local p = (w-2) * stored / total
  59.  
  60.   m.setBackgroundColour(colours.lightGrey)
  61.   m.setCursorPos(2, 8)
  62.   m.write(string.rep(" ", w-2))
  63.   m.setBackgroundColour(colours.grey)
  64.   m.setCursorPos(2, 8)
  65.   m.write(string.rep(" ", p))
  66.   m.setBackgroundColour(colours.black)
  67.  
  68.   os.pullEvent("timer")
  69.   os.startTimer(1)
  70. end
Advertisement
Add Comment
Please, Sign In to add comment