Larvix

IC2 Energy Monitor

Feb 28th, 2026 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. -- User Settings --
  2.  
  3. local CONFIG = {
  4.     title = "ENERGY MONITOR",
  5.     refreshRate = 1,
  6.  
  7.     -- Colours --
  8.     bgColor = colours.black,
  9.     textColor = colours.white,
  10.     barBg = colours.grey,
  11.     barFillLow = colours.red,
  12.     barFillMid = colours.yellow,
  13.     barFillHigh = colours.green,
  14.  
  15.     -- Bar Settings --
  16.     barWidthPercent = 0.8
  17. }
  18.  
  19. -- Setup --
  20. local mon = peripheral.find("monitor")
  21. local psd = nil
  22. for _,name in pairs(peripheral.getNames()) do
  23.     if peripheral.getType(name) == "ic2:mfe"
  24.     or peripheral.getType(name) == "ic2:mfsu"
  25.     then
  26.         psd = peripheral.wrap(name)
  27.     end
  28. end
  29.  
  30. if not mon then
  31.     error("Monitor not found")
  32. end
  33.  
  34. if not psd then
  35.     error("Power storage device not found")
  36. end
  37.  
  38. mon.setBackgroundColor(CONFIG.bgColor)
  39. mon.clear()
  40. mon.setTextScale(1)
  41.  
  42. -- Helper Functions --
  43.  
  44. local function centerText(y, text, color)
  45.     local w, _ = mon.getSize()
  46.     local x = math.floor((w - #text) / 2) + 1
  47.     mon.setCursorPos(x, y)
  48.     if color then mon.setTextColor(color) end
  49.     mon.write(text)
  50. end
  51.  
  52. local function getBarColor(percent)
  53.     if percent >= 0.75 then
  54.         return CONFIG.barFillHigh
  55.     elseif percent >= 0.30 then
  56.         return CONFIG.barFillMid
  57.     else
  58.         return CONFIG.barFillLow
  59.     end
  60. end
  61.  
  62. local function drawProgressBar(y, percent)
  63.     local w, _ = mon.getSize()
  64.     local barWidth = math.floor(w * CONFIG.barWidthPercent)
  65.     local startX = math.floor((w - barWidth) / 2) + 1
  66.     local filled = math.floor(barWidth * percent)
  67.  
  68.     -- background --
  69.     mon.setBackgroundColor(CONFIG.barBg)
  70.     mon.setCursorPos(startX, y)
  71.     mon.write(string.rep(" ", barWidth))
  72.  
  73.     -- fill --
  74.     mon.setBackgroundColor(getBarColor(percent))
  75.     mon.setCursorPos(startX, y)
  76.     mon.write(string.rep(" ", filled))
  77.  
  78.     mon.setBackgroundColor(CONFIG.bgColor)
  79. end
  80.  
  81. -- Main Loop --
  82. local lastStored = -1
  83. local lastMax = -1
  84. while true do
  85.     mon.setBackgroundColor(CONFIG.bgColor)
  86.  
  87.     local stored
  88.     local max
  89.  
  90.     if psd.getEUStored then
  91.         stored = psd:getEUStored()
  92.         max = psd:getEUCapacity()
  93.     elseif psd.getEnergyStored then
  94.         stored = psd:getEnergyStored()
  95.         max = psd:getMaxEnergyStored()
  96.     else
  97.         stored = nil
  98.         max = nil
  99.     end
  100.  
  101.     if not stored or not max then
  102.         centerText(2, "PSD METHOD ERROR", colours.red)
  103.         sleep(2)
  104.     else
  105.         local percent = stored / max
  106.         local _, h = mon.getSize()
  107.  
  108.         centerText(4, CONFIG.title, CONFIG.textColor)
  109.         centerText(8, string.format("%d / %d EU", stored, max), CONFIG.textColor)
  110.         centerText(9, string.format("%.2f%%", percent * 100), CONFIG.textColor)
  111.  
  112.         drawProgressBar(math.floor(h / 2), percent)
  113.     end
  114.     sleep(CONFIG.refreshRate)
  115. end
Advertisement
Add Comment
Please, Sign In to add comment