Advertisement
Clor

OpenComputer+computronics IIA power monitor + controller

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