Advertisement
Alakazard12

OC Monitor

Mar 23rd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local interval = 1
  2.  
  3.  
  4. local component = require("component")
  5. local event = require("event")
  6.  
  7.  
  8. local components = {
  9.     gpu = {address = "87abc", type = "gpu"};
  10.     screen = {address = "4ee56", type = "screen"};
  11.     core = {address = "68e1d", type = "draconic_rf_storage"};
  12. }
  13.  
  14. local resolution_x, resolution_y = 30, 15
  15.  
  16. local function set_resolution(gpu)
  17.     if not gpu then
  18.         gpu = components.gpu.proxy
  19.         if not gpu then
  20.             return
  21.         end
  22.     end
  23.  
  24.     gpu.setResolution(resolution_x, resolution_y)
  25. end
  26.  
  27. for i,v in pairs(components) do
  28.     local taddr = component.get(v.address, v.type)
  29.     if not taddr then
  30.         io.stderr:write("Could not find component " .. i)
  31.     else
  32.         v.proxy = component.proxy(taddr)
  33.     end
  34. end
  35.  
  36. if components.gpu.proxy and components.screen.proxy then
  37.     components.gpu.proxy.bind(component.get(components.screen.address))
  38.  
  39.     set_resolution()
  40. end
  41.  
  42.  
  43. local lbase = math.log(10)
  44. local si_prefixes = {
  45.     [-4] = "p";
  46.     [-3] = "n";
  47.     [-2] = "u";
  48.     [-1] = "m";
  49.     [1] = "k";
  50.     [2] = "M";
  51.     [3] = "G";
  52.     [4] = "T";
  53.     [5] = "P";
  54.     [6] = "E";
  55.     [7] = "Z";
  56.     [8] = "Y";
  57. }
  58. local si_low = -4
  59. local si_high = 8
  60.  
  61. local function round(number, decimals)
  62.     return math.floor((number * 10^decimals) + 0.5) / 10^decimals
  63. end
  64.  
  65. local function format_si(number, unit)
  66.     local mult = 1
  67.     if number < 0 then
  68.         mult = -1
  69.         number = -number
  70.     end
  71.  
  72.     local brack = math.floor((math.log(number) / lbase) / 3)
  73.     brack = math.min(math.max(brack, si_low), si_high)
  74.     if brack == 0 then
  75.         return tostring(mult * round(number, 3)) .. " " .. unit
  76.     end
  77.  
  78.     number = number / 10^(brack * 3)
  79.     return tostring(round(mult * number, 3)) .. " " .. si_prefixes[brack] .. unit
  80. end
  81.  
  82.  
  83. local function print_center(gpu, text, y)
  84.     local x = math.floor((resolution_x - #text) / 2)
  85.     gpu.set(x, y, text)
  86. end
  87.  
  88.  
  89. local function check_core()
  90.     local gpu = components.gpu.proxy
  91.     local core = components.core.proxy
  92.     if not gpu or not core then
  93.         return
  94.     end
  95.  
  96.     local stored_energy, max_energy, transfer = core.getEnergyStored(), core.getMaxEnergyStored(), core.getTransferPerTick()
  97.     local stored_text, max_text, transfer_text = format_si(stored_energy, "RF"), format_si(max_energy, "RF"), format_si(transfer, "RF")
  98.  
  99.     print_center(gpu, "Stored: " .. stored_text .. " / " .. max_text, math.floor(resolution_y / 2))
  100. end
  101.  
  102.  
  103. local function on_component_added(event, address, component_type)
  104.     for i,v in pairs(components) do
  105.         if address:sub(1, #v.address) == v.address then
  106.             io.stderr:write("component " .. i .. " added")
  107.             v.proxy = component.proxy(address)
  108.  
  109.             if component_type == "gpu" or component_type == "screen" then
  110.                 if components.gpu.proxy and components.screen.proxy then
  111.                     components.gpu.proxy.bind(component.get(components.screen.address))
  112.  
  113.                     set_resolution()
  114.                 end
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. local function on_component_removed(event, address, component_type)
  121.     for i,v in pairs(components) do
  122.         if address:sub(1, #v.address) == v.address then
  123.             io.stderr:write("component " .. i .. " removed")
  124.             v.proxy = nil
  125.         end
  126.     end
  127. end
  128.  
  129.  
  130. function start()
  131.     if timer then
  132.         stop()
  133.     end
  134.     timer = event.timer(interval, check_core, math.huge)
  135. end
  136.  
  137.  
  138. function stop()
  139.     if timer then
  140.         event.cancel(timer)
  141.         timer = nil
  142.  
  143.         event.ignore("component_added", on_component_added)
  144.         event.ignore("component_removed", on_component_removed)
  145.     end
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement