[glass] Split DataSource into type-specific variants (#7588)

This commit is contained in:
Peter Johnson
2025-01-03 13:36:40 -08:00
committed by GitHub
parent 148fcdca85
commit 0f6693594c
62 changed files with 667 additions and 390 deletions

View File

@@ -21,9 +21,7 @@ NTCommandSelectorModel::NTCommandSelectorModel(nt::NetworkTableInstance inst,
.GetEntry(false)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_runningData{fmt::format("NTCmd:{}", path)},
m_nameValue{wpi::rsplit(path, '/').second} {
m_runningData.SetDigital(true);
}
m_nameValue{wpi::rsplit(path, '/').second} {}
void NTCommandSelectorModel::SetRunning(bool run) {
m_running.Set(run);

View File

@@ -21,9 +21,7 @@ NTDigitalInputModel::NTDigitalInputModel(nt::NetworkTableInstance inst,
inst.GetBooleanTopic(fmt::format("{}/Value", path)).Subscribe(false)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_valueData{fmt::format("NT_DIn:{}", path)},
m_nameValue{wpi::rsplit(path, '/').second} {
m_valueData.SetDigital(true);
}
m_nameValue{wpi::rsplit(path, '/').second} {}
void NTDigitalInputModel::Update() {
for (auto&& v : m_value.ReadQueue()) {

View File

@@ -21,9 +21,7 @@ NTDigitalOutputModel::NTDigitalOutputModel(nt::NetworkTableInstance inst,
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path))
.Subscribe(false)},
m_valueData{fmt::format("NT_DOut:{}", path)} {
m_valueData.SetDigital(true);
}
m_valueData{fmt::format("NT_DOut:{}", path)} {}
void NTDigitalOutputModel::SetValue(bool val) {
m_value.Set(val);

View File

@@ -6,6 +6,8 @@
#include <stdint.h>
#include <utility>
#include <fmt/format.h>
#include <wpi/SmallVector.h>
#include <wpi/timestamp.h>
@@ -32,19 +34,9 @@ NTFMSModel::NTFMSModel(nt::NetworkTableInstance inst, std::string_view path)
m_estop{fmt::format("NT_FMS:EStop:{}", path)},
m_enabled{fmt::format("NT_FMS:RobotEnabled:{}", path)},
m_test{fmt::format("NT_FMS:TestMode:{}", path)},
m_autonomous{fmt::format("NT_FMS:AutonomousMode:{}", path)} {
m_fmsAttached.SetDigital(true);
m_dsAttached.SetDigital(true);
m_estop.SetDigital(true);
m_enabled.SetDigital(true);
m_test.SetDigital(true);
m_autonomous.SetDigital(true);
}
std::string_view NTFMSModel::GetGameSpecificMessage(
wpi::SmallVectorImpl<char>& buf) {
return m_gameSpecificMessage.Get(buf, "");
}
m_autonomous{fmt::format("NT_FMS:AutonomousMode:{}", path)},
m_gameSpecificMessageData{
fmt::format("NT_FMS:GameSpecificMessage:{}", path)} {}
void NTFMSModel::Update() {
for (auto&& v : m_alliance.ReadQueue()) {
@@ -70,6 +62,9 @@ void NTFMSModel::Update() {
m_fmsAttached.SetValue(((controlWord & 0x10) != 0) ? 1 : 0, v.time);
m_dsAttached.SetValue(((controlWord & 0x20) != 0) ? 1 : 0, v.time);
}
for (auto&& v : m_gameSpecificMessage.ReadQueue()) {
m_gameSpecificMessageData.SetValue(std::move(v.value), v.time);
}
}
bool NTFMSModel::Exists() {

View File

@@ -6,7 +6,6 @@
#include <algorithm>
#include <cinttypes>
#include <concepts>
#include <cstring>
#include <functional>
#include <initializer_list>
@@ -632,19 +631,53 @@ static void UpdateJsonValueSource(NetworkTablesModel& model,
}
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
std::string_view name, double value, int64_t time, bool digital) {
std::string_view name, bool value, int64_t time) {
valueChildren.clear();
if (!source) {
source = std::make_unique<DataSource>(fmt::format("NT:{}", name));
auto s = dynamic_cast<BooleanSource*>(source.get());
if (!s) {
source = std::make_unique<BooleanSource>(fmt::format("NT:{}", name));
s = static_cast<BooleanSource*>(source.get());
}
source->SetValue(value, time);
source->SetDigital(digital);
s->SetValue(value, time);
}
template <typename T, typename MakeValue>
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
std::string_view name, float value, int64_t time) {
valueChildren.clear();
auto s = dynamic_cast<FloatSource*>(source.get());
if (!s) {
source = std::make_unique<FloatSource>(fmt::format("NT:{}", name));
s = static_cast<FloatSource*>(source.get());
}
s->SetValue(value, time);
}
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
std::string_view name, double value, int64_t time) {
valueChildren.clear();
auto s = dynamic_cast<DoubleSource*>(source.get());
if (!s) {
source = std::make_unique<DoubleSource>(fmt::format("NT:{}", name));
s = static_cast<DoubleSource*>(source.get());
}
s->SetValue(value, time);
}
void NetworkTablesModel::ValueSource::UpdateDiscreteSource(
std::string_view name, int64_t value, int64_t time) {
valueChildren.clear();
auto s = dynamic_cast<IntegerSource*>(source.get());
if (!s) {
source = std::make_unique<IntegerSource>(fmt::format("NT:{}", name));
s = static_cast<IntegerSource*>(source.get());
}
s->SetValue(value, time);
}
template <bool IsBoolean, typename T, typename MakeValue>
void NetworkTablesModel::ValueSource::UpdateDiscreteArray(
std::string_view name, std::span<const T> arr, int64_t time,
MakeValue makeValue, bool digital) {
MakeValue makeValue) {
if (valueChildrenMap) {
valueChildren.clear();
valueChildrenMap = false;
@@ -657,7 +690,11 @@ void NetworkTablesModel::ValueSource::UpdateDiscreteArray(
child.path = fmt::format("{}{}", name, child.name);
}
child.value = makeValue(arr[i], time);
child.UpdateDiscreteSource(child.path, arr[i], time, digital);
if constexpr (IsBoolean) {
child.UpdateDiscreteSource(child.path, static_cast<bool>(arr[i]), time);
} else {
child.UpdateDiscreteSource(child.path, arr[i], time);
}
++i;
}
}
@@ -667,8 +704,7 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
std::string_view typeStr) {
switch (value.type()) {
case NT_BOOLEAN:
UpdateDiscreteSource(name, value.GetBoolean() ? 1 : 0, value.time(),
true);
UpdateDiscreteSource(name, value.GetBoolean(), value.time());
break;
case NT_INTEGER:
UpdateDiscreteSource(name, value.GetInteger(), value.time());
@@ -680,20 +716,20 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
UpdateDiscreteSource(name, value.GetDouble(), value.time());
break;
case NT_BOOLEAN_ARRAY:
UpdateDiscreteArray(name, value.GetBooleanArray(), value.time(),
nt::Value::MakeBoolean, true);
UpdateDiscreteArray<true>(name, value.GetBooleanArray(), value.time(),
nt::Value::MakeBoolean);
break;
case NT_INTEGER_ARRAY:
UpdateDiscreteArray(name, value.GetIntegerArray(), value.time(),
nt::Value::MakeInteger);
UpdateDiscreteArray<false>(name, value.GetIntegerArray(), value.time(),
nt::Value::MakeInteger);
break;
case NT_FLOAT_ARRAY:
UpdateDiscreteArray(name, value.GetFloatArray(), value.time(),
nt::Value::MakeFloat);
UpdateDiscreteArray<false>(name, value.GetFloatArray(), value.time(),
nt::Value::MakeFloat);
break;
case NT_DOUBLE_ARRAY:
UpdateDiscreteArray(name, value.GetDoubleArray(), value.time(),
nt::Value::MakeDouble);
UpdateDiscreteArray<false>(name, value.GetDoubleArray(), value.time(),
nt::Value::MakeDouble);
break;
case NT_STRING_ARRAY: {
auto arr = value.GetStringArray();
@@ -729,6 +765,13 @@ void NetworkTablesModel::ValueSource::UpdateFromValue(
os << '"';
os.write_escaped(value.GetString());
os << '"';
auto s = dynamic_cast<StringSource*>(source.get());
if (!s) {
source = std::make_unique<StringSource>(fmt::format("NT:{}", name));
s = static_cast<StringSource*>(source.get());
}
s->SetValue(value.GetString(), value.time());
}
break;
case NT_RAW: