Advertisement
mcgrenoa

EnergyMonitor

Dec 8th, 2018
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. local m = peripheral.wrap("left")
  2. local c1 = peripheral.wrap("draconic_rf_storage_0")
  3. local c2 = peripheral.wrap("draconic_rf_storage_1")
  4. local c3 = peripheral.wrap("draconic_rf_storage_2")
  5. local c4 = peripheral.wrap("draconic_rf_storage_3")
  6. monX, monY = m.getSize()
  7.  
  8. function clear()
  9.   m.setBackgroundColor(colors.black)
  10.   m.clear()
  11.   m.setCursorPos(1,1)
  12.   m.setTextScale(0.5)
  13. end
  14.  
  15. function drawText(x, y, text, color_txt, color_bg)
  16.   m.setBackgroundColor(color_bg)
  17.   m.setTextColor(color_txt)
  18.   m.setCursorPos(x,y)
  19.   m.write(text)
  20. end
  21.  
  22. function drawLine(x, y, length, height, color_line)
  23.   for yPos = y, y+height do
  24.     m.setBackgroundColor(color_line)
  25.     m.setCursorPos(x,yPos)
  26.     m.write(string.rep(" ", length))
  27.   end
  28. end
  29.  
  30. function drawProg(x, y, name, length, height, minVal, maxVal, color_bg)
  31.   if minVal > (.5*maxVal) then
  32.     color_line = colors.green
  33.   elseif minVal < (.25*maxVal) then
  34.     color_line = colors.red
  35.   else
  36.     color_line = colors.orange
  37.   end
  38.   drawLine(x, y, length, height, color_bg)
  39.   local barSize = (minVal/maxVal)*length
  40.   drawLine(x, y, barSize, height, color_line)
  41.   local text = name.." "..((minVal/maxVal)*100).."%"
  42.   if barSize > monX/2+#text/2 then
  43.     drawText(monX/2-#text/2+1, y+height/2, text, colors.black, color_line)
  44.   elseif barSize > #text then
  45.     drawText((x+barSize)-#text, y+height/2, text, colors.black, color_line)
  46.   else
  47.     drawText(monX/2-#text/2+1, y+height/2, text, colors.black, color_bg)
  48.   end
  49. end
  50.  
  51. local function comma_value(amount)
  52.   local formatted = amount
  53.   while true do  
  54.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  55.     if (k==0) then
  56.       break
  57.     end
  58.   end
  59.   return formatted
  60. end
  61.  
  62. local function ReadableNumber(num, places)
  63.     local ret
  64.     local placeValue = ("%%.%df"):format(places or 0)
  65.     if not num then
  66.         return 0
  67.     elseif num >= 1000000000000 then
  68.         ret = placeValue:format(num / 1000000000000) .. " Trillion" -- trillion
  69.     elseif num >= 1000000000 then
  70.         ret = placeValue:format(num / 1000000000) .. " Billion" -- billion
  71.     elseif num >= 1000000 then
  72.         ret = placeValue:format(num / 1000000) .. " Million" -- million
  73.     elseif num >= 1000 then
  74.         ret = placeValue:format(num / 1000) .. " Kilo" -- thousand
  75.     else
  76.         ret = num -- hundreds
  77.     end
  78.     return ret
  79. end
  80.  
  81.  
  82.  
  83.  
  84. while true do
  85.  
  86.     clear()
  87.  
  88.     c1Max = c1.getMaxEnergyStored()
  89.     c1Cur = c1.getEnergyStored()
  90.     c2Max = c2.getMaxEnergyStored()
  91.     c2Cur = c2.getEnergyStored()
  92.     c3Max = c3.getMaxEnergyStored()
  93.     c3Cur = c3.getEnergyStored()
  94.     c4Max = c4.getMaxEnergyStored()
  95.     c4Cur = c4.getEnergyStored()
  96.     pMax = c1Max + c2Max + c3Max + c4Max
  97.     pCur = c1Cur + c2Cur + c3Cur + c4Cur
  98.  
  99.     drawProg(2, 2, "Core 1:", monX-2, 2, c1Cur, c1Max, colors.gray)
  100.     drawProg(2, 6, "Core 2:", monX-2, 2, c2Cur, c2Max, colors.gray)
  101.     drawProg(2, 10, "Core 3:", monX-2, 2, c3Cur, c3Max, colors.gray)
  102.     drawProg(2, 14, "Core 4:", monX-2, 2, c4Cur, c4Max, colors.gray)
  103.  
  104.     drawText(2, 21, "Core 1: "..ReadableNumber(c1Cur, 2).." ".."RF", colors.white, colors.black)
  105.     drawText(2, 23, "Core 2: "..ReadableNumber(c2Cur, 2).." ".."RF", colors.white, colors.black)
  106.     drawText(2, 25, "Core 3: "..ReadableNumber(c3Cur, 2).." ".."RF", colors.white, colors.black)
  107.     drawText(2, 27, "Core 4: "..ReadableNumber(c4Cur, 2).." ".."RF", colors.white, colors.black)
  108.  
  109.     drawText(2, monY-7, "Total Power: "..ReadableNumber(pCur, 2).." ".."RF", colors.white, colors.black)
  110.  
  111.  
  112.     drawProg(2, monY-3, "Total Power:", monX-2, 2, pCur, pMax, colors.gray)
  113.  
  114.     sleep(0.1)
  115.  
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement