Skip to content

Commit 2d6ea4e

Browse files
Add constructor/destructor.
1 parent 69f89b0 commit 2d6ea4e

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

BGMusic.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ static CBTimer cbtimer;
2424
MusicScore_t BGMusic::score = { 0, 0, 0, 0, false };
2525
int (*BGMusic::duration_function)(int wholenote, int duration) = nullptr;
2626

27+
// Constructor / Destructor
28+
BGMusic::BGMusic() {}
29+
BGMusic::~BGMusic() {
30+
end();
31+
}
32+
2733
// Initialize music score sequencer
28-
bool BGMusic::begin(int pin, int notes[], int n_notes, int tempo, bool loop, bool start) {
34+
bool BGMusic::begin(int pin, const int notes[], int n_notes, int tempo, bool loop, bool start) {
2935
debug_begin(9600);
3036

3137
score.pin = pin;

BGMusic.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
// Music score
2727
typedef struct {
2828
int pin;
29-
int *notes;
30-
int *notes_bigin;
31-
int *notes_end;
29+
const int *notes;
30+
const int *notes_bigin;
31+
const int *notes_end;
3232
int wholenote;
3333
int loop;
3434
} MusicScore_t;
@@ -40,7 +40,9 @@ class BGMusic {
4040
static int (*duration_function)(int wholenote, int duration);
4141

4242
public:
43-
bool begin(int pin, int notes[], int n_notes, int tempo, bool loop, bool start = false);
43+
BGMusic();
44+
~BGMusic();
45+
bool begin(int pin, const int notes[], int n_notes, int tempo, bool loop, bool start = false);
4446
static bool start(void);
4547
static bool stop(void);
4648
void end(void);

README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ void loop() {
4646
}
4747
```
4848

49-
Using this BGMplay library, you can add something to do inside `loop()` like this:
49+
Using this BGMusic library, you can add something to do inside `loop()` like this:
5050

5151
```C++
5252
/*
5353
Example of music score player in background
5454
*/
5555
#include "Arduino.h"
56-
#include "BGMplay.h"
56+
#include "BGMusic.h"
5757

5858
// notes (pairs of pitch and duration) in musical score.
5959
// duration: 4 = quarter note, 8 = eighth note, etc.
@@ -62,7 +62,7 @@ static int score[] = {
6262
NOTE_C4, 4, NOTE_G3, 8, NOTE_G3, 8, NOTE_A3, 4, NOTE_G3, 4, REST, 4, NOTE_B3, 4, NOTE_C4, 4
6363
};
6464

65-
// Parameters for BGMPlay.begin()
65+
// parameters for begin()
6666
#define BUZZER_PIN 11 // pin number connected to the buzzer.
6767
#define TEMPO 140 // change this to make the song slower or faster.
6868
#define REPEAT true // playback repeatedly
@@ -74,8 +74,9 @@ void setup() {
7474
pinMode(LED_BUILTIN, OUTPUT);
7575

7676
// initialize and start BGM player
77-
BGMplay.begin(BUZZER_PIN, score, N_NOTES(score), TEMPO, REPEAT);
78-
BGMplay.start();
77+
static BGMusic music;
78+
music.begin(BUZZER_PIN, score, N_NOTES(score), TEMPO, REPEAT);
79+
music.start();
7980
}
8081

8182
void loop() {
@@ -150,9 +151,10 @@ int calc_duration(int wholenote, int duration) {
150151
void setup() {
151152
...
152153
// initialize and start the BGM player
153-
BGMusic.set_duration_function(calc_duration);
154-
BGMusic.begin(buzzer_pin, melody, notes, tempo, repeat);
155-
BGMusic.start();
154+
static BGMusic music;
155+
music.set_duration_function(calc_duration);
156+
music.begin(buzzer_pin, melody, notes, tempo, repeat);
157+
music.start();
156158
...
157159
}
158160
```

examples/blink_and_music/blink_and_music.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void setup() {
2828
pinMode(LED_BUILTIN, OUTPUT);
2929

3030
// initialize and start the BGM player
31-
BGMusic music;
31+
static BGMusic music;
3232
music.begin(BUZZER_PIN, melody, N_NOTES(melody), TEMPO, REPEAT);
3333
music.start();
3434
}

0 commit comments

Comments
 (0)