12#include <wpi/datalog/DataLog.hpp>
13#include <wpi/nt/NetworkTable.hpp>
14#include <wpi/nt/StringTopic.hpp>
15#include <wpi/system/DataLogManager.hpp>
38 m_key{std::move(key)},
40 m_defaultValue{defaultVal},
41 m_cachedValue{std::move(defaultVal)} {}
49 m_defaultValue = value;
50 m_cachedValue = std::move(value);
60 std::shared_ptr<wpi::nt::NetworkTable> tuningTable) {
61 m_dataTable = dataTable;
62 m_tuningTable = tuningTable;
63 if (!m_enabled)
return;
65 if (tuningTable && m_tunable) {
66 auto topic = tuningTable->GetStringTopic(m_key);
67 m_pubSub = topic.Publish();
68 m_pubSub->SetDefault(m_defaultValue);
69 m_subscriber = topic.Subscribe(m_defaultValue);
70 }
else if (dataTable) {
71 auto topic = dataTable->GetStringTopic(m_key);
72 m_publisher = topic.Publish();
73 m_publisher->SetDefault(m_defaultValue);
92 if (m_tunable)
return;
93 std::string path = prefix;
94 if (!path.empty() && path.back() !=
'/') path +=
'/';
96 m_dataLogEntry = wpi::log::StringLogEntry{wpi::DataLogManager::GetLog(), path};
105 bool Set(std::string_view value) {
106 if (!m_enabled)
return false;
107 if (m_dataLogEntry) {
108 m_dataLogEntry->Append(value);
111 if (m_subscriber->Get(m_defaultValue) != value)
return false;
114 m_publisher->Set(value);
125 if (!m_enabled)
return m_defaultValue;
127 return m_subscriber->Get(m_defaultValue);
129 throw std::runtime_error(
"Tuning table not configured for " + m_key +
"!");
140 m_pubSub->Set(value);
141 }
else if (m_publisher) {
142 m_publisher->Set(value);
152 if (m_subscriber && m_tunable && m_enabled) {
153 std::string current = m_subscriber->Get(m_defaultValue);
154 if (current != m_cachedValue) {
155 m_cachedValue = std::move(current);
171 void Display(
bool state) { m_enabled = state; }
179 m_subscriber.reset();
183 m_dataTable->GetEntry(m_key).Unpublish();
186 m_tuningTable->GetEntry(m_key).Unpublish();
194 bool m_enabled{
false};
195 std::string m_defaultValue;
196 std::string m_cachedValue;
198 std::optional<wpi::nt::StringPublisher> m_publisher;
199 std::optional<wpi::nt::StringSubscriber> m_subscriber;
200 std::optional<wpi::nt::StringPublisher> m_pubSub;
201 std::optional<wpi::log::StringLogEntry> m_dataLogEntry;
203 std::shared_ptr<wpi::nt::NetworkTable> m_tuningTable;
204 std::shared_ptr<wpi::nt::NetworkTable> m_dataTable;
void ForceSet(std::string_view value)
Definition StringTelemetry.hpp:138
void Enable()
Definition StringTelemetry.hpp:163
void SetupNetworkTable(std::shared_ptr< wpi::nt::NetworkTable > dataTable)
Definition StringTelemetry.hpp:82
void SetDefaultValue(std::string value)
Definition StringTelemetry.hpp:48
bool Set(std::string_view value)
Definition StringTelemetry.hpp:105
bool IsTunable()
Definition StringTelemetry.hpp:151
void SetupDataLog(const std::string &prefix)
Definition StringTelemetry.hpp:91
void SetupNetworkTables(std::shared_ptr< wpi::nt::NetworkTable > dataTable, std::shared_ptr< wpi::nt::NetworkTable > tuningTable)
Definition StringTelemetry.hpp:59
void Close()
Definition StringTelemetry.hpp:178
StringTelemetry(std::string key, std::string defaultVal, F field, bool tunable)
Definition StringTelemetry.hpp:36
void Display(bool state)
Definition StringTelemetry.hpp:171
std::string Get() const
Definition StringTelemetry.hpp:124
void Disable()
Definition StringTelemetry.hpp:165
F GetField() const
Definition StringTelemetry.hpp:173
bool IsEnabled() const
Definition StringTelemetry.hpp:175
Definition SwerveDrive.hpp:872