From 97ed713d4d42351808ebe9204794231922bac68c Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Wed, 15 Apr 2026 17:27:55 +0200 Subject: [PATCH] in python, but need to change the default to Hz again for clean code and intuition --- python/CMakeLists.txt | 1 + python/slsdet/__init__.py | 1 + python/slsdet/detector.py | 23 +++++++++++++++--- python/src/frequency.cpp | 26 +++++++++++++++++++++ python/src/main.cpp | 2 ++ python/tests/test_freq.py | 14 +++++++++++ slsDetectorSoftware/generator/commands.yaml | 8 +++---- 7 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 python/src/frequency.cpp create mode 100644 python/tests/test_freq.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 13e6abb3d..ffec1ac60 100755 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -13,6 +13,7 @@ pybind11_add_module(_slsdet src/DurationWrapper.cpp src/pedestal.cpp src/bit.cpp + src/frequency.cpp ) target_link_libraries(_slsdet PUBLIC diff --git a/python/slsdet/__init__.py b/python/slsdet/__init__.py index 7af732cef..e57c190a5 100755 --- a/python/slsdet/__init__.py +++ b/python/slsdet/__init__.py @@ -34,6 +34,7 @@ scanParameters = _slsdet.scanParameters currentSrcParameters = _slsdet.currentSrcParameters DurationWrapper = _slsdet.DurationWrapper pedestalParameters = _slsdet.pedestalParameters +Hz = _slsdet.Hz import os def read_version(): diff --git a/python/slsdet/detector.py b/python/slsdet/detector.py index a5e152988..c993e2948 100755 --- a/python/slsdet/detector.py +++ b/python/slsdet/detector.py @@ -13,11 +13,12 @@ dacIndex = slsDetectorDefs.dacIndex detectorType = slsDetectorDefs.detectorType streamingInterface = slsDetectorDefs.streamingInterface + defs = slsDetectorDefs from .utils import element_if_equal, all_equal, get_set_bits, list_to_bitmask from .utils import Geometry, to_geo, element, reduce_time, is_iterable, hostname_list -from ._slsdet import xy, freeSharedMemory, getUserDetails +from ._slsdet import xy, Hz, freeSharedMemory, getUserDetails from .gaincaps import Mythen3GainCapsWrapper from . import utils as ut from .proxy import JsonProxy, SlowAdcProxy, ClkDivProxy, MaxPhaseProxy, ClkFreqProxy, PatLoopProxy, PatNLoopProxy, PatWaitProxy, PatWaitTimeProxy @@ -3524,18 +3525,23 @@ class Detector(CppDetectorApi): Accepts decimal inputs """ + return self.getRUNClock() + ''' freq_hz = element_if_equal(self.getRUNClock()) if isinstance(freq_hz, list): return [value / 1e6 for value in freq_hz] return freq_hz / 1e6 + ''' @runclk.setter def runclk(self, freq): + ''' if isinstance(freq, dict): freq_hz = {key: int(round(value * 1e6)) for key, value in freq.items()} else: freq_hz = int(round(freq * 1e6)) - ut.set_using_dict(self.setRUNClock, freq_hz) + ''' + ut.set_using_dict(self.setRUNClock, freq) @property @element @@ -3615,18 +3621,24 @@ class Detector(CppDetectorApi): [Ctb][Xilinx Ctb] Sets clock for latching the digital bits in MHz. \n Accepts decimal inputs """ + ''' freq_hz = element_if_equal(self.getDBITClock()) if isinstance(freq_hz, list): return [value / 1e6 for value in freq_hz] return freq_hz / 1e6 + ''' + return self.getDBITClock() @dbitclk.setter def dbitclk(self, value): + ''' if isinstance(value, dict): value_hz = {key: int(round(item * 1e6)) for key, item in value.items()} else: value_hz = int(round(value * 1e6)) ut.set_using_dict(self.setDBITClock, value_hz) + ''' + ut.set_using_dict(self.setDBITClock, value) @property @element @@ -3752,19 +3764,24 @@ class Detector(CppDetectorApi): [Ctb][Xilinx Ctb] Sets ADC clock frequency in MHz. \n Accepts decimal inputs """ - + ''' freq_hz = element_if_equal(self.getADCClock()) if isinstance(freq_hz, list): return [value / 1e6 for value in freq_hz] return freq_hz / 1e6 + ''' + return self.getADCClock() @adcclk.setter def adcclk(self, value): + ''' if isinstance(value, dict): value_hz = {key: int(round(item * 1e6)) for key, item in value.items()} else: value_hz = int(round(value * 1e6)) ut.set_using_dict(self.setADCClock, value_hz) + ''' + ut.set_using_dict(self.setADCClock, value) @property @element diff --git a/python/src/frequency.cpp b/python/src/frequency.cpp new file mode 100644 index 000000000..609450204 --- /dev/null +++ b/python/src/frequency.cpp @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: LGPL-3.0-or-other +// Copyright (C) 2021 Contributors to the SLS Detector Package +/* +This file contains Python bindings for the Hz and for conversion to other units from and to string. +*/ +#include "py_headers.h" + +#include "sls/ToString.h" +#include "sls/sls_detector_defs.h" + +namespace py = pybind11; +void init_freq(py::module &m) { + + py::class_ Hz(m, "Hz"); + Hz.def(py::init(s); + })); + 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(py::self == py::self); + py::implicitly_convertible(); +} \ No newline at end of file diff --git a/python/src/main.cpp b/python/src/main.cpp index 214f2b81c..6bc583cbd 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -21,6 +21,7 @@ void init_source(py::module &); void init_duration(py::module &); void init_pedestal(py::module &); void init_bit(py::module &); +void init_freq(py::module &); PYBIND11_MODULE(_slsdet, m) { m.doc() = R"pbdoc( @@ -42,6 +43,7 @@ PYBIND11_MODULE(_slsdet, m) { init_duration(m); init_pedestal(m); init_bit(m); + init_freq(m); // init_experimental(m); py::module io = m.def_submodule("io", "Submodule for io"); diff --git a/python/tests/test_freq.py b/python/tests/test_freq.py new file mode 100644 index 000000000..a4cb47127 --- /dev/null +++ b/python/tests/test_freq.py @@ -0,0 +1,14 @@ +from slsdet import Hz +''' +def test_Hz(): + f = Hz(1) + assert f.value() == 1e6 + f = Hz('1MHz') + assert f.value() == 1e6 + f = Hz('5000kHz') + assert f.value() == 5e6 + assert Hz(1) == 1 + assert Hz(1e6) == 1e6 + assert Hz(1, 'MHz') == 1e6 + assert Hz(0.5, 'GHz') == 0.5e9 +''' \ No newline at end of file diff --git a/slsDetectorSoftware/generator/commands.yaml b/slsDetectorSoftware/generator/commands.yaml index efe173a97..d55486029 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(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] ADC clock frequency." + help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\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(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] Run clock frequency." + help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\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(default)|kHz|MHz]\n\t[Ctb][Xilinx Ctb] Clock for latching the digital bits." + help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\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(default)|kHz|MHz]\n\t[Ctb] Sync clock." + help: "[n_clk] [(optional unit) Hz|kHz|MHz(default)]\n\t[Ctb] Sync clock." actions: GET: function: getSYNCClock