Rawoas13

[mepowermonitor] [LUA]

Oct 30th, 2020
1,063
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MonitorSide = "top"
  2. Monitor = peripheral.wrap(MonitorSide)
  3. cell1 = peripheral.wrap("cofh_thermalexpansion_energycell_107")
  4. cell2 = peripheral.wrap("cofh_thermalexpansion_energycell_108")
  5.  
  6. function ClearMonitor()
  7.     Monitor.setTextColor(colours.black)
  8.     Monitor.setBackgroundColor(colours.black)
  9.     Monitor.clear()
  10.     Monitor.setCursorPos(1,1)
  11. end
  12.  
  13. function DrawText(xPos, yPos, text, textColour, backgroundColour)
  14.     Monitor.setBackgroundColor(backgroundColour)
  15.     Monitor.setTextColor(textColour)
  16.     Monitor.setCursorPos(xPos,yPos)
  17.     Monitor.write(text)
  18. end
  19.  
  20. function DrawLine(xPos, yPos, lineLength, colour)
  21.     Monitor.setBackgroundColor(colour)
  22.     Monitor.setTextColor(colour)
  23.     Monitor.setCursorPos(xPos,yPos)
  24.     Monitor.write(string.rep(" ", lineLength))
  25. end
  26.  
  27. function ProgressBar(xPos, yPos, barLength, value, maxValue, backgroundColour, progressColour)
  28.     DrawLine(xPos, yPos, barLength, backgroundColour) --backgoround bar
  29.     local barSize = math.floor((value/maxValue) * barLength)
  30.     DrawLine(xPos, yPos, barSize, progressColour) --progress so far
  31. end
  32.  
  33. function GroupBox(xPos, yPos, height, header, textColour, headerBackgroundColour, rightPadding)
  34.     GroupBoxHeader(xPos, yPos, header, textColour, headerBackgroundColour, rightPadding)
  35.     GroupBoxFooter(xPos, yPos + height + 1, textColour, headerBackgroundColour, rightPadding)
  36. end
  37.  
  38. function GroupBoxHeader(xPos, yPos, header, textColour, backgroundColour, rightPadding)
  39.     local repeatChars
  40.     local headerLength = string.len(header)
  41.    
  42.     local monX, monY = Monitor.getSize()
  43.     repeatChars = monX - 4 - headerLength - rightPadding
  44.  
  45.     if (repeatChars < 0) then
  46.         repeatChars = 0
  47.     end
  48.  
  49.     local head = "--- " .. header .. " " .. string.rep("-", repeatChars - rightPadding)
  50.     DrawText(xPos, yPos, head, textColour, backgroundColour)
  51. end
  52.  
  53. function GroupBoxFooter(xPos, yPos, textColour, backgroundColour, rightPadding)
  54.     local repeatChars
  55.    
  56.     local monX, monY = Monitor.getSize()
  57.     repeatChars = monX - rightPadding
  58.  
  59.     if (repeatChars < 0) then
  60.         repeatChars = 0
  61.     end
  62.  
  63.     local head = string.rep("-", repeatChars)
  64.     DrawText(xPos, yPos, head, textColour, backgroundColour)
  65. end
  66.  
  67. function GetColourFromPercent(percentage)
  68.     local colourOut = colours.red
  69.     if (percentage > 15)   then colourOut = colours.orange end
  70.     if (percentage > 45)  then colourOut = colours.yellow end
  71.     if (percentage > 70)  then colourOut = colours.green end
  72.     if (percentage > 90)  then colourOut = colours.cyan end
  73.     return colourOut
  74. end
  75.  
  76. function MainFunc()
  77.     Monitor.setTextScale(1)
  78.     while (true) do
  79.         local monX, monY = Monitor.getSize()
  80.  
  81.         local cell1Energy = cell1.getEnergyStored("unknown")
  82.         local cell2Energy = cell2.getEnergyStored("unknown")
  83.  
  84.         local cell1MaxEnergy = cell1.getMaxEnergyStored("unknown")
  85.         local cell2MaxEnergy = cell2.getMaxEnergyStored("unknown")
  86.  
  87.         local cell1Percent = math.floor((cell1Energy / cell1MaxEnergy) * 100)
  88.         local cell2Percent = math.floor((cell2Energy / cell2MaxEnergy) * 100)
  89.  
  90.         local c1c
  91.         local c2c
  92.  
  93.         if (cell1Percent == 0) then c1c = colours.red else c1c = colours.grey end
  94.         if (cell2Percent == 0) then c2c = colours.red else c2c = colours.grey end
  95.  
  96.         ClearMonitor()
  97.  
  98.         GroupBox(2, 2, 4, "ME Backup Cells Power", colours.orange, colours.black, 0)
  99.  
  100.         DrawText(3, 4, "Cell 1", colours.white, colours.black)
  101.         ProgressBar(10, 4, monX - 15, cell1Energy, cell1MaxEnergy, c1c, GetColourFromPercent(cell1Percent))
  102.         DrawText(monX - 4, 4, cell1Percent .. "%", colours.white, colours.black)
  103.        
  104.         DrawText(3, 6, "Cell 2", colours.white, colours.black)
  105.         ProgressBar(10, 6, monX - 15, cell2Energy, cell2MaxEnergy, c2c, GetColourFromPercent(cell2Percent))
  106.         DrawText(monX - 4, 6, cell2Percent .. "%", colours.white, colours.black)
  107.  
  108.         sleep(1)
  109.     end
  110. end
  111.  
  112. MainFunc()
RAW Paste Data