Advertisement
Clor

OpenComputer+computronics IIA power monitor + controller v2

May 22nd, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.43 KB | None | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by User.
  4. --- DateTime: 08.05.2019 15:06
  5. ---
  6. ---▁▂▃▄▅▆▇█▒
  7. local comp = require("component")
  8. local event = require("event")
  9. local gtBattBuf = comp.gt_batterybuffer
  10. local sides = require("sides")
  11. local rs = comp.redstone
  12. local gpu = comp.gpu
  13. local w, h = gpu.maxResolution()
  14.  
  15. local colors = {
  16.     white = 0xffffff,
  17.     orange = 0xff9200,
  18.     magenta = 0xff00ff,
  19.     lightblue = 0x99dbff,
  20.     yellow = 0xffff00,
  21.     lime = 0x00ff00,
  22.     pink = 0xffb6bf,
  23.     gray = 0x787878,
  24.     silver = 0xc3c3c3,
  25.     cyan = 0x00ffff,
  26.     purple = 0x990080,
  27.     blue = 0x0000ff,
  28.     brown = 0x992440,
  29.     green = 0x009200,
  30.     red = 0xff0000,
  31.     black = 0x000000
  32. }
  33.  
  34. --CONFIG BEGIN
  35. local backgroundColor = 0x26261b
  36. local mainTextColor = 0x7f1ae5
  37. local storageColor = colors.red
  38. local inputColor = colors.green
  39. local outputColor = colors.blue
  40. local runningIndicatorColor = 0xbfff00
  41. local numderLineStorageColor = colors.red
  42. local numderLineIOColor = colors.cyan
  43. local backgroundDiagramColor = colors.black
  44.  
  45. local redstoneIOControlSide = sides.front
  46.  
  47. local head = "MAIN POWER SYSTEM" -- 32 chars max
  48. --CONFIG END
  49.  
  50.  
  51.  
  52. local headOffSet = math.modf((32 - string.len(head)) / 2)
  53. local clear = "                                "
  54.  
  55. local diagramPosX = 1
  56. local diagramSizeX = w - 4
  57. local diagramPosY = 11
  58. local diagramSizeY = h - 11
  59.  
  60. local capacity
  61. local maxEnergyOutput
  62. local stepSizeCap
  63. local stepSizeIO
  64. local int_stepSizeCap
  65. local int_stepSizeIO
  66.  
  67. local partsChar = { " ", "▁", "▂", "▃", "▄", "▅", "▆", "▇" }
  68.  
  69. function numPoints(s)
  70.     local x = string.len(s)
  71.     local r = ""
  72.  
  73.     if (x <= 3) then
  74.         return s
  75.     end
  76.  
  77.     if (x % 3 == 0) then
  78.         for i = 1, x, 3 do
  79.             r = r .. string.sub(s, i, i + 2) .. "."
  80.         end
  81.     else
  82.         r = r .. string.sub(s, 1, x % 3) .. "."
  83.         for i = 1 + x % 3, x, 3 do
  84.             r = r .. string.sub(s, i, i + 2) .. "."
  85.         end
  86.     end
  87.     return string.sub(r, 1, string.len(r) - 1)
  88. end
  89.  
  90. function mark(n)
  91.     local s = string.sub(numPoints(tostring(n)), 1, 3)
  92.     if n < 1000 and n > 0 then
  93.         s = s .. " "
  94.     elseif n < 1000000 then
  95.         --k
  96.         s = s .. "K"
  97.     elseif n < 1000000000 then
  98.         --m
  99.         s = s .. "M"
  100.     elseif n < 1000000000000 then
  101.         --b
  102.         s = s .. "B"
  103.     elseif n < 1000000000000000 then
  104.         --t
  105.         s = s .. "T"
  106.     else
  107.         s = "err"
  108.     end
  109.     return s
  110. end
  111.  
  112. local m = 0
  113. function drawNext(storage, input, output)
  114.     if input > maxEnergyOutput then
  115.         input = maxEnergyOutput
  116.     end
  117.     if output > maxEnergyOutput then
  118.         output = maxEnergyOutput
  119.     end
  120.     local i = math.modf(input / stepSizeIO)
  121.     local o = math.modf(output / stepSizeIO)
  122.     gpu.copy(diagramPosX + 1, diagramPosY, diagramSizeX - 1, diagramSizeY, -1, 0)
  123.     gpu.setBackground(backgroundDiagramColor)
  124.     gpu.fill(diagramPosX + diagramSizeX - 1, diagramPosY, 1, diagramSizeY, " ")
  125.  
  126.     if (i >= o) then
  127.         gpu.setBackground(inputColor)
  128.         gpu.setForeground(outputColor)
  129.         gpu.fill(diagramPosX + diagramSizeX - 1, diagramPosY + diagramSizeY - o, 1, o, "▒")
  130.         gpu.fill(diagramPosX + diagramSizeX - 1, diagramPosY + diagramSizeY - i, 1, i - o, " ")
  131.         gpu.setBackground(backgroundColor)
  132.         gpu.setForeground(mainTextColor)
  133.     else
  134.         gpu.setBackground(inputColor)
  135.         gpu.setForeground(outputColor)
  136.         gpu.fill(diagramPosX + diagramSizeX - 1, diagramPosY + diagramSizeY - i, 1, i, "▒")
  137.         gpu.setBackground(backgroundDiagramColor)
  138.         gpu.fill(diagramPosX + diagramSizeX - 1, diagramPosY + diagramSizeY - o, 1, o - i, "▒")
  139.         gpu.setBackground(backgroundColor)
  140.         gpu.setForeground(mainTextColor)
  141.     end
  142.  
  143.     if m % 10 == 0 then
  144.         gpu.setForeground(storageColor)
  145.         local st = math.modf(storage / stepSizeCap)
  146.         local p = math.modf((storage - stepSizeCap * st) / (stepSizeCap / 8))
  147.         gpu.copy(34, 1, 160 - 33 - 4, 10, -1, 0)
  148.         gpu.fill(160 - 4, 1, 1, 10, " ")
  149.         gpu.fill(160 - 4, 10 - st + 1, 1, st, "█")
  150.         gpu.set(160 - 4, 10 - st, partsChar[p + 1])
  151.         gpu.setForeground(mainTextColor)
  152.     end
  153.     m = m + 1
  154. end
  155.  
  156. function init()
  157.     local temp = 0
  158.     gpu.setForeground(mainTextColor)
  159.     gpu.setBackground(backgroundColor)
  160.     gpu.fill(1, 1, w, h, ' ')
  161.     gpu.set(headOffSet, 1, head)
  162.  
  163.     capacity = gtBattBuf.getEUMaxStored()
  164.     for i = 1, 16 do
  165.         temp = gtBattBuf.getMaxBatteryCharge(i)
  166.         if temp ~= nil then
  167.             capacity = capacity + temp
  168.         end
  169.     end
  170.     if maxEnergyOutput == nil then
  171.         maxEnergyOutput = 131072 * 6
  172.     end
  173.     stepSizeCap = capacity / 10
  174.     stepSizeIO = maxEnergyOutput / diagramSizeY
  175.     int_stepSizeCap = math.modf(stepSizeCap)
  176.     int_stepSizeIO = math.modf(stepSizeIO)
  177.  
  178.     gpu.set(1, 3, string.sub("EU energy max   " .. numPoints(tostring(capacity)) .. clear, 1, 32))
  179.  
  180.     gpu.setForeground(storageColor)
  181.     gpu.set(1, 2, "EU energy store ")
  182.     gpu.setForeground(inputColor)
  183.     gpu.set(1, 4, "EU energy in    ")
  184.     gpu.setForeground(outputColor)
  185.     gpu.set(1, 5, "EU energy out   ")
  186.     gpu.setForeground(mainTextColor)
  187.  
  188.     gpu.setBackground(backgroundDiagramColor)
  189.     gpu.fill(diagramPosX, diagramPosY, diagramSizeX, diagramSizeY, " ")
  190.  
  191.     gpu.setForeground(numderLineStorageColor)
  192.     temp = int_stepSizeCap
  193.     for i = 10, 1, -1 do
  194.         gpu.set(160 - 3, i, mark(temp))
  195.         temp = temp + int_stepSizeCap
  196.     end
  197.     gpu.setBackground(backgroundColor)
  198.  
  199.     gpu.setForeground(numderLineIOColor)
  200.     temp = int_stepSizeIO
  201.     for i = diagramPosY + diagramSizeY - 1, diagramPosY, -1 do
  202.         gpu.set(diagramSizeX + 1, i, mark(temp))
  203.         temp = temp + int_stepSizeIO
  204.     end
  205.     gpu.setForeground(mainTextColor)
  206. end
  207.  
  208. function blink(flag)
  209.     if flag then
  210.         color = gpu.setBackground(runningIndicatorColor)
  211.         gpu.set(1, 1, "  ")
  212.         gpu.setBackground(backgroundColor)
  213.     else
  214.         gpu.set(1, 1, "  ")
  215.     end
  216.     return not flag
  217. end
  218.  
  219. function loop()
  220.     local preCapacity = capacity
  221.     local temp = 0
  222.     local flag = true
  223.     local input = 0
  224.     local output = 0
  225.     local n = 0
  226.     local energyStored = 0
  227.     local filter = function(name, ...)
  228.         if name == "touch" then
  229.             return true
  230.         else
  231.             return false
  232.         end
  233.     end
  234.     while true do
  235.         if event.pullFiltered(0.05, filter) == "touch" then
  236.             w, h = gpu.maxResolution()
  237.             if gpu.getViewport() ~= w then
  238.                 gpu.setViewport(w, h)
  239.                 gpu.set(1, 10, "                         ")
  240.             else
  241.                 gpu.setViewport(32, 10)
  242.                 w, h = 32, 10;
  243.             end
  244.         end
  245.  
  246.         temp = gtBattBuf.getMaxBatteryCharge(n % 16 + 1)
  247.         if temp ~= nil then
  248.             capacity = capacity + temp
  249.         end
  250.         if n % 16 == 0 then
  251.             capacity = capacity + gtBattBuf.getEUMaxStored()
  252.             if n ~= 0 and capacity ~= preCapacity then
  253.                 preCapacity = capacity
  254.                 stepSizeCap = capacity / 10
  255.                 int_stepSizeCap = math.modf(stepSizeCap)
  256.                 gpu.setForeground(numderLineStorageColor)
  257.                 temp = int_stepSizeCap
  258.                 for i = 10, 1, -1 do
  259.                     gpu.set(160 - 3, i, mark(temp))
  260.                     temp = temp + int_stepSizeCap
  261.                 end
  262.                 gpu.setForeground(mainTextColor)
  263.                 gpu.set(17, 3, string.sub(numPoints(tostring(capacity)) .. clear, 1, 32 - 17))
  264.             end
  265.         end
  266.  
  267.         input = input + gtBattBuf.getEUInputAverage()
  268.         output = output + gtBattBuf.getEUOutputAverage()
  269.         temp = gtBattBuf.getBatteryCharge(n % 16 + 1)
  270.         if (temp ~= nil) then
  271.             energyStored = energyStored + temp;
  272.         end
  273.         if n % 16 == 0 then
  274.             if n ~= 0 then
  275.                 energyStored = energyStored + gtBattBuf.getEUStored()
  276.                 gpu.setForeground(storageColor)
  277.                 gpu.set(17, 2, string.sub(numPoints(tostring(energyStored)) .. clear, 1, 32 - 17))
  278.                 gpu.setForeground(mainTextColor)
  279.  
  280.                 if energyStored / capacity <= 0.05 then
  281.                     rs.setOutput(redstoneIOControlSide, 15)
  282.                 elseif energyStored / capacity >= 0.98 then
  283.                     rs.setOutput(redstoneIOControlSide, 0)
  284.                 end
  285.  
  286.                 if rs.getOutput(redstoneIOControlSide) == 0 then
  287.                     gpu.set(1, h, "Energy Generation Disable")
  288.                 else
  289.                     gpu.set(1, h, "Energy Generation Enable ")
  290.                 end
  291.  
  292.                 input = math.modf(input / 16)
  293.                 output = math.modf(output / 16)
  294.  
  295.                 gpu.setForeground(inputColor)
  296.                 gpu.set(17, 4, string.sub(numPoints(tostring(input)) .. clear, 1, 32 - 17))
  297.                 gpu.setForeground(outputColor)
  298.                 gpu.set(17, 5, string.sub(numPoints(tostring(output)) .. clear, 1, 32 - 17))
  299.                 gpu.setForeground(mainTextColor)
  300.  
  301.                 drawNext(energyStored, input, output)
  302.             end
  303.             input = 0
  304.             output = 0
  305.             energyStored = 0
  306.             capacity = 0
  307.         end
  308.  
  309.         flag = blink(flag)
  310.         n = n + 1
  311.     end
  312. end
  313.  
  314. init()
  315. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement