Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local WAIT_DELAY = 5
- local RED_LEVEL = 10
- local ORANGE_LEVEL = 50
- local YELLOW_LEVEL = 70
- function PrintOnMonitor(monitor, cell_array)
- monitor_width,_ = monitor.getSize()
- title = "Energy Monitor"
- energy_level = 0
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setCursorPos(math.floor((monitor_width - string.len(title))/2), 1)
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.yellow)
- monitor.write(title)
- if (cell_array[1] == nil) then
- monitor.setCursorPos(1, 3)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.red)
- monitor.write("There in no energy cell connected.")
- end
- for i, cell in ipairs(cell_array)do
- if(cell ~=nil) then
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(1, i+1)
- monitor.write(peripheral.getName(cell))
- energyCap = cell.getEnergyCapacity()
- energy = cell.getEnergy()
- if(energy == nil or energyCap == nil) then
- error = "Error: energy or energy capacity is nil"
- monitor.setTextColor(colors.red)
- monitor.setCursorPos(monitor_width - string.len(error), i + 1)
- monitor.write(error)
- else
- monitor.setCursorPos(monitor_width-10, i+1)
- monitor.write("|")
- monitor.setCursorPos(monitor_width-7, i+1)
- energy_level = (energy/energyCap)*100
- if(energy_level < RED_LEVEL) then
- monitor.setTextColor(colors.red)
- else if(energy_level < ORANGE_LEVEL)then
- monitor.setTextColor(colors.orange)
- else if(energy_level < YELLOW_LEVEL) then
- monitor.setTextColor(colors.yellow)
- else
- monitor.setTextColor(colors.green)
- end
- end
- end
- monitor.write(string.format("%6.2f %%",energy_level))
- end
- end
- end
- end
- function SearchMonitor()
- return peripheral.find("monitor")
- end
- function SearchForEnergyCells()
- list_peripherals = peripheral.getNames()
- energy_cell_array = {}
- array_index = 1
- for i, periph in ipairs(list_peripherals) do
- if(peripheral.hasType(periph,"energy_storage")) then
- energy_cell_array[array_index] = peripheral.wrap(periph)
- array_index = array_index + 1
- end
- end
- return energy_cell_array
- end
- while true do
- local monitor = SearchMonitor()
- local energy_cell_array = SearchForEnergyCells()
- PrintOnMonitor(monitor, energy_cell_array)
- os.sleep(WAIT_DELAY)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement