TriangleWave
A source unit that can generate a range of triangle-shaped signals such as the
triangle wave and the
sawtooth wave. The signal can be
adjusted by changing the period
and/or frequency
of the oscillation.
The width
parameter represents the “turning point” during the period at which
the signals reaches its maximum and starts going down again. Changing the width
allows to generate different kinds of triangular-shaped waves. For example, by
setting width to 1.0 (100%) one obtains a sawtooth wave; by setting it to 0.0 (0%)
an inverted sawtooth is created; anything in between generates different flavors
of triangle waves.
Example
Controls a set of traffic lights that go: red, yellow, green, red, yellow, green, and so on. It uses a sawtooth to iterate through these three states.
#include <Plaquette.h>
DigitalOut green(10);
DigitalOut yellow(11);
DigitalOut red(12);
TriangleWave osc(10.0);
void begin() {
osc.width(1.0); // sawtooth wave
}
void step() {
// Shut down all lights.
0 >> led >> yellow >> green;
// Switch appropriate LED.
if (osc < 0.4)
green.on();
else if (osc < 0.6)
yellow.on();
else
red.on();
}
-
class TriangleWave : public AbstractWave
Triangle/sawtooth oscillator.
Public Functions
-
virtual void period(float period)
Sets the period (in seconds).
- Parameters:
period – the period of oscillation (in seconds)
-
virtual void frequency(float frequency)
Sets the frequency (in Hz).
- Parameters:
frequency – the frequency of oscillation (in Hz)
-
inline virtual float frequency() const
Returns the frequency (in Hz).
-
virtual void bpm(float bpm)
Sets the frequency in beats-per-minute.
- Parameters:
bpm – the frequency of oscillation (in BPM)
-
inline virtual float bpm() const
Returns the frequency (in BPM).
-
virtual void width(float width)
Sets the width of the signal as a % of period.
- Parameters:
width – the width as a value in [0, 1]
-
inline virtual float width() const
Returns the width of the signal.
-
virtual void amplitude(float amplitude)
Sets the amplitude of the wave.
- Parameters:
amplitude – a value in [0, 1] that determines the amplitude of the wave (centered at 0.5).
-
inline virtual float amplitude() const
Returns the amplitude of the wave.
-
virtual void phase(float phase)
Sets the phase (ie.
the offset, in % of period).
- Parameters:
phase – the phase (in % of period)
-
inline virtual float phase() const
Returns the phase (in % of period).
-
virtual float shiftBy(float phaseShift)
Returns oscillator’s value with given phase shift (in %).
Supports negative phase shifts. Eg. shiftBy(0.2) returns future value of oscillator after 20% of its period would have passed.
- Parameters:
phase – the phase shift (in % of period)
- Returns:
the value of oscillator with given phase shift
-
inline virtual float get()
Returns value in [0, 1].
-
inline virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
-
virtual void period(float period)