summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarwik15 <marwik15@gmail.com>2022-05-02 13:19:13 +0200
committermarwik15 <marwik15@gmail.com>2022-05-02 13:19:13 +0200
commit2ef406196751a469323ed622312df2e7e7b1834b (patch)
tree1c75c8715c7c4e5e61a9f78099c543369e36d01b
parentd417394ae791e9972fede0665587aa612f93bca0 (diff)
downloadhwd-2ef406196751a469323ed622312df2e7e7b1834b.zip
hwd-2ef406196751a469323ed622312df2e7e7b1834b.tar.gz
hwd-2ef406196751a469323ed622312df2e7e7b1834b.tar.bz2
Sine wave changes after CR
-rw-r--r--daemon/src/Sinewave.cpp15
-rw-r--r--daemon/src/Sinewave.h14
-rw-r--r--library/include/hwd.h14
-rw-r--r--library/src/sinewave.cpp66
4 files changed, 53 insertions, 56 deletions
diff --git a/daemon/src/Sinewave.cpp b/daemon/src/Sinewave.cpp
index de63553..d1ca5ef 100644
--- a/daemon/src/Sinewave.cpp
+++ b/daemon/src/Sinewave.cpp
@@ -1,9 +1,7 @@
#include "Sinewave.h"
-Sinewave::Sinewave()
+Sinewave::Sinewave() : m_start_time{clock::now()}
{
- m_start_time = std::chrono::high_resolution_clock::now();
-
m_amplitude = 11;
m_frequency = 10;
m_phase = 30;
@@ -13,10 +11,10 @@ void Sinewave::apply(AssemblyContext ctx)
{
ctx.bind("/set_amplitude", [&](double amplitude) { set_amplitude(amplitude); });
ctx.bind("/set_frequency", [&](double frequency) { set_frequency(frequency); });
- ctx.bind("/set_phase", [&](double phase) { set_phase(phase); });
+ ctx.bind("/set_phase", [&](double phase) { set_phase(phase); });
ctx.bind("/get_amplitude", [&]() -> double { return get_amplitude(); });
ctx.bind("/get_frequency", [&]() -> double { return get_frequency(); });
- ctx.bind("/get_phase", [&]() -> double { return get_phase(); });
+ ctx.bind("/get_phase", [&]() -> double { return get_phase(); });
ctx.bind("/get_point", [&]() -> double { return get_point(); });
}
@@ -50,10 +48,9 @@ double Sinewave::get_phase() const
return m_phase;
}
-double Sinewave::get_point()
+double Sinewave::get_point() const
{
- m_current_time = std::chrono::high_resolution_clock::now();
- m_duration_time = m_current_time - m_start_time;
+ const auto time = clock::now() - m_start_time;
- return m_amplitude * sin(2 * 3.14 * m_frequency * m_duration_time.count() + m_phase);
+ return m_amplitude * sin(2 * M_PI * m_frequency * time.count() + m_phase);
}
diff --git a/daemon/src/Sinewave.h b/daemon/src/Sinewave.h
index d8988ad..d5be732 100644
--- a/daemon/src/Sinewave.h
+++ b/daemon/src/Sinewave.h
@@ -1,12 +1,13 @@
#pragma once
-#include <string>
-#include <math.h>
+#include <cmath>
#include <chrono>
#include "Assembly.h"
-class Sinewave {
+class Sinewave
+{
+ using clock = std::chrono::high_resolution_clock;
public:
Sinewave();
@@ -21,13 +22,12 @@ public:
double get_frequency() const;
double get_phase() const;
+ double get_point() const;
+
private:
- std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time, m_current_time;
- std::chrono::duration<double> m_duration_time;
+ const std::chrono::high_resolution_clock::time_point m_start_time;
double m_amplitude;
double m_frequency;
double m_phase;
-
- double get_point();
};
diff --git a/library/include/hwd.h b/library/include/hwd.h
index 4ea75fa..7374291 100644
--- a/library/include/hwd.h
+++ b/library/include/hwd.h
@@ -22,13 +22,13 @@ bool write(const std::vector<char> data, std::size_t off);
namespace sinewave
{
- void set_amplitude(double);
- void set_frequency(double);
- void set_phase(double);
- double get_amplitude();
- double get_frequency();
- double get_phase();
- double get_point();
+void set_amplitude(double);
+void set_frequency(double);
+void set_phase(double);
+double get_amplitude();
+double get_frequency();
+double get_phase();
+double get_point();
} // namespace sinewave
} // namespace hwd
diff --git a/library/src/sinewave.cpp b/library/src/sinewave.cpp
index 3a1d8ec..04982df 100644
--- a/library/src/sinewave.cpp
+++ b/library/src/sinewave.cpp
@@ -6,39 +6,39 @@ namespace hwd
{
namespace sinewave
{
- void set_amplitude(double amplitude)
- {
- get_client().call("sinewave/set_amplitude", amplitude);
- }
-
- void set_frequency(double frequency)
- {
- get_client().call("sinewave/set_frequency", frequency);
- }
-
- void set_phase(double phase)
- {
- get_client().call("sinewave/set_phase", phase);
- }
-
- double get_amplitude()
- {
- return get_client().call("sinewave/get_amplitude").as<double>();
- }
-
- double get_frequency()
- {
- return get_client().call("sinewave/get_frequency").as<double>();
- }
-
- double get_phase()
- {
- return get_client().call("sinewave/get_phase").as<double>();
- }
-
- double get_point() {
- return get_client().call("sinewave/get_point").as<double>();
- }
+void set_amplitude(double amplitude)
+{
+ get_client().call("sinewave/set_amplitude", amplitude);
+}
+
+void set_frequency(double frequency)
+{
+ get_client().call("sinewave/set_frequency", frequency);
+}
+
+void set_phase(double phase)
+{
+ get_client().call("sinewave/set_phase", phase);
+}
+
+double get_amplitude()
+{
+ return get_client().call("sinewave/get_amplitude").as<double>();
+}
+
+double get_frequency()
+{
+ return get_client().call("sinewave/get_frequency").as<double>();
+}
+
+double get_phase()
+{
+ return get_client().call("sinewave/get_phase").as<double>();
+}
+
+double get_point() {
+ return get_client().call("sinewave/get_point").as<double>();
+}
} // namespace sinewave
} // namespace hwd