From c6b1f4414617711798961a618601e959ca3777df Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 6 Jun 2022 18:39:50 +0200 Subject: Reformatted newly added code to be closer to convention --- daemon/hwd.conf | 2 +- daemon/src/Sinewave.cpp | 23 ++++++++++++++++------- daemon/src/Sinewave.h | 3 ++- examples/sinewave/sinewave_example.cpp | 17 ++++++++--------- library/src/sinewave.cpp | 3 ++- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/daemon/hwd.conf b/daemon/hwd.conf index bdd1819..ba2ab7d 100644 --- a/daemon/hwd.conf +++ b/daemon/hwd.conf @@ -1 +1 @@ -u hwd - "Daemon for Educational Hardware Simulator" \ No newline at end of file +u hwd - "Daemon for Educational Hardware Simulator" diff --git a/daemon/src/Sinewave.cpp b/daemon/src/Sinewave.cpp index 9d63d78..43cad24 100644 --- a/daemon/src/Sinewave.cpp +++ b/daemon/src/Sinewave.cpp @@ -1,56 +1,65 @@ #include "Sinewave.h" -Sinewave::Sinewave() : m_start_time{clock::now()} + +Sinewave::Sinewave() : + m_start_time {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(); }); + 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() const { const std::chrono::duration time = clock::now() - m_start_time; - 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 d5be732..ed0d1de 100644 --- a/daemon/src/Sinewave.h +++ b/daemon/src/Sinewave.h @@ -5,7 +5,8 @@ #include "Assembly.h" -class Sinewave + +class Sinewave { using clock = std::chrono::high_resolution_clock; diff --git a/examples/sinewave/sinewave_example.cpp b/examples/sinewave/sinewave_example.cpp index 8b50804..afffc82 100644 --- a/examples/sinewave/sinewave_example.cpp +++ b/examples/sinewave/sinewave_example.cpp @@ -10,17 +10,22 @@ #include -volatile sig_atomic_t keep_running{ 1 }; -void termination_handler(int signum){ +volatile sig_atomic_t keep_running {1}; + + +void termination_handler(int signum) +{ keep_running = false; } + using seconds = std::chrono::duration; using Clock = std::chrono::high_resolution_clock; -int main(int argc, char* argv[]) { +int main(int argc, char* argv[]) +{ struct sigaction int_action; int_action.sa_handler = termination_handler; @@ -28,7 +33,6 @@ int main(int argc, char* argv[]) { int_action.sa_flags = 0; sigaction(SIGINT, &int_action, NULL); - seconds interval{ 0.01 }; int i = 0; int samples = 0; @@ -51,19 +55,14 @@ int main(int argc, char* argv[]) { } catch (const std::invalid_argument& ia) { std::cerr << "Invalid argument: " << ia.what() << std::endl; - return 1; } - while (keep_running) { if (i >= samples) break; i++; - const auto endTime = Clock::now() + interval; - std::cout << hwd::sinewave::get_point() << std::endl; - std::this_thread::sleep_until(endTime); } diff --git a/library/src/sinewave.cpp b/library/src/sinewave.cpp index 04982df..23feae4 100644 --- a/library/src/sinewave.cpp +++ b/library/src/sinewave.cpp @@ -2,10 +2,11 @@ #include "client.h" -namespace hwd +namespace hwd { namespace sinewave { + void set_amplitude(double amplitude) { get_client().call("sinewave/set_amplitude", amplitude); -- cgit v1.1