upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions | Protected Attributes

API for the Speaker. More...

Detailed Description

UPM module for the Speaker. This sensor can generate different tones and sounds depending on the frequency of the input signal.

It can operate in one of two modes: GPIO (default) and PWM.

Depending on which mode is selected, some methods may not be usable. In GPIO mode, the playAll() and playSound() methods are supported. In PWM mode, setFrequency(), emit(), on() and off() are supported. Calling a method not appropriate for the mode will have no effect.

speaker.jpg
// Instantiate a Speaker on digital pin D2
upm::Speaker speaker(2);
// Play all 7 of the lowest notes
speaker.playAll();
// Play a medium C-sharp
speaker.playSound('c', true, "med");
// Instantiate a Speaker (PWM) pin D3 in PWM mode
upm::Speaker speaker(3, true);
// emit a series of frequencies for 500ms each
speaker.emit(50, 500);
speaker.emit(75, 500);
speaker.emit(125, 500);
speaker.emit(250, 500);
speaker.emit(500, 500);
speaker.emit(1000, 500);
speaker.emit(2000, 500);
speaker.emit(3000, 500);
speaker.emit(5000, 500);
speaker.emit(10000, 500);
cout << "Exiting" << endl;

Public Member Functions

 Speaker (int pin, bool usePWM=false)
 
virtual ~Speaker ()
 
void playAll ()
 
void playSound (char letter, bool sharp, std::string vocalWeight)
 
void emit (unsigned int freq, unsigned int emit_ms)
 
void setFrequency (unsigned int freq)
 
void on ()
 
void off ()
 

Protected Attributes

speaker_context m_speaker
 

Constructor & Destructor Documentation

Speaker ( int  pin,
bool  usePWM = false 
)

Speaker constructor

Parameters
pinDigital pin to use
usePWMIf true, PWM mode will be used, otherwise GPIO mode (default) is used.

Here is the call graph for this function:

~Speaker ( )
virtual

Speaker destructor

Here is the call graph for this function:

Member Function Documentation

void playAll ( )

Plays all alto notes (lowest notes)

Here is the call graph for this function:

void playSound ( char  letter,
bool  sharp,
std::string  vocalWeight 
)

Plays a sound and a note whether it's sharp or not

Parameters
letterCharacter name of the note ('a', 'b', 'c', 'd', 'e', 'f', or 'g')
sharpIf true, plays a sharp version of the note; otherwise, does not play the note
vocalWeightString to determine whether to play a low ("low"), a medium ("med"), or a high ("high") note

Here is the call graph for this function:

void emit ( unsigned int  freq,
unsigned int  emit_ms 
)

Emit a specific frequency for a given period of time and return. This function only operates when in PWM mode. The frequency is limited to between 50-32000Hz. In addition, the allowable frequencies may be restricted further by the capabilities of your PWM hardware.

Parameters
freqThe frequency to emit. Must be between 50 and 32000Hz inclusive.
emit_msThe number of milliseconds to emit the frequency.

Here is the call graph for this function:

void setFrequency ( unsigned int  freq)

Set a default frequency to be used with on() and off(). This function only operates when in PWM mode. The frequency is limited to between 50-32000Hz. In addition, the allowable frequencies may be restricted further by the capabilities of your PWM hardware.

Parameters
freqThe frequency to emit. Must be between 50 and 32000Hz inclusive.

Here is the call graph for this function:

void on ( )

Turn the speaker on, and emit the frequency last specified with setFrequency() or emit(). This function only operates when in PWM mode.

Here is the call graph for this function:

void off ( )

Turn the speaker off. This function only operates when in PWM mode.

Here is the call graph for this function:

Collaboration diagram for Speaker:
Collaboration graph
[legend]

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