Martmists

Energy monitor V2

May 28th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- energy monitor v2
  2. p = peripheral.wrap("right") -- wrap Energy cell
  3. m = peripheral.wrap("top") -- wrap monitor
  4. m.setTextColor(colors.green) -- set colors
  5. m.setBackgroundColor(colors.black)
  6. term.clear()
  7. term.setCursorPos(1,1)
  8.  
  9. function colorsopt(backcol,barcol)
  10.     colorback = backcol
  11.     colorbar = barcol
  12. end
  13.    
  14.  
  15. function chooseColors()
  16.     coloragain = false
  17.  
  18.     print("Choose your colors:")
  19.     print("1)")
  20.     term.setTextColor(colors.black)
  21.     print("backgound")
  22.     term.setTextColor(colors.red)
  23.     print("bar")
  24.    
  25.     print("2)")
  26.     term.setTextColor(colors.red)
  27.     print("backgound")
  28.     term.setTextColor(colors.green)
  29.     print("bar")
  30.  
  31.     event,key = os.pullEvent("key")
  32.  
  33.     if key == 2 then
  34.         colorsopt(colors.black,colors.red)
  35.     elseif key == 3 then
  36.         colorsopt(colors.red,colors.green)
  37.     else
  38.         print("Invalid number. Please try again.")
  39.         sleep(0.5)
  40.         term.clear()
  41.         term.setCursorPos(1,1)
  42.         coloragain = true
  43.     end
  44. end
  45.  
  46. function getInfo()
  47.   me = p.getMaxEnergyStored() -- get energy levels
  48.   e = p.getEnergyStored()     -- ..
  49. end
  50.  
  51. function calculate()
  52.   perc = e / me -- e.g. 1000/4000 returns 0.25
  53.   perc = perc * 100 -- needed for calculation
  54. end
  55.  
  56. function getMonitor()
  57.   x,y = m.getSize() -- get size
  58.   if vorh == "horizontal" then
  59.     Xvalue = x / 100 -- get multiplier
  60.   else
  61.     Xvalue = y / 100
  62.   end
  63. end
  64.  
  65.  
  66. function display()
  67.   if bar then
  68.     displayBar()
  69.   else
  70.     displayRound(true)
  71.     for q = 1,20 do
  72.       displayRound(false)
  73.     end
  74.   end
  75. end
  76.  
  77. function displayRound(full)
  78.   x = 1 + math.floor(0.5 * x)
  79.   y = 1 + math.floor(0.5 * y)
  80.   r = math.min(x,y) - 1
  81.   if full then
  82.     percfinal = 3.15
  83.     colorb = colors.red
  84.   else
  85.     percfinal = perc * 3.15
  86.     colorb = colors.green
  87.   end
  88.   for i = 0,percfinal,0.01 do
  89.     py = r - (math.sin(i) * r)
  90.     px = r - (math.cos(i) * r)
  91.     m.setBackgroundColor(colorb)
  92.     m.setCursorPos((px + (0.4 * x) - 0.5),(py + y - 1))
  93.     m.write(" ")
  94.     sleep(0.1)
  95.     m.setBackgroundColor(colors.black)
  96.     m.setCursorPos(1,r-1)
  97.     perc = math.ceil(perc * 100)
  98.     m.write("The cell is "..perc.." % full.")
  99.   end
  100. end
  101.  
  102. function displayBar()
  103.   monperc = perc * Xvalue -- amount of pixels to fill
  104.   fillspace = math.ceil(monperc) -- get number, e.g. not 1,5
  105.   if vorh == "vertical" then
  106.     fillspace = fillspace - 1
  107.     fillspace = y - fillspace
  108.   end
  109.   m.setBackgroundColor(colorback)
  110.   -- sleep(0.1) -- remove "--" if experiencing lagg
  111.   m.clear() -- clear monitor
  112.   m.setBackgroundColor(colorbar)
  113.   if vorh == "horizontal" then
  114.     for i = 1,fillspace do
  115.       for l = 1,y do
  116.         m.setCursorPos(i,l)
  117.         m.write(" ")
  118.       end
  119.     end
  120.   else
  121.     for i = fillspace,y do
  122.       for l = 1,x do
  123.         m.setCursorPos(l,i)
  124.         m.write(" ")
  125.       end
  126.     end
  127.   end
  128. end
  129.  
  130. function chooseForm()
  131.   while formagain do
  132.     formagain = false
  133.  
  134.     print("Choose your form: (type the number)")
  135.     print("1) Round")
  136.     print("2) Bar")
  137.     event,key = os.pullEvent("key")
  138.  
  139.     if key == 2 then
  140.         bar = false
  141.     elseif key == 3 then
  142.         bar = true
  143.     else
  144.         print("Invalid number. Please try again.")
  145.         sleep(0.5)
  146.         term.clear()
  147.         term.setCursorPos(1,1)
  148.         formagain = true
  149.     end
  150.   end
  151. end
  152. formagain = true
  153. chooseForm()
  154.  
  155. if bar then
  156.   print("Do you want the monitor vertical or horizontal?")
  157.   vorh = read()
  158.   chooseColors()
  159. end
  160.  
  161. while true do --loop
  162.   getInfo()
  163.   sleep(0.1) -- short interval due to lagg issues
  164.   if bar then
  165.     calculate()
  166.     sleep(0.1)
  167.   end
  168.   getMonitor()
  169.   sleep(0.1)
  170.   display()
  171.   sleep(0.3)
  172.   end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment