Skip to content

Commit

Permalink
Adjusted API of Chronometer (closes #92).
Browse files Browse the repository at this point in the history
  • Loading branch information
sofian committed Jan 4, 2025
1 parent 06db231 commit b686963
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/Alarm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ alarm at random periods of time.

.. doxygenclass:: Alarm
:project: Plaquette
:members: Alarm, isOn, isOff, getInt, get, start, stop, resume, elapsed, progress, isStarted
:members: Alarm, isOn, isOff, getInt, get, start, stop, resume, elapsed, progress, isRunning

|SeeAlso|
---------
Expand Down
2 changes: 1 addition & 1 deletion docs/MinMaxScaler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ automatically adapt to incoming sensor values.

.. doxygenclass:: MinMaxScaler
:project: Plaquette
:members: MinMaxScaler, get, put, timeWindow, infiniteTimeWindow, timeWindowIsInfinite, reset, start, stop, isStarted
:members: MinMaxScaler, get, put, timeWindow, infiniteTimeWindow, timeWindowIsInfinite, reset, start, stop, isRunning

|SeeAlso|
---------
Expand Down
2 changes: 1 addition & 1 deletion docs/Normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Uses a normalizer to analyze input sensor values and detect extreme values.

.. doxygenclass:: Normalizer
:project: Plaquette
:members: Normalizer, get, put, timeWindow, infiniteTimeWindow, timeWindowIsInfinite, reset, start, stop, isStarted, targetMean, targetStdDev, isHighOutlier, isLowOutlier, isOutlier, highOutlierThreshold, lowOutlierThreshold, clamp, noClamp, isClamped
:members: Normalizer, get, put, timeWindow, infiniteTimeWindow, timeWindowIsInfinite, reset, start, stop, isRunning, targetMean, targetStdDev, isHighOutlier, isLowOutlier, isOutlier, highOutlierThreshold, lowOutlierThreshold, clamp, noClamp, isClamped

|SeeAlso|
---------
Expand Down
2 changes: 1 addition & 1 deletion docs/Ramp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Sequentially ramps through different values.

.. doxygenclass:: Ramp
:project: Plaquette
:members: Ramp, get, start, stop, resume, elapsed, progress, isStarted, isFinished, easing, noEasing, to, fromTo
:members: Ramp, get, start, stop, resume, elapsed, progress, isRunning, isFinished, easing, noEasing, to, fromTo

|SeeAlso|
---------
Expand Down
2 changes: 1 addition & 1 deletion examples/06.Filters/RescaleSignal/RescaleSignal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void begin() {}
void step() {
// After 30 seconds, stop calibration. The scaler will still rescale
// values but will stop updating its min. and max. values.
if (scaler.isStarted() && seconds() >= 30.0f)
if (scaler.isRunning() && seconds() >= 30.0f)
scaler.stop(); // stop calibration

// Analog input is rescaled then sent as LED value.
Expand Down
48 changes: 27 additions & 21 deletions src/Chronometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,35 @@
namespace pq {

Chronometer::Chronometer() : Unit() {
_begin();
stop();
}

void Chronometer::start() {
// Start.
_startTime = seconds();
_offsetTime = _elapsedTime = 0;
_isStarted = true;
_startTime = clock();
set(0);
_isRunning = true;
}

void Chronometer::addTime(float time) {
_offsetTime += time;

void Chronometer::pause() {
if (_isRunning) {
_offsetTime = elapsed(); // save current offset
_isRunning = false;
}
}

void Chronometer::stop() {
if (_isStarted) {
_offsetTime = elapsed();
_isStarted = false;
}
// Stop.
_startTime = 0;
set(0);
_isRunning = false;
}

void Chronometer::resume() {
if (!_isStarted) {
_startTime = seconds();
_isStarted = true;
if (!_isRunning) {
_startTime = clock();
_isRunning = true;
}
}

Expand All @@ -68,6 +72,14 @@ bool Chronometer::hasPassed(float timeout, bool restartIfPassed) {
}
}

void Chronometer::set(float time) {
_elapsedTime = _offsetTime = time;
}

void Chronometer::addTime(float time) {
_offsetTime += time;
}

void Chronometer::begin() {
_begin();
}
Expand All @@ -76,16 +88,10 @@ void Chronometer::step() {
// Offset elapsed time.
_elapsedTime = _offsetTime;

if (_isStarted) {
if (_isRunning) {
// Add difference to elapsed time.
_elapsedTime += (seconds() - _startTime);
_elapsedTime += (clock() - _startTime);
}
}

void Chronometer::_begin() {
// Basic reset.
_startTime = _offsetTime = _elapsedTime = 0;
_isStarted = false;
}

}
24 changes: 17 additions & 7 deletions src/Chronometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class Chronometer : public Unit {
/// Starts/restarts the chronometer.
virtual void start();

/// Adds/subtracts time to the chronometer.
virtual void addTime(float time);

/// Interrupts the chronometer.
virtual void pause();

/// Interrupts the chronometer and resets to zero.
virtual void stop();

/// Resumes process.
Expand All @@ -48,21 +48,31 @@ class Chronometer : public Unit {
/// 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 isStarted() const { return _isStarted; }
bool isRunning() const { return _isRunning; }

[[deprecated("Use isRunning() instead.")]]
bool isStarted() const { return isRunning(); }

protected:
virtual void begin();
virtual void step();

void _begin();

virtual float clock() const = 0;
// The starting time (in seconds).
float _startTime;

Expand All @@ -73,7 +83,7 @@ class Chronometer : public Unit {
float _elapsedTime;

// Is the chrono currently started.
bool _isStarted;
bool _isRunning;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/MinMaxScaler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void MinMaxScaler::reset() {

float MinMaxScaler::put(float value)
{
if (isStarted()) {
if (isRunning()) {

// Update min. value.
if (value < _minValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/MinMaxScaler.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MinMaxScaler : public MovingFilter {
virtual void reset();

/**
* Pushes value into the unit. If isStarted() is false the filter will not be
* Pushes value into the unit. If isRunning() is false the filter will not be
* updated but will just return the filtered value.
* @param value the value sent to the unit
* @return the new value of the unit
Expand Down
2 changes: 1 addition & 1 deletion src/MovingFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void MovingFilter::stop() {
_isAdaptive = false;
}

bool MovingFilter::isStarted() const {
bool MovingFilter::isRunning() const {
return _isAdaptive;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MovingFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MovingFilter : public AnalogSource {
virtual void stop();

/// Returns true iff the statistics have already been started.
virtual bool isStarted() const;
virtual bool isRunning() const;

protected:
// Start/stop calibration flag.
Expand Down
2 changes: 1 addition & 1 deletion src/Normalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ float Normalizer::put(float value) {
_nValuesStep++;

// First time put() is called this step.
if (isStarted()) {
if (isRunning()) {

if (_nValuesStep == 1) {
// Update moving average.
Expand Down
2 changes: 1 addition & 1 deletion src/Normalizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Normalizer : public MovingFilter, public MovingStats {
virtual void reset();

/**
* Pushes value into the unit. If isStarted() is false the filter will not be
* Pushes value into the unit. If isRunning() is false the filter will not be
* updated but will just return the filtered value.
* @param value the value sent to the unit
* @return the new value of the unit
Expand Down
2 changes: 1 addition & 1 deletion src/Ramp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void Ramp::go(float to, easing_function easing_) {
void Ramp::step() {
AbstractTimer::step();

if (_isStarted) {
if (_isRunning) {
// Compute value if running -- otherwise leave as is.
_value = _get();
}
Expand Down

0 comments on commit b686963

Please sign in to comment.