-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Merge class Unit and Node (closes #106)
- Add a pause() function and change behavior of stop() (closes #92)
- Loading branch information
Showing
25 changed files
with
428 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* AbstractChronometer.cpp | ||
* | ||
* (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com | ||
* (c) 2018 Thomas O Fredericks :: tof(@)t-o-f(.)info | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "AbstractChronometer.h" | ||
|
||
namespace pq { | ||
|
||
AbstractChronometer::AbstractChronometer() { | ||
stop(); | ||
} | ||
|
||
void AbstractChronometer::start() { | ||
// Start. | ||
_startTime = clock(); | ||
set(0); | ||
_isRunning = true; | ||
} | ||
|
||
|
||
void AbstractChronometer::pause() { | ||
if (_isRunning) { | ||
_offsetTime = elapsed(); // save current offset | ||
_isRunning = false; | ||
} | ||
} | ||
|
||
void AbstractChronometer::stop() { | ||
// Stop. | ||
_startTime = 0; | ||
set(0); | ||
_isRunning = false; | ||
} | ||
|
||
void AbstractChronometer::resume() { | ||
if (!_isRunning) { | ||
_startTime = clock(); | ||
_isRunning = true; | ||
} | ||
} | ||
|
||
bool AbstractChronometer::hasPassed(float timeout) const | ||
{ | ||
return (elapsed() >= timeout); | ||
} | ||
|
||
bool AbstractChronometer::hasPassed(float timeout, bool restartIfPassed) { | ||
if (hasPassed(timeout)) { | ||
if (restartIfPassed) | ||
start(); | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
|
||
void AbstractChronometer::set(float time) { | ||
_elapsedTime = _offsetTime = time; | ||
} | ||
|
||
void AbstractChronometer::addTime(float time) { | ||
_offsetTime += time; | ||
} | ||
|
||
void AbstractChronometer::update() { | ||
// Offset elapsed time. | ||
_elapsedTime = _offsetTime; | ||
|
||
if (_isRunning) { | ||
// Add difference to elapsed time. | ||
_elapsedTime += (clock() - _startTime); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* AbstractChronometer.h | ||
* | ||
* (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com | ||
* (c) 2018 Thomas O Fredericks :: tof(@)t-o-f(.)info | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef PQ_ABSTRACT_CHRONOMETER_H_ | ||
#define PQ_ABSTRACT_CHRONOMETER_H_ | ||
|
||
#include "PqCore.h" | ||
|
||
namespace pq { | ||
|
||
class AbstractChronometer { | ||
public: | ||
/// Constructor. | ||
AbstractChronometer(); | ||
virtual ~AbstractChronometer() {} | ||
|
||
/// Starts/restarts the chronometer. | ||
virtual void start(); | ||
|
||
/// Interrupts the chronometer. | ||
virtual void pause(); | ||
|
||
/// Interrupts the chronometer and resets to zero. | ||
virtual void stop(); | ||
|
||
/// Resumes process. | ||
virtual void resume(); | ||
|
||
/// The time currently elapsed by the chronometer (in seconds). | ||
virtual float elapsed() const { return _elapsedTime; } | ||
|
||
/// Returns true iff elapsed time has passed given timeout. | ||
virtual bool hasPassed(float timeout) const; | ||
|
||
/// Forces current time (in seconds). | ||
virtual void set(float time); | ||
|
||
/** | ||
* Returns true iff elapsed time has passed given timeout (optional argument to | ||
* automatically restart if true). | ||
*/ | ||
[[deprecated("Use hasPassed(float) followed by start() instead.")]] | ||
virtual bool hasPassed(float timeout, bool restartIfPassed); | ||
|
||
/// Adds/subtracts time to the chronometer. | ||
virtual void addTime(float time); | ||
|
||
/// Returns true iff the chronometer is currently running. | ||
bool isRunning() const { return _isRunning; } | ||
|
||
[[deprecated("Use isRunning() instead.")]] | ||
bool isStarted() const { return isRunning(); } | ||
|
||
protected: | ||
virtual void update(); | ||
|
||
// Returns current absolute time (in seconds). | ||
virtual float clock() const = 0; | ||
|
||
// The starting time (in seconds). | ||
float _startTime; | ||
|
||
// The offset time | ||
float _offsetTime; | ||
|
||
// The current elapsed time. | ||
float _elapsedTime; | ||
|
||
// Is the chrono currently started. | ||
bool _isRunning; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,8 @@ void Alarm::step() { | |
_setOn(isFinished()); | ||
} | ||
|
||
float Alarm::clock() const { | ||
return Plaquette.seconds(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.