Skip to content

Commit 69f89b0

Browse files
Change the class name.
1 parent 11951b1 commit 69f89b0

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

BGMusic.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#define BREATH 10
1919

2020
// Instance of Music score player in background
21-
BGMusic_t BGMusic;
22-
CBTimer_t CBTimer;
21+
static CBTimer cbtimer;
2322

2423
// Initialize static data menbers
25-
MusicScore_t BGMusic_t::score = { 0, 0, 0, 0, false };
24+
MusicScore_t BGMusic::score = { 0, 0, 0, 0, false };
25+
int (*BGMusic::duration_function)(int wholenote, int duration) = nullptr;
2626

2727
// Initialize music score sequencer
28-
bool BGMusic_t::begin(int pin, int notes[], int n_notes, int tempo, bool loop, bool start) {
28+
bool BGMusic::begin(int pin, int notes[], int n_notes, int tempo, bool loop, bool start) {
2929
debug_begin(9600);
3030

3131
score.pin = pin;
@@ -36,35 +36,35 @@ bool BGMusic_t::begin(int pin, int notes[], int n_notes, int tempo, bool loop, b
3636
score.loop = loop;
3737

3838
if (start == true) {
39-
return BGMusic.start();
39+
return BGMusic::start();
4040
} else {
4141
return true;
4242
}
4343
}
4444

4545
// Instanciate the FspTimer and start it immediately
46-
bool BGMusic_t::start(void) {
47-
if (CBTimer.begin(TIMER_MODE_ONE_SHOT, CBTIMER_START_NOW, music_callback) == true) {
48-
return CBTimer.start();
46+
bool BGMusic::start(void) {
47+
if (cbtimer.begin(TIMER_MODE_ONE_SHOT, CBTIMER_START_NOW, music_callback) == true) {
48+
return cbtimer.start();
4949
} else {
5050
return false;
5151
}
5252
}
5353

5454
// Stop playing
55-
bool BGMusic_t::stop(void) {
55+
bool BGMusic::stop(void) {
5656
noTone(score.pin);
57-
return CBTimer.stop();
57+
return cbtimer.stop();
5858
}
5959

6060
// Delete the instanciate of FspTimer
61-
void BGMusic_t::end(void) {
62-
CBTimer.end();
61+
void BGMusic::end(void) {
62+
cbtimer.end();
6363
}
6464

6565
// Play notes in music score sequentially
66-
void BGMusic_t::music_callback(void) {
67-
BGMusic.stop();
66+
void BGMusic::music_callback(void) {
67+
stop();
6868

6969
if (score.notes < score.notes_end) {
7070
debug_print("note[0] = "); debug_println(score.notes[0]);
@@ -74,7 +74,7 @@ void BGMusic_t::music_callback(void) {
7474
int duration = *score.notes++;
7575

7676
// select the method of calculating duration
77-
if (BGMusic.duration_function == nullptr) {
77+
if (duration_function == nullptr) {
7878
// https://github.com/robsoncouto/arduino-songs
7979
duration = score.wholenote / duration;
8080

@@ -86,20 +86,20 @@ void BGMusic_t::music_callback(void) {
8686

8787
else {
8888
// you can select the original method
89-
duration = BGMusic.duration_function(score.wholenote, duration);
89+
duration = duration_function(score.wholenote, duration);
9090
}
9191

9292
debug_print("duration = "); debug_println(duration);
9393

9494
tone(score.pin, (unsigned int)note, (unsigned long)(duration * 0.9));
9595

9696
// Instanciate the FspTimer and start it
97-
CBTimer.begin(duration, music_callback);
97+
cbtimer.begin(duration, music_callback);
9898
}
9999

100100
// For repeated playback
101101
else if (score.loop) {
102102
score.notes = score.notes_bigin;
103-
BGMusic.start();
103+
start();
104104
}
105105
}

BGMusic.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,21 @@ typedef struct {
3333
int loop;
3434
} MusicScore_t;
3535

36-
class BGMusic_t {
36+
class BGMusic {
3737
private:
3838
static MusicScore_t score;
3939
static void music_callback(void);
40-
int (*duration_function)(int wholenote, int duration) = nullptr;
40+
static int (*duration_function)(int wholenote, int duration);
4141

4242
public:
4343
bool begin(int pin, int notes[], int n_notes, int tempo, bool loop, bool start = false);
44-
bool start(void);
45-
bool stop(void);
44+
static bool start(void);
45+
static bool stop(void);
4646
void end(void);
4747

4848
void set_duration_function(int (*calc_duration_function)(int wholenote, int duration)) {
4949
duration_function = calc_duration_function;
5050
}
5151
};
5252

53-
extern BGMusic_t BGMusic;
54-
extern CBTimer_t CBTimer;
55-
5653
#endif

examples/blink_and_music/blink_and_music.ino

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ void setup() {
2828
pinMode(LED_BUILTIN, OUTPUT);
2929

3030
// initialize and start the BGM player
31-
BGMusic.begin(BUZZER_PIN, melody, N_NOTES(melody), TEMPO, REPEAT);
32-
BGMusic.start();
31+
BGMusic music;
32+
music.begin(BUZZER_PIN, melody, N_NOTES(melody), TEMPO, REPEAT);
33+
music.start();
3334
}
3435

3536
void loop() {

keywords.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Datatypes (KEYWORD1)
2+
BGMusic KEYWORD1
3+
4+
# Methods and Functions (KEYWORD2)
15
begin KEYWORD2
26
end KEYWORD2
37
start KEYWORD2

0 commit comments

Comments
 (0)