FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
timer::SquareWave< NTIMER_, OUTPUT_ > Class Template Reference

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
 
SquareWaveoperator= (const SquareWave &)=delete
 
 SquareWave ()
 Instantiate a SquareWave generator. More...
 
const TIMERtimer () const
 Return the underlying timer::Timer of this SquareWave generator. More...
 
TIMERtimer ()
 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...
 

Detailed Description

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
class timer::SquareWave< NTIMER_, OUTPUT_ >

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.

Template Parameters
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
See also
devices::audio::ToneGenerator
devices::audio::TonePlayer

Definition at line 46 of file square_wave.h.

Member Typedef Documentation

◆ CALC

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
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.

◆ TIMER

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
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.

◆ PWMPIN

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
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.

Constructor & Destructor Documentation

◆ SquareWave()

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
timer::SquareWave< NTIMER_, OUTPUT_ >::SquareWave ( )
inline

Instantiate a SquareWave generator.

This will create the underlying Timer with the proper arguments.

Definition at line 70 of file square_wave.h.

Member Function Documentation

◆ timer() [1/2]

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
const TIMER & timer::SquareWave< NTIMER_, OUTPUT_ >::timer ( ) const
inline

Return the underlying timer::Timer of this SquareWave generator.

Definition at line 79 of file square_wave.h.

◆ timer() [2/2]

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
TIMER & timer::SquareWave< NTIMER_, OUTPUT_ >::timer ( )
inline

Return the underlying timer::Timer of this SquareWave generator.

Definition at line 87 of file square_wave.h.

◆ start_frequency() [1/2]

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
void timer::SquareWave< NTIMER_, OUTPUT_ >::start_frequency ( uint32_t  frequency)
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).

Parameters
frequencythe frequency, in Hz, of the square wave t generate
See also
start_frequency(typename TIMER::PRESCALER, typename TIMER::TYPE)

Definition at line 105 of file square_wave.h.

◆ start_frequency() [2/2]

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
void timer::SquareWave< NTIMER_, OUTPUT_ >::start_frequency ( typename TIMER::PRESCALER  prescaler,
typename TIMER::TYPE  counter 
)
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.

Parameters
prescalerthe prescaler to use on the underlying Timer, in order to produce the desired frequency. It can be calculated by CALC::CTC_prescaler()
counterthe counter value to use on the underlying Timer, in order to produce the desired frequency. It can be calculated by CALC::CTC_counter()
See also
start_frequency(uint32_t)
timer::Calculator<NTIMER_>::CTC_prescaler()
timer::Calculator<NTIMER_>::CTC_counter()

Definition at line 129 of file square_wave.h.

◆ stop()

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
void timer::SquareWave< NTIMER_, OUTPUT_ >::stop ( )
inline

Stop square wave generation.

Definition at line 140 of file square_wave.h.

Member Data Documentation

◆ NTIMER

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
constexpr const board::Timer timer::SquareWave< NTIMER_, OUTPUT_ >::NTIMER = NTIMER_
staticconstexpr

The AVR timer used for the underlying Timer.

Definition at line 53 of file square_wave.h.

◆ OUTPUT

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
constexpr const board::PWMPin timer::SquareWave< NTIMER_, OUTPUT_ >::OUTPUT = OUTPUT_
staticconstexpr

The board::PWMPin connected to this SquareWave generator.

Definition at line 55 of file square_wave.h.

◆ PIN

template<board::Timer NTIMER_, board::PWMPin OUTPUT_>
constexpr const board::DigitalPin timer::SquareWave< NTIMER_, OUTPUT_ >::PIN = board_traits::PWMPin_trait<OUTPUT>::ACTUAL_PIN
staticconstexpr

The pin to which this SquareWave generator is connected.

Definition at line 57 of file square_wave.h.


The documentation for this class was generated from the following file: