Files
allwpilib/glass/src/libnt/native/cpp/NTCommandScheduler.cpp

51 lines
1.6 KiB
C++
Raw Normal View History

2020-12-26 14:31:24 -08:00
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2025-11-07 19:56:21 -05:00
#include "wpi/glass/networktables/NTCommandScheduler.hpp"
2024-09-20 17:43:39 -07:00
#include <utility>
#include <fmt/format.h>
2025-11-07 19:57:55 -05:00
2025-11-07 19:56:21 -05:00
#include "wpi/util/StringExtras.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi::glass;
NTCommandSchedulerModel::NTCommandSchedulerModel(std::string_view path)
2025-11-07 20:00:05 -05:00
: NTCommandSchedulerModel(wpi::nt::NetworkTableInstance::GetDefault(), path) {}
2025-11-07 20:00:05 -05:00
NTCommandSchedulerModel::NTCommandSchedulerModel(wpi::nt::NetworkTableInstance inst,
std::string_view path)
2022-10-08 10:01:31 -07:00
: m_inst{inst},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_commands{inst.GetStringArrayTopic(fmt::format("{}/Names", path))
.Subscribe({})},
m_ids{
inst.GetIntegerArrayTopic(fmt::format("{}/Ids", path)).Subscribe({})},
m_cancel{
inst.GetIntegerArrayTopic(fmt::format("{}/Cancel", path)).Publish()},
2025-11-07 20:00:05 -05:00
m_nameValue{wpi::util::rsplit(path, '/').second} {}
void NTCommandSchedulerModel::CancelCommand(size_t index) {
if (index < m_idsValue.size()) {
2022-10-08 10:01:31 -07:00
m_cancel.Set({{m_idsValue[index]}});
}
}
void NTCommandSchedulerModel::Update() {
2022-10-08 10:01:31 -07:00
for (auto&& v : m_name.ReadQueue()) {
m_nameValue = std::move(v.value);
}
for (auto&& v : m_commands.ReadQueue()) {
m_commandsValue = std::move(v.value);
}
for (auto&& v : m_ids.ReadQueue()) {
m_idsValue = std::move(v.value);
}
}
bool NTCommandSchedulerModel::Exists() {
return m_commands.Exists();
}