Timer refactor: stopwatch-like semantics, mcrfpy.timers collection closes #173

Major Timer API improvements:
- Add `stopped` flag to Timer C++ class for proper state management
- Add `start()` method to restart stopped timers (preserves callback)
- Add `stop()` method that removes from engine but preserves callback
- Make `active` property read-write (True=start/resume, False=pause)
- Add `start=True` init parameter to create timers in stopped state
- Add `mcrfpy.timers` module-level collection (tuple of active timers)
- One-shot timers now set stopped=true instead of clearing callback
- Remove deprecated `setTimer()` and `delTimer()` module functions

Timer callbacks now receive (timer, runtime) instead of just (runtime).
Updated all tests to use new Timer API and callback signature.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
John McCardle 2026-01-03 19:21:37 -05:00
commit 5d41292bf6
16 changed files with 440 additions and 262 deletions

View file

@ -63,13 +63,13 @@ def run_tests():
print("Test 5: Timer fires after step() advances past interval")
timer_fired = [False] # Use list for mutable closure
def on_timer(runtime):
"""Timer callback - receives runtime in ms"""
def on_timer(timer, runtime):
"""Timer callback - receives timer object and runtime in ms"""
timer_fired[0] = True
print(f" Timer fired at simulation time={runtime}ms")
# Set a timer for 500ms
mcrfpy.setTimer("test_timer", on_timer, 500)
test_timer = mcrfpy.Timer("test_timer", on_timer, 500)
# Step 600ms - timer should fire (500ms interval + some buffer)
dt = mcrfpy.step(0.6)
@ -88,7 +88,7 @@ def run_tests():
print(" Skipping timer test in windowed mode")
# Clean up
mcrfpy.delTimer("test_timer")
test_timer.stop()
print()
# Test 6: Error handling - invalid argument type