Arduboy2 Library  6.0.0
Arduboy2Audio Class Reference

Provide speaker and sound control. More...

#include <Arduboy2Audio.h>

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

◆ begin()

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.

Definition at line 48 of file Arduboy2Audio.cpp.

◆ enabled()

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 56 of file Arduboy2Audio.cpp.

◆ off()

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 23 of file Arduboy2Audio.cpp.

◆ on()

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 11 of file Arduboy2Audio.cpp.

◆ saveOnOff()

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 43 of file Arduboy2Audio.cpp.

◆ toggle()

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 35 of file Arduboy2Audio.cpp.


The documentation for this class was generated from the following files:
Arduboy2Audio::off
static void off()
Turn sound off (mute).
Definition: Arduboy2Audio.cpp:23
Arduboy2
The main functions provided for writing sketches for the Arduboy, including text output.
Definition: Arduboy2.h:1576
Arduboy2Audio::on
static void on()
Turn sound on.
Definition: Arduboy2Audio.cpp:11
Arduboy2Base::audio
static Arduboy2Audio audio
An object created to provide audio control functions within this class.
Definition: Arduboy2.h:227
Arduboy2.h
The Arduboy2Base and Arduboy2 classes and support objects and definitions.
Arduboy2Audio::enabled
static bool enabled()
Get the current sound state.
Definition: Arduboy2Audio.cpp:56