bobmarley12345

singlecellpowermonitor

Feb 8th, 2021 (edited)
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. MonitorSide = "right"
  2. Monitor1 = peripheral.wrap(MonitorSide)
  3. cell1 = peripheral.wrap("left")
  4. ProgressBarBackgroundColour = colours.grey
  5.  
  6. function GetHexFromColour(clr)
  7.     local colourOut = 0xCC4C4C --red
  8.     if (clr == colours.white)  then colourOut = 0xFFFFFF end
  9.     if (clr == colours.orange) then colourOut = 0xF2B233 end
  10.     if (clr == colours.yellow) then colourOut = 0xDEDE6C end
  11.     if (clr == colours.green)  then colourOut = 0x57A64E end
  12.     if (clr == colours.cyan)   then colourOut = 0x4C99B2 end
  13.     return colourOut;
  14. end
  15.  
  16. function GetColourFromPercent(percentage)
  17.     local colourOut = colours.red
  18.     if (percentage > 15) then colourOut = colours.orange end
  19.     if (percentage > 45) then colourOut = colours.yellow end
  20.     if (percentage > 70) then colourOut = colours.cyan end
  21.     if (percentage > 90) then colourOut = colours.green end
  22.     return colourOut
  23. end
  24.  
  25. function ClearScreens()
  26.     Monitor1.setTextColor(colours.black)
  27.     Monitor1.setBackgroundColor(colours.black)
  28.     Monitor1.clear()
  29.     Monitor1.setCursorPos(1,1)
  30. end
  31.  
  32. function DrawText(xPos, yPos, text, textColour, backgroundColour)
  33.     Monitor1.setBackgroundColor(backgroundColour)
  34.     Monitor1.setTextColor(textColour)
  35.     Monitor1.setCursorPos(xPos,yPos)
  36.     Monitor1.write(text)
  37. end
  38.  
  39. function DrawLine(xPos, yPos, lineLength, colour)
  40.     Monitor1.setBackgroundColor(colour)
  41.     Monitor1.setTextColor(colour)
  42.     Monitor1.setCursorPos(xPos,yPos)
  43.     Monitor1.write(string.rep(" ", lineLength))
  44. end
  45.  
  46. function ProgressBar(xPos, yPos, barLength, value, maxValue, backgroundColour, progressColour)
  47.     DrawLine(xPos, yPos, barLength, backgroundColour) --backgoround bar
  48.     local barSize = math.floor((value/maxValue) * barLength)
  49.     DrawLine(xPos, yPos, barSize, progressColour) --progress so far
  50. end
  51.  
  52. function DrawPowerBar(monitorX, yPos, cellName, cellEnergy, cellMaxEnergy, cellPercent, barColour)
  53.     DrawText(3, yPos, cellName, colours.white, colours.black)
  54.     ProgressBar(10, yPos, monitorX - 15, cellEnergy, cellMaxEnergy, barColour, GetColourFromPercent(cellPercent))
  55.     DrawText(monitorX - 4, yPos, cellPercent .. "%", colours.white, colours.black)
  56. end
  57.  
  58. function GetCellBarColour(cellEnergyPercent)
  59.     if (cellEnergyPercent == 0) then
  60.         return colours.red
  61.     else
  62.         return colours.grey
  63.     end
  64. end
  65.  
  66. function FetchAndRedraw()
  67.     Monitor1.setTextScale(1)
  68.     local monX, monY = Monitor1.getSize()
  69.  
  70.     local cell1Energy = cell1.getEnergyStored("unknown")
  71.     local cell1MaxEnergy = cell1.getMaxEnergyStored("unknown")
  72.     local cell1Percent = math.floor((cell1Energy / cell1MaxEnergy) * 100)
  73.  
  74.     ClearScreens()
  75.  
  76.     DrawText(2, 2, "ME Backup Power Cell", colours.white, colours.black)
  77.     DrawText(2, 4, cell1Percent .. "%", colours.white, colours.black)
  78.     ProgressBar(2, 6, monX - 2, cell1Energy , cell1MaxEnergy , ProgressBarBackgroundColour, GetColourFromPercent(cell1Percent))
  79. end
  80.  
  81. while true do
  82.     FetchAndRedraw()
  83.     os.sleep(2)
  84. end
Add Comment
Please, Sign In to add comment