A little cleanup at speaker.h

master
João Brázio 8 years ago
parent 8684570f68
commit 909e98b1af

@ -104,7 +104,6 @@ class Buzzer {
*/ */
void tone(uint16_t const &duration, uint16_t const &frequency = 0) { void tone(uint16_t const &duration, uint16_t const &frequency = 0) {
while (buffer.isFull()) { while (buffer.isFull()) {
delay(5);
this->tick(); this->tick();
thermalManager.manage_heater(); thermalManager.manage_heater();
} }

@ -32,7 +32,7 @@ class Speaker: public Buzzer {
struct state_t { struct state_t {
tone_t tone; tone_t tone;
uint16_t period; uint16_t period;
uint16_t cycles; uint16_t counter;
} state; } state;
protected: protected:
@ -43,7 +43,7 @@ class Speaker: public Buzzer {
void reset() { void reset() {
super::reset(); super::reset();
this->state.period = 0; this->state.period = 0;
this->state.cycles = 0; this->state.counter = 0;
} }
public: public:
@ -60,7 +60,7 @@ class Speaker: public Buzzer {
* playing the tones in the queue. * playing the tones in the queue.
*/ */
virtual void tick() { virtual void tick() {
if (!this->state.cycles) { if (!this->state.counter) {
if (this->buffer.isEmpty()) return; if (this->buffer.isEmpty()) return;
this->reset(); this->reset();
@ -69,20 +69,18 @@ class Speaker: public Buzzer {
// Period is uint16, min frequency will be ~16Hz // Period is uint16, min frequency will be ~16Hz
this->state.period = 1000000UL / this->state.tone.frequency; this->state.period = 1000000UL / this->state.tone.frequency;
this->state.cycles = this->state.counter =
(this->state.tone.duration * 1000L) / this->state.period; (this->state.tone.counter * 1000L) / this->state.period;
this->state.period >>= 1; this->state.period >>= 1;
this->state.cycles <<= 1; this->state.counter <<= 1;
} else {
const uint32_t now = micros();
static uint32_t next = now + this->state.period;
} if (now >= next) {
else { --this->state.counter;
uint32_t const us = micros(); next = now + this->state.period;
static uint32_t next = us + this->state.period;
if (us >= next) {
--this->state.cycles;
next = us + this->state.period;
if (this->state.tone.frequency > 0) this->invert(); if (this->state.tone.frequency > 0) this->invert();
} }
} }

Loading…
Cancel
Save