Advertisement
birello

Power Monitor (enderio) + monitor

Mar 27th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.28 KB | None | 0 0
  1. local upper = 0.90 --Upper limit for computer to stop transmitting redstone signal. 0.90=90% full.
  2. local lower = 0.10 --Lower limit for computer to start transmitting redstone signal.
  3.  
  4. -------------------FORMATTING-------------------------------
  5. function clear()
  6.         monitor.setBackgroundColor(colors.black)
  7.         monitor.clear()
  8.         monitor.setCursorPos(1,1)
  9. end
  10.  
  11. function draw_text_term(x, y, text, text_color, bg_color)
  12.         term.setTextColor(text_color)
  13.         term.setBackgroundColor(bg_color)
  14.         term.setCursorPos(x,y)
  15.         write(text)
  16. end
  17.  
  18. function draw_text(x, y, text, text_color, bg_color)
  19.         monitor.setBackgroundColor(bg_color)
  20.         monitor.setTextColor(text_color)
  21.         monitor.setCursorPos(x,y)
  22.         monitor.write(text)
  23. end
  24.  
  25. function draw_line(x, y, length, color)
  26.                 monitor.setBackgroundColor(color)
  27.                 monitor.setCursorPos(x,y)
  28.                 monitor.write(string.rep(" ", length))
  29. end
  30.  
  31. function draw_line_term(x, y, length, color)
  32.                 term.setBackgroundColor(color)
  33.                 term.setCursorPos(x,y)
  34.                 term.write(string.rep(" ", length))
  35. end
  36.  
  37. function progress_bar(x, y, length, minVal, eMax, bar_color, bg_color)
  38.         draw_line(x, y, length, bg_color) --backgoround bar
  39.         local barSize = math.floor((eNow/eMax) * length)
  40.         draw_line(x, y, barSize, bar_color)     --progress so far
  41. end
  42.  
  43. function progress_bar_term(x, y, length, eNow, eMax, bar_color, bg_color)
  44.         draw_line_term(x, y, length, bg_color) --backgoround bar
  45.         local barSize = math.floor((eNow/eMax) * length)
  46.         draw_line_term(x, y, barSize, bar_color)        --progress so far
  47. end
  48. -------Device detection--------------
  49. isError=0
  50.  
  51. function detectDevice(DeviceName)
  52. DeviceSide="none"
  53. for k,v in pairs(redstone.getSides()) do
  54.  if peripheral.getType(v)==DeviceName then
  55.    DeviceSide = v
  56.    break
  57.  end
  58. end
  59.   return(DeviceSide)
  60. end
  61.  
  62. -------Wrap Peripheral--------------
  63. power="none"
  64. monitor="none"
  65. local peripheralList = peripheral.getNames()
  66.  
  67. powerSide=detectDevice("power_storage")
  68.  
  69. if powerSide~="none" then
  70.    power=peripheral.wrap(powerSide)
  71.    print ("Power monitor " .. powerSide .. " connected.")
  72.    else
  73.         powerSide=detectDevice("tile_enderio_blockcapacitorbank_name")
  74.         if powerSide~="none" then
  75.                 power=peripheral.wrap(powerSide)
  76.                 print ("EnderIO capacitorbank on the " .. powerSide .. " connected.")
  77.         else
  78.                         for Index = 1, #peripheralList do
  79.                                 if string.find(peripheralList[Index], "power_storage") then
  80.                                         power=peripheral.wrap(peripheralList[Index])
  81.                                         print ("Power Monitor on wired modem: "..peripheralList[Index].." connected.")
  82.                                 elseif string.find(peripheralList[Index], "tile_enderio_blockcapacitorbank_name") then
  83.                                         power=peripheral.wrap(peripheralList[Index])
  84.                                         print ("EnderIO capacitorbank on wired modem: "..peripheralList[Index].." connected.")
  85.                                 end
  86.                         end --for
  87.                         if power == "none" then
  88.                                 print("No Energy storage found. Halting script!")
  89.                                 return
  90.                         end
  91.  
  92.         end
  93. end
  94.  
  95. -------Monitor Connect--------------
  96. MonitorSide=detectDevice("monitor")
  97.  
  98. if MonitorSide~="none" then
  99.       monitor=peripheral.wrap(MonitorSide)
  100.    print ("Monitor on the " .. MonitorSide .. " connected.")
  101.    else
  102.         for Index = 1, #peripheralList do
  103.                 if string.find(peripheralList[Index], "monitor") then
  104.                         monitor=peripheral.wrap(peripheralList[Index])
  105.                         print ("Monitor on wired modem: "..peripheralList[Index].." connected.")
  106.                 end
  107.         end --for
  108.         if monitor == "none" then
  109.                 print ("Warning - No Monitor attached, continuing without.")
  110.         end
  111. end
  112.  
  113.  
  114. -------Monitor write functions--------------
  115. function homepage()
  116.         while true do
  117.                 clear()
  118.                 menu_bar()
  119.                 terminal_screen()
  120.  
  121.         -------Monitor write text--------------
  122.         if monitor ~= "none" then
  123.                 draw_text(2, 2, "Fuel Level:", colors.yellow, colors.black)
  124.                 local eMax = power.getMaxPowerInMachines()
  125.                 local eNow = power.getPowerInMachines()
  126.                 local percent = ((eNow/eMax)*100)
  127.                 draw_text(15, 5, percent.."%", colors.white, colors.black)
  128.  
  129.                 if percent < 25 then
  130.                 progress_bar(2, 4, monX-2, eNow, eMax, colors.red, colors.gray)
  131.                 else if percent < 50 then
  132.                 progress_bar(2, 4, monX-2, eNow, eMax, colors.orange, colors.gray)
  133.                 else if percent < 75 then      
  134.                 progress_bar(2, 4, monX-2, eNow, eMax, colors.yellow, colors.gray)
  135.                 else if percent < 100 then
  136.                 progress_bar(2, 4, monX-2, eNow, eMax, colors.lime, colors.gray)
  137.                 end
  138.                 end
  139.                 end
  140.                 end
  141.         end
  142.         end
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement