sleep implemented for command line, mainly for config files for firmware developers (#982)

This commit is contained in:
maliakal_d 2024-09-30 16:57:53 +02:00 committed by GitHub
parent 2dc0963c56
commit 8144397b2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 109 additions and 0 deletions

View File

@ -2690,6 +2690,17 @@ gaincaps:
argc: -1 argc: -1
arg_types: [ defs::defs::M3_GainCaps ] arg_types: [ defs::defs::M3_GainCaps ]
sleep:
is_description: true
actions:
PUT:
args:
- argc: 1
arg_types: [ int ]
- argc: 2
arg_types: [ int, special::time_unit ]
################# special commands ########################## ################# special commands ##########################
virtual: virtual:

View File

@ -10042,6 +10042,40 @@ signalname:
\ to the given name." \ to the given name."
infer_action: true infer_action: true
template: true template: true
sleep:
actions:
PUT:
args:
- arg_types:
- int
argc: 1
cast_input: []
check_det_id: false
convert_det_id: true
function: ''
input: []
input_types: []
output: []
require_det_id: false
store_result_in_t: false
- arg_types:
- int
- special::time_unit
argc: 2
cast_input: []
check_det_id: false
convert_det_id: true
function: ''
input: []
input_types: []
output: []
require_det_id: false
store_result_in_t: false
command_name: sleep
function_alias: sleep
help: ''
infer_action: true
is_description: true
slowadc: slowadc:
actions: actions:
GET: GET:

View File

@ -285,6 +285,7 @@ class Caller {
std::string signalindex(int action); std::string signalindex(int action);
std::string signallist(int action); std::string signallist(int action);
std::string signalname(int action); std::string signalname(int action);
std::string sleep(int action);
std::string slowadc(int action); std::string slowadc(int action);
std::string slowadcindex(int action); std::string slowadcindex(int action);
std::string slowadclist(int action); std::string slowadclist(int action);
@ -631,6 +632,7 @@ class Caller {
{"signalindex", &Caller::signalindex}, {"signalindex", &Caller::signalindex},
{"signallist", &Caller::signallist}, {"signallist", &Caller::signallist},
{"signalname", &Caller::signalname}, {"signalname", &Caller::signalname},
{"sleep", &Caller::sleep},
{"slowadc", &Caller::slowadc}, {"slowadc", &Caller::slowadc},
{"slowadcindex", &Caller::slowadcindex}, {"slowadcindex", &Caller::slowadcindex},
{"slowadclist", &Caller::slowadclist}, {"slowadclist", &Caller::slowadclist},

View File

@ -5,6 +5,7 @@
#include "sls/logger.h" #include "sls/logger.h"
#include "sls/string_utils.h" #include "sls/string_utils.h"
#include <iostream> #include <iostream>
#include <thread>
namespace sls { namespace sls {
// some helper functions to print // some helper functions to print
@ -1179,4 +1180,37 @@ std::string Caller::gaincaps(int action) {
} }
return os.str(); return os.str();
} }
std::string Caller::sleep(int action) {
std::ostringstream os;
if (action == defs::HELP_ACTION) {
os << "[duration] [(optional unit) ns|us|ms|s]\n\tSleep for duration. "
"Mainly for config files for firmware developers."
"Default unit is s."
<< '\n';
} else if (action == defs::GET_ACTION) {
throw RuntimeError("Cannot get.");
} else if (action == defs::PUT_ACTION) {
if (args.size() != 1 && args.size() != 2) {
WrongNumberOfParameters(1);
}
time::ns converted_time{0};
try {
if (args.size() == 1) {
std::string tmp_time(args[0]);
std::string unit = RemoveUnit(tmp_time);
converted_time = StringTo<time::ns>(tmp_time, unit);
} else {
converted_time = StringTo<time::ns>(args[0], args[1]);
}
} catch (...) {
throw RuntimeError("Could not convert argument to time::ns");
}
std::this_thread::sleep_for(converted_time);
os << "for " << ToString(converted_time) << " completed" << '\n';
} else {
throw RuntimeError("Unknown action");
}
return os.str();
}
} // namespace sls } // namespace sls

View File

@ -3239,6 +3239,22 @@ int InferAction::signalname() {
} }
} }
int InferAction::sleep() {
if (args.size() == 1) {
return slsDetectorDefs::PUT_ACTION;
}
if (args.size() == 2) {
return slsDetectorDefs::PUT_ACTION;
}
else {
throw RuntimeError("Could not infer action: Wrong number of arguments");
}
}
int InferAction::slowadc() { int InferAction::slowadc() {
if (args.size() == 1) { if (args.size() == 1) {

View File

@ -240,6 +240,7 @@ class InferAction {
int signalindex(); int signalindex();
int signallist(); int signallist();
int signalname(); int signalname();
int sleep();
int slowadc(); int slowadc();
int slowadcindex(); int slowadcindex();
int slowadclist(); int slowadclist();
@ -574,6 +575,7 @@ class InferAction {
{"signalindex", &InferAction::signalindex}, {"signalindex", &InferAction::signalindex},
{"signallist", &InferAction::signallist}, {"signallist", &InferAction::signallist},
{"signalname", &InferAction::signalname}, {"signalname", &InferAction::signalname},
{"sleep", &InferAction::sleep},
{"slowadc", &InferAction::slowadc}, {"slowadc", &InferAction::slowadc},
{"slowadcindex", &InferAction::slowadcindex}, {"slowadcindex", &InferAction::slowadcindex},
{"slowadclist", &InferAction::slowadclist}, {"slowadclist", &InferAction::slowadclist},

View File

@ -3586,4 +3586,14 @@ TEST_CASE("CALLER::user", "[.cmdcall]") {
REQUIRE_NOTHROW(caller.call("user", {}, -1, GET)); REQUIRE_NOTHROW(caller.call("user", {}, -1, GET));
} }
TEST_CASE("CALLER::sleep", "[.cmdcall]") {
Detector det;
Caller caller(&det);
REQUIRE_NOTHROW(caller.call("sleep", {"1"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("sleep", {"100", "ms"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("sleep", {"1000", "ns"}, -1, PUT));
// This is a put only command
REQUIRE_THROWS(caller.call("sleep", {}, -1, GET));
}
} // namespace sls } // namespace sls