Browse Source

Consolidate Buzzer

Scott Lahteine 7 years ago
parent
commit
e0a189a481
5 changed files with 119 additions and 77 deletions
  1. 4
    14
      Marlin/src/Marlin.cpp
  2. 7
    10
      Marlin/src/lcd/ultralcd.cpp
  3. 1
    1
      Marlin/src/lcd/ultralcd.h
  4. 73
    0
      Marlin/src/libs/buzzer.cpp
  5. 34
    52
      Marlin/src/libs/buzzer.h

+ 4
- 14
Marlin/src/Marlin.cpp View File

@@ -258,6 +258,10 @@
258 258
 #include "libs/duration_t.h"
259 259
 #include "gcode/parser.h"
260 260
 
261
+#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
262
+  #include "libs/buzzer.h"
263
+#endif
264
+
261 265
 #if HAS_ABL
262 266
   #include "libs/vector_3.h"
263 267
   #if ENABLED(AUTO_BED_LEVELING_LINEAR)
@@ -271,10 +275,6 @@
271 275
   #include "module/planner_bezier.h"
272 276
 #endif
273 277
 
274
-#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
275
-  #include "libs/buzzer.h"
276
-#endif
277
-
278 278
 #if ENABLED(MAX7219_DEBUG)
279 279
   #include "feature/leds/Max7219_Debug_LEDs.h"
280 280
 #endif
@@ -495,16 +495,6 @@ static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL
495 495
   Stopwatch print_job_timer = Stopwatch();
496 496
 #endif
497 497
 
498
-// Buzzer - I2C on the LCD or a BEEPER_PIN
499
-#if ENABLED(LCD_USE_I2C_BUZZER)
500
-  #define BUZZ(d,f) lcd_buzz(d, f)
501
-#elif PIN_EXISTS(BEEPER)
502
-  Buzzer buzzer;
503
-  #define BUZZ(d,f) buzzer.tone(d, f)
504
-#else
505
-  #define BUZZ(d,f) NOOP
506
-#endif
507
-
508 498
 static uint8_t target_extruder;
509 499
 
510 500
 #if HAS_BED_PROBE

+ 7
- 10
Marlin/src/lcd/ultralcd.cpp View File

@@ -34,10 +34,6 @@
34 34
 
35 35
 #include "../Marlin.h"
36 36
 
37
-#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
38
-  #include "../libs/buzzer.h"
39
-#endif
40
-
41 37
 #if ENABLED(PRINTCOUNTER)
42 38
   #include "../module/printcounter.h"
43 39
   #include "../libs/duration_t.h"
@@ -59,6 +55,11 @@
59 55
   #include "../feature/bedlevel/bedlevel.h"
60 56
 #endif
61 57
 
58
+// For i2c define BUZZ to use lcd_buzz
59
+#if DISABLED(LCD_USE_I2C_BUZZER)
60
+  #include "../libs/buzzer.h"
61
+#endif
62
+
62 63
 // Initialized by settings.load()
63 64
 int16_t lcd_preheat_hotend_temp[2], lcd_preheat_bed_temp[2], lcd_preheat_fan_speed[2];
64 65
 
