Firmware2/Marlin/src/libs/buzzer.cpp

85 lines
2.4 KiB
C++
Raw Normal View History

2017-09-14 22:33:07 +02:00
/**
* Marlin 3D Printer Firmware
2020-02-03 15:00:57 +01:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2017-09-14 22:33:07 +02:00
*
* Based on Sprinter and grbl.
2019-06-28 06:57:50 +02:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2017-09-14 22:33:07 +02:00
*
* 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
2020-07-23 05:20:14 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-09-14 22:33:07 +02:00
*
*/
#include "../inc/MarlinConfig.h"
#if USE_BEEPER
2017-09-14 22:33:07 +02:00
#include "buzzer.h"
#include "../module/temperature.h"
#include "../lcd/marlinui.h"
2017-09-14 22:33:07 +02:00
2018-10-08 22:44:05 +02:00
#if ENABLED(EXTENSIBLE_UI)
2020-03-13 22:29:29 +01:00
#include "../lcd/extui/ui_api.h"
2018-10-08 22:44:05 +02:00
#endif
2017-09-14 22:33:07 +02:00
Buzzer::state_t Buzzer::state;
CircularQueue<tone_t, TONE_QUEUE_LENGTH> Buzzer::buffer;
Buzzer buzzer;
/**
* @brief Add a tone to the queue
* @details Adds a tone_t structure to the ring buffer, will block IO if the
* queue is full waiting for one slot to get available.
*
* @param duration Duration of the tone in milliseconds
* @param frequency Frequency of the tone in hertz
*/
void Buzzer::tone(const uint16_t duration, const uint16_t frequency/*=0*/) {
if (!ui.buzzer_enabled) return;
2017-09-14 22:33:07 +02:00
while (buffer.isFull()) {
tick();
thermalManager.manage_heater();
}
tone_t tone = { duration, frequency };
buffer.enqueue(tone);
}
void Buzzer::tick() {
if (!ui.buzzer_enabled) return;
2017-09-14 22:33:07 +02:00
const millis_t now = millis();
if (!state.endtime) {
if (buffer.isEmpty()) return;
state.tone = buffer.dequeue();
state.endtime = now + state.tone.duration;
if (state.tone.frequency > 0) {
#if ENABLED(EXTENSIBLE_UI) && DISABLED(EXTUI_LOCAL_BEEPER)
2020-02-11 08:13:02 +01:00
CRITICAL_SECTION_START();
ExtUI::onPlayTone(state.tone.frequency, state.tone.duration);
2020-02-11 08:13:02 +01:00
CRITICAL_SECTION_END();
#elif ENABLED(SPEAKER)
2020-02-11 08:13:02 +01:00
CRITICAL_SECTION_START();
2017-09-14 22:33:07 +02:00
::tone(BEEPER_PIN, state.tone.frequency, state.tone.duration);
2020-02-11 08:13:02 +01:00
CRITICAL_SECTION_END();
2017-09-14 22:33:07 +02:00
#else
on();
#endif
}
}
else if (ELAPSED(now, state.endtime)) reset();
}
#endif // USE_BEEPER