FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
|
Simple API to generate a square wave to an output pin. More...
#include <fastarduino/square_wave.h>
Public Types | |
using | CALC = timer::Calculator< NTIMER > |
The timer::Calculator type for the underlying timer. More... | |
using | TIMER = timer::Timer< NTIMER > |
The type of underlying timer::Timer used by this SquareWave generator. More... | |
using | PWMPIN = analog::PWMOutput< OUTPUT > |
The analog::PWMOutput type for the OUTPUT board::PWMPin. More... | |
Public Member Functions | |
SquareWave (const SquareWave &)=delete | |
SquareWave & | operator= (const SquareWave &)=delete |
SquareWave () | |
Instantiate a SquareWave generator. More... | |
const TIMER & | timer () const |
Return the underlying timer::Timer of this SquareWave generator. More... | |
TIMER & | timer () |
Return the underlying timer::Timer of this SquareWave generator. More... | |
void | start_frequency (uint32_t frequency) |
Start producing, on the ouput pin, a square wave with the specified frequency. More... | |
void | start_frequency (typename TIMER::PRESCALER prescaler, typename TIMER::TYPE counter) |
Start producing, on the output pin, a square wave, which frequency matches the specified prescaler and counter arguments. More... | |
void | stop () |
Stop square wave generation. More... | |
Static Public Attributes | |
static constexpr const board::Timer | NTIMER = NTIMER_ |
The AVR timer used for the underlying Timer. More... | |
static constexpr const board::PWMPin | OUTPUT = OUTPUT_ |
The board::PWMPin connected to this SquareWave generator. More... | |
static constexpr const board::DigitalPin | PIN = board_traits::PWMPin_trait<OUTPUT>::ACTUAL_PIN |
The pin to which this SquareWave generator is connected. More... | |
Simple API to generate a square wave to an output pin.
This uses a Timer and the Ouput pin OCxA of that Timer.
This class can be useful in many situations where you need to generate a square wave of a given frequency (or several frequencies) on an output pin. For example, it can be used to produce "music" with a buzzer. This is what devices::audio::ToneGenerator
and devices::audio::TonePlayer
do.
NTIMER_ | the AVR timer to use for the underlying Timer |
OUTPUT_ | the board::PWMPin connected to this SquareWave generator; this must be the pin OCnA, where n is the AVR Timer number |
Definition at line 46 of file square_wave.h.
using timer::SquareWave< NTIMER_, OUTPUT_ >::CALC = timer::Calculator<NTIMER> |
The timer::Calculator type for the underlying timer.
Definition at line 60 of file square_wave.h.
using timer::SquareWave< NTIMER_, OUTPUT_ >::TIMER = timer::Timer<NTIMER> |
The type of underlying timer::Timer used by this SquareWave generator.
Definition at line 62 of file square_wave.h.
using timer::SquareWave< NTIMER_, OUTPUT_ >::PWMPIN = analog::PWMOutput<OUTPUT> |
The analog::PWMOutput type for the OUTPUT
board::PWMPin.
Definition at line 64 of file square_wave.h.
|
inline |
Instantiate a SquareWave generator.
This will create the underlying Timer with the proper arguments.
Definition at line 70 of file square_wave.h.
|
inline |
Return the underlying timer::Timer of this SquareWave generator.
Definition at line 79 of file square_wave.h.
|
inline |
Return the underlying timer::Timer of this SquareWave generator.
Definition at line 87 of file square_wave.h.
|
inline |
Start producing, on the ouput pin, a square wave with the specified frequency.
This method performs heavy calculation to set the proper attributes for the underlying Timer. If possible, you should prefer the other start_frequency() that directly takes a prescaler and a counter value (those can be pre-calculated at compile-time).
frequency | the frequency, in Hz, of the square wave t generate |
Definition at line 105 of file square_wave.h.
|
inline |
Start producing, on the output pin, a square wave, which frequency matches the specified prescaler
and counter
arguments.
This method performs no heavy calculation and should be preferred over start_frequency(uint32_t)
if the used frequency is known at compile-time.
prescaler | the prescaler to use on the underlying Timer, in order to produce the desired frequency. It can be calculated by CALC::CTC_prescaler() |
counter | the counter value to use on the underlying Timer, in order to produce the desired frequency. It can be calculated by CALC::CTC_counter() |
Definition at line 129 of file square_wave.h.
|
inline |
Stop square wave generation.
Definition at line 140 of file square_wave.h.
|
staticconstexpr |
The AVR timer used for the underlying Timer.
Definition at line 53 of file square_wave.h.
|
staticconstexpr |
The board::PWMPin connected to this SquareWave generator.
Definition at line 55 of file square_wave.h.
|
staticconstexpr |
The pin to which this SquareWave generator is connected.
Definition at line 57 of file square_wave.h.