Setting pattern from memory (#218)

* ToString accepts c-style arrays

* added patternParameters to python

* fixed patwait time bug in validation

* moved load from file function to patterParameters

* server using patternparamters structure to get pattern

Co-authored-by: Erik Frojdh <erik.frojdh@gmail.com>
This commit is contained in:
Dhanya Thattil
2020-11-24 20:32:07 +01:00
committed by GitHub
parent 9e8c8f4bbc
commit e63fa1d7c2
28 changed files with 350 additions and 194 deletions

View File

@ -10,6 +10,7 @@ set(SOURCES
src/ZmqSocket.cpp
src/UdpRxSocket.cpp
src/sls_detector_exceptions.cpp
src/sls_detector_defs.cpp
)
# Header files to install as a part of the library

View File

@ -181,6 +181,23 @@ std::string ToString(const std::map<KeyType, ValueType> &m) {
return os.str();
}
/**
* Print a c style array
*/
template<typename T, size_t size>
std::string ToString(const T(&arr)[size]){
std::ostringstream os;
os << '[';
if (size){
size_t i = 0;
os << ToString(arr[i++]);
for (; i<size; ++i)
os << ", " << ToString(arr[i]);
}
os << ']';
return os.str();
}
/**
* For a container loop over all elements and call ToString on the element
* Container<std::string> is excluded

View File

@ -21,6 +21,7 @@
#include <bitset>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <string>
#else
// C includes
@ -472,19 +473,36 @@ typedef struct {
int gates{0};
scanParameters scanParams{};
} __attribute__((packed));
#endif
/** pattern structure */
#ifdef __cplusplus
struct patternParameters {
uint64_t word[MAX_PATTERN_LENGTH]{};
uint64_t patioctrl{0};
uint32_t patlimits[2]{};
uint32_t patloop[6]{};
uint32_t patnloop[3]{};
uint32_t patwait[3]{};
uint64_t patwaittime[3]{};
} __attribute__((packed));
#else
typedef struct __attribute__((packed)){
#endif
uint64_t word[MAX_PATTERN_LENGTH];
uint64_t patioctrl;
uint32_t patlimits[2];
uint32_t patloop[6];
uint32_t patnloop[3];
uint32_t patwait[3];
uint64_t patwaittime[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);
} __attribute__((packed));
#else
} patternParameters;
#endif
#ifdef __cplusplus
protected:

View File

@ -3,10 +3,10 @@
#define APILIB 0x201119
#define APIRECEIVER 0x201119
#define APIGUI 0x201119
#define APICTB 0x201119
#define APIGOTTHARD 0x201119
#define APIGOTTHARD2 0x201119
#define APIJUNGFRAU 0x201119
#define APIMYTHEN3 0x201119
#define APIMOENCH 0x201119
#define APIEIGER 0x201119
#define APICTB 0x201124
#define APIGOTTHARD 0x201124
#define APIGOTTHARD2 0x201124
#define APIJUNGFRAU 0x201124
#define APIMYTHEN3 0x201124
#define APIMOENCH 0x201124
#define APIEIGER 0x201124

View File

@ -0,0 +1,97 @@
#include "sls/sls_detector_defs.h"
#include "sls/logger.h"
#include "sls/ToString.h"
#include <fstream>
#include <sstream>
#include <iterator>
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<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));
}
word[addr] = StringTo<uint64_t>(args[2]);
} else if (cmd == "patioctrl") {
if (nargs != 1) {
throw RuntimeError("Invalid arguments for " +
ToString(args));
}
patioctrl = StringTo<uint64_t>(args[1]);
} else if (cmd == "patlimits") {
if (nargs != 2) {
throw RuntimeError("Invalid arguments for " +
ToString(args));
}
patlimits[0] = StringTo<uint32_t>(args[1]);
patlimits[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 patloop1 = StringTo<uint32_t>(args[1]);
int patloop2 = StringTo<uint32_t>(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<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';
patwait[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';
patwaittime[level] = StringTo<uint64_t>(args[1]);
} else {
throw RuntimeError("Unknown command in pattern file " + cmd);
}
}
}
}

View File

@ -3,6 +3,7 @@
#include "sls/ToString.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/container_utils.h"
#include <array>
#include <map>
#include <sstream>
@ -298,4 +299,27 @@ TEST_CASE("Streaming of slsDetectorDefs::scanParameters") {
REQUIRE(oss.str() == "[enabled\ndac vth2\nstart 500\nstop 1500\nstep "
"500\nsettleTime 0.5s\n]");
}
}
TEST_CASE("Printing c style arrays of int"){
int arr[]{3, 5};
REQUIRE(ToString(arr) == "[3, 5]");
}
TEST_CASE("Printing c style arrays of uint8"){
uint8_t arr[]{1,2,3,4,5};
REQUIRE(ToString(arr) == "[1, 2, 3, 4, 5]");
}
TEST_CASE("Printing c style arrays of double"){
double arr[]{3.4, 5.3, 6.2};
REQUIRE(ToString(arr) == "[3.4, 5.3, 6.2]");
}
TEST_CASE("Print a member of patternParameters"){
auto pat = sls::make_unique<slsDetectorDefs::patternParameters>();
pat->patlimits[0] = 4;
pat->patlimits[1] = 100;
REQUIRE(ToString(pat->patlimits) == "[4, 100]");
}