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:
parent
fc95fc2844
commit
5d41292bf6
16 changed files with 440 additions and 262 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test once=True timer functionality
|
||||
Uses mcrfpy.step() to advance time in headless mode.
|
||||
"""
|
||||
import mcrfpy
|
||||
import sys
|
||||
|
|
@ -18,20 +19,8 @@ def repeat_callback(timer, runtime):
|
|||
repeat_count += 1
|
||||
print(f"Repeat timer fired! Count: {repeat_count}, Timer.once: {timer.once}")
|
||||
|
||||
def check_results(runtime):
|
||||
print(f"\nFinal results:")
|
||||
print(f"Once timer fired {once_count} times (expected: 1)")
|
||||
print(f"Repeat timer fired {repeat_count} times (expected: 3+)")
|
||||
|
||||
if once_count == 1 and repeat_count >= 3:
|
||||
print("PASS: Once timer fired exactly once, repeat timer fired multiple times")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("FAIL: Timer behavior incorrect")
|
||||
sys.exit(1)
|
||||
|
||||
# Set up the scene
|
||||
test_scene = mcrfpy.Scene("test_scene")
|
||||
test_scene = mcrfpy.Scene("test_scene")
|
||||
test_scene.activate()
|
||||
|
||||
# Create timers
|
||||
|
|
@ -43,5 +32,20 @@ print("\nCreating repeat timer with once=False (default)...")
|
|||
repeat_timer = mcrfpy.Timer("repeat_timer", repeat_callback, 100)
|
||||
print(f"Timer: {repeat_timer}, once={repeat_timer.once}")
|
||||
|
||||
# Check results after 500ms
|
||||
mcrfpy.setTimer("check", check_results, 500)
|
||||
# Advance time using step() to let timers fire
|
||||
# Step 600ms total - once timer (100ms) fires once, repeat timer fires ~6 times
|
||||
print("\nAdvancing time with step()...")
|
||||
for i in range(6):
|
||||
mcrfpy.step(0.1) # 100ms each
|
||||
|
||||
# Check results
|
||||
print(f"\nFinal results:")
|
||||
print(f"Once timer fired {once_count} times (expected: 1)")
|
||||
print(f"Repeat timer fired {repeat_count} times (expected: 3+)")
|
||||
|
||||
if once_count == 1 and repeat_count >= 3:
|
||||
print("PASS: Once timer fired exactly once, repeat timer fired multiple times")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("FAIL: Timer behavior incorrect")
|
||||
sys.exit(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue