My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buzzer.h 3.6KB

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