Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor")
- local stressometer = peripheral.wrap("Create_Stressometer_0")
- monitor.setTextScale(0.5)
- local history = {}
- local maxPoints = 100
- local COLOR_BG = colors.black
- local COLOR_STRESS = colors.red
- local COLOR_CAP = colors.green
- local function addData(stress, capacity)
- table.insert(history, {stress = stress, capacity = capacity})
- if #history > maxPoints then
- table.remove(history, 1)
- end
- end
- local function drawPixel(x, y, color)
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(color)
- monitor.write(" ")
- end
- local function drawLine(x1, y1, x2, y2, color)
- local dx = math.abs(x2 - x1)
- local dy = math.abs(y2 - y1)
- local sx = x1 < x2 and 1 or -1
- local sy = y1 < y2 and 1 or -1
- local err = dx - dy
- while true do
- drawPixel(x1, y1, color)
- if x1 == x2 and y1 == y2 then break end
- local e2 = 2 * err
- if e2 > -dy then err = err - dy; x1 = x1 + sx end
- if e2 < dx then err = err + dx; y1 = y1 + sy end
- end
- end
- local function drawGraph()
- local w, h = monitor.getSize()
- maxPoints = w
- monitor.setBackgroundColor(COLOR_BG)
- monitor.clear()
- if #history < 2 then return end
- -- Find max value for scaling
- local maxVal = 1
- for _, v in ipairs(history) do
- if v.capacity > maxVal then maxVal = v.capacity end
- if v.stress > maxVal then maxVal = v.stress end
- end
- local function scale(value)
- return h - math.floor((value / maxVal) * (h - 2))
- end
- -- Draw lines
- for i = 2, #history do
- local x1 = i - 1
- local x2 = i
- local y1_cap = scale(history[i - 1].capacity)
- local y2_cap = scale(history[i].capacity)
- local y1_str = scale(history[i - 1].stress)
- local y2_str = scale(history[i].stress)
- drawLine(x1, y1_cap, x2, y2_cap, COLOR_CAP)
- drawLine(x1, y1_str, x2, y2_str, COLOR_STRESS)
- end
- -- Labels
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colors.white)
- monitor.write("Stress (Red) vs Capacity (Green)")
- local latest = history[#history]
- monitor.setCursorPos(1, 2)
- monitor.write("S: " .. math.floor(latest.stress))
- monitor.setCursorPos(15, 2)
- monitor.write("C: " .. math.floor(latest.capacity))
- end
- while true do
- local stress = stressometer.getStress()
- local capacity = stressometer.getStressCapacity()
- addData(stress, capacity)
- drawGraph()
- sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment