Prusa MINI Firmware overview
buzzer.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 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 #pragma once
23 
24 #include "../inc/MarlinConfig.h"
25 
26 #if USE_BEEPER
27 
28  #include "circularqueue.h"
29 
30  #define TONE_QUEUE_LENGTH 4
31 
32  /**
33  * @brief Tone structure
34  * @details Simple abstraction of a tone based on a duration and a frequency.
35  */
36  struct tone_t {
37  uint16_t duration;
38  uint16_t frequency;
39  };
40 
41  /**
42  * @brief Buzzer class
43  */
44  class Buzzer {
45  public:
46 
47  typedef struct {
48  tone_t tone;
49  uint32_t endtime;
50  } state_t;
51 
52  private:
53  static state_t state;
54 
55  protected:
57 
58  /**
59  * @brief Inverts the sate of a digital PIN
60  * @details This will invert the current state of an digital IO pin.
61  */
62  FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
63 
64  /**
65  * @brief Turn off a digital PIN
66  * @details Alias of digitalWrite(PIN, LOW) using FastIO
67  */
68  FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
69 
70  /**
71  * @brief Turn on a digital PIN
72  * @details Alias of digitalWrite(PIN, HIGH) using FastIO
73  */
74  FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
75 
76  /**
77  * @brief Resets the state of the class
78  * @details Brings the class state to a known one.
79  */
80  static inline void reset() {
81  off();
82  state.endtime = 0;
83  }
84 
85  public:
86  /**
87  * @brief Class constructor
88  */
89  Buzzer() {
91  reset();
92  }
93 
94  /**
95  * @brief Add a tone to the queue
96  * @details Adds a tone_t structure to the ring buffer, will block IO if the
97  * queue is full waiting for one slot to get available.
98  *
99  * @param duration Duration of the tone in milliseconds
100  * @param frequency Frequency of the tone in hertz
101  */
102  static void tone(const uint16_t duration, const uint16_t frequency=0);
103 
104  /**
105  * @brief Tick function
106  * @details This function should be called at loop, it will take care of
107  * playing the tones in the queue.
108  */
109  static void tick();
110  };
111 
112  // Provide a buzzer instance
113  extern Buzzer buzzer;
114 
115  // Buzz directly via the BEEPER pin tone queue
116  #define BUZZ(d,f) buzzer.tone(d, f)
117 
118 #elif HAS_BUZZER
119 
120  // Buzz indirectly via the MarlinUI instance
121  #define BUZZ(d,f) ui.buzz(d,f)
122 
123 #else
124 
125  // No buzz capability
126  #define BUZZ(d,f) NOOP
127 
128 #endif
ExtUI::onPlayTone
void onPlayTone(const uint16_t frequency, const uint16_t duration)
Definition: marlin_server.cpp:874
LOW
#define LOW
Definition: wiring_constants.h:70
circularqueue.h
state
static volatile fsensor_t state
Definition: filament_sensor.c:23
millis
uint32_t millis(void)
Definition: wiring_time.c:29
SET_OUTPUT
#define SET_OUTPUT(IO)
Definition: fastio.h:101
Temperature::manage_heater
static void manage_heater() _O2
Definition: temperature.cpp:975
buzzer.h
tone
void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0)
FORCE_INLINE
#define FORCE_INLINE
Definition: macros.h:40
CRITICAL_SECTION_END
#define CRITICAL_SECTION_END
Definition: HAL.h:49
WRITE
#define WRITE(IO, V)
Definition: fastio.h:96
TOGGLE
#define TOGGLE(IO)
Definition: fastio.h:97
ELAPSED
#define ELAPSED(NOW, SOON)
Definition: millis_t.h:29
CircularQueue
Circular Queue class.
Definition: circularqueue.h:31
HIGH
#define HIGH
Definition: wiring_constants.h:71
CRITICAL_SECTION_START
#define CRITICAL_SECTION_START
Definition: HAL.h:48
BEEPER_PIN
#define BEEPER_PIN
Definition: pins_CHEAPTRONICv2.h:125
thermalManager
Temperature thermalManager
Definition: temperature.cpp:89
millis_t
uint32_t millis_t
Definition: millis_t.h:26