From d4518b2ca325b2701e0bbdbab25dfbed92b6fa1e Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Fri, 27 Nov 2020 13:56:57 +0100 Subject: [PATCH] WIP --- slsDetectorSoftware/include/sls/Pattern.h | 33 ++- slsDetectorSoftware/src/Detector.cpp | 3 +- slsDetectorSoftware/src/Module.cpp | 2 +- slsDetectorSoftware/src/Pattern.cpp | 59 ++++- slsSupportLib/include/sls/sls_detector_defs.h | 29 --- slsSupportLib/src/sls_detector_defs.cpp | 217 ------------------ 6 files changed, 69 insertions(+), 274 deletions(-) delete mode 100644 slsSupportLib/src/sls_detector_defs.cpp diff --git a/slsDetectorSoftware/include/sls/Pattern.h b/slsDetectorSoftware/include/sls/Pattern.h index dd9cda105..28172b744 100644 --- a/slsDetectorSoftware/include/sls/Pattern.h +++ b/slsDetectorSoftware/include/sls/Pattern.h @@ -18,25 +18,24 @@ typedef struct __attribute__((packed)) { 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 {}; } +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); + void save(const std::string &fname); + std::string str() const { return {}; } }; -} //namespace sls +} // namespace sls #endif \ No newline at end of file diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 7c9699a49..84c9f2906 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -1758,7 +1758,8 @@ void Detector::savePattern(const std::string &fname) { auto t = pimpl->Parallel(&Module::getPattern, {}); auto pat = t.tsquash("Inconsistent pattern parameters between modules"); pat.validate(); - // pat.save(fname); + std::cout << ToString(pat) << std::endl; + pat.save(fname); } void Detector::setPattern(const std::string &fname, Positions pos) { diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index ae5d476f1..5208e0a31 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -1913,7 +1913,7 @@ void Module::setLEDEnable(bool enable) { // Pattern -void Module::setPattern(const Pattern& pat) { +void Module::setPattern(const Pattern &pat) { sendToDetector(F_SET_PATTERN, pat.data(), pat.size(), nullptr, 0); } diff --git a/slsDetectorSoftware/src/Pattern.cpp b/slsDetectorSoftware/src/Pattern.cpp index 60d0b35dd..a8915fab1 100644 --- a/slsDetectorSoftware/src/Pattern.cpp +++ b/slsDetectorSoftware/src/Pattern.cpp @@ -15,38 +15,40 @@ Pattern::Pattern(const Pattern &other) { } bool Pattern::operator==(const Pattern &other) const { - for (size_t i = 0; i<(sizeof(pat->word)/sizeof(pat->word[0])); ++i){ + 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){ + + 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){ + 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){ + 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){ + 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){ + 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); +bool Pattern::operator!=(const Pattern &other) const { + return !(*this == other); } patternParameters *Pattern::data() { return pat; } @@ -163,4 +165,43 @@ void Pattern::load(const std::string &fname) { } } +void Pattern::save(const std::string &fname) { + std::ofstream output_file(fname); + if (!output_file) { + throw RuntimeError("Could not open pattern file " + fname + + " for writing"); + } + std::ostringstream os; + // pattern word + for (uint32_t i = pat->limits[0]; i <= pat->limits[1]; ++i) { + output_file << "patword [" << ToStringHex(i, 4) << ", " + << ToStringHex(pat->word[i], 16) << "]" << std::endl; + } + + // patioctrl + output_file << "patioctrl " << ToStringHex(pat->ioctrl, 16) << std::endl; + + // patlimits + output_file << "patlimits " << ToStringHex(pat->limits[0], 4) << " " + << ToStringHex(pat->limits[1], 4) << std::endl; + + for (size_t i = 0; i < 3; ++i) { + // patloop + output_file << "patloop" << i << " " + << ToStringHex(pat->loop[i * 2 + 0], 4) << " " + << ToStringHex(pat->loop[i * 2 + 1], 4) << std::endl; + // patnloop + output_file << "patnloop" << i << " " << pat->nloop[i] << std::endl; + } + + for (size_t i = 0; i < 3; ++i) { + // patwait + output_file << "patwait" << i << " " << ToStringHex(pat->wait[i], 4) + << std::endl; + // patwaittime + output_file << "patwaittime" << i << " " << pat->waittime[i] + << std::endl; + } +} + } // namespace sls \ No newline at end of file diff --git a/slsSupportLib/include/sls/sls_detector_defs.h b/slsSupportLib/include/sls/sls_detector_defs.h index cbe281e19..d908b265d 100644 --- a/slsSupportLib/include/sls/sls_detector_defs.h +++ b/slsSupportLib/include/sls/sls_detector_defs.h @@ -475,35 +475,6 @@ typedef struct { } __attribute__((packed)); #endif -// /** pattern structure */ -// #ifdef __cplusplus -// struct patternParameters { -// #else -// typedef struct __attribute__((packed)) { -// #endif -// 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]; -// #ifdef __cplusplus -// public: -// patternParameters() { -// // Since the def has to be c compatible we can't use {} for the -// // members -// memset(this, 0, sizeof(patternParameters)); -// } -// void load(const std::string &fname); -// void save(const std::string &fname); -// void validate() const; -// } __attribute__((packed)); -// #else -// } patternParameters; -// #endif - #ifdef __cplusplus protected: #endif diff --git a/slsSupportLib/src/sls_detector_defs.cpp b/slsSupportLib/src/sls_detector_defs.cpp deleted file mode 100644 index ca10e0831..000000000 --- a/slsSupportLib/src/sls_detector_defs.cpp +++ /dev/null @@ -1,217 +0,0 @@ -#include "sls/sls_detector_defs.h" -#include "sls/ToString.h" -#include "sls/logger.h" -#include -#include -#include - -using sls::RuntimeError; -using sls::StringTo; -using sls::ToString; - -void slsDetectorDefs::patternParameters::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(iss); - std::vector args = std::vector( - it, std::istream_iterator()); - - 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(args[1]); - if (addr >= MAX_PATTERN_LENGTH) { - throw RuntimeError("Invalid address for " + ToString(args)); - } - word[addr] = StringTo(args[2]); - } else if (cmd == "patioctrl") { - if (nargs != 1) { - throw RuntimeError("Invalid arguments for " + - ToString(args)); - } - ioctrl = StringTo(args[1]); - } else if (cmd == "patlimits") { - if (nargs != 2) { - throw RuntimeError("Invalid arguments for " + - ToString(args)); - } - limits[0] = StringTo(args[1]); - limits[1] = StringTo(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(args[1]); - int loop2 = StringTo(args[2]); - loop[level * 2 + 0] = loop1; - 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'; - nloop[level] = StringTo(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'; - wait[level] = StringTo(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'; - waittime[level] = StringTo(args[1]); - } else { - throw RuntimeError("Unknown command in pattern file " + cmd); - } - } - } -} - -void slsDetectorDefs::patternParameters::save(const std::string &fname) { - std::ofstream output_file(fname); - if (!output_file) { - throw RuntimeError("Could not open pattern file " + fname + - " for writing"); - } - std::cout << "is it really going here??" << std : endl; - std::ostringstream os; - // pattern word - for (uint32_t i = limits[0]; i <= limits[1]; ++i) { - output_file << "patword [" << sls::ToStringHex(i, 4) << ", " - << sls::ToStringHex(word[i], 16) << "]" << std::endl; - } - - /* - for (std::string line; std::getline(output_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(iss); - std::vector args = std::vector( - it, std::istream_iterator()); - - 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(args[1]); - if (addr >= MAX_PATTERN_LENGTH) { - throw RuntimeError("Invalid address for " + - ToString(args)); - } - word[addr] = StringTo(args[2]); - } else if (cmd == "patioctrl") { - if (nargs != 1) { - throw RuntimeError("Invalid arguments for " + - ToString(args)); - } - patioctrl = StringTo(args[1]); - } else if (cmd == "patlimits") { - if (nargs != 2) { - throw RuntimeError("Invalid arguments for " + - ToString(args)); - } - patlimits[0] = StringTo(args[1]); - patlimits[1] = StringTo(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 patloop1 = StringTo(args[1]); - int patloop2 = StringTo(args[2]); - patloop[level * 2 + 0] = patloop1; - patloop[level * 2 + 1] = patloop2; - } 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'; - patnloop[level] = StringTo(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'; - patwait[level] = StringTo(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'; - patwaittime[level] = StringTo(args[1]); - } else { - throw RuntimeError("Unknown command in pattern file " + - cmd); - } - } - }*/ -} - -void slsDetectorDefs::patternParameters::validate() const { - if (limits[0] >= MAX_PATTERN_LENGTH || limits[1] >= MAX_PATTERN_LENGTH) { - throw RuntimeError("Invalid Pattern limits address [" + - ToString(limits[0]) + std::string(", ") + - ToString(limits[1]) + std::string("]")); - } - for (int i = 0; i != 3; ++i) { - if (loop[i * 2 + 0] >= MAX_PATTERN_LENGTH || - loop[i * 2 + 1] >= MAX_PATTERN_LENGTH) { - throw RuntimeError("Invalid Pattern loop address for level " + - ToString(i) + std::string(" [") + - ToString(loop[i * 2 + 0]) + std::string(", ") + - ToString(loop[i * 2 + 1]) + std::string("]")); - } - if (wait[i] >= MAX_PATTERN_LENGTH) { - throw RuntimeError("Invalid Pattern wait address for level " + - ToString(i) + std::string(" ") + - ToString(wait[i])); - } - } -} \ No newline at end of file