Advertisement
wolrah

DL_final.lua

Jul 3rd, 2025 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. -- DL_final_loop.lua
  2. -- Continuously reads FE and updates a Create Display Link until terminated.
  3.  
  4. -- CONFIG
  5. local ACC_SIDE = "right"
  6. local DL_SIDE = "top"
  7. local INTERVAL = tonumber(... ) or 60 -- seconds between measurements
  8.  
  9. --------------------------------------------------------------------------------
  10. -- Helpers
  11. --------------------------------------------------------------------------------
  12. local function safeCall(obj, method, ...)
  13. local fn = obj[method]
  14. if type(fn) ~= "function" then return nil end
  15. local ok, res = pcall(fn, obj, ...)
  16. return ok and res or nil
  17. end
  18.  
  19. local function commaFormat(n)
  20. -- (optional, not used here but handy later)
  21. local s = tostring(math.floor(n))
  22. local pos = #s % 3
  23. if pos == 0 then pos = 3 end
  24. local parts = { s:sub(1, pos) }
  25. for i = pos+1, #s, 3 do
  26. parts[#parts+1] = "," .. s:sub(i, i+2)
  27. end
  28. return table.concat(parts)
  29. end
  30.  
  31. local function fmtTime(s)
  32. if s < 0 then return "∞" end
  33. local h = math.floor(s/3600); s = s - h*3600
  34. local m = math.floor(s/60); s = s - m*60
  35. return string.format("%d:%02d:%02d", h, m, s)
  36. end
  37.  
  38. --------------------------------------------------------------------------------
  39. -- Wrap peripherals
  40. --------------------------------------------------------------------------------
  41. local acc = peripheral.wrap(ACC_SIDE)
  42. if not acc then error("Accumulator not found on side “"..ACC_SIDE.."”") end
  43. local dl = peripheral.wrap(DL_SIDE)
  44. if not dl then error("Display Link not found on side “"..DL_SIDE.."”") end
  45.  
  46. --------------------------------------------------------------------------------
  47. -- Read functions
  48. --------------------------------------------------------------------------------
  49. local function readEnergy()
  50. return safeCall(acc, "getEnergy")
  51. or safeCall(acc, "getEnergyStored")
  52. or safeCall(acc, "getCurrentEnergy")
  53. end
  54.  
  55. local function readCapacity()
  56. return safeCall(acc, "getCapacity")
  57. or safeCall(acc, "getMaxEnergyStored")
  58. end
  59.  
  60. local function readPercent()
  61. return safeCall(acc, "getPercent")
  62. end
  63.  
  64. --------------------------------------------------------------------------------
  65. -- Main loop
  66. --------------------------------------------------------------------------------
  67. while true do
  68. -- 1) Take two readings
  69. local t1, e1 = os.clock(), readEnergy()
  70. if type(e1) ~= "number" then error("Failed to read initial energy") end
  71.  
  72. os.sleep(INTERVAL)
  73.  
  74. local t2, e2 = os.clock(), readEnergy()
  75. if type(e2) ~= "number" then error("Failed to read second energy") end
  76.  
  77. -- 2) Compute rate
  78. local dt = t2 - t1
  79. local de = e2 - e1
  80. local rate = de / dt
  81.  
  82. -- 3) Get capacity & percent
  83. local cap = readCapacity() or 0
  84. local pct = readPercent()
  85. if pct and pct > 1 then pct = pct / 100 end
  86.  
  87. -- 4) Compute ETA (to full if rate>0, to empty if rate<0)
  88. local eta = -1
  89. if cap > 0 and rate ~= 0 then
  90. if rate > 0 then
  91. eta = (cap - e2) / rate
  92. else
  93. eta = -e2 / rate
  94. end
  95. end
  96.  
  97. -- 5) Build display lines
  98. local lines = {
  99. string.format("FE: %d / %d", e2, cap),
  100. string.format("Rate: %.1f FE/s", rate),
  101. "ETA: "..fmtTime(eta),
  102. string.format("Pct: %.1f%%", (pct or (cap>0 and e2/cap or 0))*100),
  103. }
  104.  
  105. -- 6) Write to Display Link
  106. dl.clear()
  107. for row, text in ipairs(lines) do
  108. dl.setCursorPos(1, row)
  109. dl.write(text)
  110. end
  111. dl.update()
  112.  
  113. -- Loop continues until you hit Ctrl+T
  114. end
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement