Browse Source

A little cleanup at speaker.h

João Brázio 8 years ago
parent
commit
909e98b1af
2 changed files with 13 additions and 16 deletions
  1. 0
    1
      Marlin/buzzer.h
  2. 13
    15
      Marlin/speaker.h

+ 0
- 1
Marlin/buzzer.h View File

104
      */
104
      */
105
     void tone(uint16_t const &duration, uint16_t const &frequency = 0) {
105
     void tone(uint16_t const &duration, uint16_t const &frequency = 0) {
106
       while (buffer.isFull()) {
106
       while (buffer.isFull()) {
107
-        delay(5);
108
         this->tick();
107
         this->tick();
109
         thermalManager.manage_heater();
108
         thermalManager.manage_heater();
110
       }
109
       }

+ 13
- 15
Marlin/speaker.h View File

32
     struct state_t {
32
     struct state_t {
33
       tone_t   tone;
33
       tone_t   tone;
34
       uint16_t period;
34
       uint16_t period;
35
-      uint16_t cycles;
35
+      uint16_t counter;
36
     } state;
36
     } state;
37
 
37
 
38
   protected:
38
   protected:
43
     void reset() {
43
     void reset() {
44
       super::reset();
44
       super::reset();
45
       this->state.period = 0;
45
       this->state.period = 0;
46
-      this->state.cycles = 0;
46
+      this->state.counter = 0;
47
     }
47
     }
48
 
48
 
49
   public:
49
   public:
60
      * playing the tones in the queue.
60
      * playing the tones in the queue.
61
      */
61
      */
62
     virtual void tick() {
62
     virtual void tick() {
63
-      if (!this->state.cycles) {
63
+      if (!this->state.counter) {
64
         if (this->buffer.isEmpty()) return;
64
         if (this->buffer.isEmpty()) return;
65
 
65
 
66
         this->reset();
66
         this->reset();
69
         // Period is uint16, min frequency will be ~16Hz
69
         // Period is uint16, min frequency will be ~16Hz
70
         this->state.period = 1000000UL / this->state.tone.frequency;
70
         this->state.period = 1000000UL / this->state.tone.frequency;
71
 
71
 
72
-        this->state.cycles =
73
-          (this->state.tone.duration * 1000L) / this->state.period;
72
+        this->state.counter =
73
+          (this->state.tone.counter * 1000L) / this->state.period;
74
 
74
 
75
-        this->state.period >>= 1;
76
-        this->state.cycles <<= 1;
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;
77
 
80
 
78
-      }
79
-      else {
80
-        uint32_t const us = micros();
81
-        static uint32_t next = us + this->state.period;
82
-
83
-        if (us >= next) {
84
-          --this->state.cycles;
85
-          next = us + this->state.period;
81
+        if (now >= next) {
82
+          --this->state.counter;
83
+          next = now + this->state.period;
86
           if (this->state.tone.frequency > 0) this->invert();
84
           if (this->state.tone.frequency > 0) this->invert();
87
         }
85
         }
88
       }
86
       }

Loading…
Cancel
Save