mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-05-13 14:55:35 +02:00
Dev/define cmd (#1312)
* basic ctb config api for register and bit names * tests for define and definelist pass. yet to implement using them for reg, setbit, clearbit and getbit * improved autocomplete for getbit,setbit, clearbit * validate autocomplete * definelist has no put * updating help * converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both * removed std::vector<std::pair<string,int> to std::map<string, int> for defiitions list * Dev/define cmd tie bit to reg (#1328) * strong type * moved everythign to bit_utils class * pybindings * added tests for python * removed duplicates * removed bit names in reg * changed BitPosition to BitAddress * Using define reg/bit from python (#1344) * define_bit, define_addr in python. * setBit/clearBit takes int or addr * added example using bits * split define into 2 commands define_reg and define_bit, definelist into 2: definelist_reg and definelist_bit * allow string for register and bit names in c++ api * refactor from github comments * naming refactoring (getRegisterDefnition to retunr name and address specifically * added marker for 8 cmd tests connected to define, changed macro to static constexpr * changed bitPosition from int to uint32_t * got rid of setbitposition and setaddress, instead overloaded constructor to take in strings so that the conversion from string to bit address members, takes place within the class for easy maintainance in case type changes * Removing implicit conversions: RegisterAddresss and RegisterValue: Removed the implicit conversions. RegisterAddress: Changed member name from address_ to value_ and method as well to value(). RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify * Allowed concatenation with other RegisterValue, made them all constexpr * fix a ctbConfig test * Maponstack works with integration tests, but need unit tests * tests on mapstack * fixed ctb tests and FixedString being initialized with gibberish * removing parsing from string inside the class RegisterAddress, BitAddress and RegisterValue * updated python bindings * fixed bit utils test * renaming getRegisterDefintiionAddress/Name=>getRegisterAddress/Name and similary for getBitDefinitionAddress/Name * updated python bindings * fix tests (format) * a few python tests added and python bindings corrected * replaceing str with __str__ for bit.cpp * repr reimplemented for bit.cpp * removed make with registerAddress etc * starting server for tests per session and nor module * killprocess throws if no process found-> github runs fails, changed to pkill and not throw * clean shm shouldnt raise, in ci binary not found * ignoring these tests for CI, which fail on CI because simulators are not generated in CI. This is in another PR, where it should work --------- Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com> Co-authored-by: froejdh_e <erik.frojdh@psi.ch>
This commit is contained in:
@@ -28,214 +28,298 @@ CtbConfig::CtbConfig() {
|
||||
for (size_t i = 0; i != num_slowADCs; ++i) {
|
||||
setSlowADCName(i, "SLOWADC" + ToString(i));
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_dac_index(size_t i) const {
|
||||
if (i >= num_dacs) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid DAC index. Options: 0 - " << num_dacs;
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_adc_index(size_t i) const {
|
||||
if (i >= num_adcs) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid ADC index. Options: 0 - " << num_adcs;
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_signal_index(size_t i) const {
|
||||
if (i >= num_signals) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid Signal index. Options: 0 - " << num_signals;
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_power_index(size_t i) const {
|
||||
if (i >= num_powers) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid Power index. Options: 0 - " << num_powers
|
||||
<< " or V_POWER_A - V_POWER_IO";
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_slow_adc_index(size_t i) const {
|
||||
if (i >= num_slowADCs) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid Slow ADC index. Options: 0 - " << num_slowADCs
|
||||
<< " or SLOW_ADC0 - SLOW_ADC7";
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::check_size(const std::string &name) const {
|
||||
|
||||
if (name.empty())
|
||||
throw RuntimeError("Name needs to be at least one character");
|
||||
|
||||
// name_length -1 to account for \0 termination
|
||||
if (!(name.size() < (name_length - 1))) {
|
||||
std::ostringstream oss;
|
||||
oss << "Length of name needs to be less than " << name_length - 1
|
||||
<< " chars";
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::setDacName(size_t index, const std::string &name) {
|
||||
check_dac_index(index);
|
||||
check_size(name);
|
||||
char *dst = &dacnames[index * name_length];
|
||||
memset(dst, '\0', name_length);
|
||||
memcpy(dst, &name[0], name.size());
|
||||
}
|
||||
|
||||
void CtbConfig::setDacNames(const std::vector<std::string> &names) {
|
||||
if (names.size() != num_dacs) {
|
||||
throw RuntimeError("Dac names need to be of size " +
|
||||
std::to_string(num_dacs));
|
||||
}
|
||||
for (size_t i = 0; i != num_dacs; ++i) {
|
||||
setDacName(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getDacName(size_t index) const {
|
||||
check_dac_index(index);
|
||||
return dacnames + index * name_length;
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getDacNames() const {
|
||||
std::vector<std::string> names;
|
||||
for (size_t i = 0; i != num_dacs; ++i)
|
||||
names.push_back(getDacName(i));
|
||||
return names;
|
||||
}
|
||||
|
||||
void CtbConfig::setAdcName(size_t index, const std::string &name) {
|
||||
check_adc_index(index);
|
||||
check_size(name);
|
||||
char *dst = &adcnames[index * name_length];
|
||||
memset(dst, '\0', name_length);
|
||||
memcpy(dst, &name[0], name.size());
|
||||
}
|
||||
|
||||
void CtbConfig::setAdcNames(const std::vector<std::string> &names) {
|
||||
if (names.size() != num_adcs) {
|
||||
throw RuntimeError("Adc names need to be of size " +
|
||||
std::to_string(num_adcs));
|
||||
}
|
||||
for (size_t i = 0; i != num_adcs; ++i) {
|
||||
setAdcName(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getAdcName(size_t index) const {
|
||||
check_adc_index(index);
|
||||
return adcnames + index * name_length;
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getAdcNames() const {
|
||||
std::vector<std::string> names;
|
||||
for (size_t i = 0; i != num_adcs; ++i)
|
||||
names.push_back(getAdcName(i));
|
||||
return names;
|
||||
}
|
||||
|
||||
void CtbConfig::setSignalName(size_t index, const std::string &name) {
|
||||
check_signal_index(index);
|
||||
check_size(name);
|
||||
char *dst = &signalnames[index * name_length];
|
||||
memset(dst, '\0', name_length);
|
||||
memcpy(dst, &name[0], name.size());
|
||||
}
|
||||
|
||||
void CtbConfig::setSignalNames(const std::vector<std::string> &names) {
|
||||
if (names.size() != num_signals) {
|
||||
throw RuntimeError("Signal names need to be of size " +
|
||||
std::to_string(num_signals));
|
||||
}
|
||||
for (size_t i = 0; i != num_signals; ++i) {
|
||||
setSignalName(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getSignalName(size_t index) const {
|
||||
check_signal_index(index);
|
||||
return signalnames + index * name_length;
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getSignalNames() const {
|
||||
std::vector<std::string> names;
|
||||
for (size_t i = 0; i != num_signals; ++i)
|
||||
names.push_back(getSignalName(i));
|
||||
return names;
|
||||
}
|
||||
|
||||
void CtbConfig::setPowerName(size_t index, const std::string &name) {
|
||||
check_power_index(index);
|
||||
check_size(name);
|
||||
char *dst = &powernames[index * name_length];
|
||||
memset(dst, '\0', name_length);
|
||||
memcpy(dst, &name[0], name.size());
|
||||
}
|
||||
|
||||
void CtbConfig::setPowerNames(const std::vector<std::string> &names) {
|
||||
if (names.size() != num_powers) {
|
||||
throw RuntimeError("Power names need to be of size " +
|
||||
std::to_string(num_powers));
|
||||
}
|
||||
for (size_t i = 0; i != num_powers; ++i) {
|
||||
setPowerName(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getPowerName(size_t index) const {
|
||||
check_power_index(index);
|
||||
return powernames + index * name_length;
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getPowerNames() const {
|
||||
std::vector<std::string> names;
|
||||
for (size_t i = 0; i != num_powers; ++i)
|
||||
names.push_back(getPowerName(i));
|
||||
return names;
|
||||
}
|
||||
|
||||
void CtbConfig::setSlowADCName(size_t index, const std::string &name) {
|
||||
check_slow_adc_index(index);
|
||||
check_size(name);
|
||||
char *dst = &slowADCnames[index * name_length];
|
||||
memset(dst, '\0', name_length);
|
||||
memcpy(dst, &name[0], name.size());
|
||||
}
|
||||
|
||||
void CtbConfig::setSlowADCNames(const std::vector<std::string> &names) {
|
||||
if (names.size() != num_slowADCs) {
|
||||
throw RuntimeError("Slow ADC names need to be of size " +
|
||||
std::to_string(num_slowADCs));
|
||||
}
|
||||
for (size_t i = 0; i != num_slowADCs; ++i) {
|
||||
setSlowADCName(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getSlowADCName(size_t index) const {
|
||||
check_slow_adc_index(index);
|
||||
return slowADCnames + index * name_length;
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getSlowADCNames() const {
|
||||
std::vector<std::string> names;
|
||||
for (size_t i = 0; i != num_slowADCs; ++i)
|
||||
names.push_back(getSlowADCName(i));
|
||||
return names;
|
||||
registers.clear();
|
||||
bits.clear();
|
||||
}
|
||||
|
||||
const char *CtbConfig::shm_tag() { return shm_tag_; }
|
||||
|
||||
void CtbConfig::check_index(size_t index, size_t max, const std::string &name,
|
||||
const std::string &suffix) const {
|
||||
if (index >= max) {
|
||||
std::ostringstream oss;
|
||||
oss << "Invalid " << name << " index. Options: 0 - " << max;
|
||||
if (!suffix.empty()) {
|
||||
oss << " " << suffix;
|
||||
}
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::set_name(const std::string &name, char dst[][name_length],
|
||||
size_t index) {
|
||||
if (name.empty())
|
||||
throw RuntimeError("Name needs to be at least one character");
|
||||
|
||||
strcpy_checked<name_length>(dst[index], name);
|
||||
}
|
||||
|
||||
void CtbConfig::setNames(const std::vector<std::string> &names,
|
||||
size_t expected_size,
|
||||
void (CtbConfig::*setNameFunc)(size_t,
|
||||
const std::string &)) {
|
||||
if (names.size() != expected_size) {
|
||||
throw RuntimeError("Name list need to be of size " +
|
||||
std::to_string(expected_size));
|
||||
}
|
||||
for (size_t i = 0; i != expected_size; ++i) {
|
||||
(this->*setNameFunc)(i, names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string>
|
||||
CtbConfig::getNames(size_t expected_size,
|
||||
std::string (CtbConfig::*getNameFunc)(size_t) const) const {
|
||||
std::vector<std::string> result;
|
||||
result.reserve(expected_size);
|
||||
for (size_t i = 0; i != expected_size; ++i) {
|
||||
result.push_back((this->*getNameFunc)(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void CtbConfig::setDacName(size_t index, const std::string &name) {
|
||||
check_index(index, num_dacs, "DAC");
|
||||
set_name(name, dacnames, index);
|
||||
}
|
||||
|
||||
void CtbConfig::setDacNames(const std::vector<std::string> &names) {
|
||||
setNames(names, num_dacs, &CtbConfig::setDacName);
|
||||
}
|
||||
|
||||
std::string CtbConfig::getDacName(size_t index) const {
|
||||
check_index(index, num_dacs, "DAC");
|
||||
return dacnames[index];
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getDacNames() const {
|
||||
return getNames(num_dacs, &CtbConfig::getDacName);
|
||||
}
|
||||
|
||||
void CtbConfig::setAdcName(size_t index, const std::string &name) {
|
||||
check_index(index, num_adcs, "ADC");
|
||||
set_name(name, adcnames, index);
|
||||
}
|
||||
|
||||
void CtbConfig::setAdcNames(const std::vector<std::string> &names) {
|
||||
setNames(names, num_adcs, &CtbConfig::setAdcName);
|
||||
}
|
||||
|
||||
std::string CtbConfig::getAdcName(size_t index) const {
|
||||
check_index(index, num_adcs, "ADC");
|
||||
return adcnames[index];
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getAdcNames() const {
|
||||
return getNames(num_adcs, &CtbConfig::getAdcName);
|
||||
}
|
||||
|
||||
void CtbConfig::setSignalName(size_t index, const std::string &name) {
|
||||
check_index(index, num_signals, "Signal");
|
||||
set_name(name, signalnames, index);
|
||||
}
|
||||
|
||||
void CtbConfig::setSignalNames(const std::vector<std::string> &names) {
|
||||
setNames(names, num_signals, &CtbConfig::setSignalName);
|
||||
}
|
||||
|
||||
std::string CtbConfig::getSignalName(size_t index) const {
|
||||
check_index(index, num_signals, "Signal");
|
||||
return signalnames[index];
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getSignalNames() const {
|
||||
return getNames(num_signals, &CtbConfig::getSignalName);
|
||||
}
|
||||
|
||||
void CtbConfig::setPowerName(size_t index, const std::string &name) {
|
||||
check_index(index, num_powers, "Power");
|
||||
set_name(name, powernames, index);
|
||||
}
|
||||
|
||||
void CtbConfig::setPowerNames(const std::vector<std::string> &names) {
|
||||
setNames(names, num_powers, &CtbConfig::setPowerName);
|
||||
}
|
||||
|
||||
std::string CtbConfig::getPowerName(size_t index) const {
|
||||
check_index(index, num_powers, "Power");
|
||||
return powernames[index];
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getPowerNames() const {
|
||||
return getNames(num_powers, &CtbConfig::getPowerName);
|
||||
}
|
||||
|
||||
void CtbConfig::setSlowADCName(size_t index, const std::string &name) {
|
||||
check_index(index, num_slowADCs, "Slow ADC", "or SLOW_ADC0 - SLOW_ADC7");
|
||||
set_name(name, slowADCnames, index);
|
||||
}
|
||||
|
||||
void CtbConfig::setSlowADCNames(const std::vector<std::string> &names) {
|
||||
setNames(names, num_slowADCs, &CtbConfig::setSlowADCName);
|
||||
}
|
||||
|
||||
std::string CtbConfig::getSlowADCName(size_t index) const {
|
||||
check_index(index, num_slowADCs, "Slow ADC", "or SLOW_ADC0 - SLOW_ADC7");
|
||||
return slowADCnames[index];
|
||||
}
|
||||
|
||||
std::vector<std::string> CtbConfig::getSlowADCNames() const {
|
||||
return getNames(num_slowADCs, &CtbConfig::getSlowADCName);
|
||||
}
|
||||
|
||||
int CtbConfig::getRegisterNamesCount() const { return registers.size(); }
|
||||
|
||||
bool CtbConfig::hasRegisterName(const std::string &name) const {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
return registers.containsKey(fixed_name);
|
||||
}
|
||||
|
||||
bool CtbConfig::hasRegisterAddress(RegisterAddress addr) const {
|
||||
return registers.hasValue(addr);
|
||||
}
|
||||
|
||||
void CtbConfig::clearRegisterNames() { registers.clear(); }
|
||||
|
||||
void CtbConfig::setRegisterName(const std::string &name, RegisterAddress addr) {
|
||||
try {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
registers.addKeyOrSetValue(fixed_name, addr);
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::ostringstream oss;
|
||||
oss << e.what();
|
||||
if (strstr(e.what(), "Maximum capacity reached")) {
|
||||
oss << ". Clear shared memory and try again.";
|
||||
}
|
||||
throw RuntimeError("Could not set register name '" + name +
|
||||
"': " + oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
RegisterAddress CtbConfig::getRegisterAddress(const std::string &name) const {
|
||||
try {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
return registers.getValue(fixed_name);
|
||||
} catch (const std::runtime_error &e) {
|
||||
throw RuntimeError("Could not get register address for name '" + name +
|
||||
"': " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::getRegisterName(RegisterAddress addr) const {
|
||||
try {
|
||||
return registers.getKey(addr).str();
|
||||
} catch (const std::runtime_error &e) {
|
||||
throw RuntimeError("Could not get register name for address '" +
|
||||
addr.str() + "': " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::setRegisterNames(
|
||||
const std::map<std::string, RegisterAddress> &list) {
|
||||
try {
|
||||
std::map<FixedString<name_length>, RegisterAddress> fixed_list;
|
||||
for (const auto &[name, value] : list) {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
fixed_list[fixed_name] = value;
|
||||
}
|
||||
registers.setMap(fixed_list);
|
||||
} catch (const std::runtime_error &e) {
|
||||
throw RuntimeError("Could not set register names: " +
|
||||
std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, RegisterAddress> CtbConfig::getRegisterNames() const {
|
||||
auto fixed_result = registers.getMap();
|
||||
std::map<std::string, RegisterAddress> result;
|
||||
for (const auto &[fixed_name, value] : fixed_result) {
|
||||
result[fixed_name.str()] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int CtbConfig::getBitNamesCount() const { return bits.size(); }
|
||||
|
||||
bool CtbConfig::hasBitName(const std::string &name) const {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
return bits.containsKey(fixed_name);
|
||||
}
|
||||
|
||||
bool CtbConfig::hasBitAddress(BitAddress addr) const {
|
||||
return bits.hasValue(addr);
|
||||
}
|
||||
|
||||
void CtbConfig::clearBitNames() { bits.clear(); }
|
||||
|
||||
void CtbConfig::setBitName(const std::string &name, BitAddress addr) {
|
||||
try {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
bits.addKeyOrSetValue(fixed_name, addr);
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::ostringstream oss;
|
||||
oss << e.what();
|
||||
if (strstr(e.what(), "Maximum capacity reached")) {
|
||||
oss << ". Clear shared memory and try again.";
|
||||
}
|
||||
throw RuntimeError("Could not set bit name '" + name +
|
||||
"': " + oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
BitAddress CtbConfig::getBitAddress(const std::string &name) const {
|
||||
try {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
return bits.getValue(fixed_name);
|
||||
} catch (const std::runtime_error &e) {
|
||||
throw RuntimeError("Could not get bit address for name '" + name +
|
||||
"': " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
std::string CtbConfig::toRegisterNameBitString(BitAddress addr) const {
|
||||
std::ostringstream oss;
|
||||
if (registers.hasValue(addr.address())) {
|
||||
oss << "[" << registers.getKey(addr.address()).str() << ", "
|
||||
<< std::to_string(addr.bitPosition()) << "]";
|
||||
} else {
|
||||
oss << addr.str();
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::string CtbConfig::getBitName(BitAddress addr) const {
|
||||
try {
|
||||
return bits.getKey(addr).str();
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::ostringstream oss;
|
||||
oss << "Could not get bit name for bit address ";
|
||||
oss << "'" << toRegisterNameBitString(addr) << "'";
|
||||
oss << ":" << e.what();
|
||||
throw RuntimeError(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void CtbConfig::setBitNames(const std::map<std::string, BitAddress> &list) {
|
||||
try {
|
||||
std::map<FixedString<name_length>, BitAddress> fixed_list;
|
||||
for (const auto &[name, value] : list) {
|
||||
auto fixed_name = FixedString<name_length>(name);
|
||||
fixed_list[fixed_name] = value;
|
||||
}
|
||||
bits.setMap(fixed_list);
|
||||
} catch (const std::runtime_error &e) {
|
||||
throw RuntimeError("Could not set bit names: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, BitAddress> CtbConfig::getBitNames() const {
|
||||
auto fixed_result = bits.getMap();
|
||||
std::map<std::string, BitAddress> result;
|
||||
for (const auto &[fixed_name, value] : fixed_result) {
|
||||
result[fixed_name.str()] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
Reference in New Issue
Block a user