Ordiance

Mekanism Power Monitor

Aug 12th, 2021 (edited)
2,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. --Setup
  2. os.loadAPI("touchpoint") --Touchpoinz API by Lyqyd
  3. mon = peripheral.find("monitor")
  4. mat = peripheral.find("peripheralProxy:inductionMatrix")
  5.  
  6. --Round Function
  7. function round(float)
  8.     local int, part = math.modf(float)
  9.     if float == math.abs(float) and part >= .5 then return int+1
  10.     elseif part <= -.5 then return int-1
  11.     end
  12.     return int
  13. end
  14.  
  15. --Round with 2 Decimals Function
  16. function roundD(exact, quantum)
  17.     local quant,frac = math.modf(exact/quantum)
  18.     return quantum * (quant + (frac > 0.5 and 1 or 0))
  19. end
  20.  
  21. --Conversion Function
  22. function conversion(exact, text)
  23.     local units = {"", "K", "M", "G", "T", "P", "E", "Z", "Y"}
  24.     local pot = 1
  25.     local absExact = math.abs(exact)
  26.     while absExact >= (1000^pot) do
  27.         pot = pot + 1
  28.     end
  29.     local out = tostring(roundD(exact / (1000^(pot - 1)), 0.1))
  30.     if text then
  31.         out = out .. " " .. units[pot]
  32.     end
  33.     return out
  34. end
  35.  
  36. --Draw Function
  37. function draw(xmin, xmax, ymin, ymax, c)
  38.     mon.setBackgroundColor(c)
  39.     mon.setCursorPos(xmin, ymin)
  40.     if xmax ~= 1 then
  41.         for i = 1, xmax, 1 do
  42.             mon.write(" ")
  43.             mon.setCursorPos(xmin+i, ymin)
  44.         end
  45.     end
  46.     if ymax ~= 1 then
  47.         for k = 1, ymax, 1 do
  48.             mon.write(" ")
  49.             mon.setCursorPos(xmin, ymin+k)
  50.         end
  51.     end
  52.     mon.setBackgroundColor(32768)
  53. end
  54.  
  55. --DrawBar Function
  56. function drawBar(xmin, xmax, y, r, c)
  57.     for i=1, r, 1 do   
  58.         draw(xmin, xmax, y+i-1, 1, c)
  59.     end
  60. end
  61.  
  62. --DrawDiagrams Function
  63. function drawDiagrams()
  64.     local energy = round((mat.getEnergyFilledPercentage())*29)
  65.     local input = round((mat.getLastInput()/mat.getTransferCap())*29)
  66.     local output = round((mat.getLastOutput()/mat.getTransferCap())*29)
  67.     drawBar(4, energy, 4, 3, 8)
  68.     drawBar(4, input, 11, 2, 32)
  69.     drawBar(4, output, 15, 2, 32)
  70. end
  71.  
  72. --Draw Visuals Function
  73. function drawVisuals()
  74.     --Frame Induction Matrix
  75.     draw(2, 2, 2, 1, 128)
  76.     draw(22, 13, 2, 1, 128)
  77.     draw(2, 1, 2, 17, 128)
  78.     draw(2, 33, 18, 1, 128)
  79.     draw(34, 1, 2, 17, 128)
  80.     draw(2, 33, 8, 1, 128)
  81.     mon.setCursorPos(5,2)
  82.     mon.write("Induction Matrix")
  83.  
  84.     --Frame SPS Control
  85.     draw(36, 2, 2, 1, 128)
  86.     draw(36, 1, 2, 9, 128)
  87.     draw(49, 1, 2, 9, 128)
  88.     draw(36, 13, 10, 1, 128)
  89.     draw(43, 6, 2, 1, 128)
  90.     mon.setCursorPos(39,2)
  91.     mon.write("SPS")
  92.  
  93.     --Frame Stats
  94.     draw(36, 1, 12, 7, 128)
  95.     draw(49, 1, 12, 7, 128)
  96.     draw(36, 13, 18, 1, 128)
  97.     draw(36, 2, 12, 1, 128)
  98.     draw(45, 4, 12, 1, 128)
  99.     mon.setCursorPos(39,12)
  100.     mon.write("Stats")
  101.    
  102.     --energyBar
  103.     drawBar(4, 29, 4, 3, 256)
  104.  
  105.     --inputBar
  106.     drawBar(4, 29, 11, 2, 256)
  107.     mon.setCursorPos(4,10)
  108.     mon.write("Input:")
  109.  
  110.     --outputBar
  111.     drawBar(4, 29, 15, 2, 256)
  112.     mon.setCursorPos(4,14)
  113.     mon.write("Output:")
  114. end
  115.  
  116. --DrawStats Function
  117. function drawStats()
  118.     local cells = mat.getInstalledCells()
  119.     local prov = mat.getInstalledProviders()
  120.     local maxEnergy = conversion(mat.getMaxEnergy(), false)
  121.     local energyStored = conversion(mat.getEnergy(), true)
  122.     local energyInput = conversion(mat.getLastInput(), true)
  123.     local energyOutput = conversion(mat.getLastOutput(), true)
  124.     mon.setCursorPos(38,14)
  125.     mon.write("Cells: "..cells)
  126.     mon.setCursorPos(38,15)
  127.     mon.write("Prov: "..prov)
  128.     mon.setCursorPos(38,16)
  129.     mon.write("Cap: "..maxEnergy)
  130.     mon.setCursorPos(23,2)
  131.     mon.write(" "..energyStored.."RF ")
  132.     mon.setCursorPos(10,10)
  133.     mon.write(" "..energyInput.."RF/t ")
  134.     mon.setCursorPos(11,14)
  135.     mon.write(" "..energyOutput.."RF/t ")
  136. end
  137.  
  138. --Buttons
  139. local t = touchpoint.new("top")
  140.  
  141. t:add("Inactive", nil, 38, 4, 47, 8, colors.red, colors.lime)
  142.  
  143. --Draw
  144. t:draw()
  145. drawVisuals()
  146. drawDiagrams()
  147. drawStats()
  148.  
  149. --Timer
  150. local lastTime = 6
  151.  
  152. --Loop
  153. while true do
  154.         local event, p1 = t:handleEvents(os.pullEvent())
  155.         if event == "button_click" then
  156.                 t:toggleButton(p1)
  157.                 if p1 == "Inactive" then
  158.                     t:rename("Inactive", "Active")
  159.                     rs.setOutput("left", true)
  160.                 elseif p1 == "Active" then
  161.                     t:rename("Active", "Inactive")
  162.                     rs.setOutput("left",false)
  163.                 end
  164.                 drawVisuals()
  165.                 drawDiagrams()
  166.                 drawStats()
  167.         end
  168.         if os.clock() - lastTime >= 5 then
  169.             mon.clear()
  170.             t:draw()
  171.             drawVisuals()
  172.             drawDiagrams()
  173.             drawStats()
  174.             lastTime = os.clock()
  175.         end
  176. end
Add Comment
Please, Sign In to add comment