18
18
#define BREATH 10
19
19
20
20
// Instance of Music score player in background
21
- BGMusic_t BGMusic;
22
- CBTimer_t CBTimer;
21
+ static CBTimer cbtimer;
23
22
24
23
// 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 ;
26
26
27
27
// 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) {
29
29
debug_begin (9600 );
30
30
31
31
score.pin = pin;
@@ -36,35 +36,35 @@ bool BGMusic_t::begin(int pin, int notes[], int n_notes, int tempo, bool loop, b
36
36
score.loop = loop;
37
37
38
38
if (start == true ) {
39
- return BGMusic. start ();
39
+ return BGMusic:: start ();
40
40
} else {
41
41
return true ;
42
42
}
43
43
}
44
44
45
45
// 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 ();
49
49
} else {
50
50
return false ;
51
51
}
52
52
}
53
53
54
54
// Stop playing
55
- bool BGMusic_t ::stop (void ) {
55
+ bool BGMusic ::stop (void ) {
56
56
noTone (score.pin );
57
- return CBTimer .stop ();
57
+ return cbtimer .stop ();
58
58
}
59
59
60
60
// Delete the instanciate of FspTimer
61
- void BGMusic_t ::end (void ) {
62
- CBTimer .end ();
61
+ void BGMusic ::end (void ) {
62
+ cbtimer .end ();
63
63
}
64
64
65
65
// Play notes in music score sequentially
66
- void BGMusic_t ::music_callback (void ) {
67
- BGMusic. stop ();
66
+ void BGMusic ::music_callback (void ) {
67
+ stop ();
68
68
69
69
if (score.notes < score.notes_end ) {
70
70
debug_print (" note[0] = " ); debug_println (score.notes [0 ]);
@@ -74,7 +74,7 @@ void BGMusic_t::music_callback(void) {
74
74
int duration = *score.notes ++;
75
75
76
76
// select the method of calculating duration
77
- if (BGMusic. duration_function == nullptr ) {
77
+ if (duration_function == nullptr ) {
78
78
// https://github.com/robsoncouto/arduino-songs
79
79
duration = score.wholenote / duration;
80
80
@@ -86,20 +86,20 @@ void BGMusic_t::music_callback(void) {
86
86
87
87
else {
88
88
// you can select the original method
89
- duration = BGMusic. duration_function (score.wholenote , duration);
89
+ duration = duration_function (score.wholenote , duration);
90
90
}
91
91
92
92
debug_print (" duration = " ); debug_println (duration);
93
93
94
94
tone (score.pin , (unsigned int )note, (unsigned long )(duration * 0.9 ));
95
95
96
96
// Instanciate the FspTimer and start it
97
- CBTimer .begin (duration, music_callback);
97
+ cbtimer .begin (duration, music_callback);
98
98
}
99
99
100
100
// For repeated playback
101
101
else if (score.loop ) {
102
102
score.notes = score.notes_bigin ;
103
- BGMusic. start ();
103
+ start ();
104
104
}
105
105
}
0 commit comments