summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authormarwik15 <marwik15@gmail.com>2022-05-06 14:36:18 +0200
committermarwik15 <marwik15@gmail.com>2022-05-06 14:36:58 +0200
commitfb6cc0ef009280d83dc9c13669f5a2788d91d4ce (patch)
tree489dbc9bc0bafc2ccdcb78b093231af76fe43770 /examples
parent788403eea6557b4d7eb7d8ec1fd4eb42f1cd3bfe (diff)
downloadhwd-fb6cc0ef009280d83dc9c13669f5a2788d91d4ce.zip
hwd-fb6cc0ef009280d83dc9c13669f5a2788d91d4ce.tar.gz
hwd-fb6cc0ef009280d83dc9c13669f5a2788d91d4ce.tar.bz2
Add extended command line arguments
Diffstat (limited to 'examples')
-rw-r--r--examples/sinewave/sinewave_example.cpp41
1 files changed, 25 insertions, 16 deletions
diff --git a/examples/sinewave/sinewave_example.cpp b/examples/sinewave/sinewave_example.cpp
index 7ab316d..10be073 100644
--- a/examples/sinewave/sinewave_example.cpp
+++ b/examples/sinewave/sinewave_example.cpp
@@ -1,8 +1,8 @@
#include <hwd.h>
#include <functional>
-#include <stdlib.h>
#include <iostream>
+#include <getopt.h>
#include <stdio.h>
#include <chrono>
#include <thread>
@@ -14,30 +14,39 @@ using Clock = std::chrono::high_resolution_clock;
int main(int argc, char* argv[]) {
std::chrono::milliseconds interval{ 10ms };
- int samples = 300, i = 0;
+ int samples, opt, i = 0;
bool end = false;
- std::function infinite = []() {};
- std::function limited = [&end, &i, &samples]() {
+ const std::function infinite = []() {};
+ const std::function limited = [&end, &i, &samples]() {
if (i > samples) end = true;
i++;
};
- auto& gen = infinite;
+ auto generatorGuard = infinite;
- if (argc == 3) {
- try {
- interval = std::chrono::milliseconds(std::stoi(argv[1]));
- samples = std::stoi(argv[2]);
- gen = limited;
+ 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';
+ }
+ catch (const std::invalid_argument& ia) {
+ std::cerr << "Invalid argument: " << ia.what() << '\n';
- return 0;
- }
+ return 0;
}
-
+
hwd::sinewave::set_amplitude(11);
hwd::sinewave::set_frequency(10);
hwd::sinewave::set_phase(30);
@@ -49,7 +58,7 @@ int main(int argc, char* argv[]) {
std::this_thread::sleep_until(endTime);
- gen();
+ generatorGuard();
}
return 0;