My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

buzzer.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef __BUZZER_H__
  23. #define __BUZZER_H__
  24. #include "types.h"
  25. #include "fastio.h"
  26. #include "circularqueue.h"
  27. #include "temperature.h"
  28. #include "MarlinConfig.h"
  29. #define TONE_QUEUE_LENGTH 4
  30. /**
  31. * @brief Tone structure
  32. * @details Simple abstraction of a tone based on a duration and a frequency.
  33. */
  34. struct tone_t {
  35. uint16_t duration;
  36. uint16_t frequency;
  37. };
  38. /**
  39. * @brief Buzzer class
  40. */
  41. class Buzzer {
  42. private:
  43. struct state_t {
  44. tone_t tone;
  45. uint32_t endtime;
  46. } state;
  47. protected:
  48. CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
  49. /**
  50. * @brief Inverts the sate of a digital PIN
  51. * @details This will invert the current state of an digital IO pin.
  52. */
  53. void invert() {
  54. TOGGLE(BEEPER_PIN);
  55. }
  56. /**
  57. * @brief Turn off a digital PIN
  58. * @details Alias of digitalWrite(PIN, LOW) using FastIO
  59. */
  60. void off() {
  61. WRITE(BEEPER_PIN, LOW);
  62. }
  63. /**
  64. * @brief Turn on a digital PIN
  65. * @details Alias of digitalWrite(PIN, HIGH) using FastIO
  66. */
  67. void on() {
  68. WRITE(BEEPER_PIN, HIGH);
  69. }
  70. /**
  71. * @brief Resets the state of the class
  72. * @details Brings the class state to a known one.
  73. */
  74. void reset() {
  75. this->off();
  76. this->state.endtime = 0;
  77. }
  78. public:
  79. /**
  80. * @brief Class constructor
  81. */
  82. Buzzer() {
  83. SET_OUTPUT(BEEPER_PIN);
  84. this->reset();
  85. }
  86. /**
  87. * @brief Add a tone to the queue
  88. * @details Adds a tone_t structure to the ring buffer, will block IO if the
  89. * queue is full waiting for one slot to get available.
  90. *
  91. * @param duration Duration of the tone in milliseconds
  92. * @param frequency Frequency of the tone in hertz
  93. */
  94. void tone(const uint16_t &duration, const uint16_t &frequency = 0) {
  95. while (buffer.isFull()) {
  96. this->tick();
  97. thermalManager.manage_heater();
  98. }
  99. tone_t tone = { duration, frequency };
  100. this->buffer.enqueue(tone);
  101. }
  102. /**
  103. * @brief Loop function
  104. * @details This function should be called at loop, it will take care of
  105. * playing the tones in the queue.
  106. */
  107. virtual void tick() {
  108. const millis_t now = millis();
  109. if (!this->state.endtime) {
  110. if (this->buffer.isEmpty()) return;
  111. this->state.tone = this->buffer.dequeue();
  112. this->state.endtime = now + this->state.tone.duration;
  113. if (this->state.tone.frequency > 0) {
  114. #if ENABLED(SPEAKER)
  115. CRITICAL_SECTION_START;
  116. ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
  117. CRITICAL_SECTION_END;
  118. #else
  119. this->on();
  120. #endif
  121. }
  122. }
  123. else if (ELAPSED(now, this->state.endtime)) this->reset();
  124. }
  125. };
  126. extern Buzzer buzzer;
  127. #endif