allow ints, doubles, implicit conversions

This commit is contained in:
2026-04-16 16:07:02 +02:00
parent 97ed713d4d
commit f925c5b1a6
6 changed files with 36 additions and 19 deletions
+2
View File
@@ -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():
+23 -8
View File
@@ -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 <cmath>
#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_<slsDetectorDefs::Hz> Hz(m, "Hz");
Hz.def(py::init<int v) {
return slsDetectorDefs::Hz(v * 1000000);
}));
Hz.def(py::init([](const std::string &s) {
return sls::StringTo<slsDetectorDefs::Hz>(s);
Hz.def(py::init<int>());
Hz.def(py::init([](double v) {
return slsDetectorDefs::Hz(static_cast<int>(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<std::string, slsDetectorDefs::Hz>();
m.def("kHz", [](double v) {
return slsDetectorDefs::Hz(static_cast<int>(std::round(v * kHz)));
});
m.def("MHz", [](double v) {
return slsDetectorDefs::Hz(static_cast<int>(std::round(v * MHz)));
});
py::implicitly_convertible<int, slsDetectorDefs::Hz>();
py::implicitly_convertible<double, slsDetectorDefs::Hz>();
}
+4 -4
View File
@@ -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
@@ -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");
}
{
+2 -2
View File
@@ -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<int>(fval * 1e6));
} else if (unitLower == "khz") {
return T(static_cast<int>(fval * 1e3));
} else if (unitLower == "hz") {
} else if (unitLower.empty() || unitLower == "hz") {
return T(static_cast<int>(fval));
} else {
throw RuntimeError(
+1 -1
View File
@@ -147,7 +147,7 @@ TEST_CASE("string to std::chrono::duration", "[support]") {
}
TEST_CASE("string to frequency", "[support]") {
REQUIRE(StringTo<defs::Hz>("150Hz") == defs::Hz(150));
REQUIRE(StringTo<defs::Hz>("150") == defs::Hz(150));
REQUIRE(StringTo<defs::Hz>("150Hz") == defs::Hz(150));
REQUIRE(StringTo<defs::Hz>("1.5kHz") == defs::Hz(1500));
REQUIRE(StringTo<defs::Hz>("1.5MHz") == defs::Hz(1500000));