summaryrefslogtreecommitdiff
path: root/daemon
diff options
context:
space:
mode:
authormarwik15 <marwik15@gmail.com>2022-05-02 11:30:54 +0200
committermarwik15 <marwik15@gmail.com>2022-05-02 11:30:54 +0200
commitd417394ae791e9972fede0665587aa612f93bca0 (patch)
tree1087d58f9db079327b9f516157a31b6821b7997f /daemon
parentac5e26518a780f19483585cd6b5d62de9094c2b8 (diff)
downloadhwd-d417394ae791e9972fede0665587aa612f93bca0.zip
hwd-d417394ae791e9972fede0665587aa612f93bca0.tar.gz
hwd-d417394ae791e9972fede0665587aa612f93bca0.tar.bz2
Add sine wave generator
Diffstat (limited to 'daemon')
-rw-r--r--daemon/CMakeLists.txt1
-rw-r--r--daemon/src/Sinewave.cpp59
-rw-r--r--daemon/src/Sinewave.h33
-rw-r--r--daemon/src/daemon.cpp4
4 files changed, 97 insertions, 0 deletions
diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt
index 54d3950..6a01e15 100644
--- a/daemon/CMakeLists.txt
+++ b/daemon/CMakeLists.txt
@@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME}
src/daemon.cpp
src/Gpio.cpp
src/Memory.cpp
+ src/Sinewave.cpp
)
target_link_libraries(${PROJECT_NAME}
PRIVATE rpclib::rpc
diff --git a/daemon/src/Sinewave.cpp b/daemon/src/Sinewave.cpp
new file mode 100644
index 0000000..de63553
--- /dev/null
+++ b/daemon/src/Sinewave.cpp
@@ -0,0 +1,59 @@
+#include "Sinewave.h"
+
+Sinewave::Sinewave()
+{
+ m_start_time = std::chrono::high_resolution_clock::now();
+
+ m_amplitude = 11;
+ m_frequency = 10;
+ m_phase = 30;
+}
+
+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("/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_point", [&]() -> double { return get_point(); });
+}
+
+void Sinewave::set_amplitude(double amplitude)
+{
+ m_amplitude = amplitude;
+}
+
+void Sinewave::set_frequency(double frequency)
+{
+ m_frequency = frequency;
+}
+
+void Sinewave::set_phase(double phase)
+{
+ m_phase = phase;
+}
+
+double Sinewave::get_amplitude() const
+{
+ return m_amplitude;
+}
+
+double Sinewave::get_frequency() const
+{
+ return m_frequency;
+}
+
+double Sinewave::get_phase() const
+{
+ return m_phase;
+}
+
+double Sinewave::get_point()
+{
+ m_current_time = std::chrono::high_resolution_clock::now();
+ m_duration_time = m_current_time - m_start_time;
+
+ return m_amplitude * sin(2 * 3.14 * m_frequency * m_duration_time.count() + m_phase);
+}
diff --git a/daemon/src/Sinewave.h b/daemon/src/Sinewave.h
new file mode 100644
index 0000000..d8988ad
--- /dev/null
+++ b/daemon/src/Sinewave.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <string>
+#include <math.h>
+#include <chrono>
+
+#include "Assembly.h"
+
+class Sinewave {
+
+public:
+ Sinewave();
+
+ void apply(AssemblyContext ctx);
+
+ void set_amplitude(double amplitude);
+ void set_frequency(double frequency);
+ void set_phase(double phase);
+
+ double get_amplitude() const;
+ double get_frequency() const;
+ double get_phase() const;
+
+private:
+ std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time, m_current_time;
+ std::chrono::duration<double> m_duration_time;
+
+ double m_amplitude;
+ double m_frequency;
+ double m_phase;
+
+ double get_point();
+};
diff --git a/daemon/src/daemon.cpp b/daemon/src/daemon.cpp
index 28b95a4..1b9dcab 100644
--- a/daemon/src/daemon.cpp
+++ b/daemon/src/daemon.cpp
@@ -6,10 +6,12 @@
#include "Assembly.h"
#include "Gpio.h"
#include "Memory.h"
+#include "Sinewave.h"
static Gpio gpio;
static Memory memory;
+static Sinewave sinewave;
int main(int, char **)
@@ -18,5 +20,7 @@ int main(int, char **)
Assembly assembly {server};
assembly.add("gpio", gpio);
assembly.add("memory", memory);
+ assembly.add("sinewave", sinewave);
+
server.run();
}