diff options
author | marwik15 <marwik15@gmail.com> | 2022-05-07 17:32:53 +0200 |
---|---|---|
committer | marwik15 <marwik15@gmail.com> | 2022-05-07 17:32:53 +0200 |
commit | 0c63ebc494d6e909eff6cfa15eb23960a0e2f015 (patch) | |
tree | 471ef25e787f377671cad77f0687dbf3ab52edb8 /examples | |
parent | ad9bf20fee855b819f99171e116d7889d8f85754 (diff) | |
download | hwd-0c63ebc494d6e909eff6cfa15eb23960a0e2f015.zip hwd-0c63ebc494d6e909eff6cfa15eb23960a0e2f015.tar.gz hwd-0c63ebc494d6e909eff6cfa15eb23960a0e2f015.tar.bz2 |
Reimplement sample counter
Diffstat (limited to 'examples')
-rw-r--r-- | examples/sinewave/sinewave_example.cpp | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/examples/sinewave/sinewave_example.cpp b/examples/sinewave/sinewave_example.cpp index 2f7c47e..606cab2 100644 --- a/examples/sinewave/sinewave_example.cpp +++ b/examples/sinewave/sinewave_example.cpp @@ -14,20 +14,11 @@ using Clock = std::chrono::high_resolution_clock; int main(int argc, char* argv[]) { - seconds interval{ 10.0 }; - bool end = false; - int i = 1; + seconds interval{ 0.01 }; + int i = 0; 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) { @@ -36,8 +27,6 @@ int main(int argc, char* argv[]) { 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; @@ -55,14 +44,15 @@ int main(int argc, char* argv[]) { hwd::sinewave::set_frequency(10); hwd::sinewave::set_phase(30); - while (!end) { + while (1) { + if (i >= samples) break; + i++; + const auto endTime = Clock::now() + interval; std::cout << hwd::sinewave::get_point() << std::endl; std::this_thread::sleep_until(endTime); - - generatorGuard(); } return 0; |