summaryrefslogtreecommitdiff
path: root/examples/sinewave/sinewave_example.cpp
blob: 7ab316d14cad12318910336e5ba04426807ac30e (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
#include <hwd.h>

#include <functional>
#include <stdlib.h>
#include <iostream>
#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 = 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);

	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;
}