-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdtmf.cpp
129 lines (114 loc) · 3.08 KB
/
dtmf.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
/**
dtmf.cpp - DTMF generator
Copyright (C) 2018 Costin STROIE <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dtmf.h"
DTMF::DTMF(uint8_t pulse, uint8_t pause) {
// Set the duration
this->setDuration(pulse, pause);
// Initialize the indices
rowIdx = 0;
colIdx = 0;
// Initilize the state
state = DTMF_DISB;
}
DTMF::~DTMF() {
}
/**
Set DTMF digit duration and the silence interval between adjacent digits
*/
void DTMF::setDuration(uint8_t pulse, uint8_t pause) {
// Make the pause equal to pulse, if not specified
if (pause == 0) pause = pulse;
// Compute the wave steps, as Q8.8
for (uint8_t i = 0; i < ROWSCOLS; i++) {
stpRows[i] = wave.getStep(frqRows[i]);
stpCols[i] = wave.getStep(frqCols[i]);
}
// Compute the pulse and pause samples
lenPulse = (uint32_t)pulse * F_SAMPLE / 1000;
lenPause = (uint32_t)pause * F_SAMPLE / 1000;
}
/**
Get the next DTMF sample, according to the DTMF state
*/
bool DTMF::getSample() {
if (state == DTMF_WAVE) {
sample = (wave.sample(rowIdx) >> 1) +
(wave.sample(colIdx) >> 1);
// Step up the indices for the next samples
rowIdx += stpRows[row];
colIdx += stpCols[col];
// Step up the samples counter to the pulse length
if (++counter > lenPulse) {
// Silence and reset the sample and the counter
state = DTMF_SLNC;
counter = 0;
sample = 0x80;
}
return true;
}
else if (state == DTMF_SLNC) {
// Step up the counter to pause lenght
if (++counter > lenPause) {
// Disable and reset the counter
state = DTMF_DISB;
counter = 0;
}
return true;
}
return false;
}
/**
Start the DTMF generator to send a char
@param chr the character to send
*/
char DTMF::send(char chr) {
if (getRowCol(chr)) {
// Start the wave
state = DTMF_WAVE;
counter = 0;
return chr;
}
else {
// Disable
state = DTMF_DISB;
return '\0';
}
}
/**
Send the buffer as DTMF tones
@param chrs the buffer to send
@param len the buffer length
*/
uint8_t DTMF::send(char *chrs, size_t len) {
for (uint8_t i = 0; i < len; i++)
if (chrs[i] != '\0')
this->send(chrs[i]);
else
break;
}
/**
Get the row and column of the character in the DTMF table
@param chr the character to test
*/
bool DTMF::getRowCol(char chr) {
for (row = 0; row < ROWSCOLS; row++) {
for (col = 0; col < ROWSCOLS; col++) {
if (chr == dtmfRowsCols[row][col]) {
return true;
}
}
}
return false;
}