Martmists

Energy monitor

May 18th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. p = peripheral.wrap("right") -- wrap Energy cell
  2. m = peripheral.wrap("top") -- wrap monitor
  3. m.setTextColor(colors.green) -- set colors
  4. m.setBackgroundColor(colors.red)
  5. term.clear()
  6. term.setCursorPos(1,1)
  7.  
  8. function chooseColors()
  9.     coloragain = false
  10.     print("Choose your colors:")
  11.     print("1)")
  12.     term.setTextColor(colors.black)
  13.     print("backgound")
  14.     term.setTextColor(colors.red)
  15.     print("bar")
  16.     print("2)")
  17.     term.setTextColor(colors.red)
  18.     print("backgound")
  19.     term.setTextColor(colors.green)
  20.     print("bar")
  21.     setcolors = read()
  22.     if setcolors == "1" then
  23.         colorback = colors.black
  24.         colorbar = colors.red
  25.     elseif setcolors == "2" then
  26.         colorback = colors.red
  27.         colorbar = colors.green
  28.     else
  29.         print("Invalid number. Please try again.")
  30.         sleep(0.5)
  31.         term.clear()
  32.         term.setCursorPos(1,1)
  33.         coloragain = true
  34.     end
  35. end
  36.  
  37. function getInfo()
  38.   me = p.getMaxEnergyStored() -- get energy levels
  39.   e = p.getEnergyStored()     -- ..
  40. end
  41.  
  42. function calculate()
  43.   -- perc = percentage
  44.   perc = e / me -- e.g. 1000/4000 returns 0.25
  45.   perc = perc * 100 -- needed for calculation
  46. end
  47.  
  48. function getMonitor()
  49.   x,y = m.getSize() -- get size
  50.   if vorh == "horizontal" then
  51.     Xvalue = x / 100 -- get multiplier
  52.   else
  53.     Xvalue = y / 100
  54.   end
  55. end
  56.  
  57. function display()
  58.   monperc = perc * Xvalue -- amount of pixels to fill
  59.   fillspace = math.ceil(monperc) -- get number, e.g. not 1,5
  60.   if vorh == "vertical" then
  61.     fillspace = fillspace - 1
  62.     fillspace = y - fillspace
  63.   end
  64.   m.setBackgroundColor(colorback)
  65.   -- sleep(0.1) -- remove "--" if experiencing lagg
  66.   m.clear() -- clear monitor
  67.   -- sleep(0.1)
  68.   m.setBackgroundColor(colorbar)
  69.   if vorh == "horizontal" then
  70.     for i = 1,fillspace do
  71.       for l = 1,y do
  72.         m.setCursorPos(i,l)
  73.         m.write(" ")
  74.       end
  75.     end
  76.   else
  77.     for i = fillspace,y do
  78.       for l = 1,x do
  79.         m.setCursorPos(l,i)
  80.         m.write(" ")
  81.       end
  82.     end
  83.   end
  84. end
  85.  
  86. print("Do you want the monitor vertical or horizontal?")
  87. vorh = read()
  88.  
  89. while true do --loop
  90. if coloragain == true then
  91.     chooseColor()
  92.   else
  93.   getInfo()
  94.   sleep(0.1) -- short interval due to lagg issues
  95.   calculate()
  96.   sleep(0.1)
  97.   getMonitor()
  98.   sleep(0.1)
  99.   display()
  100.   sleep(0.3)
  101.   end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment