mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-24 02:27:59 +02:00
Introduced pattern class
This commit is contained in:
@ -4,6 +4,7 @@ set(SOURCES
|
||||
src/Detector.cpp
|
||||
src/CmdProxy.cpp
|
||||
src/CmdParser.cpp
|
||||
src/Pattern.cpp
|
||||
)
|
||||
|
||||
add_library(slsDetectorObject OBJECT
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "sls/Result.h"
|
||||
#include "sls/network_utils.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/Pattern.h"
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@ -15,6 +16,7 @@ class DetectorImpl;
|
||||
class MacAddr;
|
||||
class IpAddr;
|
||||
|
||||
|
||||
// Free function to avoid dependence on class
|
||||
// and avoid the option to free another objects
|
||||
// shm by mistake
|
||||
@ -1459,7 +1461,7 @@ class Detector {
|
||||
|
||||
/** [CTB][Moench][Mythen3] Loads pattern parameters structure directly to
|
||||
* server */
|
||||
void setPattern(const defs::patternParameters *pat, Positions pos = {});
|
||||
void setPattern(const Pattern& pat, Positions pos = {});
|
||||
|
||||
/** [CTB][Moench][Mythen3] [Ctb][Moench][Mythen3] Saves pattern to file
|
||||
* (ascii). \n [Ctb][Moench] Also executes pattern.*/
|
||||
|
42
slsDetectorSoftware/include/sls/Pattern.h
Normal file
42
slsDetectorSoftware/include/sls/Pattern.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "sls/sls_detector_defs.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <memory>
|
||||
namespace sls {
|
||||
#endif
|
||||
|
||||
// Common C/C++ structure to handle pattern data
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint64_t word[MAX_PATTERN_LENGTH];
|
||||
uint64_t ioctrl;
|
||||
uint32_t limits[2];
|
||||
// loop0 start, loop0 stop .. loop2 start, loop2 stop
|
||||
uint32_t loop[6];
|
||||
uint32_t nloop[3];
|
||||
uint32_t wait[3];
|
||||
uint64_t waittime[3];
|
||||
} patternParameters;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
class Pattern{
|
||||
patternParameters* pat = new patternParameters{};
|
||||
|
||||
public:
|
||||
Pattern();
|
||||
~Pattern();
|
||||
Pattern(const Pattern& other);
|
||||
bool operator==(const Pattern& other) const;
|
||||
bool operator!=(const Pattern& other) const;
|
||||
patternParameters* data();
|
||||
patternParameters* data() const;
|
||||
constexpr size_t size() const noexcept{ return sizeof(patternParameters);}
|
||||
void validate() const;
|
||||
void load(const std::string &fname);
|
||||
std::string str() const { return {}; }
|
||||
|
||||
};
|
||||
|
||||
} //namespace sls
|
||||
#endif
|
@ -9,6 +9,7 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/versionAPI.h"
|
||||
#include "sls/Pattern.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
@ -1754,19 +1755,19 @@ void Detector::setLEDEnable(bool enable, Positions pos) {
|
||||
// Pattern
|
||||
|
||||
void Detector::savePattern(const std::string &fname) {
|
||||
auto t = pimpl->Parallel(&Module::getPattern, {});
|
||||
// auto t = pimpl->Parallel(&Module::getPattern, {});
|
||||
// auto pat = t.tsquash("Inconsistent pattern parameters between modules");
|
||||
auto pat = std::move(t[0]);
|
||||
// pat->save(fname);
|
||||
} // namespace sls
|
||||
|
||||
void Detector::setPattern(const std::string &fname, Positions pos) {
|
||||
auto pat = sls::make_unique<defs::patternParameters>();
|
||||
pat->load(fname);
|
||||
pimpl->Parallel(&Module::setPattern, pos, pat.get());
|
||||
// Pattern pat;
|
||||
// pat.load(fname);
|
||||
// setPattern(pat, pos);
|
||||
}
|
||||
|
||||
void Detector::setPattern(const defs::patternParameters *pat, Positions pos) {
|
||||
void Detector::setPattern(const Pattern& pat, Positions pos) {
|
||||
pat.validate();
|
||||
pimpl->Parallel(&Module::setPattern, pos, pat);
|
||||
}
|
||||
|
||||
|
@ -1913,15 +1913,13 @@ void Module::setLEDEnable(bool enable) {
|
||||
|
||||
// Pattern
|
||||
|
||||
void Module::setPattern(const defs::patternParameters *pat) {
|
||||
pat->validate();
|
||||
sendToDetector(F_SET_PATTERN, pat, sizeof(patternParameters), nullptr, 0);
|
||||
void Module::setPattern(const Pattern& pat) {
|
||||
sendToDetector(F_SET_PATTERN, pat.data(), pat.size(), nullptr, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<defs::patternParameters> Module::getPattern() {
|
||||
auto pat = sls::make_unique<defs::patternParameters>();
|
||||
*pat = sendToDetector<defs::patternParameters>(F_GET_PATTERN);
|
||||
pat->validate();
|
||||
Pattern Module::getPattern() {
|
||||
Pattern pat;
|
||||
sendToDetector(F_GET_PATTERN, nullptr, 0, pat.data(), pat.size());
|
||||
return pat;
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/network_utils.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/Pattern.h"
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
@ -462,8 +463,8 @@ class Module : public virtual slsDetectorDefs {
|
||||
* Pattern *
|
||||
* *
|
||||
* ************************************************/
|
||||
void setPattern(const defs::patternParameters *pat);
|
||||
std::unique_ptr<defs::patternParameters> getPattern();
|
||||
void setPattern(const Pattern& pat);
|
||||
Pattern getPattern();
|
||||
uint64_t getPatternIOControl() const;
|
||||
void setPatternIOControl(uint64_t word);
|
||||
uint64_t getPatternWord(int addr) const;
|
||||
|
166
slsDetectorSoftware/src/Pattern.cpp
Normal file
166
slsDetectorSoftware/src/Pattern.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include "sls/Pattern.h"
|
||||
#include "sls/ToString.h"
|
||||
#include "sls/logger.h"
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
|
||||
namespace sls {
|
||||
|
||||
Pattern::Pattern() = default;
|
||||
Pattern::~Pattern() { delete pat; }
|
||||
|
||||
Pattern::Pattern(const Pattern &other) {
|
||||
memcpy(pat, other.pat, sizeof(patternParameters));
|
||||
}
|
||||
|
||||
bool Pattern::operator==(const Pattern &other) const {
|
||||
for (size_t i = 0; i<(sizeof(pat->word)/sizeof(pat->word[0])); ++i){
|
||||
if (pat->word[i] != other.pat->word[i])
|
||||
return false;
|
||||
}
|
||||
if (pat->ioctrl != other.pat->ioctrl)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i<(sizeof(pat->limits)/sizeof(pat->limits[0])); ++i){
|
||||
if (pat->limits[i] != other.pat->limits[i])
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i<(sizeof(pat->loop)/sizeof(pat->loop[0])); ++i){
|
||||
if (pat->loop[i] != other.pat->loop[i])
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i<(sizeof(pat->nloop)/sizeof(pat->nloop[0])); ++i){
|
||||
if (pat->nloop[i] != other.pat->nloop[i])
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i<(sizeof(pat->wait)/sizeof(pat->wait[0])); ++i){
|
||||
if (pat->wait[i] != other.pat->wait[i])
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i<(sizeof(pat->waittime)/sizeof(pat->waittime[0])); ++i){
|
||||
if (pat->waittime[i] != other.pat->waittime[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Pattern::operator!=(const Pattern& other) const{
|
||||
return !(*this==other);
|
||||
}
|
||||
|
||||
patternParameters *Pattern::data() { return pat; }
|
||||
patternParameters *Pattern::data() const { return pat; }
|
||||
|
||||
void Pattern::validate() const {
|
||||
if (pat->limits[0] >= MAX_PATTERN_LENGTH ||
|
||||
pat->limits[1] >= MAX_PATTERN_LENGTH) {
|
||||
throw RuntimeError("Invalid Pattern limits address [" +
|
||||
ToString(pat->limits[0]) + std::string(", ") +
|
||||
ToString(pat->limits[1]) + std::string("]"));
|
||||
}
|
||||
for (int i = 0; i != 3; ++i) {
|
||||
if (pat->loop[i * 2 + 0] >= MAX_PATTERN_LENGTH ||
|
||||
pat->loop[i * 2 + 1] >= MAX_PATTERN_LENGTH) {
|
||||
throw RuntimeError(
|
||||
"Invalid Pattern loop address for level " + ToString(i) +
|
||||
std::string(" [") + ToString(pat->loop[i * 2 + 0]) +
|
||||
std::string(", ") + ToString(pat->loop[i * 2 + 1]) +
|
||||
std::string("]"));
|
||||
}
|
||||
if (pat->wait[i] >= MAX_PATTERN_LENGTH) {
|
||||
throw RuntimeError("Invalid Pattern wait address for level " +
|
||||
ToString(i) + std::string(" ") +
|
||||
ToString(pat->wait[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Pattern::load(const std::string &fname) {
|
||||
std::ifstream input_file(fname);
|
||||
if (!input_file) {
|
||||
throw RuntimeError("Could not open pattern file " + fname +
|
||||
" for reading");
|
||||
}
|
||||
for (std::string line; std::getline(input_file, line);) {
|
||||
if (line.find('#') != std::string::npos) {
|
||||
line.erase(line.find('#'));
|
||||
}
|
||||
LOG(logDEBUG1) << "line after removing comments:\n\t" << line;
|
||||
if (line.length() > 1) {
|
||||
|
||||
// convert command and string to a vector
|
||||
std::istringstream iss(line);
|
||||
auto it = std::istream_iterator<std::string>(iss);
|
||||
std::vector<std::string> args = std::vector<std::string>(
|
||||
it, std::istream_iterator<std::string>());
|
||||
|
||||
std::string cmd = args[0];
|
||||
int nargs = args.size() - 1;
|
||||
|
||||
if (cmd == "patword") {
|
||||
if (nargs != 2) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
uint32_t addr = StringTo<uint32_t>(args[1]);
|
||||
if (addr >= MAX_PATTERN_LENGTH) {
|
||||
throw RuntimeError("Invalid address for " + ToString(args));
|
||||
}
|
||||
pat->word[addr] = StringTo<uint64_t>(args[2]);
|
||||
} else if (cmd == "patioctrl") {
|
||||
if (nargs != 1) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
pat->ioctrl = StringTo<uint64_t>(args[1]);
|
||||
} else if (cmd == "patlimits") {
|
||||
if (nargs != 2) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
pat->limits[0] = StringTo<uint32_t>(args[1]);
|
||||
pat->limits[1] = StringTo<uint32_t>(args[2]);
|
||||
} else if (cmd == "patloop0" || cmd == "patloop1" ||
|
||||
cmd == "patloop2") {
|
||||
if (nargs != 2) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
int level = cmd[cmd.find_first_of("012")] - '0';
|
||||
int loop1 = StringTo<uint32_t>(args[1]);
|
||||
int loop2 = StringTo<uint32_t>(args[2]);
|
||||
pat->loop[level * 2 + 0] = loop1;
|
||||
pat->loop[level * 2 + 1] = loop2;
|
||||
} else if (cmd == "patnloop0" || cmd == "patnloop1" ||
|
||||
cmd == "patnloop2") {
|
||||
if (nargs != 1) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
int level = cmd[cmd.find_first_of("012")] - '0';
|
||||
pat->nloop[level] = StringTo<uint32_t>(args[1]);
|
||||
} else if (cmd == "patwait0" || cmd == "patwait1" ||
|
||||
cmd == "patwait2") {
|
||||
if (nargs != 1) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
int level = cmd[cmd.find_first_of("012")] - '0';
|
||||
pat->wait[level] = StringTo<uint32_t>(args[1]);
|
||||
} else if (cmd == "patwaittime0" || cmd == "patwaittime1" ||
|
||||
cmd == "patwaittime2") {
|
||||
if (nargs != 1) {
|
||||
throw RuntimeError("Invalid arguments for " +
|
||||
ToString(args));
|
||||
}
|
||||
int level = cmd[cmd.find_first_of("012")] - '0';
|
||||
pat->waittime[level] = StringTo<uint64_t>(args[1]);
|
||||
} else {
|
||||
throw RuntimeError("Unknown command in pattern file " + cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sls
|
@ -15,6 +15,7 @@ target_sources(tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Result.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdParser.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Module.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Pattern.cpp
|
||||
)
|
||||
|
||||
target_include_directories(tests PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src>")
|
31
slsDetectorSoftware/tests/test-Pattern.cpp
Normal file
31
slsDetectorSoftware/tests/test-Pattern.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "catch.hpp"
|
||||
#include "sls/Pattern.h"
|
||||
|
||||
using sls::Pattern;
|
||||
|
||||
TEST_CASE("Pattern is default constructable and has zeroed fields"){
|
||||
Pattern p;
|
||||
for (int i = 0; i!=MAX_PATTERN_LENGTH; ++i)
|
||||
REQUIRE(p.data()->word[i] == 0);
|
||||
REQUIRE(p.data()->ioctrl == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Copy construct pattern"){
|
||||
Pattern p;
|
||||
p.data()->loop[0] = 7;
|
||||
Pattern p1(p);
|
||||
REQUIRE(p1.data()->loop[0] == 7);
|
||||
}
|
||||
|
||||
TEST_CASE("Compare patterns"){
|
||||
Pattern p;
|
||||
Pattern p1;
|
||||
REQUIRE(p == p1);
|
||||
|
||||
p1.data()->word[500] = 1;
|
||||
REQUIRE_FALSE(p == p1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user