Alarm

An alarm clock digital source unit. Counts time and becomes “on” when time is up. The alarm can be started, stopped, and resumed.

When started, the alarm stays “off” until it reaches its timeout duration, after which it becomes “on”.

Example

Uses an alarm to activate built-in LED. Button is used to reset the alarm at random periods of time.

#include <Plaquette.h>

Alarm myAlarm(2.0); // an alarm with 2 seconds duration

DigitalOut led(13);

DigitalIn button(2, INTERNAL_PULLUP);

void begin() {
  myAlarm.start(); // start alarm
}

void step() {
  // Activate LED when alarm rings.
  myAlarm >> led; // the alarm will stay "on" until it is stopped or restarted

  // Reset alarm when button is pushed.
  if (myAlarm && button.rose())
  {
    // Restarts the alarm with a random duration between 1 and 5 seconds.
    myAlarm.duration(randomFloat(1.0, 5.0));
    myAlarm.start();
  }
}

Reference

class Alarm : public DigitalSource, public AbstractTimer

Chronometer class which becomes “on” after a given duration.

Public Functions

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

Constructor.

Parameters

engine – the engine running this unit

Alarm(float duration, Engine &engine = Engine::primary())

Constructor with duration.

Parameters
  • duration – duration of the alarm

  • engine – the engine running this unit

inline virtual bool finished()

Returns true iff the alarm just finished its process this step.

inline virtual void onFinish(EventCallback callback)

Registers event callback on finish event.

virtual void setTime(float time)

Set alarm at specific time.

inline virtual bool isOn()

Returns true iff the input is “on”.

inline virtual bool putOn(bool value)

Pushes value into the unit.

Parameters

value – the value sent to the unit

Returns

the new value of the unit

inline virtual bool rose()

Returns true if the value rose.

inline virtual bool fell()

Returns true if the value fell.

inline virtual bool changed()

Returns true if the value changed.

inline virtual bool toggle()

Switches between on and off.

inline virtual int8_t changeState()

Difference between current and previous value of the unit.

inline virtual void onRise(EventCallback callback)

Registers event callback on rise event.

inline virtual void onFall(EventCallback callback)

Registers event callback on fall event.

inline virtual void onChange(EventCallback callback)

Registers event callback on change event.

inline virtual bool isOff()

Returns true iff the input is “off”.

inline virtual int getInt()

Returns value as integer (0 or 1).

inline virtual float get()

Returns value as float (either 0.0 or 1.0).

inline virtual bool on()

Sets output to “on” (ie. true, 1).

inline virtual bool off()

Sets output to “off” (ie. false, 0).

inline virtual float put(float value)

Pushes value into the unit.

Parameters

value – the value sent to the unit

Returns

the new value of the unit

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

inline operator bool()

Operator that allows usage in conditional expressions.

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.

virtual void start()

Starts/restarts the chronometer.

virtual void start(float duration)

Starts/restarts the chronometer with specific duration.

virtual void duration(float duration)

Sets the duration of the chronometer.

inline virtual float duration() const

Returns duration.

virtual float progress() const

The progress of the timer process (in %).

inline virtual bool isFinished() const

Returns true iff the chronometer has finished its process.

inline virtual bool isComplete() const

Deprecated:

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 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 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