Advertisement
wolrah

AccuTest

Jul 2nd, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. -- Energy Tracking with Create Addition Display Link (Final, Commented)
  2. --
  3. -- This script measures the energy (FE) stored in a modular accumulator,
  4. -- calculates the rate of change over a specified interval, estimates
  5. -- time to full or empty, and outputs four lines to a Create Crafts &
  6. -- Additions Display Link attached to the "top" of the ComputerCraft computer.
  7.  
  8. -- Wraps a peripheral on a given side and ensures it implements required methods.
  9. local function wrapPeripheral(side, methods)
  10. -- Attempt to wrap the peripheral; error if not found.
  11. local obj = peripheral.wrap(side)
  12. if not obj then
  13. error("No peripheral found on '" .. side .. "'")
  14. end
  15. -- Verify each required method exists on the wrapped object.
  16. for _, m in ipairs(methods) do
  17. if type(obj[m]) ~= "function" then
  18. error("Peripheral on '" .. side .. "' missing method: " .. m)
  19. end
  20. end
  21. return obj
  22. end
  23.  
  24. -- Wrap the FE accumulator (right side) and the Display Link (top side).
  25. local acc = wrapPeripheral("right", {"getEnergy", "getCapacity", "getPercent"})
  26. local disp = wrapPeripheral("top", {"clear", "setLine", "print"})
  27.  
  28. -- Helper functions to read data from the accumulator.
  29. local function readEnergy()
  30. -- Returns current FE in the accumulator.
  31. return acc.getEnergy(acc)
  32. end
  33. local function readCapacity()
  34. -- Returns maximum FE capacity of the accumulator.
  35. return acc.getCapacity(acc)
  36. end
  37. local function readPercent()
  38. -- Returns current FE as a fraction or percentage.
  39. return acc.getPercent(acc)
  40. end
  41.  
  42. -- Determine measurement interval from command-line arg or default to 60 seconds.
  43. local interval = tonumber(arg and arg[1]) or 60
  44.  
  45. -- Take the first energy reading and timestamp.
  46. local e1 = readEnergy()
  47. local t1 = os.clock()
  48.  
  49. -- Sleep for the specified interval to measure change over time.
  50. os.sleep(interval)
  51.  
  52. -- Take the second energy reading and timestamp.
  53. local e2 = readEnergy()
  54. local t2 = os.clock()
  55.  
  56. -- Compute rate of energy change (FE per second).
  57. local dt = t2 - t1
  58. if dt == 0 then error("Zero time difference; cannot compute rate.") end
  59. local dE = e2 - e1
  60. local rate = dE / dt
  61.  
  62. -- Read capacity and compute estimated time to full/empty (in seconds).
  63. local capacity = readCapacity()
  64. local eta
  65. if rate > 0 then
  66. -- Filling: time until full
  67. eta = (capacity - e2) / rate
  68. else
  69. -- Draining: time until empty
  70. eta = e2 / -rate
  71. end
  72. eta = math.max(0, eta) -- Prevent negative ETA
  73.  
  74. -- Prepare the four display lines:
  75. -- 1) "FE: current/maximum"
  76. -- 2) "Rate: X.X FE/s"
  77. -- 3) "ETA: Y s"
  78. -- 4) "%: Z.Z%"
  79. local percent = readPercent()
  80. -- Normalize percent: if <=1, treat as fraction
  81. if percent <= 1 then percent = percent * 100 end
  82.  
  83. local line1 = string.format("FE: %d/%d", e2, capacity)
  84. local line2 = string.format("Rate: %.1f FE/s", rate)
  85. local line3 = string.format("ETA: %ds", math.floor(eta))
  86. local line4 = string.format("%%: %.1f%%", percent)
  87.  
  88. -- Output the lines to the Display Link.
  89. -- The Create Addition Display Link API requires setLine() then print().
  90. disp.clear()
  91. disp.setLine(1)
  92. disp.print(line1)
  93. disp.setLine(2)
  94. disp.print(line2)
  95. disp.setLine(3)
  96. disp.print(line3)
  97. disp.setLine(4)
  98. disp.print(line4)
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement