blob: 2f7c47e41272ca4eea95a70d96b28411a8817d5d (
plain)
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
|
#include <unistd.h>
#include <chrono>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
#include <hwd.h>
using seconds = std::chrono::duration<double>;
using Clock = std::chrono::high_resolution_clock;
int main(int argc, char* argv[]) {
seconds interval{ 10.0 };
bool end = false;
int i = 1;
int samples;
int opt;
const std::function infinite = []() {};
const std::function limited = [&end, &i, &samples]() {
i++;
if (i > samples) end = true;
};
auto generatorGuard = infinite;
try {
while ((opt = getopt(argc, argv, "n:i:")) != -1) {
switch (opt) {
case 'i':
interval = seconds{ std::stod(optarg) };
break;
case 'n':
samples = std::stoi(optarg);
if (samples == 0) end = true;
generatorGuard = limited;
break;
default:
std::cerr << "Usage: " << argv[0] << " [-i interval][-n samples]"<<std::endl;
return 1;
}
}
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
return 1;
}
hwd::sinewave::set_amplitude(11);
hwd::sinewave::set_frequency(10);
hwd::sinewave::set_phase(30);
while (!end) {
const auto endTime = Clock::now() + interval;
std::cout << hwd::sinewave::get_point() << std::endl;
std::this_thread::sleep_until(endTime);
generatorGuard();
}
return 0;
}
|