summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authormarwik15 <marwik15@gmail.com>2022-05-06 12:26:31 +0200
committermarwik15 <marwik15@gmail.com>2022-05-06 12:26:31 +0200
commit788403eea6557b4d7eb7d8ec1fd4eb42f1cd3bfe (patch)
tree7aef217554fe3473296e54a7498dd66b61dfaadf /examples
parent41bd771b2c2f8c17275e77c961cafa0c58324cd9 (diff)
downloadhwd-788403eea6557b4d7eb7d8ec1fd4eb42f1cd3bfe.zip
hwd-788403eea6557b4d7eb7d8ec1fd4eb42f1cd3bfe.tar.gz
hwd-788403eea6557b4d7eb7d8ec1fd4eb42f1cd3bfe.tar.bz2
Get interval and sample count from supplied parameters
Diffstat (limited to 'examples')
-rw-r--r--examples/sinewave/sinewave_example.cpp44
1 files changed, 40 insertions, 4 deletions
diff --git a/examples/sinewave/sinewave_example.cpp b/examples/sinewave/sinewave_example.cpp
index e6583d6..7ab316d 100644
--- a/examples/sinewave/sinewave_example.cpp
+++ b/examples/sinewave/sinewave_example.cpp
@@ -1,19 +1,55 @@
#include <hwd.h>
-#include <math.h>
-#include <stdio.h>
+#include <functional>
#include <stdlib.h>
#include <iostream>
+#include <stdio.h>
+#include <chrono>
+#include <thread>
+#include <math.h>
-int main() {
+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 = 300, i = 0;
+ bool end = false;
+
+ std::function infinite = []() {};
+ std::function limited = [&end, &i, &samples]() {
+ if (i > samples) end = true;
+ i++;
+ };
+
+ auto& gen = infinite;
+
+ if (argc == 3) {
+ try {
+ interval = std::chrono::milliseconds(std::stoi(argv[1]));
+ samples = std::stoi(argv[2]);
+ gen = limited;
+ }
+ 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);
- for (int i = 1; i < 1200; i++) {
+ while (!end) {
+ const auto endTime = Clock::now() + interval;
+
std::cout << hwd::sinewave::get_point() << std::endl;
+
+ std::this_thread::sleep_until(endTime);
+ gen();
}
return 0;