battlemanmk2

BatteryMonitor

Jul 5th, 2022 (edited)
619
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Printing right aligned text on a line
  2. -- This is used for printing our dynamic
  3. -- values to the screen properly
  4. --
  5. -- @param line The line to print on
  6. -- @param text The text to print there
  7. -- @param color The color to print in
  8. -- @return void
  9. function putValue (line, text, color)
  10.   local align = 39 - string.len(text)
  11.   term.setCursorPos(align, line)
  12.   term.setTextColor(color)
  13.   print(text)
  14. end
  15.  
  16. ENF = require("ENF")
  17.  
  18. --Start Program
  19. print("Running Battery Monitor")
  20.  
  21. -- Initialize our interface!
  22. -- Find all of our peripherals
  23. monitor = peripheral.wrap("top")
  24. battery = peripheral.wrap("back")
  25.  
  26. -- Check and bind monitor
  27. if monitor == nil then
  28.   error("ER: No screen found to display!")
  29. else
  30.   monitor.clear()
  31.   term.redirect(monitor)
  32.   term.setCursorPos(1, 1)
  33.   term.setBackgroundColor(colors.black)
  34. end
  35.  
  36. -- Check if we have a battery
  37. if battery == nil then
  38.     battery = peripheral.wrap("bottom")
  39.     if battery == nil then
  40.         error("ER: No battery connected to computer")
  41.         reboot()
  42.     end
  43. end
  44.  
  45. -- Draw our static fill volume box
  46. term.setTextColor(colors.white)
  47. term.setCursorPos(1,1)
  48. --print("123456789012345678901234567890123456789")
  49. term.setCursorPos(2, 2)
  50. print("+------+")
  51. term.setCursorPos(2, 11)
  52. print("+------+")
  53. for i = 3, 10 do
  54.   term.setCursorPos(2, i)
  55.   print("|")
  56.   term.setCursorPos(9, i)
  57.   print("|")
  58. end
  59.  
  60. -- Draw our static interface text
  61. term.setCursorPos(11, 2);
  62. print("Max Storage:")
  63. term.setCursorPos(11, 3);
  64. print("Max Throughput:")
  65.  
  66. term.setCursorPos(11, 5);
  67. print("Input:")
  68. term.setCursorPos(11, 6);
  69. print("Output:")
  70. term.setCursorPos(11, 7);
  71. print("Net:")
  72.  
  73. term.setCursorPos(11, 9);
  74. print("Stored:")
  75. term.setCursorPos(11, 10);
  76. print("Percent Full:")
  77. term.setCursorPos(11, 11);
  78. print("Critically Low:")
  79.  
  80.  
  81. -- Entering our infinite loop of checking the battery
  82. -- This will refresh all information every 2 seconds
  83. alarmIsRinging = false
  84. alarmIsActive = false
  85. alarmCounter = 0
  86.  
  87. while true do
  88.  
  89.   -- Get our fixed battery values
  90.   batteryMaxCharge = battery.getMaxEnergy()
  91.   batteryMaxInOuts = battery.getTransferCap()
  92.   putValue(2, ENF.formatNumber(batteryMaxCharge), colors.lightBlue)
  93.   putValue(3, ENF.formatNumber(batteryMaxInOuts), colors.lightBlue)
  94.  
  95.   -- Get our input/output/balance battery values
  96.   batteryCurrentIn = battery.getLastInput()
  97.   batteryCurrentOut = battery.getLastOutput()
  98.   putValue(5, ENF.formatNumber(batteryCurrentIn), colors.lightBlue)
  99.   putValue(6, ENF.formatNumber(batteryCurrentOut), colors.lightBlue)
  100.  
  101.   batteryCurrentBalance = batteryCurrentIn - batteryCurrentOut
  102.   batteryCurrentColor = (batteryCurrentBalance >= 0) and colors.green or colors.red
  103.   batteryCurrentSign = (batteryCurrentBalance >= 0) and "+" or " "
  104.   putValue(7, batteryCurrentSign .. ENF.formatNumber(batteryCurrentBalance), batteryCurrentColor)
  105.  
  106.   -- Get our statistical values
  107.   batteryCurrentCharge = battery.getEnergy()
  108.   putValue(9, ENF.formatNumber(batteryCurrentCharge), colors.lightBlue)
  109.  
  110.   batteryCurrentColor = colors.red
  111.   batteryCurrentPercentage = ((batteryCurrentCharge / batteryMaxCharge) * 100)
  112.   if batteryCurrentPercentage > 30 then batteryCurrentColor = colors.orange end
  113.   if batteryCurrentPercentage > 60 then batteryCurrentColor = colors.green end
  114.   putValue(10, string.sub(batteryCurrentPercentage, 1, 5) .. "%", batteryCurrentColor)
  115.   --putValue(10, string.sub(ENF.formatNumber(batteryCurrentPercentage), 0, 4) .. "%", batteryCurrentColor)
  116.  
  117.   batteryCurrentCritical = "Yes"
  118.   batteryCurrentColor = colors.red
  119.   if batteryCurrentPercentage > 15 then
  120.     batteryCurrentCritical = "No"
  121.     batteryCurrentColor = colors.green
  122.   end
  123.   putValue(11, batteryCurrentCritical, batteryCurrentColor)
  124.  
  125.   -- Draw the current charge graph
  126.   batteryCurrentColor = colors.red
  127.   if batteryCurrentPercentage > 30 then term.setTextColor(colors.orange) end
  128.   if batteryCurrentPercentage > 60 then term.setTextColor(colors.green) end
  129.   if batteryCurrentPercentage >= 100 then term.setTextColor(colors.lightBlue) end
  130.  
  131.   for i = 3, 10 do
  132.     term.setCursorPos(3, i)
  133.     print("      ")
  134.   end
  135.  
  136.   iterator = 0
  137.   for i = 3, 10 do
  138.     local compare = (12.5 * iterator)
  139.     if (batteryCurrentPercentage >= compare) then
  140.       term.setCursorPos(3, (13 - i))
  141.       local filler = ""
  142.       for i = 1, 6 do
  143.         local inhere = (compare + (i*2))
  144.         if batteryCurrentPercentage >= inhere then
  145.           filler = filler .. "#"
  146.         else
  147.           if batteryCurrentPercentage >= (inhere - 1) then
  148.             filler = filler .. "="
  149.           else
  150.             filler = filler .. "_"
  151.           end
  152.         end
  153.       end
  154.       print(filler)
  155.     end
  156.  
  157.     iterator = iterator + 1
  158.   end
  159.  
  160.   -- Hey, ring the alarm!
  161.   if batteryCurrentPercentage > 10 then
  162.     alarmIsRinging = false
  163.     alarmIsActive = false
  164.   end
  165.  
  166.   if batteryCurrentPercentage < 10 and alarmIsActive == false then
  167.     alarmIsRinging = true
  168.     alarmIsActive = true
  169.     alarmCounter = 0
  170.     redstone.setOutput("left", true)
  171.   end
  172.  
  173.   if alarmIsRinging == true then
  174.     alarmCounter = alarmCounter + 1
  175.     if alarmCounter >= 10 then
  176.       redstone.setOutput("left", false)
  177.       alarmIsRinging = false
  178.     end
  179.   end
  180.  
  181.   -- Wait 1s until next iteration
  182.     os.sleep(1)
  183.  
  184. end
RAW Paste Data Copied