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