blob: 10be07320db08464e270971f71a448ec3e2fe814 (
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
|
#include <hwd.h>
#include <functional>
#include <iostream>
#include <getopt.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <math.h>
using namespace std::chrono_literals;
using Clock = std::chrono::high_resolution_clock;
int main(int argc, char* argv[]) {
std::chrono::milliseconds interval{ 10ms };
int samples, opt, i = 0;
bool end = false;
const std::function infinite = []() {};
const std::function limited = [&end, &i, &samples]() {
if (i > samples) end = true;
i++;
};
auto generatorGuard = infinite;
try {
while ((opt = getopt(argc, argv, "n:i:")) != -1) {
switch (opt) {
case 'i':
interval = std::chrono::milliseconds(std::stoi(optarg));
break;
case 'n':
samples = std::stoi(optarg);
generatorGuard = limited;
break;
default:
std::cerr << "Usage: " << argv[0] << " [-i interval][-n samples] \n",
exit(EXIT_FAILURE);
}
}
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
return 0;
}
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;
}
|