|
@@ -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
|