Skip to content

Commit

Permalink
Merge pull request #882 from synnaxlabs/eislam/sy-1388-labjack-writes
Browse files Browse the repository at this point in the history
Eislam/sy 1388 labjack writes
  • Loading branch information
Lham42 authored Oct 21, 2024
2 parents 299e234 + b3e7454 commit 7020d3f
Show file tree
Hide file tree
Showing 10 changed files with 765 additions and 119 deletions.
17 changes: 17 additions & 0 deletions driver/labjack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ cc_library(
"labjack.h",
"scanner.h",
"reader.h",
"writer.h",
"task.h",
],
srcs = [
"scanner.cpp",
"factory.cpp",
"reader.cpp",
"writer.cpp",
"task.cpp",
],
deps = [
Expand Down Expand Up @@ -90,6 +92,21 @@ cc_test(
})
)

cc_test(
name = "writer_test",
srcs = ["writer_test.cpp"],
deps = [
":labjack",
"@com_google_googletest//:gtest_main",
"//driver/testutil",
],
linkopts = select({
# TODO: This is a temporary fix to supress multiple definitions error when building, should find better workaroud
"@bazel_tools//src/conditions:windows": ["/FORCE:MULTIPLE"],
"//conditions:default": [],
})
)


cc_binary(
name = "eReadNames",
Expand Down
4 changes: 4 additions & 0 deletions driver/labjack/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ std::pair<std::unique_ptr<task::Task>, bool> labjack::Factory::configure_task(
const std::shared_ptr<task::Context> &ctx,
const synnax::Task &task
) {

if (task.type == "labjack_scan")
return {std::make_unique<labjack::ScannerTask>(ctx, task), true}; // TODO: replace with configure for scanner
if (task.type == "labjack_read")
return {labjack::ReaderTask::configure(ctx, task), true};
if(task.type == "labjack_write")
return {labjack::WriterTask::configure(ctx, task), true};

LOG(ERROR) << "[labjack] Unknown task type: " << task.type;
return {nullptr, false};
}
Expand Down
2 changes: 1 addition & 1 deletion driver/labjack/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ void labjack::Source::acquire_data(){
data_queue.enqueue(data_packet);
}
auto err = LJM_eStreamStop(handle);
}
}
3 changes: 2 additions & 1 deletion driver/labjack/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ namespace labjack{
index_keys(parser.optional<std::set<uint32_t>>("index_keys", {})),
serial_number(parser.optional<std::string>("serial_number", "")),
connection_type(parser.optional<std::string>("connection_type", "")),
data_saving(parser.optional<bool>("data_saving", false)) {
data_saving(parser.optional<bool>("data_saving", false)
) {

// Parse the channels
parser.iter("channels", [this](config::Parser &channel_parser) {
Expand Down
217 changes: 101 additions & 116 deletions driver/labjack/reader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,123 +24,108 @@ using json = nlohmann::json;
// Basic Tests //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//TEST(read_tests, labjack_t4){
// auto client = std::make_shared<synnax::Synnax>(new_test_client());
//
// auto [time, tErr] = client->channels.create("idx", synnax::TIMESTAMP, 0, true);
// ASSERT_FALSE(tErr) << tErr.message();
//
// auto [data, dErr] = client->channels.create("ai", synnax::FLOAT32, time.key, false);
// ASSERT_FALSE(dErr) << dErr.message();
//
// auto config = json{
// {"sample_rate", 10000},
// {"stream_rate", 30},
// {"device_type", "T4"},
// {"device_key", "440022190"},
// {"serial_number", "440022190"},
// {"connection_type", "USB"},
// {"data_saving", true},
// {"channels", json::array({
// {
// {"location", "AIN0"},
// {"enabled", true},
// {"data_type", "float32"},
// {"channel_key", data.key},
// {"range", 10.0},
// {"channel_types", "AIN"}
// }//,
//// {
//// {"location", "AIN1"},
//// {"enabled", true},
//// {"data_type", "float32"},
//// {"channel_key", data.key},
//// {"range", 10.0},
//// {"channel_types", "AIN"}
//// }//,
//// {
//// {"location", "FIO4"},
//// {"enabled", true},
//// {"data_type", "uint8"},
//// {"channel_key", data.key},
//// {"channel_types", "DIN"}
//// }
// })},
// {"index_keys", json::array({time.key})},
// {"channel_map", {
// {"AIN0", data.key}
// }}
// };
//
// auto task = synnax::Task("my_task", "labjack_read", to_string(config));
// auto mockCtx = std::make_shared<task::MockContext>(client);
//
// auto reader_task = labjack::ReaderTask::configure(mockCtx, task);
// // create commands
// auto start_cmd = task::Command{task.key, "start", {}};
// auto stop_cmd = task::Command{task.key, "stop", {}};
// reader_task->exec(start_cmd);
// std::this_thread::sleep_for(std::chrono::seconds(30000));
//// std::this_thread::sleep_for(std::chrono::seconds(2));
// reader_task->exec(stop_cmd);
//
//}
TEST(read_tests, labjack_t4){
auto client = std::make_shared<synnax::Synnax>(new_test_client());

//TEST(read_tests, labjack_t4_multi_ain){
// auto client = std::make_shared<synnax::Synnax>(new_test_client());
//
// auto [time, tErr] = client->channels.create("idx", synnax::TIMESTAMP, 0, true);
// ASSERT_FALSE(tErr) << tErr.message();
//
// auto [data1, dErr1] = client->channels.create("ai_1", synnax::FLOAT32, time.key, false);
// ASSERT_FALSE(dErr1) << dErr1.message();
//
// auto [data2, dErr2] = client->channels.create("ai_2", synnax::FLOAT32, time.key, false);
// ASSERT_FALSE(dErr2) << dErr2.message();
//
//
// auto config = json{
// {"sample_rate", 10000},
// {"stream_rate", 30},
// {"device_type", "T4"},
// {"device_key", "440022190"},
// {"serial_number", "440022190"},
// {"connection_type", "USB"},
// {"data_saving", true},
// {"channels", json::array({
// {
// {"location", "AIN0"},
// {"enabled", true},
// {"data_type", "float32"},
// {"channel_key", data1.key},
// {"range", 10.0},
// {"channel_types", "AIN"}
// },
// {
// {"location", "AIN1"},
// {"enabled", true},
// {"data_type", "float32"},
// {"channel_key", data2.key},
// {"range", 10.0},
// {"channel_types", "AIN"}
// }
// })},
// {"index_keys", json::array({time.key})},
// };
//
// auto task = synnax::Task("my_task", "labjack_read", to_string(config));
// auto mockCtx = std::make_shared<task::MockContext>(client);
//
// auto reader_task = labjack::ReaderTask::configure(mockCtx, task);
// // create commands
// auto start_cmd = task::Command{task.key, "start", {}};
// auto stop_cmd = task::Command{task.key, "stop", {}};
// reader_task->exec(start_cmd);
// std::this_thread::sleep_for(std::chrono::seconds(30000));
// // std::this_thread::sleep_for(std::chrono::seconds(2));
// reader_task->exec(stop_cmd);
//
//}
auto [time, tErr] = client->channels.create("idx", synnax::TIMESTAMP, 0, true);
ASSERT_FALSE(tErr) << tErr.message();

auto [data, dErr] = client->channels.create("ai", synnax::FLOAT32, time.key, false);
ASSERT_FALSE(dErr) << dErr.message();

auto config = json{
{"sample_rate", 10000},
{"stream_rate", 30},
{"device_type", "T4"},
{"device_key", "440022190"},
{"serial_number", "440022190"},
{"connection_type", "USB"},
{"data_saving", true},
{"channels", json::array({
{
{"location", "AIN0"},
{"enabled", true},
{"data_type", "float32"},
{"channel_key", data.key},
{"range", 10.0},
{"channel_types", "AIN"}
}
})},
{"index_keys", json::array({time.key})},
{"channel_map", {
{"AIN0", data.key}
}}
};

auto task = synnax::Task("my_task", "labjack_read", to_string(config));
auto mockCtx = std::make_shared<task::MockContext>(client);

auto reader_task = labjack::ReaderTask::configure(mockCtx, task);
// create commands
auto start_cmd = task::Command{task.key, "start", {}};
auto stop_cmd = task::Command{task.key, "stop", {}};
reader_task->exec(start_cmd);
std::this_thread::sleep_for(std::chrono::seconds(30000));
// std::this_thread::sleep_for(std::chrono::seconds(2));
reader_task->exec(stop_cmd);

}

TEST(read_tests, labjack_t4_multi_ain){
auto client = std::make_shared<synnax::Synnax>(new_test_client());

auto [time, tErr] = client->channels.create("idx", synnax::TIMESTAMP, 0, true);
ASSERT_FALSE(tErr) << tErr.message();

auto [data1, dErr1] = client->channels.create("ai_1", synnax::FLOAT32, time.key, false);
ASSERT_FALSE(dErr1) << dErr1.message();

auto [data2, dErr2] = client->channels.create("ai_2", synnax::FLOAT32, time.key, false);
ASSERT_FALSE(dErr2) << dErr2.message();


auto config = json{
{"sample_rate", 10000},
{"stream_rate", 30},
{"device_type", "T4"},
{"device_key", "440022190"},
{"serial_number", "440022190"},
{"connection_type", "USB"},
{"data_saving", true},
{"channels", json::array({
{
{"location", "AIN0"},
{"enabled", true},
{"data_type", "float32"},
{"channel_key", data1.key},
{"range", 10.0},
{"channel_types", "AIN"}
},
{
{"location", "AIN1"},
{"enabled", true},
{"data_type", "float32"},
{"channel_key", data2.key},
{"range", 10.0},
{"channel_types", "AIN"}
}
})},
{"index_keys", json::array({time.key})},
};

auto task = synnax::Task("my_task", "labjack_read", to_string(config));
auto mockCtx = std::make_shared<task::MockContext>(client);

auto reader_task = labjack::ReaderTask::configure(mockCtx, task);
// create commands
auto start_cmd = task::Command{task.key, "start", {}};
auto stop_cmd = task::Command{task.key, "stop", {}};
reader_task->exec(start_cmd);
std::this_thread::sleep_for(std::chrono::seconds(30000));
// std::this_thread::sleep_for(std::chrono::seconds(2));
reader_task->exec(stop_cmd);

}

TEST(read_tests, labjack_t4_ai_fio){
auto client = std::make_shared<synnax::Synnax>(new_test_client());
Expand Down
Loading

0 comments on commit 7020d3f

Please sign in to comment.