Explorar el Código

Non-blocking speaker now uses arduino's tone()

João Brázio hace 8 años
padre
commit
8093c5f534
Se han modificado 1 ficheros con 7 adiciones y 23 borrados
  1. 7
    23
      Marlin/speaker.h

+ 7
- 23
Marlin/speaker.h Ver fichero

31
 
31
 
32
     struct state_t {
32
     struct state_t {
33
       tone_t   tone;
33
       tone_t   tone;
34
-      uint16_t period;
35
-      uint16_t counter;
34
+      millis_t next;
36
     } state;
35
     } state;
37
 
36
 
38
   protected:
37
   protected:
42
      */
41
      */
43
     void reset() {
42
     void reset() {
44
       super::reset();
43
       super::reset();
45
-      this->state.period = 0;
46
-      this->state.counter = 0;
44
+      this->state.next = 0;
47
     }
45
     }
48
 
46
 
49
   public:
47
   public:
60
      * playing the tones in the queue.
58
      * playing the tones in the queue.
61
      */
59
      */
62
     virtual void tick() {
60
     virtual void tick() {
63
-      if (!this->state.counter) {
61
+      const uint32_t now = millis();
62
+
63
+      if (now >= this->state.next) {
64
         if (this->buffer.isEmpty()) return;
64
         if (this->buffer.isEmpty()) return;
65
 
65
 
66
         this->reset();
66
         this->reset();
67
         this->state.tone = this->buffer.dequeue();
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
-        }
68
+        this->state.next = now + this->state.tone.duration;
69
+        ::tone(BEEPER_PIN, this->state.tone.frequency, this->state.tone.duration);
86
       }
70
       }
87
     }
71
     }
88
 };
72
 };

Loading…
Cancelar
Guardar