-- Energy Tracking with Create Addition Display Link (Final, Commented)
--
-- This script measures the energy (FE) stored in a modular accumulator,
-- calculates the rate of change over a specified interval, estimates
-- time to full or empty, and outputs four lines to a Create Crafts &
-- Additions Display Link attached to the "top" of the ComputerCraft computer.
-- Wraps a peripheral on a given side and ensures it implements required methods.
local function wrapPeripheral(side, methods)
-- Attempt to wrap the peripheral; error if not found.
local obj = peripheral.wrap(side)
if not obj then
error("No peripheral found on '" .. side .. "'")
end
-- Verify each required method exists on the wrapped object.
for _, m in ipairs(methods) do
if type(obj[m]) ~= "function" then
error("Peripheral on '" .. side .. "' missing method: " .. m)
end
end
return obj
end
-- Wrap the FE accumulator (right side) and the Display Link (top side).
local acc = wrapPeripheral("right", {"getEnergy", "getCapacity", "getPercent"})
local disp = wrapPeripheral("top", {"clear", "setLine", "print"})
-- Helper functions to read data from the accumulator.
local function readEnergy()
-- Returns current FE in the accumulator.
return acc.getEnergy(acc)
end
local function readCapacity()
-- Returns maximum FE capacity of the accumulator.
return acc.getCapacity(acc)
end
local function readPercent()
-- Returns current FE as a fraction or percentage.
return acc.getPercent(acc)
end
-- Determine measurement interval from command-line arg or default to 60 seconds.
local interval = tonumber(arg and arg[1]) or 60
-- Take the first energy reading and timestamp.
local e1 = readEnergy()
local t1 = os.clock()
-- Sleep for the specified interval to measure change over time.
os.sleep(interval)
-- Take the second energy reading and timestamp.
local e2 = readEnergy()
local t2 = os.clock()
-- Compute rate of energy change (FE per second).
local dt = t2 - t1
if dt == 0 then error("Zero time difference; cannot compute rate.") end
local dE = e2 - e1
local rate = dE / dt
-- Read capacity and compute estimated time to full/empty (in seconds).
local capacity = readCapacity()
local eta
if rate > 0 then
-- Filling: time until full
eta = (capacity - e2) / rate
else
-- Draining: time until empty
eta = e2 / -rate
end
eta = math.max(0, eta) -- Prevent negative ETA
-- Prepare the four display lines:
-- 1) "FE: current/maximum"
-- 2) "Rate: X.X FE/s"
-- 3) "ETA: Y s"
-- 4) "%: Z.Z%"
local percent = readPercent()
-- Normalize percent: if <=1, treat as fraction
if percent <= 1 then percent = percent * 100 end
local line1 = string.format("FE: %d/%d", e2, capacity)
local line2 = string.format("Rate: %.1f FE/s", rate)
local line3 = string.format("ETA: %ds", math.floor(eta))
local line4 = string.format("%%: %.1f%%", percent)
-- Output the lines to the Display Link.
-- The Create Addition Display Link API requires setLine() then print().
disp.clear()
disp.setLine(1)
disp.print(line1)
disp.setLine(2)
disp.print(line2)
disp.setLine(3)
disp.print(line3)
disp.setLine(4)
disp.print(line4)
Comments