FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
devices::audio::ToneGenerator< NTIMER, OUTPUT > Class Template Reference

API class for tone generation to a buzzer (or better an amplifier) connected to pin OUTPUT. More...

#include <fastarduino/devices/tones.h>

Public Types

using PRESCALER = typename SQWGEN::TIMER::PRESCALER
 The TimerPrescaler type matching the selected NTIMER.
 
using COUNTER = typename SQWGEN::TIMER::TYPE
 The counter type (uint8_t or uint16_t) for the selected NTIMER.
 

Public Member Functions

 ToneGenerator (const ToneGenerator< NTIMER, OUTPUT > &)=delete
 
ToneGenerator< NTIMER, OUTPUT > & operator= (const ToneGenerator< NTIMER, OUTPUT > &)=delete
 
 ToneGenerator ()=default
 Create a new generator of tones.
 
void start_tone (Tone tone)
 Start generating a tone on the connected buzzer until stop_tone() is called. More...
 
void start_tone (PRESCALER prescaler, COUNTER counter)
 Start generating a tone on the connected buzzer until stop_tone() is called. More...
 
void stop_tone ()
 Stop the tone being currently generated to the connected buzzer. More...
 

Detailed Description

template<board::Timer NTIMER, board::PWMPin OUTPUT>
class devices::audio::ToneGenerator< NTIMER, OUTPUT >

API class for tone generation to a buzzer (or better an amplifier) connected to pin OUTPUT.

This is a rather low-level API. If you want to play suites of tones ("melodies"), then you should better use devices::audio::TonePlayer. Most provided methods come in two flavours:

  • one that takes a Tone argument (that will be converted to a note frequency)
  • one that takes a TimerPrescaler and a counter value, that will be used to generate the note frequency from raw timer values. The first flavour is more readable but less efficient (code size and performance). The second flavour allows for performance optimization (compile-time calculations) but is less easy to read and understand.
Template Parameters
NTIMERthe AVR timer to use for the underlying Timer
OUTPUTthe board::PWMPin connected to the buzzer; this must be the pin OCnA, where n is the AVR Timer number
See also
Tone
TonePlayer

Definition at line 182 of file tones.h.

Member Function Documentation

◆ start_tone() [1/2]

template<board::Timer NTIMER, board::PWMPin OUTPUT>
void devices::audio::ToneGenerator< NTIMER, OUTPUT >::start_tone ( Tone  tone)
inline

Start generating a tone on the connected buzzer until stop_tone() is called.

If you would like to generate a tone for a given duration, you should use tone() instead.

Parameters
tonethe tone to generate
See also
stop_tone()
tone(Tone)
start_tone(PRESCALER, COUNTER)

Definition at line 213 of file tones.h.

◆ start_tone() [2/2]

template<board::Timer NTIMER, board::PWMPin OUTPUT>
void devices::audio::ToneGenerator< NTIMER, OUTPUT >::start_tone ( PRESCALER  prescaler,
COUNTER  counter 
)
inline

Start generating a tone on the connected buzzer until stop_tone() is called.

If you would like to generate a tone for a given duration, you should use tone() instead.

Parameters
prescalerthe timer prescaler value to use to produce the required tone
counterthe timer counter value to use to produce the required tone
See also
stop_tone()
tone(PRESCALER, COUNTER)
start_tone(Tone)

Definition at line 231 of file tones.h.

◆ stop_tone()

template<board::Timer NTIMER, board::PWMPin OUTPUT>
void devices::audio::ToneGenerator< NTIMER, OUTPUT >::stop_tone ( )
inline

Stop the tone being currently generated to the connected buzzer.

See also
start_toner()

Definition at line 240 of file tones.h.


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