LezChap

Mekanism Induction Matrix Power Monitor for OC v2

Apr 27th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. local charts = require("charts") --Using the Charts Library (https://oc.cil.li/topic/1359-charts-library/)
  2. local component = require("component")
  3. local event = require("event")
  4. local term = require("term")
  5. local text = require("text")
  6.  
  7. term.clear()
  8. local gpu = term.gpu()
  9. gpu.setResolution(96, 30) --to make the text bigger, using a 3x2 monitor
  10.  
  11. local matrix = component.list("induction_matrix")
  12. for a, _ in pairs(matrix) do matrix = component.proxy(a) end
  13. local maxEnergy = math.floor(matrix.getMaxEnergy() / 2.5) --values are 2.5x higher than listed in the matrix gui for some reason, this fixes it
  14. --local transferCap = matrix.getTransferCap()  --saving in case I need the value in the future
  15.  
  16. local powerBar = charts.Container{
  17.   x = 1,
  18.   y = 1,
  19.   width = 96,
  20.   height = 5,
  21.   payload = charts.ProgressBar {
  22.     direction = charts.sides.RIGHT,
  23.     value = 0,
  24.     min = 0,
  25.     max = maxEnergy,
  26.     colorFunc = function(_, perc )
  27.       if perc >= .9 then
  28.         return 0x00db00
  29.       end
  30.       if perc >= .75 then
  31.         return 0x99db00
  32.       end
  33.       if perc >= .5 then
  34.         return 0xffdb00
  35.       end
  36.       if perc >= .25 then
  37.         return 0xff4900
  38.       end
  39.       if perc >= .1 then
  40.         return 0xff0000
  41.       end
  42.     end
  43.   }
  44. }
  45.  
  46. local inputHist = charts.Container {
  47.   x = 1,
  48.   y = 6,
  49.   width = 96,
  50.   height = 12,
  51.   payload = charts.Histogram {
  52.     max = 1,
  53.     colorFunc = function(index, norm, value, self, container)
  54.       return 0x0049ff
  55.     end
  56.   }
  57. }
  58. local outputHist = charts.Container {
  59.   x = 1,
  60.   y = 18,
  61.   width = 96,
  62.   height = 12,
  63.   payload = charts.Histogram {
  64.     max = 1,
  65.     min = -1,
  66.     level = { value = 0 , y = 12},
  67.     colorFunc = function(index, norm, value, self, container)
  68.       return 0xff6d40
  69.     end
  70.   }
  71. }
  72.  
  73. function centerText(_str, y, x1, x2)  --function to write text centered between two X values
  74.   local x1 = x1 or 1  --if no values for x are given, assume full screen.
  75.   local x2 = x2 or 96
  76.   local start = math.floor(((x2 - x1) - #_str)/2) + x1
  77.   gpu.set(start, y, _str)
  78. end
  79.  
  80. function comma_value(amount) --format big numbers with commas.
  81.   local formatted = tostring(amount)
  82.   while true do  
  83.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  84.     if (k==0) then
  85.       break
  86.     end
  87.   end
  88.   return formatted
  89. end
  90.  
  91. function round(num, numDecimalPlaces)
  92.   local mult = 10^(numDecimalPlaces or 0)
  93.   num = math.floor(num * mult + 0.5) / mult
  94.   if num ~= 100 then
  95.     return string.format("%."..numDecimalPlaces.."f", num)
  96.   end
  97.   return "100"
  98. end
  99.  
  100. function sum(table)
  101.     local sum = 0
  102.     for k,v in pairs(table) do
  103.         sum = sum + v
  104.     end
  105.     return sum
  106. end
  107.  
  108. function getMaxValue(table)  --find the maximum value in a table
  109.   local highest = 1
  110.   for _,v in pairs(table) do
  111.     if math.abs(v) > highest then highest = math.abs(v) end
  112.   end
  113.   return highest
  114. end
  115.  
  116. function setMaxValue()  --Makes sure the Histograms are using the same scale
  117.   local highest = 1
  118.   local inMax = getMaxValue(inputHist.payload.values)
  119.   local outMax = getMaxValue(outputHist.payload.values)
  120.   if inMax > highest then highest = inMax end
  121.   if outMax > highest then highest = outMax end
  122.   inputHist.payload.max = highest
  123.   outputHist.payload.min = -highest
  124. end
  125.  
  126. local average = "Average I/O Rate: 0 RF/t"
  127. local draws = 0
  128. while true do
  129.   --local startTime = computer.uptime()
  130.  
  131.   local energy = math.floor(matrix.getEnergy() / 2.5)  --values are 2.5x higher than listed in the matrix gui for some reason, this fixes it
  132.   local input = math.floor(matrix.getInput() / 2.5)
  133.   local output = math.floor(matrix.getOutput() / 2.5)
  134.    
  135.   table.insert(inputHist.payload.values, input)
  136.   table.insert(outputHist.payload.values, -output)
  137.   powerBar.payload.value = energy
  138.  
  139.   if #inputHist.payload.values > 96 then --don't let the Histograms overflow their area and stop displaying data
  140.     table.remove(inputHist.payload.values, 1)
  141.     table.remove(outputHist.payload.values, 1)
  142.   end
  143.   setMaxValue()
  144.    
  145.   if draws % 10 == 0 then
  146.     average = (sum(inputHist.payload.values) + sum(outputHist.payload.values))/96
  147.     average = round(average, 0)
  148.     average = comma_value(average)
  149.     average = "Average I/O Rate: " .. average .. " RF/t"
  150.   end
  151.  
  152.   if draws % 3 == 0 then
  153.     powerBar:draw()
  154.     centerText("Induction Matrix", 1)
  155.     local tmp = comma_value(energy).." RF"  
  156.     centerText(tmp, 2)
  157.     tmp = round(((energy/maxEnergy) * 100), 2) .. "%"
  158.     centerText(tmp, 3)
  159.     centerText(average, 4)
  160.   elseif draws % 3 == 1 then  
  161.     inputHist:draw()
  162.     gpu.set(1, 15, "Input Rate")
  163.     local tmp = comma_value(input).." RF"
  164.     gpu.set(2, 16, tmp)
  165.   else    
  166.     outputHist:draw()    
  167.     gpu.set(1, 19, "Output Rate")
  168.     local tmp = comma_value(output).." RF"
  169.     gpu.set(2, 20, tmp)
  170.   end
  171.   draws = draws + 1
  172.  
  173.   --term.setCursor(1, 15)
  174.   --centerText(tostring(computer.uptime()-startTime))
  175.  
  176.   local _, _, x, y = event.pull( .04, "touch")
  177.   if x and y then goto quit end
  178. end
  179.  
  180. ::quit::
Advertisement
Add Comment
Please, Sign In to add comment