My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

buzzer.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #if HAS_BEEPER
  25. #include "circularqueue.h"
  26. #define TONE_QUEUE_LENGTH 4
  27. /**
  28. * @brief Tone structure
  29. * @details Simple abstraction of a tone based on a duration and a frequency.
  30. */
  31. struct tone_t {
  32. uint16_t duration;
  33. uint16_t frequency;
  34. };
  35. /**
  36. * @brief Buzzer class
  37. */
  38. class Buzzer {
  39. public:
  40. typedef struct {
  41. tone_t tone;
  42. uint32_t endtime;
  43. } state_t;
  44. private:
  45. static state_t state;
  46. protected:
  47. static CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
  48. /**
  49. * @brief Inverts the state of a digital PIN
  50. * @details This will invert the current state of an digital IO pin.
  51. */
  52. FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
  53. /**
  54. * @brief Resets the state of the class
  55. * @details Brings the class state to a known one.
  56. */
  57. static void reset() {
  58. off();
  59. state.endtime = 0;
  60. }
  61. public:
  62. /**
  63. * @brief Init Buzzer
  64. */
  65. static void init() {
  66. SET_OUTPUT(BEEPER_PIN);
  67. reset();
  68. }
  69. /**
  70. * @brief Turn on a digital PIN
  71. * @details Alias of digitalWrite(PIN, HIGH) using FastIO
  72. */
  73. FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
  74. /**
  75. * @brief Turn off a digital PIN
  76. * @details Alias of digitalWrite(PIN, LOW) using FastIO
  77. */
  78. FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
  79. static void click(const uint16_t duration) { on(); delay(duration); off(); }
  80. /**
  81. * @brief Add a tone to the queue
  82. * @details Adds a tone_t structure to the ring buffer, will block IO if the
  83. * queue is full waiting for one slot to get available.
  84. *
  85. * @param duration Duration of the tone in milliseconds
  86. * @param frequency Frequency of the tone in hertz
  87. */
  88. static void tone(const uint16_t duration, const uint16_t frequency=0);
  89. /**
  90. * @brief Tick function
  91. * @details This function should be called at loop, it will take care of
  92. * playing the tones in the queue.
  93. */
  94. static void tick();
  95. };
  96. // Provide a buzzer instance
  97. extern Buzzer buzzer;
  98. // Buzz directly via the BEEPER pin tone queue
  99. #define BUZZ(d,f) buzzer.tone(d, f)
  100. #elif USE_MARLINUI_BUZZER
  101. // Use MarlinUI for a buzzer on the LCD
  102. #include "../lcd/marlinui.h"
  103. #define BUZZ(d,f) ui.buzz(d,f)
  104. #else
  105. // No buzz capability
  106. #define BUZZ(d,f) NOOP
  107. #endif
  108. #define ERR_BUZZ() BUZZ(400, 40);
  109. #define OKAY_BUZZ() do{ BUZZ(100, 659); BUZZ(10, 0); BUZZ(100, 698); }while(0)
  110. #define DONE_BUZZ(OK) do{ if (OK) OKAY_BUZZ(); else ERR_BUZZ(); }while(0)