Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- User Settings --
- local CONFIG = {
- title = "ENERGY MONITOR",
- refreshRate = 1,
- -- Colours --
- bgColor = colours.black,
- textColor = colours.white,
- barBg = colours.grey,
- barFillLow = colours.red,
- barFillMid = colours.yellow,
- barFillHigh = colours.green,
- -- Bar Settings --
- barWidthPercent = 0.8
- }
- -- Setup --
- local mon = peripheral.find("monitor")
- local psd = nil
- for _,name in pairs(peripheral.getNames()) do
- if peripheral.getType(name) == "ic2:mfe"
- or peripheral.getType(name) == "ic2:mfsu"
- then
- psd = peripheral.wrap(name)
- end
- end
- if not mon then
- error("Monitor not found")
- end
- if not psd then
- error("Power storage device not found")
- end
- mon.setBackgroundColor(CONFIG.bgColor)
- mon.clear()
- mon.setTextScale(1)
- -- Helper Functions --
- local function centerText(y, text, color)
- local w, _ = mon.getSize()
- local x = math.floor((w - #text) / 2) + 1
- mon.setCursorPos(x, y)
- if color then mon.setTextColor(color) end
- mon.write(text)
- end
- local function getBarColor(percent)
- if percent >= 0.75 then
- return CONFIG.barFillHigh
- elseif percent >= 0.30 then
- return CONFIG.barFillMid
- else
- return CONFIG.barFillLow
- end
- end
- local function drawProgressBar(y, percent)
- local w, _ = mon.getSize()
- local barWidth = math.floor(w * CONFIG.barWidthPercent)
- local startX = math.floor((w - barWidth) / 2) + 1
- local filled = math.floor(barWidth * percent)
- -- background --
- mon.setBackgroundColor(CONFIG.barBg)
- mon.setCursorPos(startX, y)
- mon.write(string.rep(" ", barWidth))
- -- fill --
- mon.setBackgroundColor(getBarColor(percent))
- mon.setCursorPos(startX, y)
- mon.write(string.rep(" ", filled))
- mon.setBackgroundColor(CONFIG.bgColor)
- end
- -- Main Loop --
- local lastStored = -1
- local lastMax = -1
- while true do
- mon.setBackgroundColor(CONFIG.bgColor)
- local stored
- local max
- if psd.getEUStored then
- stored = psd:getEUStored()
- max = psd:getEUCapacity()
- elseif psd.getEnergyStored then
- stored = psd:getEnergyStored()
- max = psd:getMaxEnergyStored()
- else
- stored = nil
- max = nil
- end
- if not stored or not max then
- centerText(2, "PSD METHOD ERROR", colours.red)
- sleep(2)
- else
- local percent = stored / max
- local _, h = mon.getSize()
- centerText(4, CONFIG.title, CONFIG.textColor)
- centerText(8, string.format("%d / %d EU", stored, max), CONFIG.textColor)
- centerText(9, string.format("%.2f%%", percent * 100), CONFIG.textColor)
- drawProgressBar(math.floor(h / 2), percent)
- end
- sleep(CONFIG.refreshRate)
- end
Advertisement
Add Comment
Please, Sign In to add comment