ksaw000

monitorTest

Apr 3rd, 2026 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local stressometer = peripheral.wrap("Create_Stressometer_0")
  3.  
  4. monitor.setTextScale(0.5)
  5.  
  6. local history = {}
  7. local maxPoints = 100
  8.  
  9. local COLOR_BG = colors.black
  10. local COLOR_STRESS = colors.red
  11. local COLOR_CAP = colors.green
  12.  
  13. local function addData(stress, capacity)
  14. table.insert(history, {stress = stress, capacity = capacity})
  15. if #history > maxPoints then
  16. table.remove(history, 1)
  17. end
  18. end
  19.  
  20. local function drawPixel(x, y, color)
  21. monitor.setCursorPos(x, y)
  22. monitor.setBackgroundColor(color)
  23. monitor.write(" ")
  24. end
  25.  
  26. local function drawLine(x1, y1, x2, y2, color)
  27. local dx = math.abs(x2 - x1)
  28. local dy = math.abs(y2 - y1)
  29. local sx = x1 < x2 and 1 or -1
  30. local sy = y1 < y2 and 1 or -1
  31. local err = dx - dy
  32.  
  33. while true do
  34. drawPixel(x1, y1, color)
  35. if x1 == x2 and y1 == y2 then break end
  36. local e2 = 2 * err
  37. if e2 > -dy then err = err - dy; x1 = x1 + sx end
  38. if e2 < dx then err = err + dx; y1 = y1 + sy end
  39. end
  40. end
  41.  
  42. local function drawGraph()
  43. local w, h = monitor.getSize()
  44. maxPoints = w
  45.  
  46. monitor.setBackgroundColor(COLOR_BG)
  47. monitor.clear()
  48.  
  49. if #history < 2 then return end
  50.  
  51. -- Find max value for scaling
  52. local maxVal = 1
  53. for _, v in ipairs(history) do
  54. if v.capacity > maxVal then maxVal = v.capacity end
  55. if v.stress > maxVal then maxVal = v.stress end
  56. end
  57.  
  58. local function scale(value)
  59. return h - math.floor((value / maxVal) * (h - 2))
  60. end
  61.  
  62. -- Draw lines
  63. for i = 2, #history do
  64. local x1 = i - 1
  65. local x2 = i
  66.  
  67. local y1_cap = scale(history[i - 1].capacity)
  68. local y2_cap = scale(history[i].capacity)
  69.  
  70. local y1_str = scale(history[i - 1].stress)
  71. local y2_str = scale(history[i].stress)
  72.  
  73. drawLine(x1, y1_cap, x2, y2_cap, COLOR_CAP)
  74.  
  75. drawLine(x1, y1_str, x2, y2_str, COLOR_STRESS)
  76. end
  77.  
  78. -- Labels
  79. monitor.setCursorPos(1, 1)
  80. monitor.setTextColor(colors.white)
  81. monitor.write("Stress (Red) vs Capacity (Green)")
  82.  
  83. local latest = history[#history]
  84. monitor.setCursorPos(1, 2)
  85. monitor.write("S: " .. math.floor(latest.stress))
  86.  
  87. monitor.setCursorPos(15, 2)
  88. monitor.write("C: " .. math.floor(latest.capacity))
  89. end
  90.  
  91. while true do
  92. local stress = stressometer.getStress()
  93. local capacity = stressometer.getStressCapacity()
  94.  
  95. addData(stress, capacity)
  96. drawGraph()
  97.  
  98. sleep(0.5)
  99. end
Advertisement
Add Comment
Please, Sign In to add comment