@@ -25,7 +25,7 @@ class Tone {
25
25
}
26
26
27
27
void start (void ) {
28
- if (frequency) {
28
+ if (( frequency != 0 ) && (channel != - 1 ) ) {
29
29
tone_timer.start ();
30
30
}
31
31
if (duration != 0 ) {
@@ -42,7 +42,7 @@ class Tone {
42
42
}
43
43
44
44
void stop (void ) {
45
- if (frequency) {
45
+ if (( frequency != 0 ) && (channel != - 1 ) ) {
46
46
tone_timer.stop ();
47
47
}
48
48
digitalWrite (pin, LOW);
@@ -51,15 +51,18 @@ class Tone {
51
51
static void timer_config (uint32_t period_us) {
52
52
// Configure and enable the tone timer.
53
53
uint8_t type = 0 ;
54
- if (channel == -1 ) {
54
+ if (tone_timer.is_opened ()) {
55
+ tone_timer.set_frequency (1000000 .0f /period_us);
56
+ } else {
55
57
channel = FspTimer::get_available_timer (type);
58
+ if (channel != -1 ) {
59
+ tone_timer.begin (TIMER_MODE_PERIODIC, type, channel,
60
+ 1000000 .0f /period_us, 50 .0f , tone_timer_callback, nullptr );
61
+ tone_timer.setup_overflow_irq ();
62
+ tone_timer.open ();
63
+ tone_timer.stop ();
64
+ }
56
65
}
57
-
58
- tone_timer.begin (TIMER_MODE_PERIODIC, type, channel,
59
- 1000000 .0f /period_us, 50 .0f , tone_timer_callback, nullptr );
60
- tone_timer.setup_overflow_irq ();
61
- tone_timer.open ();
62
- tone_timer.stop ();
63
66
}
64
67
};
65
68
@@ -76,8 +79,8 @@ void tone(pin_size_t pin, unsigned int frequency, unsigned long duration) {
76
79
delete active_tone;
77
80
}
78
81
Tone* t = new Tone (pin, frequency, duration);
79
- t->start ();
80
82
active_tone = t;
83
+ t->start ();
81
84
};
82
85
83
86
void noTone (pin_size_t __attribute__ ((unused)) pin) {
0 commit comments