[ntcore] NetworkTables 4 (#3217)

This commit is contained in:
Peter Johnson
2022-10-08 10:01:31 -07:00
committed by GitHub
parent 90cfa00115
commit 77301b126c
380 changed files with 34573 additions and 22095 deletions

View File

@@ -4,16 +4,37 @@
#include "frc/smartdashboard/SendableBuilderImpl.h"
#include <networktables/BooleanArrayTopic.h>
#include <networktables/BooleanTopic.h>
#include <networktables/DoubleArrayTopic.h>
#include <networktables/DoubleTopic.h>
#include <networktables/FloatArrayTopic.h>
#include <networktables/FloatTopic.h>
#include <networktables/IntegerArrayTopic.h>
#include <networktables/IntegerTopic.h>
#include <networktables/RawTopic.h>
#include <networktables/StringArrayTopic.h>
#include <ntcore_cpp.h>
#include <wpi/SmallString.h>
#include <wpi/SmallVector.h>
#include "frc/smartdashboard/SmartDashboard.h"
using namespace frc;
template <typename Topic>
void SendableBuilderImpl::PropertyImpl<Topic>::Update(bool controllable,
int64_t time) {
if (controllable && sub && updateLocal) {
updateLocal(sub);
} else if (pub && updateNetwork) {
updateNetwork(pub, time);
}
}
void SendableBuilderImpl::SetTable(std::shared_ptr<nt::NetworkTable> table) {
m_table = table;
m_controllableEntry = table->GetEntry(".controllable");
m_controllablePublisher = table->GetBooleanTopic(".controllable").Publish();
m_controllablePublisher.SetDefault(false);
}
std::shared_ptr<nt::NetworkTable> SendableBuilderImpl::GetTable() {
@@ -31,9 +52,7 @@ bool SendableBuilderImpl::IsActuator() const {
void SendableBuilderImpl::Update() {
uint64_t time = nt::Now();
for (auto& property : m_properties) {
if (property.update) {
property.update(property.entry, time);
}
property->Update(m_controllable, time);
}
for (auto& updateTable : m_updateTables) {
updateTable();
@@ -41,20 +60,16 @@ void SendableBuilderImpl::Update() {
}
void SendableBuilderImpl::StartListeners() {
for (auto& property : m_properties) {
property.StartListener();
}
if (m_controllableEntry) {
m_controllableEntry.SetBoolean(true);
m_controllable = true;
if (m_controllablePublisher) {
m_controllablePublisher.Set(true);
}
}
void SendableBuilderImpl::StopListeners() {
for (auto& property : m_properties) {
property.StopListener();
}
if (m_controllableEntry) {
m_controllableEntry.SetBoolean(false);
m_controllable = false;
if (m_controllablePublisher) {
m_controllablePublisher.Set(false);
}
}
@@ -77,11 +92,17 @@ void SendableBuilderImpl::ClearProperties() {
}
void SendableBuilderImpl::SetSmartDashboardType(std::string_view type) {
m_table->GetEntry(".type").SetString(type);
if (!m_typePublisher) {
m_typePublisher = m_table->GetStringTopic(".type").Publish();
}
m_typePublisher.Set(type);
}
void SendableBuilderImpl::SetActuator(bool value) {
m_table->GetEntry(".actuator").SetBoolean(value);
if (!m_actuatorPublisher) {
m_actuatorPublisher = m_table->GetBooleanTopic(".actuator").Publish();
}
m_actuatorPublisher.Set(value);
m_actuator = value;
}
@@ -89,272 +110,183 @@ void SendableBuilderImpl::SetSafeState(std::function<void()> func) {
m_safeState = func;
}
void SendableBuilderImpl::SetUpdateTable(std::function<void()> func) {
void SendableBuilderImpl::SetUpdateTable(wpi::unique_function<void()> func) {
m_updateTables.emplace_back(std::move(func));
}
nt::NetworkTableEntry SendableBuilderImpl::GetEntry(std::string_view key) {
return m_table->GetEntry(key);
nt::Topic SendableBuilderImpl::GetTopic(std::string_view key) {
return m_table->GetTopic(key);
}
template <typename Topic, typename Getter, typename Setter>
void SendableBuilderImpl::AddPropertyImpl(Topic topic, Getter getter,
Setter setter) {
auto prop = std::make_unique<PropertyImpl<Topic>>();
if (getter) {
prop->pub = topic.Publish();
prop->updateNetwork = [=](auto& pub, int64_t time) {
pub.Set(getter(), time);
};
}
if (setter) {
prop->sub = topic.Subscribe({});
prop->updateLocal = [=](auto& sub) {
for (auto&& val : sub.ReadQueue()) {
setter(val.value);
}
};
}
m_properties.emplace_back(std::move(prop));
}
void SendableBuilderImpl::AddBooleanProperty(std::string_view key,
std::function<bool()> getter,
std::function<void(bool)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeBoolean(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBoolean()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBoolean()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetBooleanTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddIntegerProperty(
std::string_view key, std::function<int64_t()> getter,
std::function<void(int64_t)> setter) {
AddPropertyImpl(m_table->GetIntegerTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddFloatProperty(std::string_view key,
std::function<float()> getter,
std::function<void(float)> setter) {
AddPropertyImpl(m_table->GetFloatTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddDoubleProperty(
std::string_view key, std::function<double()> getter,
std::function<void(double)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeDouble(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDouble()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDouble()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetDoubleTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddStringProperty(
std::string_view key, std::function<std::string()> getter,
std::function<void(std::string_view)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeString(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsString()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetString()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetStringTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddBooleanArrayProperty(
std::string_view key, std::function<std::vector<int>()> getter,
std::function<void(wpi::span<const int>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeBooleanArray(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBooleanArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBooleanArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetBooleanArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddIntegerArrayProperty(
std::string_view key, std::function<std::vector<int64_t>()> getter,
std::function<void(wpi::span<const int64_t>)> setter) {
AddPropertyImpl(m_table->GetIntegerArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddFloatArrayProperty(
std::string_view key, std::function<std::vector<float>()> getter,
std::function<void(wpi::span<const float>)> setter) {
AddPropertyImpl(m_table->GetFloatArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddDoubleArrayProperty(
std::string_view key, std::function<std::vector<double>()> getter,
std::function<void(wpi::span<const double>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeDoubleArray(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDoubleArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDoubleArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetDoubleArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddStringArrayProperty(
std::string_view key, std::function<std::vector<std::string>()> getter,
std::function<void(wpi::span<const std::string>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeStringArray(getter(), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsStringArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetStringArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddPropertyImpl(m_table->GetStringArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddRawProperty(
std::string_view key, std::function<std::string()> getter,
std::function<void(std::string_view)> setter) {
m_properties.emplace_back(*m_table, key);
std::string_view key, std::string_view typeString,
std::function<std::vector<uint8_t>()> getter,
std::function<void(wpi::span<const uint8_t>)> setter) {
auto topic = m_table->GetRawTopic(key);
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(nt::Value::MakeRaw(getter(), time));
prop->pub = topic.Publish(typeString);
prop->updateNetwork = [=](auto& pub, int64_t time) {
pub.Set(getter(), time);
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsRaw()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetRaw()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
prop->sub = topic.Subscribe(typeString, {});
prop->updateLocal = [=](auto& sub) {
for (auto&& val : sub.ReadQueue()) {
setter(val.value);
}
};
}
m_properties.emplace_back(std::move(prop));
}
void SendableBuilderImpl::AddValueProperty(
std::string_view key, std::function<std::shared_ptr<nt::Value>()> getter,
std::function<void(std::shared_ptr<nt::Value>)> setter) {
m_properties.emplace_back(*m_table, key);
template <typename T, size_t Size, typename Topic, typename Getter,
typename Setter>
void SendableBuilderImpl::AddSmallPropertyImpl(Topic topic, Getter getter,
Setter setter) {
auto prop = std::make_unique<PropertyImpl<Topic>>();
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
entry.SetValue(getter());
prop->pub = topic.Publish();
prop->updateNetwork = [=](auto& pub, int64_t time) {
wpi::SmallVector<T, Size> buf;
pub.Set(getter(buf), time);
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
SmartDashboard::PostListenerTask([=] { setter(event.value); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
prop->sub = topic.Subscribe({});
prop->updateLocal = [=](auto& sub) {
for (auto&& val : sub.ReadQueue()) {
setter(val.value);
}
};
}
m_properties.emplace_back(std::move(prop));
}
void SendableBuilderImpl::AddSmallStringProperty(
std::string_view key,
std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(std::string_view)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
wpi::SmallString<128> buf;
entry.SetValue(nt::Value::MakeString(getter(buf), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsString()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetString()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddSmallPropertyImpl<char, 128>(m_table->GetStringTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallBooleanArrayProperty(
std::string_view key,
std::function<wpi::span<const int>(wpi::SmallVectorImpl<int>& buf)> getter,
std::function<void(wpi::span<const int>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
wpi::SmallVector<int, 16> buf;
entry.SetValue(nt::Value::MakeBooleanArray(getter(buf), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBooleanArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBooleanArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddSmallPropertyImpl<int, 16>(m_table->GetBooleanArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallIntegerArrayProperty(
std::string_view key,
std::function<wpi::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
getter,
std::function<void(wpi::span<const int64_t>)> setter) {
AddSmallPropertyImpl<int64_t, 16>(m_table->GetIntegerArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallFloatArrayProperty(
std::string_view key,
std::function<wpi::span<const float>(wpi::SmallVectorImpl<float>& buf)>
getter,
std::function<void(wpi::span<const float>)> setter) {
AddSmallPropertyImpl<float, 16>(m_table->GetFloatArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallDoubleArrayProperty(
@@ -362,28 +294,8 @@ void SendableBuilderImpl::AddSmallDoubleArrayProperty(
std::function<wpi::span<const double>(wpi::SmallVectorImpl<double>& buf)>
getter,
std::function<void(wpi::span<const double>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
wpi::SmallVector<double, 16> buf;
entry.SetValue(nt::Value::MakeDoubleArray(getter(buf), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDoubleArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDoubleArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddSmallPropertyImpl<double, 16>(m_table->GetDoubleArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallStringArrayProperty(
@@ -392,54 +304,31 @@ void SendableBuilderImpl::AddSmallStringArrayProperty(
wpi::span<const std::string>(wpi::SmallVectorImpl<std::string>& buf)>
getter,
std::function<void(wpi::span<const std::string>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
wpi::SmallVector<std::string, 16> buf;
entry.SetValue(nt::Value::MakeStringArray(getter(buf), time));
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsStringArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetStringArray()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
};
}
AddSmallPropertyImpl<std::string, 16>(m_table->GetStringArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallRawProperty(
std::string_view key,
std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(std::string_view)> setter) {
m_properties.emplace_back(*m_table, key);
std::string_view key, std::string_view typeString,
std::function<wpi::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
getter,
std::function<void(wpi::span<const uint8_t>)> setter) {
auto topic = m_table->GetRawTopic(key);
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
if (getter) {
m_properties.back().update = [=](nt::NetworkTableEntry entry,
uint64_t time) {
wpi::SmallVector<char, 128> buf;
entry.SetValue(nt::Value::MakeRaw(getter(buf), time));
prop->pub = topic.Publish(typeString);
prop->updateNetwork = [=](auto& pub, int64_t time) {
wpi::SmallVector<uint8_t, 128> buf;
pub.Set(getter(buf), time);
};
}
if (setter) {
m_properties.back().createListener =
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsRaw()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetRaw()); });
},
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
prop->sub = topic.Subscribe(typeString, {});
prop->updateLocal = [=](auto& sub) {
for (auto&& val : sub.ReadQueue()) {
setter(val.value);
}
};
}
m_properties.emplace_back(std::move(prop));
}