upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Member Functions | Protected Attributes
Buzzer Class Reference

API for the Buzzer component. More...

Detailed Description

This module defines the Buzzer interface for libbuzzer. This sensor can make different tones when connected to a pin capable of analog pulse-width modulation. It emits sound using a piezoelectric material that vibrates at different frequencies based on the input voltage.

buzzer.jpg
int chord[] = { DO, RE, MI, FA, SOL, LA, SI, DO, SI };
// create Buzzer instance
upm::Buzzer* sound = new upm::Buzzer(5);
// print sensor name
std::cout << sound->name() << std::endl;
// play sound (DO, RE, MI, etc...), pausing for 0.1 seconds between notes
for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
// play each note for one second
std::cout << sound->playSound(chord[chord_ind], 1000000) << std::endl;
usleep(100000);
}

Public Member Functions

 Buzzer (int pinNumber)
 
 ~Buzzer ()
 
int playSound (int note, int delay)
 
void stopSound ()
 
void setVolume (float vol)
 
float getVolume ()
 
std::string name ()
 

Protected Attributes

std::string m_name
 

Constructor & Destructor Documentation

Buzzer ( int  pinNumber)

Instantiates a Buzzer object.

Parameters
pinNumberBuzzer pin number

Here is the call graph for this function:

~Buzzer ( )

Buzzer object destructor.

Here is the call graph for this function:

Member Function Documentation

int playSound ( int  note,
int  delay 
)

Plays a tone for a certain amount of time or indefinitely. When delay is not used, the sound can be stopped by calling stopSound().

Parameters
noteNote to play (C, D, E, etc.) or frequency
delayTime in microseconds for which to play the sound; if the value is 0, the sound is played indefinitely
Returns
Note played

Here is the call graph for this function:

void stopSound ( )

Stops the sound currently playing. Should be called when playSound() does not have a delay value.

Here is the caller graph for this function:

void setVolume ( float  vol)

Sets the volume for the buzzer, but may affect the sound timbre. Works best with halved values; e.g., 1.0, 0.5, 0.25, etc.

Parameters
volValue to set the volume to, from 0.0 to 1.0

Here is the caller graph for this function:

float getVolume ( )

Gets the buzzer volume.

Returns
Value the volume was set to
std::string name ( )
inline

Returns the name of the sensor.

Returns
Name of the sensor

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