diff --git a/python/slsdet/dacs.py b/python/slsdet/dacs.py index 5628eb3a6..7b454e783 100755 --- a/python/slsdet/dacs.py +++ b/python/slsdet/dacs.py @@ -59,7 +59,10 @@ class DetectorDacs: def __getattr__(self, name): return self.__getattribute__('_' + name) - + @property + def dacnames(self): + return [_d[0] for _d in _dacs] + def __setattr__(self, name, value): if name in self._dacnames: return self.__getattribute__('_' + name).__setitem__(slice(None, None, None), value) diff --git a/python/src/detector.cpp b/python/src/detector.cpp index 45ad603ed..e6332a37f 100644 --- a/python/src/detector.cpp +++ b/python/src/detector.cpp @@ -1427,6 +1427,20 @@ void init_det(py::module &m) { (void (Detector::*)(bool, sls::Positions)) & Detector::setLEDEnable, py::arg(), py::arg() = Positions{}) + .def("setDacNames", + (void (Detector::*)(const std::vector)) & + Detector::setDacNames, + py::arg()) + .def("getDacNames", (std::vector(Detector::*)() const) & + Detector::getDacNames) + .def("decodeNamedDac", + (defs::dacIndex(Detector::*)(const std::string &)) & + Detector::decodeNamedDac, + py::arg()) + .def("decodeNamedDac", + (std::string(Detector::*)(defs::dacIndex)) & + Detector::decodeNamedDac, + py::arg()) .def("setPattern", (void (Detector::*)(const std::string &, sls::Positions)) & Detector::setPattern, diff --git a/slsDetectorSoftware/include/sls/Detector.h b/slsDetectorSoftware/include/sls/Detector.h index 121403418..54ab6c68d 100644 --- a/slsDetectorSoftware/include/sls/Detector.h +++ b/slsDetectorSoftware/include/sls/Detector.h @@ -1613,6 +1613,14 @@ class Detector { /** [CTB] Default is enabled. */ void setLEDEnable(bool enable, Positions pos = {}); + + + void setDacNames(const std::vector names); + + std::vector getDacNames() const; + + defs::dacIndex decodeNamedDac(const std::string& name); + std::string decodeNamedDac(defs::dacIndex i); ///@} /** @name Pattern */ diff --git a/slsDetectorSoftware/src/CmdProxy.cpp b/slsDetectorSoftware/src/CmdProxy.cpp index 0e8a1a3db..1f49873c6 100644 --- a/slsDetectorSoftware/src/CmdProxy.cpp +++ b/slsDetectorSoftware/src/CmdProxy.cpp @@ -1056,11 +1056,11 @@ std::string CmdProxy::TemperatureValues(int action) { std::string CmdProxy::Dac(int action) { std::ostringstream os; os << cmd << ' '; - + auto type = det->getDetectorType().squash(); // dac indices only for ctb if (args.size() > 0 && action != defs::HELP_ACTION) { if (is_int(args[0]) && - det->getDetectorType().squash() != defs::CHIPTESTBOARD) { + type != defs::CHIPTESTBOARD) { throw sls::RuntimeError( "Dac indices can only be used for chip test board. Use daclist " "to get list of dac names for current detector."); @@ -1077,7 +1077,13 @@ std::string CmdProxy::Dac(int action) { if (args.empty()) WrongNumberOfParameters(1); // This prints slightly wrong - defs::dacIndex dacIndex = StringTo(args[0]); + defs::dacIndex dacIndex{}; + if (type == defs::CHIPTESTBOARD && !is_int(args[0])){ + dacIndex = det->decodeNamedDac(args[0]); + }else{ + dacIndex = StringTo(args[0]); + } + bool mV = false; if (args.size() == 2) { @@ -1095,7 +1101,11 @@ std::string CmdProxy::Dac(int action) { if (args.empty()) WrongNumberOfParameters(1); // This prints slightly wrong - defs::dacIndex dacIndex = StringTo(args[0]); + defs::dacIndex dacIndex{}; + if (type == defs::CHIPTESTBOARD && !is_int(args[0])) + dacIndex = det->decodeNamedDac(args[0]); + else + dacIndex = StringTo(args[0]); bool mV = false; if (args.size() == 3) { if ((args[2] != "mv") && (args[2] != "mV")) { @@ -1134,13 +1144,15 @@ std::string CmdProxy::DacValues(int action) { WrongNumberOfParameters(1); } auto t = det->getDacList(); + auto names = det->getDacNames(); + auto name_it = names.begin(); os << '['; auto it = t.cbegin(); - os << ToString(*it) << ' '; + os << ToString(*name_it++) << ' '; os << OutString(det->getDAC(*it++, mv, std::vector{det_id})) << (!args.empty() ? " mV" : ""); while (it != t.cend()) { - os << ", " << ToString(*it) << ' '; + os << ", " << ToString(*name_it++) << ' '; os << OutString(det->getDAC(*it++, mv, std::vector{det_id})) << (!args.empty() ? " mV" : ""); } diff --git a/slsDetectorSoftware/src/CmdProxy.h b/slsDetectorSoftware/src/CmdProxy.h index 0f8952c91..33969f70a 100644 --- a/slsDetectorSoftware/src/CmdProxy.h +++ b/slsDetectorSoftware/src/CmdProxy.h @@ -1466,7 +1466,7 @@ class CmdProxy { /* dacs */ GET_COMMAND_NOID( - daclist, getDacList, + daclist, getDacNames, "\n\tGets the list of commands for every dac for this detector."); /* on chip dacs */ diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 8aed2b2b1..7bc8347cb 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2067,6 +2067,69 @@ void Detector::setLEDEnable(bool enable, Positions pos) { pimpl->Parallel(&Module::setLEDEnable, pos, enable); } +void Detector::setDacNames(const std::vector names) { + if (getDetectorType().squash() != defs::CHIPTESTBOARD) + throw RuntimeError("Named dacs only for CTB"); + if (names.size() != 18) + throw RuntimeError("Need to set all 18 dacs when naming dacs"); + + std::ofstream ofs("/dev/shm/slsDetectorPackage_ctbdacnames"); + if (!ofs) + throw RuntimeError("Failed to open dacnames file in shared memory"); + + std::string s = sls::ToString(names); + ofs.write(&s[0], s.size()); +} + +std::vector Detector::getDacNames() const { + + auto type = getDetectorType().squash(); + if (type == defs::CHIPTESTBOARD) { + try { + std::ifstream ifs("/dev/shm/slsDetectorPackage_ctbdacnames"); + if (!ifs) + throw RuntimeError("Could not read dacnames form shm"); + std::string dacnames; + ifs.seekg(0, std::ios::end); + dacnames.resize(ifs.tellg()); + ifs.seekg(0, std::ios::beg); + ifs.read(&dacnames[0], dacnames.size()); + + std::string chars = "[] "; + + dacnames.erase(std::remove_if(dacnames.begin(), dacnames.end(), + [&chars](const char &c) { + return chars.find(c) != + std::string::npos; + }), + dacnames.end()); + auto names = sls::split(dacnames, ','); + return names; + } catch (...) { + } + } + std::vector names; + for (const auto& index : getDacList()) + names.push_back(ToString(index)); + return names; +} + +defs::dacIndex Detector::decodeNamedDac(const std::string &name) { + auto names = getDacNames(); + auto it = std::find(names.begin(), names.end(), name); + if (it == names.end()) + throw RuntimeError("Dacname not found"); + return static_cast(it - names.begin()); +} + +std::string Detector::decodeNamedDac(defs::dacIndex i) { + auto names = getDacNames(); + auto index = static_cast(i); + if (index > names.size()) + throw RuntimeError("Dac index out of range"); + return names[index]; +} + // Pattern void Detector::setPattern(const std::string &fname, Positions pos) {