Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DL_final_loop.lua
- -- Continuously reads FE and updates a Create Display Link until terminated.
- -- CONFIG
- local ACC_SIDE = "right"
- local DL_SIDE = "top"
- local INTERVAL = tonumber(... ) or 60 -- seconds between measurements
- --------------------------------------------------------------------------------
- -- Helpers
- --------------------------------------------------------------------------------
- local function safeCall(obj, method, ...)
- local fn = obj[method]
- if type(fn) ~= "function" then return nil end
- local ok, res = pcall(fn, obj, ...)
- return ok and res or nil
- end
- local function commaFormat(n)
- -- (optional, not used here but handy later)
- local s = tostring(math.floor(n))
- local pos = #s % 3
- if pos == 0 then pos = 3 end
- local parts = { s:sub(1, pos) }
- for i = pos+1, #s, 3 do
- parts[#parts+1] = "," .. s:sub(i, i+2)
- end
- return table.concat(parts)
- end
- local function fmtTime(s)
- if s < 0 then return "∞" end
- local h = math.floor(s/3600); s = s - h*3600
- local m = math.floor(s/60); s = s - m*60
- return string.format("%d:%02d:%02d", h, m, s)
- end
- --------------------------------------------------------------------------------
- -- Wrap peripherals
- --------------------------------------------------------------------------------
- local acc = peripheral.wrap(ACC_SIDE)
- if not acc then error("Accumulator not found on side “"..ACC_SIDE.."”") end
- local dl = peripheral.wrap(DL_SIDE)
- if not dl then error("Display Link not found on side “"..DL_SIDE.."”") end
- --------------------------------------------------------------------------------
- -- Read functions
- --------------------------------------------------------------------------------
- local function readEnergy()
- return safeCall(acc, "getEnergy")
- or safeCall(acc, "getEnergyStored")
- or safeCall(acc, "getCurrentEnergy")
- end
- local function readCapacity()
- return safeCall(acc, "getCapacity")
- or safeCall(acc, "getMaxEnergyStored")
- end
- local function readPercent()
- return safeCall(acc, "getPercent")
- end
- --------------------------------------------------------------------------------
- -- Main loop
- --------------------------------------------------------------------------------
- while true do
- -- 1) Take two readings
- local t1, e1 = os.clock(), readEnergy()
- if type(e1) ~= "number" then error("Failed to read initial energy") end
- os.sleep(INTERVAL)
- local t2, e2 = os.clock(), readEnergy()
- if type(e2) ~= "number" then error("Failed to read second energy") end
- -- 2) Compute rate
- local dt = t2 - t1
- local de = e2 - e1
- local rate = de / dt
- -- 3) Get capacity & percent
- local cap = readCapacity() or 0
- local pct = readPercent()
- if pct and pct > 1 then pct = pct / 100 end
- -- 4) Compute ETA (to full if rate>0, to empty if rate<0)
- local eta = -1
- if cap > 0 and rate ~= 0 then
- if rate > 0 then
- eta = (cap - e2) / rate
- else
- eta = -e2 / rate
- end
- end
- -- 5) Build display lines
- local lines = {
- string.format("FE: %d / %d", e2, cap),
- string.format("Rate: %.1f FE/s", rate),
- "ETA: "..fmtTime(eta),
- string.format("Pct: %.1f%%", (pct or (cap>0 and e2/cap or 0))*100),
- }
- -- 6) Write to Display Link
- dl.clear()
- for row, text in ipairs(lines) do
- dl.setCursorPos(1, row)
- dl.write(text)
- end
- dl.update()
- -- Loop continues until you hit Ctrl+T
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement