Advertisement
colhaydutu

atm10 energy monitor

Feb 12th, 2025 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. local function findMonitors()
  2. local monitors = {}
  3. for _, name in pairs(peripheral.getNames()) do
  4. if string.match(name, "^monitor_") then
  5. local monitor = peripheral.wrap(name)
  6. monitor.setTextScale(0.9)
  7. table.insert(monitors, monitor)
  8. end
  9. end
  10. return monitors
  11. end
  12.  
  13. local function drawBar(monitor, x, y, width, fillPercentage)
  14. local w, h = monitor.getSize()
  15. term.redirect(monitor)
  16.  
  17. local borderOffset = math.floor(2 * 0.9)
  18. local barWidth = math.floor(width * 0.9)
  19.  
  20. paintutils.drawLine(2, 2, 2, 2, colors.lime)
  21. paintutils.drawLine(math.floor(11 * 0.9), 2, w - borderOffset, 2, colors.lime)
  22. paintutils.drawLine(2, 2, 2, h - borderOffset, colors.lime)
  23. paintutils.drawLine(w - borderOffset, 2, w - borderOffset, h - borderOffset, colors.lime)
  24. paintutils.drawLine(w - borderOffset, h - borderOffset, 2, h - borderOffset, colors.lime)
  25.  
  26. monitor.setBackgroundColor(colors.gray)
  27. monitor.setTextColor(colors.white)
  28. monitor.setCursorPos(3, 2)
  29. monitor.write("[INFO]")
  30.  
  31. local fillWidth = math.floor(barWidth * fillPercentage)
  32.  
  33. monitor.setBackgroundColor(colors.gray)
  34. monitor.setTextColor(colors.white)
  35. monitor.setCursorPos(x, y)
  36. monitor.write("[ ")
  37.  
  38. for i = 1, barWidth do
  39. if i <= fillWidth then
  40. monitor.setBackgroundColor(colors.green)
  41. monitor.write(" ")
  42. else
  43. monitor.setBackgroundColor(colors.red)
  44. monitor.write(" ")
  45. end
  46. end
  47.  
  48. monitor.setBackgroundColor(colors.gray)
  49. monitor.write(" ] " .. math.floor(fillPercentage * 100) .. "%")
  50. end
  51.  
  52. local jouleToFeRatio = 276 / 690 -- 1 J = (276 / 690) Fe
  53.  
  54. local monitors = findMonitors()
  55.  
  56. if #monitors == 0 then
  57. print("No monitors found. Please connect a monitor.")
  58. return
  59. end
  60.  
  61. local monitor = monitors[1]
  62. local modem = peripheral.wrap("back")
  63. modem.open(11)
  64.  
  65. local battery
  66. for _, name in pairs(peripheral.getNames()) do
  67. if string.match(name, "^eliteEnergyCube") then
  68. battery = peripheral.wrap(name)
  69. break
  70. end
  71. end
  72.  
  73.  
  74. while true do
  75. local totalEnergyJoule = battery.getEnergy()
  76. local capPercentageJoule = battery.getEnergyFilledPercentage() or 0
  77. local totalEnergyFe = totalEnergyJoule * jouleToFeRatio
  78. local capPercentageFe = capPercentageJoule
  79.  
  80. monitor.clear()
  81. drawBar(monitor, 5, 7, 18, capPercentageFe) -- Bar genişliği küçültüldü
  82.  
  83. monitor.setCursorPos(5, 5)
  84. monitor.setBackgroundColor(colors.gray)
  85. monitor.setTextColor(colors.white)
  86. monitor.write("Energy: " .. string.format("%.2f", totalEnergyFe / 1000) .. " kFe")
  87.  
  88. os.sleep(1)
  89. end
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement