PX4 Firmware
PX4 Autopilot Software http://px4.io
Tunes Class Reference

Library for parsing tunes from melody-strings or dedicated tune messages. More...

#include <tunes.h>

Collaboration diagram for Tunes:

Public Types

enum  NoteMode { NoteMode::NORMAL, NoteMode::LEGATO, NoteMode::STACCATO }
 

Public Member Functions

 Tunes (unsigned default_note_length=TUNE_DEFAULT_NOTE_LENGTH, NoteMode default_note_mode=NoteMode::NORMAL, unsigned default_octave=TUNE_DEFAULT_OCTAVE, unsigned default_tempo=TUNE_DEFAULT_TEMPO)
 Constructor with the default parameters set to: default_tempo: TUNE_DEFAULT_TEMPO default_octave: TUNE_DEFAULT_OCTAVE default_note_length: TUNE_DEFAULT_NOTE_LENGTH default_mode: NORMAL. More...
 
 ~Tunes ()=default
 
int set_control (const tune_control_s &tune_control)
 Set tune to be played using the message. More...
 
void set_string (const char *const string, uint8_t volume)
 Set tune to be played using a string. More...
 
int get_next_note (unsigned &frequency, unsigned &duration, unsigned &silence)
 Get next note in the current tune, which has been provided by either set_control or play_string. More...
 
int get_next_note (unsigned &frequency, unsigned &duration, unsigned &silence, uint8_t &volume)
 Get next note in the current tune, which has been provided by either set_control or play_string. More...
 
unsigned int get_default_tunes_size () const
 Get the number of default tunes. More...
 
unsigned int get_maximum_update_interval ()
 

Private Member Functions

uint32_t note_to_frequency (unsigned note) const
 Convert note to frequency. More...
 
unsigned note_duration (unsigned &silence, unsigned note_length, unsigned dots) const
 Calculate the duration in microseconds of play and silence for a note given the current tempo, length and mode and the number of dots following in the play string. More...
 
unsigned rest_duration (unsigned rest_length, unsigned dots) const
 Calculate the duration in microseconds of a rest corresponding to a given note length. More...
 
int next_char ()
 Find the next character in the string, discard any whitespace. More...
 
unsigned next_number ()
 Extract a number from the string, consuming all the digit characters. More...
 
unsigned next_dots ()
 Consume dot characters from the string. More...
 
void reset (bool repeat_flag)
 Reset the tune parameters. More...
 
int tune_end ()
 
int tune_error ()
 

Private Attributes

const char * _next_tune = nullptr
 next note in the string More...
 
const char * _tune = nullptr
 current tune string More...
 
const char * _tune_start_ptr = nullptr
 pointer to repeat tune More...
 
int _current_tune_id = static_cast<int>(TuneID::NONE)
 
bool _repeat = false
 if true, tune restarts at end More...
 
unsigned int _default_note_length = TUNE_DEFAULT_NOTE_LENGTH
 
NoteMode _default_note_mode = NoteMode::NORMAL
 
unsigned int _default_octave = TUNE_DEFAULT_OCTAVE
 
unsigned int _default_tempo = TUNE_DEFAULT_TEMPO
 
unsigned int _note_length = TUNE_DEFAULT_NOTE_LENGTH
 
NoteMode _note_mode = NoteMode::NORMAL
 
unsigned int _octave = TUNE_DEFAULT_OCTAVE
 
unsigned int _tempo = TUNE_DEFAULT_TEMPO
 
unsigned int _duration = 0
 
unsigned int _frequency = 0
 
unsigned int _silence = 0
 
uint8_t _volume = 0
 
bool _using_custom_msg = false
 

Static Private Attributes

static const char *const _default_tunes []
 
static const bool _default_tunes_interruptable []
 
static const unsigned int _default_tunes_size = sizeof(_default_tunes) / sizeof(_default_tunes[0])
 
static const uint8_t _note_tab [] = {9, 11, 0, 2, 4, 5, 7}
 

Detailed Description

Library for parsing tunes from melody-strings or dedicated tune messages.

Needs to be instantiated as it keeps track of which tune is to be played next. Also handles repeated tunes.

Definition at line 56 of file tunes.h.

Member Enumeration Documentation

◆ NoteMode

enum Tunes::NoteMode
strong
Enumerator
NORMAL 
LEGATO 
STACCATO 

Definition at line 59 of file tunes.h.

Constructor & Destructor Documentation

◆ Tunes()

Tunes::Tunes ( unsigned  default_note_length = TUNE_DEFAULT_NOTE_LENGTH,
NoteMode  default_note_mode = NoteMode::NORMAL,
unsigned  default_octave = TUNE_DEFAULT_OCTAVE,
unsigned  default_tempo = TUNE_DEFAULT_TEMPO 
)

Constructor with the default parameters set to: default_tempo: TUNE_DEFAULT_TEMPO default_octave: TUNE_DEFAULT_OCTAVE default_note_length: TUNE_DEFAULT_NOTE_LENGTH default_mode: NORMAL.

Definition at line 56 of file tunes.cpp.

References reset().

Here is the call graph for this function:

◆ ~Tunes()

Tunes::~Tunes ( )
default

Member Function Documentation

◆ get_default_tunes_size()

unsigned int Tunes::get_default_tunes_size ( ) const
inline

Get the number of default tunes.

This is useful for when a tune is requested via its tune ID.

Returns
Number of default tunes accessible via tune ID

Definition at line 121 of file tunes.h.

References _default_tunes_size.

Referenced by tune_control_main().

Here is the caller graph for this function:

◆ get_maximum_update_interval()

unsigned int Tunes::get_maximum_update_interval ( )
inline

Definition at line 123 of file tunes.h.

References next_char(), next_dots(), next_number(), note_duration(), note_to_frequency(), reset(), rest_duration(), tune_end(), tune_error(), and TUNE_MAX_UPDATE_INTERVAL_US.

Referenced by ToneAlarm::next_note(), and tune_control_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_next_note() [1/2]

int Tunes::get_next_note ( unsigned &  frequency,
unsigned &  duration,
unsigned &  silence 
)

Get next note in the current tune, which has been provided by either set_control or play_string.

Parameters
frequencyreturn frequency value (Hz)
durationreturn duration of the tone (us)
silencereturn silence duration (us)
Returns
-1 for error, 0 for play one tone and 1 for continue a sequence

Definition at line 172 of file tunes.cpp.

References _duration, _frequency, _next_tune, _note_length, _note_mode, _note_tab, _octave, _repeat, _silence, _tempo, _tune, _using_custom_msg, LEGATO, next_char(), next_dots(), next_number(), NORMAL, note_duration(), note_to_frequency(), rest_duration(), STACCATO, TUNE_CONTINUE, tune_end(), and tune_error().

Referenced by get_next_note(), ToneAlarm::next_note(), and tune_control_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_next_note() [2/2]

int Tunes::get_next_note ( unsigned &  frequency,
unsigned &  duration,
unsigned &  silence,
uint8_t &  volume 
)

Get next note in the current tune, which has been provided by either set_control or play_string.

Parameters
frequencyreturn frequency value (Hz)
durationreturn duration of the note (us)
silencereturn silence duration (us)
volumereturn the volume level of the note (between 0-100)
Returns
-1 for no tune available/error, 0 to not play anything and 1 to play

Definition at line 156 of file tunes.cpp.

References _volume, and get_next_note().

Here is the call graph for this function:

◆ next_char()

int Tunes::next_char ( )
private

Find the next character in the string, discard any whitespace.

Returns
uppercase version of the char.

Definition at line 441 of file tunes.cpp.

References _next_tune.

Referenced by get_maximum_update_interval(), get_next_note(), next_dots(), and next_number().

Here is the caller graph for this function:

◆ next_dots()

unsigned Tunes::next_dots ( )
private

Consume dot characters from the string.

Returns
number of consumed dots

Definition at line 467 of file tunes.cpp.

References _next_tune, and next_char().

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ next_number()

unsigned Tunes::next_number ( )
private

Extract a number from the string, consuming all the digit characters.

Returns
extracted number.

Definition at line 450 of file tunes.cpp.

References _next_tune, and next_char().

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ note_duration()

unsigned Tunes::note_duration ( unsigned &  silence,
unsigned  note_length,
unsigned  dots 
) const
private

Calculate the duration in microseconds of play and silence for a note given the current tempo, length and mode and the number of dots following in the play string.

Parameters
silencereturn silence duration (us)
note_lengthnote length
dotsextention of the note length
Returns
duration of the note (us)

Definition at line 384 of file tunes.cpp.

References _note_mode, _tempo, BEAT_TIME_CONVERSION, LEGATO, NORMAL, and STACCATO.

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the caller graph for this function:

◆ note_to_frequency()

uint32_t Tunes::note_to_frequency ( unsigned  note) const
private

Convert note to frequency.

Parameters
noteunsigned value of the semitone from C
Returns
frequency (Hz)

Definition at line 378 of file tunes.cpp.

References f().

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void Tunes::reset ( bool  repeat_flag)
private

Reset the tune parameters.

This is necessary when for example a tune moved one or more octaves up or down. reset() should always be called before (re)-starting a tune.

Definition at line 66 of file tunes.cpp.

References _default_note_length, _default_note_mode, _default_octave, _default_tempo, _next_tune, _note_length, _note_mode, _octave, _tempo, _tune, and _tune_start_ptr.

Referenced by get_maximum_update_interval(), set_control(), tune_end(), tune_error(), and Tunes().

Here is the caller graph for this function:

◆ rest_duration()

unsigned Tunes::rest_duration ( unsigned  rest_length,
unsigned  dots 
) const
private

Calculate the duration in microseconds of a rest corresponding to a given note length.

Parameters
rest_lengthrest lenght in fraction of a note
dotsnumber of extension dots
Returns
rest duration (us)

Definition at line 421 of file tunes.cpp.

References _tempo, and BEAT_TIME_CONVERSION.

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the caller graph for this function:

◆ set_control()

int Tunes::set_control ( const tune_control_s tune_control)

Set tune to be played using the message.

If a tune is already being played the call to this function will be ignored, unless the override flag is set or the tune being already played is a repeated tune.

Parameters
tune_controlstruct containig the uORB message
Returns
return -EINVAL if the default tune does not exist.

Definition at line 85 of file tunes.cpp.

References _current_tune_id, _default_tunes, _default_tunes_interruptable, _default_tunes_size, _duration, _frequency, _next_tune, _repeat, _silence, _tune, _tune_start_ptr, _using_custom_msg, _volume, tune_control_s::duration, tune_control_s::frequency, OK, reset(), tune_control_s::silence, tune_control_s::tune_id, tune_control_s::tune_override, and tune_control_s::volume.

Referenced by ToneAlarm::orb_update(), and tune_control_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_string()

void Tunes::set_string ( const char *const  string,
uint8_t  volume 
)

Set tune to be played using a string.

Parses a tune string, formatted with the syntax of the Microsoft GWBasic/QBasic. Ownership of the string is NOT transferred. The string has to be kept in memory for the whole duration of the melody.

Parameters
stringtune input string

Definition at line 138 of file tunes.cpp.

References _next_tune, _tune, _tune_start_ptr, and _volume.

Referenced by test_tone(), and tune_control_main().

Here is the caller graph for this function:

◆ tune_end()

int Tunes::tune_end ( )
private

Definition at line 356 of file tunes.cpp.

References _repeat, reset(), TUNE_CONTINUE, and TUNE_STOP.

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tune_error()

int Tunes::tune_error ( )
private

Definition at line 370 of file tunes.cpp.

References _repeat, reset(), and TUNE_ERROR.

Referenced by get_maximum_update_interval(), and get_next_note().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _current_tune_id

int Tunes::_current_tune_id = static_cast<int>(TuneID::NONE)
private

Definition at line 198 of file tunes.h.

Referenced by set_control().

◆ _default_note_length

