-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfm_transmitter.cpp
More file actions
100 lines (91 loc) · 2.79 KB
/
Copy pathfm_transmitter.cpp
File metadata and controls
100 lines (91 loc) · 2.79 KB
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
#include "transmitter.hpp"
#include <iostream>
#include <csignal>
#include <unistd.h>
std::mutex mtx; // 互斥锁对象,用于控制对共享资源的并发访问。
bool enable = true; // 用于控制程序的运行状态。
Transmitter *transmitter = nullptr;
// 信号处理函数,用于处理SIGINT信号
void sigIntHandler(int sigNum) //
{
if (transmitter)
{
std::cout << "Stopping..." << std::endl;
transmitter->Stop();
enable = false;
}
}
int main(int argc, char **argv) // 接受命令行参数,并根据这些参数执行相应的操作
{
float frequency = 100.f, bandwidth = 200.f;
uint16_t dmaChannel = 0;
bool showUsage = true, loop = false;
int opt, filesOffset;
while ((opt = getopt(argc, argv, "rf:d:b:v")) != -1)
{
switch (opt)
{
case 'r':
loop = true;
break;
case 'f':
frequency = std::stof(optarg);
break;
case 'd':
dmaChannel = std::stoi(optarg);
break;
case 'b':
bandwidth = std::stof(optarg);
break;
case 'v':
std::cout << EXECUTABLE << " version: " << VERSION << std::endl;
return 0;
}
}
if (optind < argc)
{
filesOffset = optind;
showUsage = false;
}
if (showUsage)
{
std::cout << "Usage: " << EXECUTABLE << " [-f <frequency>] [-b <bandwidth>] [-d <dma_channel>] [-r] <file>" << std::endl;
return 0;
}
int result = EXIT_SUCCESS;
std::signal(SIGINT, sigIntHandler);
std::signal(SIGTERM, sigIntHandler);
try
{
transmitter = new Transmitter();
std::cout << "Broadcasting at " << frequency << " MHz with "
<< bandwidth << " kHz bandwidth" << std::endl;
do
{
std::string filename = argv[optind++];
if ((optind == argc) && loop)
{
optind = filesOffset;
}
WaveReader reader(filename != "-" ? filename : std::string(), enable, mtx);
WaveHeader header = reader.GetHeader();
std::cout << "Playing: " << reader.GetFilename() << ", "
<< header.sampleRate << " Hz, "
<< header.bitsPerSample << " bits, "
<< ((header.channels > 0x01) ? "stereo" : "mono") << std::endl;
transmitter->Transmit(reader, frequency, bandwidth, dmaChannel, optind < argc);
} while (enable && (optind < argc));
}
catch (std::exception &catched)
{
std::cout << "Error: " << catched.what() << std::endl;
result = EXIT_FAILURE;
}
if (transmitter)
{
auto temp = transmitter;
transmitter = nullptr;
delete temp;
}
return result;
}