From 2ef406196751a469323ed622312df2e7e7b1834b Mon Sep 17 00:00:00 2001 From: marwik15 Date: Mon, 2 May 2022 13:19:13 +0200 Subject: Sine wave changes after CR --- daemon/src/Sinewave.cpp | 15 ++++++--------- daemon/src/Sinewave.h | 14 +++++++------- 2 files changed, 13 insertions(+), 16 deletions(-) (limited to 'daemon') 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 -#include +#include #include #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 m_start_time, m_current_time; - std::chrono::duration 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(); }; -- cgit v1.1