diff --git a/python/src/bit.cpp b/python/src/bit.cpp index e63db2471..6909900f5 100644 --- a/python/src/bit.cpp +++ b/python/src/bit.cpp @@ -22,7 +22,7 @@ void init_bit(py::module &m) { .def(py::init()) .def("__repr__", &RegisterAddress::str) .def("str", &RegisterAddress::str) - .def("uint32", [](const RegisterAddress &v) { return static_cast(v); }) + .def("value", &RegisterAddress::value) .def(py::self == py::self) .def(py::self != py::self); @@ -44,9 +44,12 @@ void init_bit(py::module &m) { .def(py::init()) .def("__repr__", &RegisterValue::str) .def("str", &RegisterValue::str) - .def("uint32", [](const RegisterValue &v) { return static_cast(v); }) + .def("value", &RegisterValue::value) .def(py::self == py::self) .def(py::self != py::self) + .def("__or__", [](const RegisterValue& v, uint32_t rhs) { + return v | rhs; + }) .def("__ior__", [](RegisterValue &v, uint32_t rhs) -> RegisterValue& { v |= rhs; return v; diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index a0d8eb84b..45726fad0 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2835,7 +2835,7 @@ Result Detector::readRegister(uint32_t addr, Positions pos) const { auto t = pimpl->readRegister(RegisterAddress(addr), pos); Result res; for (const auto &val : t) { - res.push_back(val); + res.push_back(val.value()); } return res; } diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index b13156eef..6167f585d 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -2916,32 +2916,29 @@ void Module::setUpdateMode(const bool updatemode) { } RegisterValue Module::readRegister(RegisterAddress addr) const { - return sendToDetectorStop(F_READ_REGISTER, addr); + return sendToDetectorStop(F_READ_REGISTER, addr.value()); } void Module::writeRegister(RegisterAddress addr, RegisterValue val, bool validate) { - uint32_t args[]{addr, val, static_cast(validate)}; + uint32_t args[]{addr.value(), val.value(), static_cast(validate)}; return sendToDetectorStop(F_WRITE_REGISTER, args, nullptr); } void Module::setBit(BitAddress bitAddr, bool validate) { - uint32_t args[] = {bitAddr.address(), - static_cast(bitAddr.bitPosition()), + uint32_t args[] = {bitAddr.address().value(), bitAddr.bitPosition(), static_cast(validate)}; sendToDetectorStop(F_SET_BIT, args, nullptr); } void Module::clearBit(BitAddress bitAddr, bool validate) { - uint32_t args[] = {bitAddr.address(), - static_cast(bitAddr.bitPosition()), + uint32_t args[] = {bitAddr.address().value(), bitAddr.bitPosition(), static_cast(validate)}; sendToDetectorStop(F_CLEAR_BIT, args, nullptr); } int Module::getBit(BitAddress bitAddr) const { - uint32_t args[2] = {bitAddr.address(), - static_cast(bitAddr.bitPosition())}; + uint32_t args[2] = {bitAddr.address().value(), bitAddr.bitPosition()}; return sendToDetectorStop(F_GET_BIT, args); } diff --git a/slsSupportLib/include/sls/bit_utils.h b/slsSupportLib/include/sls/bit_utils.h index df58eec14..649ca8bb5 100644 --- a/slsSupportLib/include/sls/bit_utils.h +++ b/slsSupportLib/include/sls/bit_utils.h @@ -22,21 +22,20 @@ template std::vector getSetBits(T val) { class RegisterAddress { private: - uint32_t addr_{0}; + uint32_t value_{0}; public: constexpr RegisterAddress() noexcept = default; - constexpr explicit RegisterAddress(uint32_t address) : addr_(address) {} - explicit RegisterAddress(const std::string &address); - + constexpr explicit RegisterAddress(uint32_t value) : value_(value) {} + explicit RegisterAddress(const std::string &value); std::string str() const; - operator uint32_t() const noexcept { return addr_; } + uint32_t value() const noexcept { return value_; } constexpr bool operator==(const RegisterAddress &other) const { - return (addr_ == other.addr_); + return (value_ == other.value_); } constexpr bool operator!=(const RegisterAddress &other) const { - return (addr_ != other.addr_); + return (value_ != other.value_); } }; @@ -50,7 +49,6 @@ class BitAddress { BitAddress(RegisterAddress address, uint32_t bitPosition); BitAddress(const std::string &address, const std::string &bitPosition); std::string str() const; - RegisterAddress address() const noexcept { return addr_; } uint32_t bitPosition() const noexcept { return bitPos_; } @@ -72,19 +70,26 @@ class RegisterValue { explicit RegisterValue(const std::string &value); std::string str() const; - operator uint32_t() const noexcept { return value_; } + uint32_t value() const noexcept { return value_; } RegisterValue &operator|=(uint32_t rhs) noexcept { value_ |= rhs; return *this; } + + RegisterValue operator|(uint32_t rhs) const noexcept { + RegisterValue tmp(*this); + tmp |= rhs; + return tmp; + } + constexpr bool operator==(const RegisterValue &other) const noexcept { return value_ == other.value_; } constexpr bool operator!=(const RegisterValue &other) const noexcept { return value_ != other.value_; } -} __attribute__((packed)); +}; std::ostream &operator<<(std::ostream &os, const RegisterAddress &r); std::ostream &operator<<(std::ostream &os, const BitAddress &r); diff --git a/slsSupportLib/src/bit_utils.cpp b/slsSupportLib/src/bit_utils.cpp index c8ffcc37c..e779cceb0 100644 --- a/slsSupportLib/src/bit_utils.cpp +++ b/slsSupportLib/src/bit_utils.cpp @@ -6,15 +6,14 @@ namespace sls { -RegisterAddress::RegisterAddress(const std::string &address) { - if (!is_hex_or_dec_uint(address)) { +RegisterAddress::RegisterAddress(const std::string &value) { + if (!is_hex_or_dec_uint(value)) { throw RuntimeError("Address must be an integer value."); } - uint32_t addr = StringTo(address); - addr_ = addr; + value_ = StringTo(value); } -std::string RegisterAddress::str() const { return ToStringHex(addr_); } +std::string RegisterAddress::str() const { return ToStringHex(value_); } BitAddress::BitAddress(RegisterAddress address, uint32_t bitPosition) : addr_(address) { @@ -47,8 +46,7 @@ RegisterValue::RegisterValue(const std::string &value) { if (!is_hex_or_dec_uint(value)) { throw RuntimeError("Value must be an integer value."); } - uint32_t val = StringTo(value); - value_ = val; + value_ = StringTo(value); } std::string RegisterValue::str() const { return ToStringHex(value_); } diff --git a/slsSupportLib/tests/test-bit_utils.cpp b/slsSupportLib/tests/test-bit_utils.cpp index 007daaea9..7fe022d67 100644 --- a/slsSupportLib/tests/test-bit_utils.cpp +++ b/slsSupportLib/tests/test-bit_utils.cpp @@ -57,12 +57,10 @@ TEST_CASE("Convert RegisterAddress using classes ", "[support][.bit_utils]") { CHECK(reg0 == reg1); if (i != 0) CHECK(reg2 != reg1); - CHECK(reg0 == vec_addr[i]); - CHECK(reg1 == vec_addr[i]); + CHECK(reg0.value() == vec_addr[i]); + CHECK(reg1.value() == vec_addr[i]); CHECK(reg0.str() == vec_ans[i]); CHECK(reg1.str() == vec_ans[i]); - CHECK(static_cast(reg0) == vec_addr[i]); - CHECK(static_cast(reg1) == vec_addr[i]); } } @@ -80,13 +78,11 @@ TEST_CASE("Convert RegisterValue using classes ", "[support][.bit_utils]") { CHECK(reg0 == reg1); if (i != 0) CHECK(reg2 != reg1); - CHECK(reg0 == vec_addr[i]); - CHECK(reg1 == vec_addr[i]); + CHECK(reg0.value() == vec_addr[i]); + CHECK(reg1.value() == vec_addr[i]); CHECK(reg0.str() == vec_ans[i]); CHECK(reg1.str() == vec_ans[i]); - CHECK(static_cast(reg0) == vec_addr[i]); - CHECK(static_cast(reg1) == vec_addr[i]); - CHECK((reg0 | 0xffffffffu) == 0xffffffffu); + CHECK((reg0 | 0xffffffffu) == RegisterValue(0xffffffffu)); CHECK((reg0 | 0x0) == reg0); } } @@ -190,15 +186,4 @@ TEST_CASE("Strange input throws", "[support][.bit_utils]") { } } -TEST_CASE("Implicitly convert to uint for sending over network", - "[support][.bit_utils]") { - RegisterAddress addr{0x305}; - uint64_t a = addr; - CHECK(a == 0x305); - - RegisterValue addr1{0x305}; - uint64_t a1 = addr1; - CHECK(a1 == 0x305); -} - } // namespace sls