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
23
src/Timer.h
23
src/Timer.h
|
|
@ -17,37 +17,42 @@ private:
|
|||
bool paused;
|
||||
int pause_start_time;
|
||||
int total_paused_time;
|
||||
|
||||
|
||||
// One-shot timer support
|
||||
bool once;
|
||||
|
||||
// Stopped state: timer is not in engine map, but callback is preserved
|
||||
bool stopped;
|
||||
|
||||
public:
|
||||
uint64_t serial_number = 0; // For Python object cache
|
||||
|
||||
|
||||
Timer(); // for map to build
|
||||
Timer(PyObject* target, int interval, int now, bool once = false);
|
||||
Timer(PyObject* target, int interval, int now, bool once = false, bool start = true);
|
||||
~Timer();
|
||||
|
||||
|
||||
// Core timer functionality
|
||||
bool test(int now);
|
||||
bool hasElapsed(int now) const;
|
||||
|
||||
|
||||
// Timer control methods
|
||||
void pause(int current_time);
|
||||
void resume(int current_time);
|
||||
void restart(int current_time);
|
||||
void cancel();
|
||||
|
||||
void start(int current_time); // Clear stopped flag, reset progress
|
||||
void stop(); // Set stopped flag, preserve callback
|
||||
|
||||
// Timer state queries
|
||||
bool isPaused() const { return paused; }
|
||||
bool isActive() const;
|
||||
bool isStopped() const { return stopped; }
|
||||
bool isActive() const; // Running: not paused AND not stopped AND has callback
|
||||
int getInterval() const { return interval; }
|
||||
void setInterval(int new_interval) { interval = new_interval; }
|
||||
int getRemaining(int current_time) const;
|
||||
int getElapsed(int current_time) const;
|
||||
bool isOnce() const { return once; }
|
||||
void setOnce(bool value) { once = value; }
|
||||
|
||||
|
||||
// Callback management
|
||||
PyObject* getCallback();
|
||||
void setCallback(PyObject* new_callback);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue