From 339b96cdd3cf12402b170c94f10084b8d7edc598 Mon Sep 17 00:00:00 2001 From: Martin Mueller Date: Wed, 18 Mar 2026 15:28:16 +0100 Subject: [PATCH] changed runclk command to use units and float, TODO: dbit, adcclk, why is everything called StringTo ? --- slsDetectorSoftware/include/sls/Detector.h | 4 +-- slsDetectorSoftware/src/Caller.cpp | 34 ++++++++++++++++------ slsDetectorSoftware/src/Detector.cpp | 4 +-- slsSupportLib/include/sls/ToString.h | 3 ++ slsSupportLib/src/ToString.cpp | 19 ++++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/slsDetectorSoftware/include/sls/Detector.h b/slsDetectorSoftware/include/sls/Detector.h index c4d5b670f..d07cfb360 100644 --- a/slsDetectorSoftware/include/sls/Detector.h +++ b/slsDetectorSoftware/include/sls/Detector.h @@ -1622,8 +1622,8 @@ class Detector { /** [CTB] in MHz, [XCTB] in kHz */ Result getRUNClock(Positions pos = {}) const; - /** [CTB] in MHz, [XCTB] in kHz */ - void setRUNClock(int value_in_MHz, Positions pos = {}); + /** [CTB] in Hz, [XCTB] in Hz */ + void setRUNClock(int value_in_Hz, Positions pos = {}); /** [CTB] in MHZ */ Result getSYNCClock(Positions pos = {}) const; diff --git a/slsDetectorSoftware/src/Caller.cpp b/slsDetectorSoftware/src/Caller.cpp index 067cd0e26..6cab6f85a 100644 --- a/slsDetectorSoftware/src/Caller.cpp +++ b/slsDetectorSoftware/src/Caller.cpp @@ -10211,9 +10211,7 @@ std::string Caller::runclk(int action) { std::ostringstream os; // print help if (action == slsDetectorDefs::HELP_ACTION) { - os << R"V0G0N([n_clk in MHz] - [Ctb] Run clock in MHz. - [xilinx Ctb] Run clock in kHz. )V0G0N" + os << R"V0G0N([Run clock frequency] [(optional unit) MHz|kHz|Hz])V0G0N" << std::endl; return os.str(); } @@ -10230,15 +10228,25 @@ std::string Caller::runclk(int action) { } else if (action == slsDetectorDefs::PUT_ACTION) { - if (1 && args.size() != 1) { + if (1 && args.size() != 1 && args.size() != 2) { throw RuntimeError("Wrong number of arguments for action PUT"); } if (args.size() == 1) { try { - StringTo(args[0]); + std::string tmp_freq(args[0]); + std::string unit = RemoveUnit(tmp_freq); + StringToHz(tmp_freq, unit); } catch (...) { - throw RuntimeError("Could not convert argument 0 to int"); + throw RuntimeError("Could not convert argument to frequency"); + } + } + + if (args.size() == 2) { + try { + StringToHz(args[0], args[1]); + } catch (...) { + throw RuntimeError("Could not convert arguments to frequency"); } } @@ -10260,9 +10268,17 @@ std::string Caller::runclk(int action) { if (action == slsDetectorDefs::PUT_ACTION) { if (args.size() == 1) { - auto arg0 = StringTo(args[0]); - det->setRUNClock(arg0, std::vector{det_id}); - os << args.front() << '\n'; + std::string tmp_freq(args[0]); + std::string unit = RemoveUnit(tmp_freq); + auto converted_freq_hz = StringToHz(tmp_freq, unit); + det->setRUNClock(converted_freq_hz, std::vector{det_id}); + os << args[0] << '\n'; + } + + if (args.size() == 2) { + auto converted_freq_hz = StringToHz(args[0], args[1]); + det->setRUNClock(converted_freq_hz, std::vector{det_id}); + os << args[0] << args[1] << '\n'; } } diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 39706ec56..b3318fc97 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2157,9 +2157,9 @@ Result Detector::getRUNClock(Positions pos) const { return pimpl->Parallel(&Module::getClockFrequency, pos, defs::RUN_CLOCK); } -void Detector::setRUNClock(int value_in_MHz, Positions pos) { +void Detector::setRUNClock(int value_in_Hz, Positions pos) { pimpl->Parallel(&Module::setClockFrequency, pos, defs::RUN_CLOCK, - value_in_MHz); + value_in_Hz); } Result Detector::getSYNCClock(Positions pos) const { diff --git a/slsSupportLib/include/sls/ToString.h b/slsSupportLib/include/sls/ToString.h index 5b56fbfc9..c9eb15f2b 100644 --- a/slsSupportLib/include/sls/ToString.h +++ b/slsSupportLib/include/sls/ToString.h @@ -353,4 +353,7 @@ std::vector StringTo(const std::vector &strings) { return result; } +/** Convert frequency string with unit (MHz, kHz, Hz) to Hz */ +int StringToHz(const std::string &s, const std::string &unit); + } // namespace sls diff --git a/slsSupportLib/src/ToString.cpp b/slsSupportLib/src/ToString.cpp index 98790f7cd..562fe9049 100644 --- a/slsSupportLib/src/ToString.cpp +++ b/slsSupportLib/src/ToString.cpp @@ -1181,4 +1181,23 @@ template <> int64_t StringTo(const std::string &s) { return std::stol(s, nullptr, base); } +int StringToHz(const std::string &s, const std::string &unit) { + double fval{0}; + try { + fval = std::stod(s); + } catch (const std::invalid_argument &e) { + throw RuntimeError("Could not convert string to frequency"); + } + if (unit.empty() || unit == "MHz") { + return static_cast(fval * 1000000.0); + } else if (unit == "kHz") { + return static_cast(fval * 1000.0); + } else if (unit == "Hz") { + return static_cast(fval); + } else { + throw RuntimeError("Unknown unit: " + unit + + ". Supported units: MHz, kHz, Hz"); + } +} + } // namespace sls \ No newline at end of file