Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ME System Monitor for CC:Tweaked (Optimized Drawing)
- local monitor = peripheral.find("monitor")
- local meBridge = peripheral.wrap("bottom")
- if not monitor then
- error("Monitor not found")
- end
- if not meBridge then
- error("ME Bridge not found")
- end
- monitor.setTextScale(1)
- local width, height = monitor.getSize()
- -- Ensure width and height are valid numbers
- if not width or not height or width <= 0 or height <= 0 then
- error("Invalid monitor dimensions: " .. tostring(width) .. "x" .. tostring(height))
- end
- local expanded = nil
- local currentCPU = 1
- local blink = true
- local lastBlink = 0
- local lastRefresh = 0
- local lastClockTime = ""
- -- Cache previous values to avoid unnecessary redraws
- local prevValues = {
- storage_pct = -1,
- crafting_pct = -1,
- power_usage = -1,
- power_stored = -1,
- storage_warning = false
- }
- -- Make sure math operations return valid numbers
- local halfWidth = math.max(1, math.floor(width/2))
- local halfHeight = math.max(1, math.floor(height/2))
- local quads = {
- {1, 1, halfWidth, halfHeight},
- {halfWidth + 1, 1, width, halfHeight},
- {1, halfHeight + 1, halfWidth, height},
- {halfWidth + 1, halfHeight + 1, width, height}
- }
- local histories = {{}, {}, {}, {}}
- local events = {}
- local MAX_HISTORY = math.max(10, width - 4)
- local MAX_EVENTS = 5
- local function logEvent(msg)
- table.insert(events, 1, os.date("%H:%M:%S") .. " - " .. msg)
- if #events > MAX_EVENTS then
- table.remove(events)
- end
- end
- local function clearArea(x1, y1, x2, y2, color)
- -- Validate coordinates
- x1, y1, x2, y2 = math.max(1, x1), math.max(1, y1), math.min(width, x2), math.min(height, y2)
- monitor.setBackgroundColor(color or colors.black)
- for y = y1, y2 do
- monitor.setCursorPos(x1, y)
- monitor.write(string.rep(" ", math.max(0, x2 - x1 + 1)))
- end
- end
- local function drawCenteredText(x1, x2, y, text, textColor, bg)
- -- Validate coordinates and text
- if not text or y < 1 or y > height then return end
- monitor.setTextColor(textColor or colors.white)
- monitor.setBackgroundColor(bg or colors.black)
- -- Clear the line first
- local lineWidth = x2 - x1 + 1
- monitor.setCursorPos(x1, y+1)
- monitor.write(string.rep(" ", lineWidth))
- -- Then draw centered text
- local cx = math.max(x1, math.floor((x1 + x2 - #text) / 2))
- monitor.setCursorPos(cx, y)
- monitor.write(string.sub(text, 1, lineWidth))
- end
- local function drawBorder(x1, y1, x2, y2, color)
- -- Validate coordinates
- x1, y1, x2, y2 = math.max(1, x1), math.max(1, y1), math.min(width, x2), math.min(height, y2)
- monitor.setBackgroundColor(color)
- for x = x1, x2 do
- if y1 >= 1 and y1 <= height then
- monitor.setCursorPos(x, y1)
- monitor.write(" ")
- end
- if y2 >= 1 and y2 <= height and y2 ~= y1 then
- monitor.setCursorPos(x, y2)
- monitor.write(" ")
- end
- end
- for y = y1 + 1, y2 - 1 do
- if y >= 1 and y <= height then
- monitor.setCursorPos(x1, y)
- monitor.write(" ")
- if x2 ~= x1 then
- monitor.setCursorPos(x2, y)
- monitor.write(" ")
- end
- end
- end
- end
- local function drawProgressBar(x1, y, x2, percent, bg, fg)
- -- Validate inputs
- if y < 1 or y > height or x1 > x2 then return end
- local barLen = math.max(0, x2 - x1 + 1)
- local fill = math.max(0, math.min(barLen, math.floor(barLen * math.max(0, math.min(1, percent or 0)))))
- -- Always redraw the entire progress bar (consistent height and colors)
- monitor.setCursorPos(x1, y)
- monitor.setBackgroundColor(bg)
- monitor.write(string.rep(" ", barLen))
- if fill > 0 then
- monitor.setCursorPos(x1, y)
- monitor.setBackgroundColor(fg)
- monitor.write(string.rep(" ", fill))
- end
- end
- local function getColorByPercent(p)
- p = p or 0
- if p > 0.75 then return colors.green
- elseif p > 0.5 then return colors.yellow
- elseif p > 0.25 then return colors.orange
- else return colors.red end
- end
- local function drawClock()
- local t = textutils.formatTime(os.time(), true)
- if t ~= lastClockTime then
- -- Clear only the clock area
- monitor.setCursorPos(1, height)
- monitor.setBackgroundColor(colors.black)
- monitor.write(string.rep(" ", width))
- -- Draw new time
- monitor.setTextColor(colors.lightGray)
- monitor.setBackgroundColor(colors.black)
- local cx = math.floor((width - #t) / 2)
- monitor.setCursorPos(cx, height)
- monitor.write(t)
- lastClockTime = t
- end
- end
- local function updateHistories()
- -- Safely get ME system data with error handling
- local success, used = pcall(function() return meBridge.getUsedItemStorage() end)
- if not success then used = 0 end
- local success, total = pcall(function() return meBridge.getTotalItemStorage() end)
- if not success or not total or total <= 0 then total = 1 end
- table.insert(histories[2], used / total)
- local success, cpus = pcall(function() return meBridge.getCraftingCPUs() end)
- if not success then cpus = {} end
- cpus = cpus or {}
- local busy = 0
- for _, c in ipairs(cpus) do
- if c.busy then busy = busy + 1 end
- end
- table.insert(histories[1], #cpus > 0 and (busy / #cpus) or 0)
- local success, power = pcall(function() return meBridge.getEnergyStorage() end)
- if not success then power = 0 end
- power = power or 0
- table.insert(histories[4], power / 1000000)
- -- Trim histories
- for i = 1, 4 do
- if #histories[i] > MAX_HISTORY then
- table.remove(histories[i], 1)
- end
- end
- end
- local function drawHistograph(x1, y1, x2, y2, data, color)
- -- Validate coordinates
- if x1 > x2 or y1 > y2 or y1 < 1 or y2 > height then return end
- local w = x2 - x1 + 1
- local h = y2 - y1
- if w <= 0 or h <= 0 then return end
- -- Always redraw the entire histograph area (consistent display)
- for i = 1, math.min(w, #data) do
- local val = math.max(0, math.min(1, data[#data - i + 1] or 0))
- local heightFill = math.floor(h * val)
- for j = 0, h - 1 do
- local plotY = y2 - j
- local plotX = x2 - i + 1
- if plotY >= 1 and plotY <= height and plotX >= 1 and plotX <= width then
- monitor.setCursorPos(plotX, plotY)
- if j < heightFill then
- monitor.setBackgroundColor(color)
- else
- monitor.setBackgroundColor(colors.black)
- end
- monitor.write(" ")
- end
- end
- end
- end
- local function drawEvents(x1, x2, startY)
- -- Always redraw events (they change frequently)
- for i = 1, MAX_EVENTS do
- local drawY = startY + i - 1
- if drawY >= 1 and drawY <= height - 1 then
- monitor.setCursorPos(x1, drawY)
- monitor.setTextColor(colors.gray)
- monitor.setBackgroundColor(colors.black)
- local event = events[i]
- if event then
- monitor.write(string.sub(event, 1, math.max(0, x2 - x1 + 1)))
- else
- monitor.write(string.rep(" ", math.max(0, x2 - x1 + 1)))
- end
- end
- end
- end
- local function drawStorage(x1, y1, x2, y2, forceRedraw)
- local success, used = pcall(function() return meBridge.getUsedItemStorage() end)
- if not success then used = 0 end
- local success, total = pcall(function() return meBridge.getTotalItemStorage() end)
- if not success or not total or total <= 0 then total = 1 end
- local pct = used / total
- local isWarning = pct > 0.75
- local bg = isWarning and colors.red or colors.black
- -- Only redraw if values changed or forced
- if forceRedraw or math.abs(pct - prevValues.storage_pct) > 0.001 or prevValues.storage_warning ~= isWarning then
- clearArea(x1, y1, x2, y2, bg)
- drawCenteredText(x1, x2, y1 + 1, "STORAGE", colors.white, bg)
- drawBorder(x1, y1, x2, y2, colors.white)
- prevValues.storage_pct = pct
- prevValues.storage_warning = isWarning
- end
- -- Always update percentage and progress bar
- drawCenteredText(x1, x2, y1 + 3, (math.floor(pct * 1000)/10).."% Used", colors.white, bg)
- if y1 + 5 <= y2 and x1 + 4 <= x2 then
- drawProgressBar(x1+2, y1+5, x2-2, pct, colors.blue, getColorByPercent(pct))
- end
- -- Handle blinking warning (keep as is)
- if isWarning and blink and y2 - 2 >= y1 then
- drawCenteredText(x1, x2, y2-2, "!!WARNING!!", colors.white, colors.red)
- if not prevValues.storage_warning then
- logEvent("Low Free Storage")
- end
- elseif isWarning and not blink and y2 - 2 >= y1 then
- -- Clear warning line when not blinking
- monitor.setCursorPos(x1, y2-2)
- monitor.setBackgroundColor(bg)
- monitor.write(string.rep(" ", x2 - x1 + 1))
- end
- end
- local function drawCrafting(x1, y1, x2, y2, forceRedraw)
- local success, cpus = pcall(function() return meBridge.getCraftingCPUs() end)
- if not success then cpus = {} end
- cpus = cpus or {}
- local count = #cpus
- local busy = 0
- for _, cpu in ipairs(cpus) do
- if cpu.busy then busy = busy + 1 end
- end
- local pct = count > 0 and (busy / count) or 0
- -- Only redraw static elements if forced or first time
- if forceRedraw or prevValues.crafting_pct < 0 then
- clearArea(x1, y1, x2, y2, colors.black)
- drawCenteredText(x1, x2, y1 + 1, "CRAFTING", colors.white)
- drawBorder(x1, y1, x2, y2, colors.white)
- end
- -- Always update dynamic content
- if math.abs(pct - prevValues.crafting_pct) > 0.001 or forceRedraw then
- drawCenteredText(x1, x2, y1 + 3, "Busy: " .. busy .. "/" .. count, colors.white)
- if y1 + 5 <= y2 and x1 + 4 <= x2 then
- drawProgressBar(x1+2, y1+5, x2-2, pct, colors.gray, colors.green)
- end
- prevValues.crafting_pct = pct
- end
- end
- local function drawPower(x1, y1, x2, y2, forceRedraw)
- local success, usage = pcall(function() return meBridge.getAvgPowerUsage() end)
- if not success then usage = 0 end
- usage = usage or 0
- local success, stored = pcall(function() return meBridge.getEnergyStorage() end)
- if not success then stored = 0 end
- stored = stored or 0
- -- Only redraw static elements if forced or first time
- if forceRedraw or prevValues.power_usage < 0 then
- clearArea(x1, y1, x2, y2, colors.black)
- drawCenteredText(x1, x2, y1 + 1, "POWER", colors.white)
- drawBorder(x1, y1, x2, y2, colors.white)
- end
- -- Update dynamic content only when changed
- if math.abs(usage - prevValues.power_usage) > 1 or math.abs(stored - prevValues.power_stored) > 100 or forceRedraw then
- drawCenteredText(x1, x2, y1 + 3, "Avg: "..math.floor(usage).." RF/t", colors.white)
- drawCenteredText(x1, x2, y1 + 4, "Stored: "..math.floor(stored), colors.white)
- if stored < 10000 and prevValues.power_stored >= 10000 then
- logEvent("Low Power: "..stored)
- end
- prevValues.power_usage = usage
- prevValues.power_stored = stored
- end
- end
- local function drawWIP(x1, y1, x2, y2, forceRedraw)
- if forceRedraw then
- clearArea(x1, y1, x2, y2, colors.black)
- local midY = y1 + math.floor((y2 - y1) / 2)
- drawCenteredText(x1, x2, midY, "WIP", colors.gray)
- drawBorder(x1, y1, x2, y2, colors.white)
- end
- end
- local function drawAll(forceRedraw)
- if forceRedraw then
- monitor.clear()
- end
- for i = 1, 4 do
- local q = quads[i]
- if i == 1 then drawCrafting(q[1], q[2], q[3], q[4], forceRedraw)
- elseif i == 2 then drawStorage(q[1], q[2], q[3], q[4], forceRedraw)
- elseif i == 3 then drawWIP(q[1], q[2], q[3], q[4], forceRedraw)
- elseif i == 4 then drawPower(q[1], q[2], q[3], q[4], forceRedraw) end
- end
- drawClock()
- end
- local function drawExpanded(index, forceRedraw)
- -- Only clear when first expanding or explicitly forced
- if forceRedraw then
- monitor.clear()
- clearArea(1, 1, width, height, colors.black)
- -- Draw back button (only when clearing)
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- monitor.setCursorPos(math.max(1, width - 8), 1)
- monitor.write("[ BACK ]")
- end
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- monitor.setCursorPos(math.max(1, width - 8), 1)
- monitor.write("[ BACK ]")
- -- Draw expanded view (selective updates)
- if index == 1 then drawCrafting(1, 1, width, height, forceRedraw)
- elseif index == 2 then drawStorage(1, 1, width, height, forceRedraw)
- elseif index == 3 then drawWIP(1, 1, width, height, forceRedraw)
- elseif index == 4 then drawPower(1, 1, width, height, forceRedraw) end
- -- Draw histograph if there's space (always update as data changes)
- local histY1 = math.max(1, math.floor(height/2) - 6)
- local histY2 = math.min(height, math.floor(height/2) + 2)
- if histY1 < histY2 and width > 4 then
- drawHistograph(2, histY1, width - 2, histY2, histories[index], colors.lime)
- end
- -- Draw events (always update as they change)
- local eventsY = math.min(height - 1, math.floor(height/2) + 3)
- if eventsY > 0 and width > 4 then
- drawEvents(2, width - 2, eventsY)
- end
- drawClock()
- end
- local function handleClick(x, y)
- if expanded then
- if x >= width - 8 and y == 1 then
- expanded = nil
- -- Force redraw when going back to quad view
- monitor.clear()
- drawAll(true)
- end
- else
- for i, q in ipairs(quads) do
- if x >= q[1] and x <= q[3] and y >= q[2] and y <= q[4] then
- expanded = i
- drawExpanded(expanded, true) -- Force clear when first expanding
- return
- end
- end
- end
- end
- local function tick()
- while true do
- local t = os.clock()
- local needsUpdate = false
- -- Handle blinking
- if t - lastBlink >= 0.5 then
- blink = not blink
- lastBlink = t
- needsUpdate = true
- end
- -- Handle refresh
- if t - lastRefresh >= 1 then
- updateHistories()
- lastRefresh = t
- needsUpdate = true
- end
- -- Only redraw when needed
- if needsUpdate then
- if expanded then
- drawExpanded(expanded, false) -- Don't force clear in expanded view
- else
- drawAll(false) -- Don't force clear
- end
- end
- os.sleep(0.1)
- end
- end
- local function clickHandler()
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- handleClick(x, y)
- end
- end
- -- Initialize display
- monitor.clear()
- logEvent("System Started")
- -- Reset cache values to force initial draw
- prevValues.storage_pct = -1
- prevValues.crafting_pct = -1
- prevValues.power_usage = -1
- prevValues.power_stored = -1
- drawAll(true)
- -- Run the program
- parallel.waitForAny(tick, clickHandler)
Advertisement