-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsound.cpp
198 lines (181 loc) · 3.57 KB
/
sound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <Arduino.h>
#include "sound.h"
#ifdef ESP32
#include <driver/dac.h>
#define CHANNEL_FOR(p) (p == 25)? DAC_CHANNEL_1: DAC_CHANNEL_2
#endif
void soundOff(int pin)
{
#if defined(__AVR__)
switch (pin) {
#ifdef TCCR1A
case 10:
TCCR1A &= ~_BV(COM1B1); // Disable PWM
break;
case 9:
TCCR1A &= ~_BV(COM1A1);
break;
#endif
#ifdef TCCR3A
case 5:
TCCR3A = 0;// &= ~_BV(COM3A1);
break;
#endif
#ifdef TCCR2A
case 3:
TCCR2A &= ~_BV(COM2B1);
break;
#endif
#ifdef TCCR5A
case 46:
TCCR5A &= ~_BV(COM5A1);
break;
case 45:
TCCR5A &= ~_BV(COM5B1);
break;
case 44:
TCCR5A &= ~_BV(COM5C1);
break;
#endif
}
#elif defined(ESP32)
dac_output_disable(CHANNEL_FOR(pin));
#else
analogWrite(pin, 0);
#endif
}
//https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328
void soundOn(int pin)
{
#if defined(__AVR__)
switch (pin) {
#ifdef TCCR1A
case 10:
TCCR1A = 0;
ICR1 = PWM_TOP;
TCCR1B = _BV(WGM13) | _BV(CS10);
TCNT1 = 0;
TCCR1A |= _BV(COM1B1);
break;
case 9:
TCCR1A = 0;
ICR1 = PWM_TOP;
TCCR1B = _BV(WGM13) | _BV(CS10);
TCNT1 = 0;
TCCR1A |= _BV(COM1A1);
break;
#endif
#ifdef TCCR3A
case 5:
TCCR3A = 0;
ICR3 = PWM_TOP;
TCCR3B = _BV(WGM33) | _BV(CS30);
TCNT3 = 0;
TCCR3A |= _BV(COM3A1);
break;
#endif
#ifdef TCCR2A
case 3:
TCCR2A = _BV(COM2B1) | _BV(WGM20); // Non-inverted, PWM Phase Corrected
TCCR2B = _BV(CS20) | _BV(WGM22); // No prescaling, ditto
OCR2B = PWM_TOP;
TCNT2 = 0;
break;
#endif
#ifdef TCCR5A
case 46:
TCCR5A = 0;
ICR5 = PWM_TOP;
TCCR5B = _BV(WGM13) | _BV(CS10);
TCNT5 = 0;
TCCR5A |= _BV(COM5A1);
break;
case 45:
TCCR5A = 0;
ICR5 = PWM_TOP;
TCCR5B = _BV(WGM13) | _BV(CS10);
TCNT5 = 0;
TCCR5A |= _BV(COM5B1);
break;
case 44:
TCCR5A = 0;
ICR5 = PWM_TOP;
TCCR5B = _BV(WGM13) | _BV(CS10);
TCNT5 = 0;
TCCR5A |= _BV(COM5C1);
break;
#endif
}
#elif defined(ESP8266)
analogWriteFreq(12000);
analogWriteRange(PWM_TOP);
#elif defined(ESP32)
dac_output_enable(CHANNEL_FOR(pin));
#endif
}
void sound(int pin, byte b)
{
// Update PWM volume
b = (b & 15);
#if defined(__AVR__)
// scale volume to PWM_TOP / 2
uint16_t duty = (PWM_TOP * b) / 16 / 2;
switch (pin) {
#ifdef TCCR1A:
case 10:
if (duty != OCR1B) {
TCNT1 = 0;
OCR1B = duty;
}
break;
case 9:
if (duty != OCR1A) {
TCNT1 = 0;
OCR1A = duty;
}
break;
#endif
#ifdef TCCR3A:
case 5:
if (duty != OCR3A) {
TCNT3 = 0;
OCR3A = duty;
}
break;
#endif
#ifdef TCCR2A:
case 3:
duty /= 256;
if (duty != OCR2B) {
TCNT2 = 0;
OCR2B = duty;
}
break;
#endif
#ifdef TCCR5A:
case 46:
if (duty != OCR5B) {
TCNT5 = 0;
OCR5B = duty;
}
break;
case 45:
if (duty != OCR5B) {
TCNT5 = 0;
OCR5B = duty;
}
break;
case 44:
if (duty != OCR5B) {
TCNT5 = 0;
OCR5B = duty;
}
break;
#endif
}
#elif defined(ESP32)
dac_output_voltage(CHANNEL_FOR(pin), b*8);
#else
analogWrite(pin, b*8);
#endif
}