mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
[ntcore] NetworkTables 4 (#3217)
This commit is contained in:
@@ -12,8 +12,9 @@
|
||||
#include <frc/livewindow/LiveWindow.h>
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <networktables/IntegerArrayTopic.h>
|
||||
#include <networktables/NTSendableBuilder.h>
|
||||
#include <networktables/NetworkTableEntry.h>
|
||||
#include <networktables/StringArrayTopic.h>
|
||||
#include <wpi/DenseMap.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
@@ -418,34 +419,35 @@ void CommandScheduler::OnCommandFinish(Action action) {
|
||||
|
||||
void CommandScheduler::InitSendable(nt::NTSendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Scheduler");
|
||||
auto namesEntry = builder.GetEntry("Names");
|
||||
auto idsEntry = builder.GetEntry("Ids");
|
||||
auto cancelEntry = builder.GetEntry("Cancel");
|
||||
builder.SetUpdateTable(
|
||||
[this,
|
||||
namesPub = nt::StringArrayTopic{builder.GetTopic("Names")}.Publish(),
|
||||
idsPub = nt::IntegerArrayTopic{builder.GetTopic("Ids")}.Publish(),
|
||||
cancelEntry = nt::IntegerArrayTopic{builder.GetTopic("Cancel")}.GetEntry(
|
||||
{})]() mutable {
|
||||
auto toCancel = cancelEntry.Get();
|
||||
if (!toCancel.empty()) {
|
||||
for (auto cancel : cancelEntry.Get()) {
|
||||
uintptr_t ptrTmp = static_cast<uintptr_t>(cancel);
|
||||
Command* command = reinterpret_cast<Command*>(ptrTmp);
|
||||
if (m_impl->scheduledCommands.find(command) !=
|
||||
m_impl->scheduledCommands.end()) {
|
||||
Cancel(command);
|
||||
}
|
||||
}
|
||||
cancelEntry.Set({});
|
||||
}
|
||||
|
||||
builder.SetUpdateTable([=] {
|
||||
double tmp[1];
|
||||
tmp[0] = 0;
|
||||
auto toCancel = cancelEntry.GetDoubleArray(tmp);
|
||||
for (auto cancel : toCancel) {
|
||||
uintptr_t ptrTmp = static_cast<uintptr_t>(cancel);
|
||||
Command* command = reinterpret_cast<Command*>(ptrTmp);
|
||||
if (m_impl->scheduledCommands.find(command) !=
|
||||
m_impl->scheduledCommands.end()) {
|
||||
Cancel(command);
|
||||
}
|
||||
nt::NetworkTableEntry(cancelEntry).SetDoubleArray({});
|
||||
}
|
||||
|
||||
wpi::SmallVector<std::string, 8> names;
|
||||
wpi::SmallVector<double, 8> ids;
|
||||
for (Command* command : m_impl->scheduledCommands) {
|
||||
names.emplace_back(command->GetName());
|
||||
uintptr_t ptrTmp = reinterpret_cast<uintptr_t>(command);
|
||||
ids.emplace_back(static_cast<double>(ptrTmp));
|
||||
}
|
||||
nt::NetworkTableEntry(namesEntry).SetStringArray(names);
|
||||
nt::NetworkTableEntry(idsEntry).SetDoubleArray(ids);
|
||||
});
|
||||
wpi::SmallVector<std::string, 8> names;
|
||||
wpi::SmallVector<int64_t, 8> ids;
|
||||
for (Command* command : m_impl->scheduledCommands) {
|
||||
names.emplace_back(command->GetName());
|
||||
uintptr_t ptrTmp = reinterpret_cast<uintptr_t>(command);
|
||||
ids.emplace_back(static_cast<int64_t>(ptrTmp));
|
||||
}
|
||||
namesPub.Set(names);
|
||||
idsPub.Set(ids);
|
||||
});
|
||||
}
|
||||
|
||||
void CommandScheduler::SetDefaultCommandImpl(Subsystem* subsystem,
|
||||
|
||||
@@ -6,15 +6,21 @@
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
NetworkButton::NetworkButton(nt::NetworkTableEntry entry)
|
||||
: Button([entry] {
|
||||
return entry.GetInstance().IsConnected() && entry.GetBoolean(false);
|
||||
NetworkButton::NetworkButton(nt::BooleanTopic topic)
|
||||
: NetworkButton(topic.Subscribe(false)) {}
|
||||
|
||||
NetworkButton::NetworkButton(nt::BooleanSubscriber sub)
|
||||
: Button([sub = std::make_shared<nt::BooleanSubscriber>(std::move(sub))] {
|
||||
return sub->GetTopic().GetInstance().IsConnected() && sub->Get();
|
||||
}) {}
|
||||
|
||||
NetworkButton::NetworkButton(std::shared_ptr<nt::NetworkTable> table,
|
||||
std::string_view field)
|
||||
: NetworkButton(table->GetEntry(field)) {}
|
||||
: NetworkButton(table->GetBooleanTopic(field)) {}
|
||||
|
||||
NetworkButton::NetworkButton(std::string_view table, std::string_view field)
|
||||
: NetworkButton(nt::NetworkTableInstance::GetDefault().GetTable(table),
|
||||
field) {}
|
||||
: NetworkButton(nt::NetworkTableInstance::GetDefault(), table, field) {}
|
||||
|
||||
NetworkButton::NetworkButton(nt::NetworkTableInstance inst,
|
||||
std::string_view table, std::string_view field)
|
||||
: NetworkButton(inst.GetTable(table), field) {}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/BooleanTopic.h>
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
|
||||
@@ -23,9 +24,16 @@ class NetworkButton : public Button {
|
||||
/**
|
||||
* Creates a NetworkButton that commands can be bound to.
|
||||
*
|
||||
* @param entry The entry that is the value.
|
||||
* @param topic The boolean topic that contains the value.
|
||||
*/
|
||||
explicit NetworkButton(nt::NetworkTableEntry entry);
|
||||
explicit NetworkButton(nt::BooleanTopic topic);
|
||||
|
||||
/**
|
||||
* Creates a NetworkButton that commands can be bound to.
|
||||
*
|
||||
* @param sub The boolean subscriber that provides the value.
|
||||
*/
|
||||
explicit NetworkButton(nt::BooleanSubscriber sub);
|
||||
|
||||
/**
|
||||
* Creates a NetworkButton that commands can be bound to.
|
||||
@@ -43,5 +51,15 @@ class NetworkButton : public Button {
|
||||
* @param field The field that is the value.
|
||||
*/
|
||||
NetworkButton(std::string_view table, std::string_view field);
|
||||
|
||||
/**
|
||||
* Creates a NetworkButton that commands can be bound to.
|
||||
*
|
||||
* @param inst The NetworkTable instance to use
|
||||
* @param table The table where the networktable value is located.
|
||||
* @param field The field that is the value.
|
||||
*/
|
||||
NetworkButton(nt::NetworkTableInstance inst, std::string_view table,
|
||||
std::string_view field);
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
Reference in New Issue
Block a user