Consolidates Buzzer and Speaker into a single object

This commit is contained in:
João Brázio
2016-07-29 17:39:07 +01:00
parent 8093c5f534
commit 2b5faa61e2
4 changed files with 14 additions and 89 deletions

View File

@@ -32,7 +32,6 @@
/**
* @brief Tone structure
* @details Simple abstraction of a tone based on a duration and a frequency.
*
*/
struct tone_t {
uint16_t duration;
@@ -116,14 +115,23 @@ class Buzzer {
* playing the tones in the queue.
*/
virtual void tick() {
const millis_t now = millis();
if (!this->state.endtime) {
if (this->buffer.isEmpty()) return;
this->state.tone = this->buffer.dequeue();
this->state.endtime = millis() + this->state.tone.duration;
if (this->state.tone.frequency > 0) this->on();
this->state.endtime = now + this->state.tone.duration;
if (this->state.tone.frequency > 0) {
#if ENABLED(SPEAKER)
::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
#else
this->on();
#endif
}
}
else if (ELAPSED(millis(), this->state.endtime)) this->reset();
else if (ELAPSED(now, this->state.endtime)) this->reset();
}
};