Chronometer

An analog unit that counts time in seconds. It can be started, stopped, paused, and resumed.

Example

Uses a chronometer to change the frequency a blinking LED. Restarts after 10 seconds.

#include <Plaquette.h>

Chronometer chrono;

DigitalOut led(13);

Wave osc(1.0); // a square oscillator

void begin() {
  chrono.start(); // start chrono
}

void step() {
  // Adjust oscillator's duty cycle according to current timer progress.
  osc.frequency(chrono);

  // Apply oscillator to LED state.
  osc >> led;

  // If the chronometer reaches 10 seconds: restart it.
  if (chrono >= 10.0)
  {
    // Restarts the chronometer.
    chrono.start();
  }
}

Reference

class Chronometer : public Unit, public AbstractChronometer

Public Functions

Chronometer(Engine &engine = Engine::primary())

Constructor.

inline virtual float get()

Returns elapsed time since start (in seconds).

virtual float put(float value)

Sets current time in seconds and returns it.

inline float seconds() const

Returns engine time in seconds.

inline uint32_t milliSeconds() const

Returns engine time in milliseconds.

inline uint64_t microSeconds() const

Returns engine time in microseconds.

inline unsigned long nSteps() const

Returns number of engine steps.

inline float sampleRate() const

Returns engine sample rate.

inline float samplePeriod() const

Returns enginesample period.

inline operator float()

Object can be used directly to access its value.

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

This function guarantees that the value is within [toLow, toHigh]. If the unit’s values are unbounded, returns get() constrained to [toLow, toHigh].

inline explicit operator bool()

Operator that allows usage in conditional expressions.

virtual void pause()

Interrupts the chronometer.

virtual void resume()

Resumes process.

inline virtual float elapsed() const

The time currently elapsed by the chronometer (in seconds).

virtual bool hasPassed(float timeout) const

Returns true iff elapsed time has passed given timeout.

virtual bool hasPassed(float timeout, bool restartIfPassed)

Deprecated:

Returns true iff elapsed time has passed given timeout (optional argument to automatically restart if true).

virtual void setTime(float time)

Forces current time (in seconds).

virtual void addTime(float time)

Adds/subtracts time to the chronometer.

inline virtual bool isRunning() const

Returns true iff the chronometer is currently running.

inline bool isStarted() const

Deprecated:

virtual void start()

Starts/restarts the chronometer.

virtual void stop()

Interrupts the chronometer and resets to zero.

virtual void togglePause()

Toggles pause/unpause.

Public Static Functions

static inline bool analogToDigital(float f)

Converts analog (float) value to digital (bool) value.

static inline float digitalToAnalog(bool b)

Converts digital (bool) value to analog (float) value.

See Also