unsigned int Tunes::_default_note_length = TUNE_DEFAULT_NOTE_LENGTH
private

Definition at line 202 of file tunes.h.

Referenced by reset().

◆ _default_note_mode

NoteMode Tunes::_default_note_mode = NoteMode::NORMAL
private

Definition at line 203 of file tunes.h.

Referenced by reset().

◆ _default_octave

unsigned int Tunes::_default_octave = TUNE_DEFAULT_OCTAVE
private

Definition at line 204 of file tunes.h.

Referenced by reset().

◆ _default_tempo

unsigned int Tunes::_default_tempo = TUNE_DEFAULT_TEMPO
private

Definition at line 205 of file tunes.h.

Referenced by reset().

◆ _default_tunes

const char *const Tunes::_default_tunes
staticprivate
Initial value:
= {
}

Definition at line 189 of file tunes.h.

Referenced by set_control().

◆ _default_tunes_interruptable

const bool Tunes::_default_tunes_interruptable
staticprivate
Initial value:
= {
}

Definition at line 190 of file tunes.h.

Referenced by set_control().

◆ _default_tunes_size

const unsigned int Tunes::_default_tunes_size = sizeof(_default_tunes) / sizeof(_default_tunes[0])
staticprivate

Definition at line 191 of file tunes.h.

Referenced by get_default_tunes_size(), and set_control().

◆ _duration

unsigned int Tunes::_duration = 0
private

Definition at line 212 of file tunes.h.

Referenced by get_next_note(), and set_control().

◆ _frequency

unsigned int Tunes::_frequency = 0
private

Definition at line 213 of file tunes.h.

Referenced by get_next_note(), and set_control().

◆ _next_tune

const char* Tunes::_next_tune = nullptr
private

next note in the string

Definition at line 194 of file tunes.h.

Referenced by get_next_note(), next_char(), next_dots(), next_number(), reset(), set_control(), and set_string().

◆ _note_length

unsigned int Tunes::_note_length = TUNE_DEFAULT_NOTE_LENGTH
private

Definition at line 207 of file tunes.h.

Referenced by get_next_note(), and reset().

◆ _note_mode

NoteMode Tunes::_note_mode = NoteMode::NORMAL
private

Definition at line 208 of file tunes.h.

Referenced by get_next_note(), note_duration(), and reset().

◆ _note_tab

const uint8_t Tunes::_note_tab = {9, 11, 0, 2, 4, 5, 7}
staticprivate

Definition at line 192 of file tunes.h.

Referenced by get_next_note().

◆ _octave

unsigned int Tunes::_octave = TUNE_DEFAULT_OCTAVE
private

Definition at line 209 of file tunes.h.

Referenced by get_next_note(), and reset().

◆ _repeat

bool Tunes::_repeat = false
private

if true, tune restarts at end

Definition at line 200 of file tunes.h.

Referenced by get_next_note(), set_control(), tune_end(), and tune_error().

◆ _silence

unsigned int Tunes::_silence = 0
private

Definition at line 214 of file tunes.h.

Referenced by get_next_note(), and set_control().

◆ _tempo

unsigned int Tunes::_tempo = TUNE_DEFAULT_TEMPO
private

Definition at line 210 of file tunes.h.

Referenced by get_next_note(), note_duration(), reset(), and rest_duration().

◆ _tune

const char* Tunes::_tune = nullptr
private

current tune string

Definition at line 195 of file tunes.h.

Referenced by get_next_note(), reset(), set_control(), and set_string().

◆ _tune_start_ptr

const char* Tunes::_tune_start_ptr = nullptr
private

pointer to repeat tune

Definition at line 196 of file tunes.h.

Referenced by reset(), set_control(), and set_string().

◆ _using_custom_msg

bool Tunes::_using_custom_msg = false
private

Definition at line 217 of file tunes.h.

Referenced by get_next_note(), and set_control().

◆ _volume

uint8_t Tunes::_volume = 0
private

Definition at line 215 of file tunes.h.

Referenced by get_next_note(), set_control(), and set_string().


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