Skip to content

Commit

Permalink
- Merge class Unit and Node (closes #106)
Browse files Browse the repository at this point in the history
- Add a pause() function and change behavior of stop() (closes #92)
  • Loading branch information
sofian committed Jan 4, 2025
1 parent 0896b6c commit 7bf92b6
Show file tree
Hide file tree
Showing 25 changed files with 428 additions and 99 deletions.
92 changes: 92 additions & 0 deletions src/AbstractChronometer.cpp
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);
}
}

}
92 changes: 92 additions & 0 deletions src/AbstractChronometer.h
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
4 changes: 2 additions & 2 deletions src/AbstractTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

namespace pq {

AbstractTimer::AbstractTimer(float duration_) : Chronometer() {
AbstractTimer::AbstractTimer(float duration_) : AbstractChronometer() {
duration(duration_);
}

void AbstractTimer::start() {
Chronometer::start();
AbstractChronometer::start();
}

void AbstractTimer::start(float duration_) {
Expand Down
5 changes: 3 additions & 2 deletions src/AbstractTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

namespace pq {

class AbstractTimer : public Chronometer {
class AbstractTimer : public AbstractChronometer {
protected:
AbstractTimer(float duration);
virtual ~AbstractTimer() {}

public:
/// Starts/restarts the chronometer.
Expand All @@ -46,7 +47,7 @@ class AbstractTimer : public Chronometer {
/// The progress of the timer process (in %).
virtual float progress() const;

/// Returns true iff the chronometer has completed its process.
/// Returns true iff the chronometer has finished its process.
virtual bool isFinished() const { return progress() >= 1.0; }

/// @deprecated
Expand Down
4 changes: 4 additions & 0 deletions src/Alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ void Alarm::step() {
_setOn(isFinished());
}

float Alarm::clock() const {
return Plaquette.seconds();
}

}
3 changes: 3 additions & 0 deletions src/Alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Alarm : public DigitalSource, public AbstractTimer {

protected:
virtual void step();

// Returns current absolute time (in seconds).
virtual float clock() const;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/Metro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace pq {

Metro::Metro(float period_) : DigitalNode(), _phase(0), _onValue(0) {
Metro::Metro(float period_) : DigitalUnit(), _phase(0), _onValue(0) {
period(period_);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Metro.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace pq {
/**
* Chronometer digital unit which emits 1/true/"on" for one frame, at a regular pace.
*/
class Metro : public DigitalNode {
class Metro : public DigitalUnit {
public:
/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/PeakDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace pq {

PeakDetector::PeakDetector(float triggerThreshold_, uint8_t mode_)
: DigitalNode()
: DigitalUnit()
{
// Assign mode.
mode(mode_);
Expand Down
5 changes: 4 additions & 1 deletion src/PeakDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace pq {
/**
* Emits a signals when a signal peaks.
*/
class PeakDetector : public DigitalNode {
class PeakDetector : public DigitalUnit {
public:
/**
* Constructor. Possible modes are:
Expand Down Expand Up @@ -116,6 +116,9 @@ class PeakDetector : public DigitalNode {
bool _isHigh : 1;
bool _wasLow : 1;
bool _crossed : 1;

// Unused extra space.
uint8_t _data : 2;
};

}
Expand Down
Loading

0 comments on commit 7bf92b6

Please sign in to comment.