![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A DTMF sequence editor and player for HAM radio equipment command & control.
mbed_dtmf_generator.cpp@0:1324e7d9d471, 2011-03-07 (annotated)
- Committer:
- osmeest
- Date:
- Mon Mar 07 22:51:19 2011 +0000
- Revision:
- 0:1324e7d9d471
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 0:1324e7d9d471 | 1 | #include "mbed_dtmf_generator.hpp" |
osmeest | 0:1324e7d9d471 | 2 | #include "snd_wave_generator/SineWave.h" |
osmeest | 0:1324e7d9d471 | 3 | #include "snd_wave_generator/WaveCombo.h" |
osmeest | 0:1324e7d9d471 | 4 | #include <iostream> |
osmeest | 0:1324e7d9d471 | 5 | #include <sstream> |
osmeest | 0:1324e7d9d471 | 6 | #include <string> |
osmeest | 0:1324e7d9d471 | 7 | |
osmeest | 0:1324e7d9d471 | 8 | using namespace snd_wave_generator; |
osmeest | 0:1324e7d9d471 | 9 | |
osmeest | 0:1324e7d9d471 | 10 | MbedDtmfGenerator::MbedDtmfGenerator() : |
osmeest | 0:1324e7d9d471 | 11 | generator(16000) |
osmeest | 0:1324e7d9d471 | 12 | { |
osmeest | 0:1324e7d9d471 | 13 | std::cout << "Init Wave" << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 14 | makeWaves(); |
osmeest | 0:1324e7d9d471 | 15 | } |
osmeest | 0:1324e7d9d471 | 16 | |
osmeest | 0:1324e7d9d471 | 17 | void MbedDtmfGenerator::makeKeyWave(int pos, int row, int col) |
osmeest | 0:1324e7d9d471 | 18 | { |
osmeest | 0:1324e7d9d471 | 19 | std::cout << "Init Key Wave #" << pos << " R" << row << "C" << col << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 20 | std::auto_ptr<WaveCombo> wave(new WaveCombo()); |
osmeest | 0:1324e7d9d471 | 21 | wave->add(colWaves[col].get()); |
osmeest | 0:1324e7d9d471 | 22 | wave->add(rowWaves[row].get()); |
osmeest | 0:1324e7d9d471 | 23 | |
osmeest | 0:1324e7d9d471 | 24 | waves[pos] = wave; |
osmeest | 0:1324e7d9d471 | 25 | } |
osmeest | 0:1324e7d9d471 | 26 | |
osmeest | 0:1324e7d9d471 | 27 | void MbedDtmfGenerator::makeWaves() |
osmeest | 0:1324e7d9d471 | 28 | { |
osmeest | 0:1324e7d9d471 | 29 | static int colFreq[4] = { 1209, 1336, 1477, 1633 }; |
osmeest | 0:1324e7d9d471 | 30 | static int rowFreq[4] = { 697, 770, 852, 941 }; |
osmeest | 0:1324e7d9d471 | 31 | |
osmeest | 0:1324e7d9d471 | 32 | for(int i = 0; i < 4; ++i) { |
osmeest | 0:1324e7d9d471 | 33 | std::cout << "Init ColWave " << i << " hz=" << colFreq[i] << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 34 | colWaves[i].reset(new SineWave(colFreq[i])); |
osmeest | 0:1324e7d9d471 | 35 | |
osmeest | 0:1324e7d9d471 | 36 | std::cout << "Init RowWave " << i << " hz=" << rowFreq[i] << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 37 | rowWaves[i].reset(new SineWave(rowFreq[i])); |
osmeest | 0:1324e7d9d471 | 38 | } |
osmeest | 0:1324e7d9d471 | 39 | |
osmeest | 0:1324e7d9d471 | 40 | makeKeyWave(0, 3, 1); |
osmeest | 0:1324e7d9d471 | 41 | for(int row = 0; row < 3; ++row) { |
osmeest | 0:1324e7d9d471 | 42 | for(int col = 0; col < 3; ++col) { |
osmeest | 0:1324e7d9d471 | 43 | makeKeyWave(row * 3 + col + 1, row, col); |
osmeest | 0:1324e7d9d471 | 44 | } |
osmeest | 0:1324e7d9d471 | 45 | } |
osmeest | 0:1324e7d9d471 | 46 | for(int ltr=0; ltr < 4; ++ltr) { |
osmeest | 0:1324e7d9d471 | 47 | makeKeyWave(ltr+10, ltr, 3); |
osmeest | 0:1324e7d9d471 | 48 | } |
osmeest | 0:1324e7d9d471 | 49 | makeKeyWave(14, 3, 0); |
osmeest | 0:1324e7d9d471 | 50 | makeKeyWave(15, 3, 2); |
osmeest | 0:1324e7d9d471 | 51 | } |
osmeest | 0:1324e7d9d471 | 52 | |
osmeest | 0:1324e7d9d471 | 53 | Wave * MbedDtmfGenerator::getWaveFor(char ch) |
osmeest | 0:1324e7d9d471 | 54 | { |
osmeest | 0:1324e7d9d471 | 55 | int index = -1; |
osmeest | 0:1324e7d9d471 | 56 | if (ch >= '0' && ch <= '9') { |
osmeest | 0:1324e7d9d471 | 57 | index = ch - '0'; |
osmeest | 0:1324e7d9d471 | 58 | } |
osmeest | 0:1324e7d9d471 | 59 | else if (ch >= 'A' && ch <= 'D') { |
osmeest | 0:1324e7d9d471 | 60 | index = ch - 'A' + 10; |
osmeest | 0:1324e7d9d471 | 61 | } |
osmeest | 0:1324e7d9d471 | 62 | else if (ch == '*') { |
osmeest | 0:1324e7d9d471 | 63 | index = 14; |
osmeest | 0:1324e7d9d471 | 64 | } |
osmeest | 0:1324e7d9d471 | 65 | else if (ch == '#') { |
osmeest | 0:1324e7d9d471 | 66 | index = 15; |
osmeest | 0:1324e7d9d471 | 67 | } |
osmeest | 0:1324e7d9d471 | 68 | std::cout << "Wave for '" << ch << "' => " << index << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 69 | return this->waves[index].get(); |
osmeest | 0:1324e7d9d471 | 70 | } |
osmeest | 0:1324e7d9d471 | 71 | |
osmeest | 0:1324e7d9d471 | 72 | void MbedDtmfGenerator::play(char ch) |
osmeest | 0:1324e7d9d471 | 73 | { |
osmeest | 0:1324e7d9d471 | 74 | Wave *wave = getWaveFor(ch); |
osmeest | 0:1324e7d9d471 | 75 | std::cout << "Play Wave '" << ch << "'" << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 76 | if (wave) { |
osmeest | 0:1324e7d9d471 | 77 | generator.play(getWaveFor(ch)); |
osmeest | 0:1324e7d9d471 | 78 | } |
osmeest | 0:1324e7d9d471 | 79 | else { |
osmeest | 0:1324e7d9d471 | 80 | std::cout << "No wave !\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 81 | } |
osmeest | 0:1324e7d9d471 | 82 | } |
osmeest | 0:1324e7d9d471 | 83 | |
osmeest | 0:1324e7d9d471 | 84 | void MbedDtmfGenerator::stop() |
osmeest | 0:1324e7d9d471 | 85 | { |
osmeest | 0:1324e7d9d471 | 86 | std::cout << "Stop Wave" << "\r" << std::endl; |
osmeest | 0:1324e7d9d471 | 87 | generator.stop(); |
osmeest | 0:1324e7d9d471 | 88 | } |