diff --git a/python/slsdet/__init__.py b/python/slsdet/__init__.py index e57c190a5..50abdb59e 100755 --- a/python/slsdet/__init__.py +++ b/python/slsdet/__init__.py @@ -35,6 +35,8 @@ currentSrcParameters = _slsdet.currentSrcParameters DurationWrapper = _slsdet.DurationWrapper pedestalParameters = _slsdet.pedestalParameters Hz = _slsdet.Hz +kHz = _slsdet.kHz +MHz = _slsdet.MHz import os def read_version(): diff --git a/python/src/frequency.cpp b/python/src/frequency.cpp index 609450204..792ebf302 100644 --- a/python/src/frequency.cpp +++ b/python/src/frequency.cpp @@ -4,23 +4,38 @@ This file contains Python bindings for the Hz and for conversion to other units from and to string. */ #include "py_headers.h" +#include #include "sls/ToString.h" #include "sls/sls_detector_defs.h" namespace py = pybind11; + +constexpr double kHz = 1e3; +constexpr double MHz = 1e6; + void init_freq(py::module &m) { py::class_ Hz(m, "Hz"); - Hz.def(py::init(s); + Hz.def(py::init()); + Hz.def(py::init([](double v) { + return slsDetectorDefs::Hz(static_cast(std::round(v))); })); Hz.def_readwrite("value", &slsDetectorDefs::Hz::value); - Hz.def("__repr__", [](const slsDetectorDefs::Hz &f) { return sls::ToString(f); }); - Hz.def("__str__", [](const slsDetectorDefs::Hz &f) { return sls::ToString(f); }); + Hz.def("__repr__", [](const slsDetectorDefs::Hz &f) { + return sls::ToString(f); + }); + Hz.def("__str__", [](const slsDetectorDefs::Hz &f) { + return sls::ToString(f); + }); Hz.def(py::self == py::self); - py::implicitly_convertible(); + m.def("kHz", [](double v) { + return slsDetectorDefs::Hz(static_cast(std::round(v * kHz))); + }); + + m.def("MHz", [](double v) { + return slsDetectorDefs::Hz(static_cast(std::round(v * MHz))); + }); + py::implicitly_convertible(); + py::implicitly_convertible(); } \ No newline at end of file diff --git a/slsDetectorSoftware/generator/commands.yaml b/slsDetectorSoftware/generator/commands.yaml index d55486029..efe173a97 100644 --- a/slsDetectorSoftware/generator/commands.yaml +++ b/slsDetectorSoftware/generator/commands.yaml @@ -481,7 +481,7 @@ CTB_GET_INDEX: ################# COMMANDS ################################## ################# FREQ_COMMAND ############# adcclk: - help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\n\t[Ctb][Xilinx Ctb] ADC clock frequency." + help: "[n_clk] [(optional unit) Hz(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] ADC clock frequency." inherit_actions: FREQ_COMMAND actions: GET: @@ -490,7 +490,7 @@ adcclk: function: setADCClock runclk: - help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\n\t[Ctb][Xilinx Ctb] Run clock frequency." + help: "[n_clk] [(optional unit) Hz(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] Run clock frequency." inherit_actions: FREQ_COMMAND actions: GET: @@ -500,7 +500,7 @@ runclk: dbitclk: - help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\n\t[Ctb][Xilinx Ctb] Clock for latching the digital bits." + help: "[n_clk] [(optional unit) Hz(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] Clock for latching the digital bits." inherit_actions: FREQ_COMMAND actions: GET: @@ -512,7 +512,7 @@ dbitclk: syncclk: inherit_actions: FREQ_GET_COMMAND - help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\n\t[Ctb] Sync clock." + help: "[n_clk] [(optional unit) Hz(default)|kHz|MHz]\n\t[Ctb] Sync clock." actions: GET: function: getSYNCClock diff --git a/slsDetectorSoftware/tests/Caller/test-Caller-chiptestboard.cpp b/slsDetectorSoftware/tests/Caller/test-Caller-chiptestboard.cpp index 47f03254f..035263c8a 100644 --- a/slsDetectorSoftware/tests/Caller/test-Caller-chiptestboard.cpp +++ b/slsDetectorSoftware/tests/Caller/test-Caller-chiptestboard.cpp @@ -787,13 +787,13 @@ TEST_CASE("runclk", "[.detectorintegration]") { { std::ostringstream oss; - caller.call("runclk", {"20"}, -1, PUT, oss); - REQUIRE(oss.str() == "runclk 20\n"); + caller.call("runclk", {"20MHz"}, -1, PUT, oss); + REQUIRE(oss.str() == "runclk 20MHz\n"); } { std::ostringstream oss; - caller.call("runclk", {"10"}, -1, PUT, oss); - REQUIRE(oss.str() == "runclk 10\n"); + caller.call("runclk", {"10000000"}, -1, PUT, oss); + REQUIRE(oss.str() == "runclk 10000000\n"); } { diff --git a/slsSupportLib/include/sls/ToString.h b/slsSupportLib/include/sls/ToString.h index f38fa0019..01ce73415 100644 --- a/slsSupportLib/include/sls/ToString.h +++ b/slsSupportLib/include/sls/ToString.h @@ -354,11 +354,11 @@ T StringTo(const std::string &f, const std::string &unit) { return result; }(); - if (unitLower.empty() || unitLower == "mhz") { + if (unitLower == "mhz") { return T(static_cast(fval * 1e6)); } else if (unitLower == "khz") { return T(static_cast(fval * 1e3)); - } else if (unitLower == "hz") { + } else if (unitLower.empty() || unitLower == "hz") { return T(static_cast(fval)); } else { throw RuntimeError( diff --git a/slsSupportLib/tests/test-ToString.cpp b/slsSupportLib/tests/test-ToString.cpp index d920838a0..5e0d56d39 100644 --- a/slsSupportLib/tests/test-ToString.cpp +++ b/slsSupportLib/tests/test-ToString.cpp @@ -147,7 +147,7 @@ TEST_CASE("string to std::chrono::duration", "[support]") { } TEST_CASE("string to frequency", "[support]") { - REQUIRE(StringTo("150Hz") == defs::Hz(150)); + REQUIRE(StringTo("150") == defs::Hz(150)); REQUIRE(StringTo("150Hz") == defs::Hz(150)); REQUIRE(StringTo("1.5kHz") == defs::Hz(1500)); REQUIRE(StringTo("1.5MHz") == defs::Hz(1500000));