fergenbergel

BatteryLevelMonitor

Aug 11th, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.22 KB | None | 0 0
  1. -- Power Monitor for induction matrix
  2. --
  3. -- Written and copyrighted 2016+
  4. -- by Sven Marc 'cybrox' Gehring
  5. -- Licensed under MIT license
  6.  
  7.  
  8.  
  9.  
  10. -- Printing right aligned text on a line
  11. -- This is used for printing our dynamic
  12. -- values to the screen properly
  13. --
  14. -- @param line The line to print on
  15. -- @param text The text to print there
  16. -- @param color The color to print in
  17. -- @return void
  18. function putValue (line, text, color)
  19.   local align = 30 - string.len(text)
  20.   term.setCursorPos(align, line)
  21.   term.setTextColor(color)
  22.   print(text)
  23. end
  24.  
  25.  
  26. -- Get a number in a human readable format
  27. -- This is used to display numbers on screen
  28. --
  29. -- @param number The number to format
  30. -- @return The formatted number
  31. function putNumber (number)
  32.   local round = 0
  33.   local texts = ""
  34.  
  35.   if number >= 1000000000000000000 then
  36.     round = (number / 1000000000000000000)
  37.     texts = string.sub(round, 0, 5) .. " ERF"
  38.   else
  39.     if number >= 1000000000000000 then
  40.       round = (number / 1000000000000000)
  41.       texts = string.sub(round, 0, 5) .. " PRF"
  42.     else
  43.       if number >= 1000000000000 then
  44.         round = (number / 1000000000000)
  45.         texts = string.sub(round, 0, 5) .. " TRF"
  46.       else
  47.         if number >= 1000000000 then
  48.           round = (number / 1000000000)
  49.           texts = string.sub(round, 0, 5) .. " GRF"
  50.         else
  51.           if number >= 1000000 then
  52.             round = (number / 1000000)
  53.             texts = string.sub(round, 0, 5) .. " MRF"
  54.           else
  55.             if number >= 1000 then
  56.               round = (number / 1000)
  57.               texts = string.sub(round, 0, 5) .. " kRF"
  58.             else
  59.               texts = string.sub(number, 0, 5) .. "  RF"
  60.             end
  61.           end
  62.         end
  63.       end
  64.     end
  65.   end
  66.  
  67.   return texts
  68. end
  69.  
  70.  
  71.  
  72.  
  73. -- Initialize our interface!
  74. -- Find all of our peripherals
  75. monitor = peripheral.wrap("top")
  76. battery = peripheral.find("InductionMatrix")
  77.  
  78. -- Check and bind monitor
  79. if monitor == nil then
  80.   error("ER: No screen found to display!")
  81. else
  82.   monitor.clear()
  83.   term.redirect(monitor)
  84.   term.setCursorPos(1, 1)
  85.   term.setBackgroundColor(colors.black)
  86. end
  87.  
  88. -- Check if we have a battery
  89. if battery == nil then
  90.   error("ER: No battery connected to computer")
  91. end
  92.  
  93. -- Draw our static fill volume box
  94. term.setTextColor(colors.white)
  95. term.setCursorPos(2, 2)
  96. print("+------+")
  97. term.setCursorPos(2, 11)
  98. print("+------+")
  99. for i = 3, 10 do
  100.   term.setCursorPos(2, i)
  101.   print("|")
  102.   term.setCursorPos(9, i)
  103.   print("|")
  104. end
  105.  
  106. -- Draw our static interface text
  107. term.setCursorPos(11, 2);
  108. print("Max RF:")
  109. term.setCursorPos(11, 3);
  110. print("Max Thru:")
  111.  
  112. term.setCursorPos(11, 5);
  113. print("Cur In:")
  114. term.setCursorPos(11, 6);
  115. print("Cur Out:")
  116. term.setCursorPos(11, 7);
  117. print("Cur Bal:")
  118.  
  119. term.setCursorPos(11, 9);
  120. print("Stored:")
  121. term.setCursorPos(11, 10);
  122. print("Filled:")
  123. term.setCursorPos(11, 11);
  124. print("Critical:")
  125.  
  126.  
  127. -- Entering our infinite loop of checking the battery
  128. -- This will refresh all information every 2 seconds
  129. alarmIsRinging = false
  130. alarmIsActive = false
  131. alarmCounter = 0
  132.  
  133. while true do
  134.  
  135.   -- Get our fixed battery values
  136.   batteryMaxCharge = battery.getMaxEnergy()
  137.   batteryMaxInOuts = battery.getTransferCap()
  138.   putValue(2, putNumber(batteryMaxCharge), colors.lightBlue)
  139.   putValue(3, putNumber(batteryMaxInOuts), colors.lightBlue)
  140.  
  141.   -- Get our input/output/balance battery values
  142.   batteryCurrentIn = battery.getLastInput()
  143.   batteryCurrentOut = battery.getLastOutput()
  144.   putValue(5, putNumber(batteryCurrentIn), colors.lightBlue)
  145.   putValue(6, putNumber(batteryCurrentOut), colors.lightBlue)
  146.  
  147.   batteryCurrentBalance = batteryCurrentIn - batteryCurrentOut
  148.   batteryCurrentColor = (batteryCurrentBalance >= 0) and colors.green or colors.red
  149.   batteryCurrentSign = (batteryCurrentBalance >= 0) and "+" or ""
  150.   putValue(7, batteryCurrentSign .. putNumber(batteryCurrentBalance), batteryCurrentColor)
  151.  
  152.   -- Get our statistical values
  153.   batteryCurrentCharge = battery.getStored()
  154.   putValue(9, putNumber(batteryCurrentCharge), colors.lightBlue)
  155.  
  156.   batteryCurrentColor = colors.red
  157.   batteryCurrentPercentage = ((batteryCurrentCharge / batteryMaxCharge) * 100)
  158.   if batteryCurrentPercentage > 30 then batteryCurrentColor = colors.orange end
  159.   if batteryCurrentPercentage > 60 then batteryCurrentColor = colors.green end
  160.   putValue(10, string.sub(putNumber(batteryCurrentPercentage), 0, 4) .. "%", batteryCurrentColor)
  161.  
  162.   batteryCurrentCritical = "Yes"
  163.   batteryCurrentColor = colors.red
  164.   if batteryCurrentPercentage > 15 then
  165.     batteryCurrentCritical = "No"
  166.     batteryCurrentColor = colors.green
  167.   end
  168.   putValue(11, batteryCurrentCritical, batteryCurrentColor)
  169.  
  170.   -- Draw the current charge graph
  171.   batteryCurrentColor = colors.red
  172.   if batteryCurrentPercentage > 30 then term.setTextColor(colors.orange) end
  173.   if batteryCurrentPercentage > 60 then term.setTextColor(colors.green) end
  174.   if batteryCurrentPercentage >= 100 then term.setTextColor(colors.lightBlue) end
  175.  
  176.   for i = 3, 10 do
  177.     term.setCursorPos(3, i)
  178.     print("      ")
  179.   end
  180.  
  181.   iterator = 0
  182.   for i = 3, 10 do
  183.     local compare = (12.5 * iterator)
  184.     if (batteryCurrentPercentage >= compare) then
  185.       term.setCursorPos(3, (13 - i))
  186.       local filler = ""
  187.       for i = 1, 6 do
  188.         local inhere = (compare + (i*2))
  189.         if batteryCurrentPercentage >= inhere then
  190.           filler = filler .. "#"
  191.         else
  192.           if batteryCurrentPercentage >= (inhere - 1) then
  193.             filler = filler .. "="
  194.           else
  195.             filler = filler .. "_"
  196.           end
  197.         end
  198.       end
  199.       print(filler)
  200.     end
  201.  
  202.     iterator = iterator + 1
  203.   end
  204.  
  205.   -- Hey, ring the alarm!
  206.   if batteryCurrentPercentage > 10 then
  207.     alarmIsRinging = false
  208.     alarmIsActive = false
  209.   end
  210.  
  211.   if batteryCurrentPercentage < 10 and alarmIsActive == false then
  212.     alarmIsRinging = true
  213.     alarmIsActive = true
  214.     alarmCounter = 0
  215.     redstone.setOutput("left", true)
  216.   end
  217.  
  218.   if alarmIsRinging == true then
  219.     alarmCounter = alarmCounter + 1
  220.     if alarmCounter >= 10 then
  221.       redstone.setOutput("left", false)
  222.       alarmIsRinging = false
  223.     end
  224.   end
  225.  
  226.   -- Wait 2s until next iteration
  227.   os.sleep(2)
  228.  
  229. end
Add Comment
Please, Sign In to add comment