Arduboy2 Library
Arduboy2Audio Class Reference

Provide speaker and sound control. More...

#include <Arduboy2Audio.h>

+ Collaboration diagram for Arduboy2Audio:

Static Public Member Functions

static void begin ()
 Initialize the speaker based on the current mute setting. More...
 
static void on ()
 Turn sound on. More...
 
static void off ()
 Turn sound off (mute). More...
 
static void toggle ()
 Toggle the sound on/off state. More...
 
static void saveOnOff ()
 Save the current sound state in EEPROM. More...
 
static bool enabled ()
 Get the current sound state. More...
 

Detailed Description

Provide speaker and sound control.

This class provides functions to initialize the speaker and control the enabling and disabling (muting) of sound. It doesn't provide any functions to actually produce sound.

The state of sound muting is stored in system EEPROM and so is retained over power cycles.

An Arduboy2Audio class object named audio will be created by the Arduboy2Base class, so there is no need for a sketch itself to create an Arduboy2Audio object. Arduboy2Audio functions can be called using the Arduboy2 or Arduboy2Base audio object.

Example:

#include <Arduboy2.h>
Arduboy2 arduboy;
// Arduboy2Audio functions can be called as follows:
arduboy.audio.on();
arduboy.audio.off();
Note

In order for this class to be fully functional, the external library or functions used by a sketch to actually to produce sounds should be compliant with this class. This means they should only produce sound if it is enabled, or mute the sound if it's disabled. The enabled() function can be used to determine if sound is enabled or muted. Generally a compliant library would accept the enabled() function as an initialization parameter and then call it as necessary to determine the current state.

For example, the ArduboyTones and ArduboyPlaytune libraries require an enabled() type function to be passed as a parameter in the constructor, like so:

#include <Arduboy2.h>
#include <ArduboyTones.h>
Arduboy2 arduboy;
ArduboyTones sound(arduboy.audio.enabled);
Note
A friend class named Arduboy2Ex is declared by this class. The intention is to allow a sketch to create an Arduboy2Ex class which would have access to the private and protected members of the Arduboy2Audio class. It is hoped that this may eliminate the need to create an entire local copy of the library, in order to extend the functionality, in most circumstances.

Definition at line 73 of file Arduboy2Audio.h.

Member Function Documentation

void Arduboy2Audio::begin ( )
static

Initialize the speaker based on the current mute setting.

The speaker is initialized based on the current mute setting saved in system EEPROM. This function is called by Arduboy2Base::begin() so it isn't normally required to call it within a sketch. However, if Arduboy2Core::boot() is used instead of Arduboy2Base::begin() and the sketch includes sound, then this function should be called after boot().

Definition at line 49 of file Arduboy2Audio.cpp.

bool Arduboy2Audio::enabled ( )
static

Get the current sound state.

Returns
true if sound is currently enabled (not muted).

This function should be used by code that actually generates sound. If true is returned, sound can be produced. If false is returned, sound should be muted.

See also
on() off() toggle()

Definition at line 57 of file Arduboy2Audio.cpp.

void Arduboy2Audio::off ( )
static

Turn sound off (mute).

The system is configured to not produce sound (mute). This function sets the sound mode only until the unit is powered off. To save the current mode use saveOnOff().

See also
on() toggle() saveOnOff()

Definition at line 24 of file Arduboy2Audio.cpp.

void Arduboy2Audio::on ( )
static

Turn sound on.

The system is configured to generate sound. This function sets the sound mode only until the unit is powered off. To save the current mode use saveOnOff().

See also
off() toggle() saveOnOff()

Definition at line 12 of file Arduboy2Audio.cpp.

void Arduboy2Audio::saveOnOff ( )
static

Save the current sound state in EEPROM.

The current sound state, set by on() or off(), is saved to the reserved system area in EEPROM. This allows the state to carry over between power cycles and after uploading a different sketch.

Note
EEPROM is limited in the number of times it can be written to. Sketches should not continuously change and then save the state rapidly.
See also
on() off() toggle()

Definition at line 44 of file Arduboy2Audio.cpp.

void Arduboy2Audio::toggle ( )
static

Toggle the sound on/off state.

If the system is configured for sound on, it will be changed to sound off (mute). If sound is off, it will be changed to on. This function sets the sound mode only until the unit is powered off. To save the current mode use saveOnOff().

See also
on() off() saveOnOff()

Definition at line 36 of file Arduboy2Audio.cpp.


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