2016-03-25 01:19:46 -05:00
|
|
|
/**
|
2016-03-24 13:01:20 -05:00
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-06-04 14:29:01 -05:00
|
|
|
#ifndef __BUZZER_H__
|
|
|
|
#define __BUZZER_H__
|
2015-06-17 09:31:14 -05:00
|
|
|
|
2016-06-04 14:29:01 -05:00
|
|
|
#include "fastio.h"
|
|
|
|
#include "circularqueue.h"
|
2016-06-11 17:34:31 -05:00
|
|
|
#include "temperature.h"
|
2015-06-17 09:31:14 -05:00
|
|
|
|
2016-06-04 14:29:01 -05:00
|
|
|
#define TONE_QUEUE_LENGTH 4
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tone structure
|
2016-07-27 05:40:44 -05:00
|
|
|
* @details Simple abstraction of a tone based on a duration and a frequency.
|
2016-06-04 14:29:01 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct tone_t {
|
|
|
|
uint16_t duration;
|
|
|
|
uint16_t frequency;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Buzzer class
|
|
|
|
*/
|
|
|
|
class Buzzer {
|
|
|
|
private:
|
|
|
|
struct state_t {
|
|
|
|
tone_t tone;
|
2016-07-10 21:36:46 -05:00
|
|
|
uint32_t endtime;
|
2016-06-04 14:29:01 -05:00
|
|
|
} state;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Inverts the sate of a digital PIN
|
|
|
|
* @details This will invert the current state of an digital IO pin.
|
|
|
|
*/
|
|
|
|
void invert() {
|
2016-07-27 05:40:44 -05:00
|
|
|
TOGGLE(BEEPER_PIN);
|
2016-06-04 14:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Turn off a digital PIN
|
|
|
|
* @details Alias of digitalWrite(PIN, LOW) using FastIO
|
|
|
|
*/
|
|
|
|
void off() {
|
|
|
|
WRITE(BEEPER_PIN, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Turn on a digital PIN
|
|
|
|
* @details Alias of digitalWrite(PIN, HIGH) using FastIO
|
|
|
|
*/
|
|
|
|
void on() {
|
|
|
|
WRITE(BEEPER_PIN, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resets the state of the class
|
|
|
|
* @details Brings the class state to a known one.
|
|
|
|
*/
|
|
|
|
void reset() {
|
|
|
|
this->off();
|
2016-07-10 21:36:46 -05:00
|
|
|
this->state.endtime = 0;
|
2016-06-04 14:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Class constructor
|
|
|
|
*/
|
|
|
|
Buzzer() {
|
|
|
|
SET_OUTPUT(BEEPER_PIN);
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a tone to the queue
|
|
|
|
* @details Adds a tone_t structure to the ring buffer, will block IO if the
|
2016-07-10 21:36:46 -05:00
|
|
|
* queue is full waiting for one slot to get available.
|
2016-06-04 14:29:01 -05:00
|
|
|
*
|
|
|
|
* @param duration Duration of the tone in milliseconds
|
|
|
|
* @param frequency Frequency of the tone in hertz
|
|
|
|
*/
|
|
|
|
void tone(uint16_t const &duration, uint16_t const &frequency = 0) {
|
|
|
|
while (buffer.isFull()) {
|
|
|
|
this->tick();
|
2016-06-11 17:34:31 -05:00
|
|
|
thermalManager.manage_heater();
|
2016-06-04 14:29:01 -05:00
|
|
|
}
|
|
|
|
this->buffer.enqueue((tone_t) { duration, frequency });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Loop function
|
|
|
|
* @details This function should be called at loop, it will take care of
|
2016-07-10 21:36:46 -05:00
|
|
|
* playing the tones in the queue.
|
2016-06-04 14:29:01 -05:00
|
|
|
*/
|
|
|
|
virtual void tick() {
|
2016-07-10 21:36:46 -05:00
|
|
|
if (!this->state.endtime) {
|
2016-06-04 14:29:01 -05:00
|
|
|
if (this->buffer.isEmpty()) return;
|
|
|
|
|
|
|
|
this->state.tone = this->buffer.dequeue();
|
2016-07-10 21:36:46 -05:00
|
|
|
this->state.endtime = millis() + this->state.tone.duration;
|
2016-06-04 14:29:01 -05:00
|
|
|
if (this->state.tone.frequency > 0) this->on();
|
|
|
|
}
|
2016-07-10 21:36:46 -05:00
|
|
|
else if (ELAPSED(millis(), this->state.endtime)) this->reset();
|
2016-06-04 14:29:01 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|