Clor

OpenComputer+computronics IIA power monitor + controller v3

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×