-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToneGenerator.cpp
80 lines (72 loc) · 3.26 KB
/
ToneGenerator.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
/***************************************************************************
* *
* 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 2 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, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: ToneGenerator.cpp *
* *
***************************************************************************/
#include "main.h"
/// Reserve memory for singleton object.
static ToneGenerator toneGeneratorSingletonObject;
const uint16_t ToneGenerator::sineTable[] = {
0x2C3, 0x369, 0x3D8, 0x3FF,
0x3D8, 0x369, 0x2C3, 0x200,
0x13C, 0x096, 0x027, 0x000,
0x027, 0x096, 0x13C, 0x200
};
/**
* Constructor
*/
ToneGenerator::ToneGenerator() {
Timer0::GetInstance()->EnableWithoutInt();
// default to give a 1 kHz tone
Timer0::GetInstance()->SetPeriod(63);
}
/**
* Get a pointer to the repeater control object.
*/
ToneGenerator * ToneGenerator::GetInstance()
{
return &toneGeneratorSingletonObject;
}
/**
* Generate a single-frequency tone for the specified duration
*
* @param freq Frequency in Hz
* @param duration Duration in milliseconds
*/
void ToneGenerator::SingleTone(uint16_t freq, uint16_t duration) {
// figure out the timer period
uint32_t periodus = 1000000 / (freq * 16);
uint32_t durationus = duration * 1000;
uint32_t accumDur = 0;
Timer0::GetInstance()->SetPeriod(periodus);
int index = 0;
while(accumDur < durationus) {
// spin until the timer clicks
while(T0TCR & 0x01);
// restart the timer
T0TCR = TxTCR_COUNTER_ENABLE;
DAC::GetInstance()->Set(sineTable[index++]);
index &= 0x0F;
accumDur += periodus;
}
}