Advertisement
Guest User

YES

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