@@ -679,7 +680,7 @@ void kill_screen(const char* lcd_msg) {
679 680
    * Audio feedback for controller clicks
680 681
    *
681 682
    */
682
-  void lcd_buzz(long duration, uint16_t freq) {
683
+  void lcd_buzz(const long duration, const uint16_t freq) {
683 684
     #if ENABLED(LCD_USE_I2C_BUZZER)
684 685
       lcd.buzz(duration, freq);
685 686
     #elif PIN_EXISTS(BEEPER)
@@ -4803,11 +4804,7 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
4803 4804
   #if ENABLED(AUTO_BED_LEVELING_UBL)
4804 4805
 
4805 4806
     void chirp_at_user() {
4806
-      #if ENABLED(LCD_USE_I2C_BUZZER)
4807
-        lcd.buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
4808
-      #elif PIN_EXISTS(BEEPER)
4809
-        buzzer.tone(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
4810
-      #endif
4807
+      lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
4811 4808
     }
4812 4809
 
4813 4810
     bool ubl_lcd_clicked() { return LCD_CLICKED; }

+ 1
- 1
Marlin/src/lcd/ultralcd.h View File

@@ -56,7 +56,7 @@
56 56
   inline void lcd_refresh() { lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; }
57 57
 
58 58
   #if HAS_BUZZER
59
-    void lcd_buzz(long duration, uint16_t freq);
59
+    void lcd_buzz(const long duration, const uint16_t freq);
60 60
   #endif
61 61
 
62 62
   #if ENABLED(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0

+ 73
- 0
Marlin/src/libs/buzzer.cpp View File

@@ -0,0 +1,73 @@
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
+
23
+#include "../inc/MarlinConfig.h"
24
+
25
+#if DISABLED(LCD_USE_I2C_BUZZER) && PIN_EXISTS(BEEPER)
26
+
27
+#include "buzzer.h"
28
+#include "../module/temperature.h"
29
+
30
+Buzzer::state_t Buzzer::state;
31
+CircularQueue<tone_t, TONE_QUEUE_LENGTH> Buzzer::buffer;
32
+Buzzer buzzer;
33
+
34
+/**
35
+ * @brief Add a tone to the queue
36
+ * @details Adds a tone_t structure to the ring buffer, will block IO if the
37
+ *          queue is full waiting for one slot to get available.
38
+ *
39
+ * @param duration Duration of the tone in milliseconds
40
+ * @param frequency Frequency of the tone in hertz
41
+ */
42
+void Buzzer::tone(const uint16_t duration, const uint16_t frequency/*=0*/) {
43
+  while (buffer.isFull()) {
44
+    tick();
45
+    thermalManager.manage_heater();
46
+  }
47
+  tone_t tone = { duration, frequency };
48
+  buffer.enqueue(tone);
49
+}
50
+
51
+void Buzzer::tick() {
52
+  const millis_t now = millis();
53
+
54
+  if (!state.endtime) {
55
+    if (buffer.isEmpty()) return;
56
+
57
+    state.tone = buffer.dequeue();
58
+    state.endtime = now + state.tone.duration;
59
+
60
+    if (state.tone.frequency > 0) {
61
+      #if ENABLED(SPEAKER)
62
+        CRITICAL_SECTION_START;
63
+        ::tone(BEEPER_PIN, state.tone.frequency, state.tone.duration);
64
+        CRITICAL_SECTION_END;
65
+      #else
66
+        on();
67
+      #endif
68
+    }
69
+  }
70
+  else if (ELAPSED(now, state.endtime)) reset();
71
+}
72
+
73
+#endif // !LCD_USE_I2C_BUZZER && BEEPER

+ 34
- 52
Marlin/src/libs/buzzer.h View File

@@ -23,11 +23,14 @@
23 23
 #ifndef __BUZZER_H__
24 24
 #define __BUZZER_H__
25 25
 
26
-#include "types.h"
27
-#include "circularqueue.h"
28
-#include "temperature.h"
26
+#include "../inc/MarlinConfig.h"
27
+
28
+// Make a buzzer and macro
29
+#if ENABLED(LCD_USE_I2C_BUZZER)
30
+  // BUZZ() will be defined in ultralcd.h
31
+#elif PIN_EXISTS(BEEPER)
29 32
 
30
-#include "MarlinConfig.h"
33
+#include "circularqueue.h"
31 34
 
32 35
 #define TONE_QUEUE_LENGTH 4
33 36
 
@@ -44,46 +47,44 @@ struct tone_t {
44 47
  * @brief Buzzer class
45 48
  */
46 49
 class Buzzer {
47
-  private:
48
-    struct state_t {
50
+  public:
51
+
52
+    typedef struct {
49 53
       tone_t   tone;
50 54
       uint32_t endtime;
51
-    } state;
55
+    } state_t;
56
+
57
+  private:
58
+    static state_t state;
52 59
 
53 60
   protected:
54
-    CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
61
+    static CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
55 62
 
56 63
     /**
57 64
      * @brief Inverts the sate of a digital PIN
58 65
      * @details This will invert the current state of an digital IO pin.
59 66
      */
60
-    void invert() {
61
-      TOGGLE(BEEPER_PIN);
62
-    }
67
+    FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
63 68
 
64 69
     /**
65 70
      * @brief Turn off a digital PIN
66 71
      * @details Alias of digitalWrite(PIN, LOW) using FastIO
67 72
      */
68
-    void off() {
69
-      WRITE(BEEPER_PIN, LOW);
70
-    }
73
+    FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
71 74
 
72 75
     /**
73 76
      * @brief Turn on a digital PIN
74 77
      * @details Alias of digitalWrite(PIN, HIGH) using FastIO
75 78
      */
76
-    void on() {
77
-      WRITE(BEEPER_PIN, HIGH);
78
-    }
79
+    FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
79 80
 
80 81
     /**
81 82
      * @brief Resets the state of the class
82 83
      * @details Brings the class state to a known one.
83 84
      */
84
-    void reset() {
85
-      this->off();
86
-      this->state.endtime = 0;
85
+    inline static void reset() {
86
+      off();
87
+      state.endtime = 0;
87 88
     }
88 89
 
89 90
   public:
@@ -92,7 +93,7 @@ class Buzzer {
92 93
      */
93 94
     Buzzer() {
94 95
       SET_OUTPUT(BEEPER_PIN);
95
-      this->reset();
96
+      reset();
96 97
     }
97 98
 
98 99
     /**
@@ -103,43 +104,24 @@ class Buzzer {
103 104
      * @param duration Duration of the tone in milliseconds
104 105
      * @param frequency Frequency of the tone in hertz
105 106
      */
106
-    void tone(const uint16_t &duration, const uint16_t &frequency = 0) {
107
-      while (buffer.isFull()) {
108
-        this->tick();
109
-        thermalManager.manage_heater();
110
-      }
111
-      tone_t tone = { duration, frequency };
112
-      this->buffer.enqueue(tone);
113
-    }
107
+    static void tone(const uint16_t duration, const uint16_t frequency=0);
114 108
 
115 109
     /**
116
-     * @brief Loop function
110
+     * @brief Tick function
117 111
      * @details This function should be called at loop, it will take care of
118 112
      *          playing the tones in the queue.
119 113
      */
120
-    virtual void tick() {
121
-      const millis_t now = millis();
122
-
123
-      if (!this->state.endtime) {
124
-        if (this->buffer.isEmpty()) return;
125
-
126
-        this->state.tone = this->buffer.dequeue();
127
-        this->state.endtime = now + this->state.tone.duration;
128
-
129
-        if (this->state.tone.frequency > 0) {
130
-          #if ENABLED(SPEAKER)
131
-            CRITICAL_SECTION_START;
132
-            ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
133
-            CRITICAL_SECTION_END;
134
-          #else
135
-            this->on();
136
-          #endif
137
-        }
138
-      }
139
-      else if (ELAPSED(now, this->state.endtime)) this->reset();
140
-    }
114
+    static void tick();
141 115
 };
142 116
 
143
-extern Buzzer buzzer;
117
+  // Provide a buzzer instance
118
+  extern Buzzer buzzer;
119
+  #define BUZZ(d,f) buzzer.tone(d, f)
120
+
121
+#else // No buzz capability
122
+
123
+  #define BUZZ(d,f) NOOP
124
+
125
+#endif
144 126
 
145 127
 #endif

Loading…
Cancel
Save