|
@@ -31,8 +31,7 @@ class Speaker: public Buzzer {
|
31
|
31
|
|
32
|
32
|
struct state_t {
|
33
|
33
|
tone_t tone;
|
34
|
|
- uint16_t period;
|
35
|
|
- uint16_t counter;
|
|
34
|
+ millis_t next;
|
36
|
35
|
} state;
|
37
|
36
|
|
38
|
37
|
protected:
|
|
@@ -42,8 +41,7 @@ class Speaker: public Buzzer {
|
42
|
41
|
*/
|
43
|
42
|
void reset() {
|
44
|
43
|
super::reset();
|
45
|
|
- this->state.period = 0;
|
46
|
|
- this->state.counter = 0;
|
|
44
|
+ this->state.next = 0;
|
47
|
45
|
}
|
48
|
46
|
|
49
|
47
|
public:
|
|
@@ -60,29 +58,15 @@ class Speaker: public Buzzer {
|
60
|
58
|
* playing the tones in the queue.
|
61
|
59
|
*/
|
62
|
60
|
virtual void tick() {
|
63
|
|
- if (!this->state.counter) {
|
|
61
|
+ const uint32_t now = millis();
|
|
62
|
+
|
|
63
|
+ if (now >= this->state.next) {
|
64
|
64
|
if (this->buffer.isEmpty()) return;
|
65
|
65
|
|
66
|
66
|
this->reset();
|
67
|
67
|
this->state.tone = this->buffer.dequeue();
|
68
|
|
-
|
69
|
|
- // Period is uint16, min frequency will be ~16Hz
|
70
|
|
- this->state.period = 1000000UL / this->state.tone.frequency;
|
71
|
|
-
|
72
|
|
- this->state.counter =
|
73
|
|
- (this->state.tone.duration * 1000L) / this->state.period;
|
74
|
|
-
|
75
|
|
- this->state.period >>= 1;
|
76
|
|
- this->state.counter <<= 1;
|
77
|
|
- } else {
|
78
|
|
- const uint32_t now = micros();
|
79
|
|
- static uint32_t next = now + this->state.period;
|
80
|
|
-
|
81
|
|
- if (now >= next) {
|
82
|
|
- --this->state.counter;
|
83
|
|
- next = now + this->state.period;
|
84
|
|
- if (this->state.tone.frequency > 0) this->invert();
|
85
|
|
- }
|
|
68
|
+ this->state.next = now + this->state.tone.duration;
|
|
69
|
+ ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
|
86
|
70
|
}
|
87
|
71
|
}
|
88
|
72
|
};
|