Procházet zdrojové kódy

Merge pull request #4456 from jbrazio/speaker-type3

Improvement to non-blocking speaker
Scott Lahteine před 8 roky
rodič
revize
8e2f095dde
4 změnil soubory, kde provedl 14 přidání a 105 odebrání
  1. 2
    7
      Marlin/Marlin.h
  2. 0
    4
      Marlin/Marlin_main.cpp
  3. 12
    4
      Marlin/buzzer.h
  4. 0
    90
      Marlin/speaker.h

+ 2
- 7
Marlin/Marlin.h Zobrazit soubor

@@ -385,13 +385,8 @@ void calculate_volumetric_multipliers();
385 385
 
386 386
 // Buzzer
387 387
 #if HAS_BUZZER
388
-  #if ENABLED(SPEAKER)
389
-    #include "speaker.h"
390
-    extern Speaker buzzer;
391
-  #else
392
-    #include "buzzer.h"
393
-    extern Buzzer buzzer;
394
-  #endif
388
+  #include "buzzer.h"
389
+  extern Buzzer buzzer;
395 390
 #endif
396 391
 
397 392
 /**

+ 0
- 4
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -375,11 +375,7 @@ static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL
375 375
 
376 376
 // Buzzer
377 377
 #if HAS_BUZZER
378
-  #if ENABLED(SPEAKER)
379
-    Speaker buzzer;
380
-  #else
381 378
     Buzzer buzzer;
382
-  #endif
383 379
 #endif
384 380
 
385 381
 static uint8_t target_extruder;

+ 12
- 4
Marlin/buzzer.h Zobrazit soubor

@@ -32,7 +32,6 @@
32 32
 /**
33 33
  * @brief Tone structure
34 34
  * @details Simple abstraction of a tone based on a duration and a frequency.
35
- *
36 35
  */
37 36
 struct tone_t {
38 37
   uint16_t duration;
@@ -116,14 +115,23 @@ class Buzzer {
116 115
      *          playing the tones in the queue.
117 116
      */
118 117
     virtual void tick() {
118
+      const millis_t now = millis();
119
+
119 120
       if (!this->state.endtime) {
120 121
         if (this->buffer.isEmpty()) return;
121 122
 
122 123
         this->state.tone = this->buffer.dequeue();
123
-        this->state.endtime = millis() + this->state.tone.duration;
124
-        if (this->state.tone.frequency > 0) this->on();
124
+        this->state.endtime = now + this->state.tone.duration;
125
+
126
+        if (this->state.tone.frequency > 0) {
127
+          #if ENABLED(SPEAKER)
128
+            ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
129
+          #else
130
+            this->on();
131
+          #endif
132
+        }
125 133
       }
126
-      else if (ELAPSED(millis(), this->state.endtime)) this->reset();
134
+      else if (ELAPSED(now, this->state.endtime)) this->reset();
127 135
     }
128 136
 };
129 137
 

+ 0
- 90
Marlin/speaker.h Zobrazit soubor

@@ -1,90 +0,0 @@
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
-#ifndef __SPEAKER_H__
24
-#define __SPEAKER_H__
25
-
26
-#include "buzzer.h"
27
-
28
-class Speaker: public Buzzer {
29
-  private:
30
-    typedef Buzzer super;
31
-
32
-    struct state_t {
33
-      tone_t   tone;
34
-      uint16_t period;
35
-      uint16_t counter;
36
-    } state;
37
-
38
-  protected:
39
-    /**
40
-     * @brief Resets the state of the class
41
-     * @details Brings the class state to a known one.
42
-     */
43
-    void reset() {
44
-      super::reset();
45
-      this->state.period = 0;
46
-      this->state.counter = 0;
47
-    }
48
-
49
-  public:
50
-    /**
51
-     * @brief Class constructor
52
-     */
53
-    Speaker() {
54
-      this->reset();
55
-    }
56
-
57
-    /**
58
-     * @brief Loop function
59
-     * @details This function should be called at loop, it will take care of
60
-     * playing the tones in the queue.
61
-     */
62
-    virtual void tick() {
63
-      if (!this->state.counter) {
64
-        if (this->buffer.isEmpty()) return;
65
-
66
-        this->reset();
67
-        this->state.tone = this->buffer.dequeue();
68
-
69
-        // Period is uint16, min frequency will be ~16Hz
70
-        this->state.period = 1000000UL / this->state.tone.frequency;
71
-
72
-        this->state.counter =
73
-          (this->state.tone.duration * 1000L) / this->state.period;
74
-
75
-        this->state.period   >>= 1;
76
-        this->state.counter <<= 1;
77
-      } else {
78
-        const  uint32_t  now = micros();
79
-        static uint32_t next = now + this->state.period;
80
-
81
-        if (now >= next) {
82
-          --this->state.counter;
83
-          next = now + this->state.period;
84
-          if (this->state.tone.frequency > 0) this->invert();
85
-        }
86
-      }
87
-    }
88
-};
89
-
90
-#endif

Loading…
Zrušit
Uložit