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

buzzer.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "fastio.h"
  25. #include "circularqueue.h"
  26. #include "temperature.h"
  27. #define TONE_QUEUE_LENGTH 4
  28. /**
  29. * @brief Tone structure
  30. * @details Simple abstraction of a tone based on a duration and a frequency.
  31. */
  32. struct tone_t {
  33. uint16_t duration;
  34. uint16_t frequency;
  35. };
  36. /**
  37. * @brief Buzzer class
  38. */
  39. class Buzzer {
  40. private:
  41. struct state_t {
  42. tone_t tone;
  43. uint32_t endtime;
  44. } state;
  45. protected:
  46. CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
  47. /**
  48. * @brief Inverts the sate of a digital PIN
  49. * @details This will invert the current state of an digital IO pin.
  50. */
  51. void invert() {
  52. TOGGLE(BEEPER_PIN);
  53. }
  54. /**
  55. * @brief Turn off a digital PIN
  56. * @details Alias of digitalWrite(PIN, LOW) using FastIO
  57. */
  58. void off() {
  59. WRITE(BEEPER_PIN, LOW);
  60. }
  61. /**
  62. * @brief Turn on a digital PIN
  63. * @details Alias of digitalWrite(PIN, HIGH) using FastIO
  64. */
  65. void on() {
  66. WRITE(BEEPER_PIN, HIGH);
  67. }
  68. /**
  69. * @brief Resets the state of the class
  70. * @details Brings the class state to a known one.
  71. */
  72. void reset() {
  73. this->off();
  74. this->state.endtime = 0;
  75. }
  76. public:
  77. /**
  78. * @brief Class constructor
  79. */
  80. Buzzer() {
  81. SET_OUTPUT(BEEPER_PIN);
  82. this->reset();
  83. }
  84. /**
  85. * @brief Add a tone to the queue
  86. * @details Adds a tone_t structure to the ring buffer, will block IO if the
  87. * queue is full waiting for one slot to get available.
  88. *
  89. * @param duration Duration of the tone in milliseconds
  90. * @param frequency Frequency of the tone in hertz
  91. */
  92. void tone(uint16_t const &duration, uint16_t const &frequency = 0) {
  93. while (buffer.isFull()) {
  94. this->tick();
  95. thermalManager.manage_heater();
  96. }
  97. this->buffer.enqueue((tone_t) { duration, frequency });
  98. }
  99. /**
  100. * @brief Loop function
  101. * @details This function should be called at loop, it will take care of
  102. * playing the tones in the queue.
  103. */
  104. virtual void tick() {
  105. const millis_t now = millis();
  106. if (!this->state.endtime) {
  107. if (this->buffer.isEmpty()) return;
  108. this->state.tone = this->buffer.dequeue();
  109. this->state.endtime = now + this->state.tone.duration;
  110. if (this->state.tone.frequency > 0) {
  111. #if ENABLED(SPEAKER)
  112. ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
  113. #else
  114. this->on();
  115. #endif
  116. }
  117. }
  118. else if (ELAPSED(now, this->state.endtime)) this->reset();
  119. }
  120. };
  121. #endif