April_The_Sergal

AE2 Display

Jul 21st, 2025 (edited)
719
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.54 KB | None | 0 0
  1. -- ME System Monitor for CC:Tweaked (Optimized Drawing)
  2.  
  3. local monitor = peripheral.find("monitor")
  4. local meBridge = peripheral.wrap("bottom")
  5.  
  6. if not monitor then
  7.     error("Monitor not found")
  8. end
  9. if not meBridge then
  10.     error("ME Bridge not found")
  11. end
  12.  
  13. monitor.setTextScale(1)
  14. local width, height = monitor.getSize()
  15.  
  16. -- Ensure width and height are valid numbers
  17. if not width or not height or width <= 0 or height <= 0 then
  18.     error("Invalid monitor dimensions: " .. tostring(width) .. "x" .. tostring(height))
  19. end
  20.  
  21. local expanded = nil
  22. local currentCPU = 1
  23. local blink = true
  24. local lastBlink = 0
  25. local lastRefresh = 0
  26. local lastClockTime = ""
  27.  
  28. -- Cache previous values to avoid unnecessary redraws
  29. local prevValues = {
  30.     storage_pct = -1,
  31.     crafting_pct = -1,
  32.     power_usage = -1,
  33.     power_stored = -1,
  34.     storage_warning = false
  35. }
  36.  
  37. -- Make sure math operations return valid numbers
  38. local halfWidth = math.max(1, math.floor(width/2))
  39. local halfHeight = math.max(1, math.floor(height/2))
  40.  
  41. local quads = {
  42.     {1, 1, halfWidth, halfHeight},
  43.     {halfWidth + 1, 1, width, halfHeight},
  44.     {1, halfHeight + 1, halfWidth, height},
  45.     {halfWidth + 1, halfHeight + 1, width, height}
  46. }
  47.  
  48. local histories = {{}, {}, {}, {}}
  49. local events = {}
  50. local MAX_HISTORY = math.max(10, width - 4)
  51. local MAX_EVENTS = 5
  52.  
  53. local function logEvent(msg)
  54.     table.insert(events, 1, os.date("%H:%M:%S") .. " - " .. msg)
  55.     if #events > MAX_EVENTS then
  56.         table.remove(events)
  57.     end
  58. end
  59.  
  60. local function clearArea(x1, y1, x2, y2, color)
  61.     -- Validate coordinates
  62.     x1, y1, x2, y2 = math.max(1, x1), math.max(1, y1), math.min(width, x2), math.min(height, y2)
  63.    
  64.     monitor.setBackgroundColor(color or colors.black)
  65.     for y = y1, y2 do
  66.         monitor.setCursorPos(x1, y)
  67.         monitor.write(string.rep(" ", math.max(0, x2 - x1 + 1)))
  68.     end
  69. end
  70.  
  71. local function drawCenteredText(x1, x2, y, text, textColor, bg)
  72.     -- Validate coordinates and text
  73.     if not text or y < 1 or y > height then return end
  74.    
  75.     monitor.setTextColor(textColor or colors.white)
  76.     monitor.setBackgroundColor(bg or colors.black)
  77.    
  78.     -- Clear the line first
  79.     local lineWidth = x2 - x1 + 1
  80.     monitor.setCursorPos(x1, y+1)
  81.     monitor.write(string.rep(" ", lineWidth))
  82.    
  83.     -- Then draw centered text
  84.     local cx = math.max(x1, math.floor((x1 + x2 - #text) / 2))
  85.     monitor.setCursorPos(cx, y)
  86.     monitor.write(string.sub(text, 1, lineWidth))
  87. end
  88.  
  89. local function drawBorder(x1, y1, x2, y2, color)
  90.     -- Validate coordinates
  91.     x1, y1, x2, y2 = math.max(1, x1), math.max(1, y1), math.min(width, x2), math.min(height, y2)
  92.    
  93.     monitor.setBackgroundColor(color)
  94.     for x = x1, x2 do
  95.         if y1 >= 1 and y1 <= height then
  96.             monitor.setCursorPos(x, y1)
  97.             monitor.write(" ")
  98.         end
  99.         if y2 >= 1 and y2 <= height and y2 ~= y1 then
  100.             monitor.setCursorPos(x, y2)
  101.             monitor.write(" ")
  102.         end
  103.     end
  104.     for y = y1 + 1, y2 - 1 do
  105.         if y >= 1 and y <= height then
  106.             monitor.setCursorPos(x1, y)
  107.             monitor.write(" ")
  108.             if x2 ~= x1 then
  109.                 monitor.setCursorPos(x2, y)
  110.                 monitor.write(" ")
  111.             end
  112.         end
  113.     end
  114. end
  115.  
  116. local function drawProgressBar(x1, y, x2, percent, bg, fg)
  117.     -- Validate inputs
  118.     if y < 1 or y > height or x1 > x2 then return end
  119.    
  120.     local barLen = math.max(0, x2 - x1 + 1)
  121.     local fill = math.max(0, math.min(barLen, math.floor(barLen * math.max(0, math.min(1, percent or 0)))))
  122.    
  123.     -- Always redraw the entire progress bar (consistent height and colors)
  124.     monitor.setCursorPos(x1, y)
  125.     monitor.setBackgroundColor(bg)
  126.     monitor.write(string.rep(" ", barLen))
  127.    
  128.     if fill > 0 then
  129.         monitor.setCursorPos(x1, y)
  130.         monitor.setBackgroundColor(fg)
  131.         monitor.write(string.rep(" ", fill))
  132.     end
  133. end
  134.  
  135. local function getColorByPercent(p)
  136.     p = p or 0
  137.     if p > 0.75 then return colors.green
  138.     elseif p > 0.5 then return colors.yellow
  139.     elseif p > 0.25 then return colors.orange
  140.     else return colors.red end
  141. end
  142.  
  143. local function drawClock()
  144.     local t = textutils.formatTime(os.time(), true)
  145.     if t ~= lastClockTime then
  146.         -- Clear only the clock area
  147.         monitor.setCursorPos(1, height)
  148.         monitor.setBackgroundColor(colors.black)
  149.         monitor.write(string.rep(" ", width))
  150.        
  151.         -- Draw new time
  152.         monitor.setTextColor(colors.lightGray)
  153.         monitor.setBackgroundColor(colors.black)
  154.         local cx = math.floor((width - #t) / 2)
  155.         monitor.setCursorPos(cx, height)
  156.         monitor.write(t)
  157.         lastClockTime = t
  158.     end
  159. end
  160.  
  161. local function updateHistories()
  162.     -- Safely get ME system data with error handling
  163.     local success, used = pcall(function() return meBridge.getUsedItemStorage() end)
  164.     if not success then used = 0 end
  165.    
  166.     local success, total = pcall(function() return meBridge.getTotalItemStorage() end)
  167.     if not success or not total or total <= 0 then total = 1 end
  168.    
  169.     table.insert(histories[2], used / total)
  170.  
  171.     local success, cpus = pcall(function() return meBridge.getCraftingCPUs() end)
  172.     if not success then cpus = {} end
  173.     cpus = cpus or {}
  174.    
  175.     local busy = 0
  176.     for _, c in ipairs(cpus) do
  177.         if c.busy then busy = busy + 1 end
  178.     end
  179.     table.insert(histories[1], #cpus > 0 and (busy / #cpus) or 0)
  180.  
  181.     local success, power = pcall(function() return meBridge.getEnergyStorage() end)
  182.     if not success then power = 0 end
  183.     power = power or 0
  184.    
  185.     table.insert(histories[4], power / 1000000)
  186.  
  187.     -- Trim histories
  188.     for i = 1, 4 do
  189.         if #histories[i] > MAX_HISTORY then
  190.             table.remove(histories[i], 1)
  191.         end
  192.     end
  193. end
  194.  
  195. local function drawHistograph(x1, y1, x2, y2, data, color)
  196.     -- Validate coordinates
  197.     if x1 > x2 or y1 > y2 or y1 < 1 or y2 > height then return end
  198.    
  199.     local w = x2 - x1 + 1
  200.     local h = y2 - y1
  201.    
  202.     if w <= 0 or h <= 0 then return end
  203.    
  204.     -- Always redraw the entire histograph area (consistent display)
  205.     for i = 1, math.min(w, #data) do
  206.         local val = math.max(0, math.min(1, data[#data - i + 1] or 0))
  207.         local heightFill = math.floor(h * val)
  208.        
  209.         for j = 0, h - 1 do
  210.             local plotY = y2 - j
  211.             local plotX = x2 - i + 1
  212.            
  213.             if plotY >= 1 and plotY <= height and plotX >= 1 and plotX <= width then
  214.                 monitor.setCursorPos(plotX, plotY)
  215.                 if j < heightFill then
  216.                     monitor.setBackgroundColor(color)
  217.                 else
  218.                     monitor.setBackgroundColor(colors.black)
  219.                 end
  220.                 monitor.write(" ")
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226. local function drawEvents(x1, x2, startY)
  227.     -- Always redraw events (they change frequently)
  228.     for i = 1, MAX_EVENTS do
  229.         local drawY = startY + i - 1
  230.         if drawY >= 1 and drawY <= height - 1 then
  231.             monitor.setCursorPos(x1, drawY)
  232.             monitor.setTextColor(colors.gray)
  233.             monitor.setBackgroundColor(colors.black)
  234.            
  235.             local event = events[i]
  236.             if event then
  237.                 monitor.write(string.sub(event, 1, math.max(0, x2 - x1 + 1)))
  238.             else
  239.                 monitor.write(string.rep(" ", math.max(0, x2 - x1 + 1)))
  240.             end
  241.         end
  242.     end
  243. end
  244.  
  245. local function drawStorage(x1, y1, x2, y2, forceRedraw)
  246.     local success, used = pcall(function() return meBridge.getUsedItemStorage() end)
  247.     if not success then used = 0 end
  248.    
  249.     local success, total = pcall(function() return meBridge.getTotalItemStorage() end)
  250.     if not success or not total or total <= 0 then total = 1 end
  251.    
  252.     local pct = used / total
  253.     local isWarning = pct > 0.75
  254.     local bg = isWarning and colors.red or colors.black
  255.  
  256.     -- Only redraw if values changed or forced
  257.     if forceRedraw or math.abs(pct - prevValues.storage_pct) > 0.001 or prevValues.storage_warning ~= isWarning then
  258.         clearArea(x1, y1, x2, y2, bg)
  259.         drawCenteredText(x1, x2, y1 + 1, "STORAGE", colors.white, bg)
  260.         drawBorder(x1, y1, x2, y2, colors.white)
  261.         prevValues.storage_pct = pct
  262.         prevValues.storage_warning = isWarning
  263.     end
  264.    
  265.     -- Always update percentage and progress bar
  266.     drawCenteredText(x1, x2, y1 + 3, (math.floor(pct * 1000)/10).."% Used", colors.white, bg)
  267.    
  268.     if y1 + 5 <= y2 and x1 + 4 <= x2 then
  269.         drawProgressBar(x1+2, y1+5, x2-2, pct, colors.blue, getColorByPercent(pct))
  270.     end
  271.  
  272.     -- Handle blinking warning (keep as is)
  273.     if isWarning and blink and y2 - 2 >= y1 then
  274.         drawCenteredText(x1, x2, y2-2, "!!WARNING!!", colors.white, colors.red)
  275.         if not prevValues.storage_warning then
  276.             logEvent("Low Free Storage")
  277.         end
  278.     elseif isWarning and not blink and y2 - 2 >= y1 then
  279.         -- Clear warning line when not blinking
  280.         monitor.setCursorPos(x1, y2-2)
  281.         monitor.setBackgroundColor(bg)
  282.         monitor.write(string.rep(" ", x2 - x1 + 1))
  283.     end
  284. end
  285.  
  286. local function drawCrafting(x1, y1, x2, y2, forceRedraw)
  287.     local success, cpus = pcall(function() return meBridge.getCraftingCPUs() end)
  288.     if not success then cpus = {} end
  289.     cpus = cpus or {}
  290.    
  291.     local count = #cpus
  292.     local busy = 0
  293.     for _, cpu in ipairs(cpus) do
  294.         if cpu.busy then busy = busy + 1 end
  295.     end
  296.     local pct = count > 0 and (busy / count) or 0
  297.  
  298.     -- Only redraw static elements if forced or first time
  299.     if forceRedraw or prevValues.crafting_pct < 0 then
  300.         clearArea(x1, y1, x2, y2, colors.black)
  301.         drawCenteredText(x1, x2, y1 + 1, "CRAFTING", colors.white)
  302.         drawBorder(x1, y1, x2, y2, colors.white)
  303.     end
  304.    
  305.     -- Always update dynamic content
  306.     if math.abs(pct - prevValues.crafting_pct) > 0.001 or forceRedraw then
  307.         drawCenteredText(x1, x2, y1 + 3, "Busy: " .. busy .. "/" .. count, colors.white)
  308.        
  309.         if y1 + 5 <= y2 and x1 + 4 <= x2 then
  310.             drawProgressBar(x1+2, y1+5, x2-2, pct, colors.gray, colors.green)
  311.         end
  312.         prevValues.crafting_pct = pct
  313.     end
  314. end
  315.  
  316. local function drawPower(x1, y1, x2, y2, forceRedraw)
  317.     local success, usage = pcall(function() return meBridge.getAvgPowerUsage() end)
  318.     if not success then usage = 0 end
  319.     usage = usage or 0
  320.    
  321.     local success, stored = pcall(function() return meBridge.getEnergyStorage() end)
  322.     if not success then stored = 0 end
  323.     stored = stored or 0
  324.  
  325.     -- Only redraw static elements if forced or first time
  326.     if forceRedraw or prevValues.power_usage < 0 then
  327.         clearArea(x1, y1, x2, y2, colors.black)
  328.         drawCenteredText(x1, x2, y1 + 1, "POWER", colors.white)
  329.         drawBorder(x1, y1, x2, y2, colors.white)
  330.     end
  331.    
  332.     -- Update dynamic content only when changed
  333.     if math.abs(usage - prevValues.power_usage) > 1 or math.abs(stored - prevValues.power_stored) > 100 or forceRedraw then
  334.         drawCenteredText(x1, x2, y1 + 3, "Avg: "..math.floor(usage).." RF/t", colors.white)
  335.         drawCenteredText(x1, x2, y1 + 4, "Stored: "..math.floor(stored), colors.white)
  336.        
  337.         if stored < 10000 and prevValues.power_stored >= 10000 then
  338.             logEvent("Low Power: "..stored)
  339.         end
  340.        
  341.         prevValues.power_usage = usage
  342.         prevValues.power_stored = stored
  343.     end
  344. end
  345.  
  346. local function drawWIP(x1, y1, x2, y2, forceRedraw)
  347.     if forceRedraw then
  348.         clearArea(x1, y1, x2, y2, colors.black)
  349.         local midY = y1 + math.floor((y2 - y1) / 2)
  350.         drawCenteredText(x1, x2, midY, "WIP", colors.gray)
  351.         drawBorder(x1, y1, x2, y2, colors.white)
  352.     end
  353. end
  354.  
  355. local function drawAll(forceRedraw)
  356.     if forceRedraw then
  357.         monitor.clear()
  358.     end
  359.  
  360.     for i = 1, 4 do
  361.         local q = quads[i]
  362.         if i == 1 then drawCrafting(q[1], q[2], q[3], q[4], forceRedraw)
  363.         elseif i == 2 then drawStorage(q[1], q[2], q[3], q[4], forceRedraw)
  364.         elseif i == 3 then drawWIP(q[1], q[2], q[3], q[4], forceRedraw)
  365.         elseif i == 4 then drawPower(q[1], q[2], q[3], q[4], forceRedraw) end
  366.     end
  367.     drawClock()
  368. end
  369.  
  370. local function drawExpanded(index, forceRedraw)
  371.     -- Only clear when first expanding or explicitly forced
  372.     if forceRedraw then
  373.         monitor.clear()
  374.         clearArea(1, 1, width, height, colors.black)
  375.        
  376.         -- Draw back button (only when clearing)
  377.         monitor.setBackgroundColor(colors.white)
  378.         monitor.setTextColor(colors.black)
  379.         monitor.setCursorPos(math.max(1, width - 8), 1)
  380.         monitor.write("[ BACK ]")
  381.     end
  382.  
  383.     monitor.setBackgroundColor(colors.white)
  384.     monitor.setTextColor(colors.black)
  385.     monitor.setCursorPos(math.max(1, width - 8), 1)
  386.     monitor.write("[ BACK ]")
  387.  
  388.     -- Draw expanded view (selective updates)
  389.     if index == 1 then drawCrafting(1, 1, width, height, forceRedraw)
  390.     elseif index == 2 then drawStorage(1, 1, width, height, forceRedraw)
  391.     elseif index == 3 then drawWIP(1, 1, width, height, forceRedraw)
  392.     elseif index == 4 then drawPower(1, 1, width, height, forceRedraw) end
  393.  
  394.     -- Draw histograph if there's space (always update as data changes)
  395.     local histY1 = math.max(1, math.floor(height/2) - 6)
  396.     local histY2 = math.min(height, math.floor(height/2) + 2)
  397.    
  398.     if histY1 < histY2 and width > 4 then
  399.         drawHistograph(2, histY1, width - 2, histY2, histories[index], colors.lime)
  400.     end
  401.    
  402.     -- Draw events (always update as they change)
  403.     local eventsY = math.min(height - 1, math.floor(height/2) + 3)
  404.     if eventsY > 0 and width > 4 then
  405.         drawEvents(2, width - 2, eventsY)
  406.     end
  407.    
  408.     drawClock()
  409. end
  410.  
  411. local function handleClick(x, y)
  412.     if expanded then
  413.         if x >= width - 8 and y == 1 then
  414.             expanded = nil
  415.             -- Force redraw when going back to quad view
  416.             monitor.clear()
  417.             drawAll(true)
  418.         end
  419.     else
  420.         for i, q in ipairs(quads) do
  421.             if x >= q[1] and x <= q[3] and y >= q[2] and y <= q[4] then
  422.                 expanded = i
  423.                 drawExpanded(expanded, true)  -- Force clear when first expanding
  424.                 return
  425.             end
  426.         end
  427.     end
  428. end
  429.  
  430. local function tick()
  431.     while true do
  432.         local t = os.clock()
  433.         local needsUpdate = false
  434.        
  435.         -- Handle blinking
  436.         if t - lastBlink >= 0.5 then
  437.             blink = not blink
  438.             lastBlink = t
  439.             needsUpdate = true
  440.         end
  441.  
  442.         -- Handle refresh
  443.         if t - lastRefresh >= 1 then
  444.             updateHistories()
  445.             lastRefresh = t
  446.             needsUpdate = true
  447.         end
  448.        
  449.         -- Only redraw when needed
  450.         if needsUpdate then
  451.             if expanded then
  452.                 drawExpanded(expanded, false) -- Don't force clear in expanded view
  453.             else
  454.                 drawAll(false) -- Don't force clear
  455.             end
  456.         end
  457.  
  458.         os.sleep(0.1)
  459.     end
  460. end
  461.  
  462. local function clickHandler()
  463.     while true do
  464.         local event, side, x, y = os.pullEvent("monitor_touch")
  465.         handleClick(x, y)
  466.     end
  467. end
  468.  
  469. -- Initialize display
  470. monitor.clear()
  471. logEvent("System Started")
  472.  
  473. -- Reset cache values to force initial draw
  474. prevValues.storage_pct = -1
  475. prevValues.crafting_pct = -1
  476. prevValues.power_usage = -1
  477. prevValues.power_stored = -1
  478.  
  479. drawAll(true)
  480.  
  481. -- Run the program
  482. parallel.waitForAny(tick, clickHandler)
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment