Advertisement
Tcharim

Computer Craft Energy monitoring

Nov 24th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | Source Code | 0 0
  1. local WAIT_DELAY = 5
  2.  
  3. local RED_LEVEL = 10
  4. local ORANGE_LEVEL = 50
  5. local YELLOW_LEVEL = 70
  6.  
  7. function PrintOnMonitor(monitor, cell_array)
  8.     monitor_width,_ = monitor.getSize()
  9.     title = "Energy Monitor"
  10.     energy_level = 0
  11.     monitor.setBackgroundColor(colors.black)
  12.     monitor.clear()
  13.     monitor.setCursorPos(math.floor((monitor_width - string.len(title))/2), 1)
  14.     monitor.setBackgroundColor(colors.red)
  15.     monitor.setTextColor(colors.yellow)
  16.     monitor.write(title)
  17.     if (cell_array[1] == nil) then
  18.         monitor.setCursorPos(1, 3)
  19.         monitor.setBackgroundColor(colors.black)
  20.         monitor.setTextColor(colors.red)
  21.         monitor.write("There in no energy cell connected.")
  22.     end
  23.  
  24.     for i, cell in ipairs(cell_array)do
  25.         if(cell ~=nil) then
  26.             monitor.setBackgroundColor(colors.black)
  27.             monitor.setTextColor(colors.white)
  28.             monitor.setCursorPos(1, i+1)
  29.             monitor.write(peripheral.getName(cell))
  30.             energyCap = cell.getEnergyCapacity()
  31.             energy = cell.getEnergy()
  32.             if(energy == nil or energyCap == nil) then
  33.                 error = "Error: energy or energy capacity is nil"
  34.                 monitor.setTextColor(colors.red)
  35.                 monitor.setCursorPos(monitor_width - string.len(error), i + 1)
  36.                 monitor.write(error)
  37.             else
  38.                 monitor.setCursorPos(monitor_width-10, i+1)
  39.                 monitor.write("|")
  40.                 monitor.setCursorPos(monitor_width-7, i+1)
  41.                 energy_level = (energy/energyCap)*100
  42.                 if(energy_level < RED_LEVEL) then
  43.                     monitor.setTextColor(colors.red)
  44.                 else if(energy_level < ORANGE_LEVEL)then
  45.                         monitor.setTextColor(colors.orange)
  46.                     else if(energy_level < YELLOW_LEVEL) then
  47.                             monitor.setTextColor(colors.yellow)
  48.                         else
  49.                             monitor.setTextColor(colors.green)
  50.                         end
  51.                     end
  52.                 end
  53.                 monitor.write(string.format("%6.2f %%",energy_level))
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. function SearchMonitor()
  60.     return peripheral.find("monitor")
  61. end
  62.  
  63. function SearchForEnergyCells()
  64.     list_peripherals = peripheral.getNames()
  65.     energy_cell_array = {}
  66.     array_index = 1
  67.     for i, periph in ipairs(list_peripherals) do
  68.         if(peripheral.hasType(periph,"energy_storage")) then
  69.             energy_cell_array[array_index] = peripheral.wrap(periph)
  70.             array_index = array_index + 1
  71.         end
  72.     end
  73.     return energy_cell_array
  74. end
  75.  
  76. while true do
  77.     local monitor = SearchMonitor()
  78.     local energy_cell_array = SearchForEnergyCells()
  79.  
  80.     PrintOnMonitor(monitor, energy_cell_array)
  81.     os.sleep(WAIT_DELAY)
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement