Ramp

A source unit that generates a smooth transition between two values. The unit can be triggered to start transitioning to a target value for a certain duration.

There are two ways to start the ramp.

By calling go(from, to, duration) the ramp will transition from value from to value to in duration seconds.

Alternatively, calling go(to, duration) will start a transition from the ramp’s current value to to in duration seconds.

The following diagram shows what happens to the ramp signal if go(5.0, 1.0, 2.0) is called, followed later by go(3.0, 1.0):

_images/Plaquette-Ramp.png

Important

Ramps also support the use of easing functions in order to create different kinds of expressive effects with signals. An easing function can optionally be specified at the end of a go() command or by calling the easing() function.

Please refer to this page for a full list of available easing functions.

Example

Sequentially ramps through different values.

#include <Plaquette.h>

Ramp myRamp(3.0); // initial duration: 3 seconds

StreamOut serialOut(Serial);

void begin() {
  // Apply an easing function (optional).
  myRamp.easing(easeOutSine);
  // Launch ramp: ramp from -10 to 10.
  myRamp.go(-10, 10);
}

void step() {
  if (myRamp.isFinished())
  {
    // Launch ramp from current value to half, increasing duration by one second.
    myRamp.go(myRamp / 2, myRamp.duration() + 1);
  }

  myRamp >> serialOut;
}

Reference

class Ramp : public Unit, public AbstractTimer

Provides a ramping / tweening mechanism that allows smooth transitions between two values.

Public Functions

Ramp(float duration = 1.0f)

Constructor with duration.

Parameters:

duration – duration of the ramp

inline virtual float get()

Returns value of ramp.

void easing(easing_function easing)

Sets easing function to apply to ramp.

Parameters:

easing – the easing function

inline void noEasing()

Remove easing function (linear/no easing).

virtual void to(float to)

Assign final value of the ramp starting from current value.

Parameters:

to – the final value

virtual void from(float from)

Assign initial value of the ramp.

Parameters:

from – the initial value

virtual void fromTo(float from, float to)

Assign initial and final values of the ramp.

Parameters:
  • from – the initial value

  • to – the final value

virtual void duration(float duration)

Sets the duration of the chronometer.

inline virtual float duration() const

Returns duration.

virtual void speed(float speed)

Sets the speed (rate of change) of the ramp in change-per-second.

virtual float speed() const

Returns speed (rate of change) of the ramp in change-per-second.

virtual void start()

Starts/restarts the ramp. Will repeat the last ramp.

virtual void go(float from, float to, float durationOrSpeed, easing_function easing = 0)

Starts a new ramp.

Parameters:
  • from – the initial value

  • to – the final value

  • durationOrSpeed – the duration of the ramp (in seconds) or speed (in change-per-second) depending on mode

  • easing – the easing function (optional).

virtual void go(float to, float durationOrSpeed, easing_function easing = 0)

Starts a new ramp, starting from current value.

Parameters:
  • to – the final value

  • durationOrSpeed – the duration of the ramp (in seconds) or speed (in change-per-second) depending on mode

  • easing – the easing function (optional)

virtual void go(float to, easing_function easing = 0)

Starts a new ramp, starting from current value (keeping the same duration/speed).

Parameters:
  • to – the final value

  • easing – the easing function (optional)

virtual void mode(uint8_t mode)

Changes the mode of the component (RAMP_DURATION or RAMP_SPEED).

inline uint8_t mode() const

Returns the mode of the component (RAMP_DURATION or RAMP_SPEED).

inline virtual bool finished()

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

inline virtual void onFinish(EventCallback callback)

Registers event callback on finish event.

virtual void set(float time)

Forces current time (in seconds).

virtual void start(float duration)

Starts/restarts the chronometer with specific 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.

virtual void stop()

Interrupts the chronometer and resets to zero.

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 void add(float time)

Adds/subtracts time to the chronometer.

inline bool isRunning() const

Returns true iff the chronometer is currently running.

See Also