mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
[wpilib] SendableChooser: implement Sendable instead of NTSendable (#5718)
This commit is contained in:
@@ -19,8 +19,6 @@ SendableChooserBase::SendableChooserBase(SendableChooserBase&& oth)
|
||||
m_defaultChoice(std::move(oth.m_defaultChoice)),
|
||||
m_selected(std::move(oth.m_selected)),
|
||||
m_haveSelected(std::move(oth.m_haveSelected)),
|
||||
m_instancePubs(std::move(oth.m_instancePubs)),
|
||||
m_activePubs(std::move(oth.m_activePubs)),
|
||||
m_instance(std::move(oth.m_instance)) {}
|
||||
|
||||
SendableChooserBase& SendableChooserBase::operator=(SendableChooserBase&& oth) {
|
||||
@@ -29,8 +27,6 @@ SendableChooserBase& SendableChooserBase::operator=(SendableChooserBase&& oth) {
|
||||
m_defaultChoice = std::move(oth.m_defaultChoice);
|
||||
m_selected = std::move(oth.m_selected);
|
||||
m_haveSelected = std::move(oth.m_haveSelected);
|
||||
m_instancePubs = std::move(oth.m_instancePubs);
|
||||
m_activePubs = std::move(oth.m_activePubs);
|
||||
m_instance = std::move(oth.m_instance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class SendableChooser : public SendableChooserBase {
|
||||
*/
|
||||
void OnChange(std::function<void(T)>);
|
||||
|
||||
void InitSendable(nt::NTSendableBuilder& builder) override;
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <networktables/NTSendableBuilder.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
|
||||
#include "frc/smartdashboard/SendableChooser.h"
|
||||
|
||||
@@ -58,16 +58,9 @@ void SendableChooser<T>::OnChange(std::function<void(T)> listener) {
|
||||
|
||||
template <class T>
|
||||
requires std::copy_constructible<T> && std::default_initializable<T>
|
||||
void SendableChooser<T>::InitSendable(nt::NTSendableBuilder& builder) {
|
||||
void SendableChooser<T>::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("String Chooser");
|
||||
{
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_instancePubs.emplace_back(
|
||||
nt::IntegerTopic{builder.GetTopic(kInstance)}.Publish());
|
||||
m_instancePubs.back().Set(m_instance);
|
||||
m_activePubs.emplace_back(
|
||||
nt::StringTopic{builder.GetTopic(kActive)}.Publish());
|
||||
}
|
||||
builder.PublishConstInteger(kInstance, m_instance);
|
||||
builder.AddStringArrayProperty(
|
||||
kOptions,
|
||||
[=, this] {
|
||||
@@ -109,9 +102,6 @@ void SendableChooser<T>::InitSendable(nt::NTSendableBuilder& builder) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_haveSelected = true;
|
||||
m_selected = val;
|
||||
for (auto& pub : m_activePubs) {
|
||||
pub.Set(val);
|
||||
}
|
||||
if (m_previousVal != val && m_listener) {
|
||||
choice = m_choices[val];
|
||||
listener = m_listener;
|
||||
|
||||
@@ -7,11 +7,8 @@
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
|
||||
#include <networktables/IntegerTopic.h>
|
||||
#include <networktables/NTSendable.h>
|
||||
#include <networktables/StringTopic.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
namespace frc {
|
||||
@@ -22,7 +19,7 @@ namespace frc {
|
||||
* It contains static, non-templated variables to avoid their duplication in the
|
||||
* template class.
|
||||
*/
|
||||
class SendableChooserBase : public nt::NTSendable,
|
||||
class SendableChooserBase : public wpi::Sendable,
|
||||
public wpi::SendableHelper<SendableChooserBase> {
|
||||
public:
|
||||
SendableChooserBase();
|
||||
@@ -41,8 +38,6 @@ class SendableChooserBase : public nt::NTSendable,
|
||||
std::string m_defaultChoice;
|
||||
std::string m_selected;
|
||||
bool m_haveSelected = false;
|
||||
wpi::SmallVector<nt::IntegerPublisher, 2> m_instancePubs;
|
||||
wpi::SmallVector<nt::StringPublisher, 2> m_activePubs;
|
||||
wpi::mutex m_mutex;
|
||||
int m_instance;
|
||||
std::string m_previousVal;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <networktables/StringTopic.h>
|
||||
|
||||
class SendableChooserTest : public ::testing::TestWithParam<int> {};
|
||||
|
||||
|
||||
@@ -6,16 +6,10 @@ package edu.wpi.first.wpilibj.smartdashboard;
|
||||
|
||||
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
|
||||
|
||||
import edu.wpi.first.networktables.IntegerPublisher;
|
||||
import edu.wpi.first.networktables.IntegerTopic;
|
||||
import edu.wpi.first.networktables.NTSendable;
|
||||
import edu.wpi.first.networktables.NTSendableBuilder;
|
||||
import edu.wpi.first.networktables.StringPublisher;
|
||||
import edu.wpi.first.networktables.StringTopic;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -33,7 +27,7 @@ import java.util.function.Consumer;
|
||||
*
|
||||
* @param <V> The type of the values to be stored
|
||||
*/
|
||||
public class SendableChooser<V> implements NTSendable, AutoCloseable {
|
||||
public class SendableChooser<V> implements Sendable, AutoCloseable {
|
||||
/** The key for the default value. */
|
||||
private static final String DEFAULT = "default";
|
||||
|
||||
@@ -67,14 +61,6 @@ public class SendableChooser<V> implements NTSendable, AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
SendableRegistry.remove(this);
|
||||
m_mutex.lock();
|
||||
try {
|
||||
for (StringPublisher pub : m_activePubs) {
|
||||
pub.close();
|
||||
}
|
||||
} finally {
|
||||
m_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,15 +122,12 @@ public class SendableChooser<V> implements NTSendable, AutoCloseable {
|
||||
}
|
||||
|
||||
private String m_selected;
|
||||
private final List<StringPublisher> m_activePubs = new ArrayList<>();
|
||||
private final ReentrantLock m_mutex = new ReentrantLock();
|
||||
|
||||
@Override
|
||||
public void initSendable(NTSendableBuilder builder) {
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("String Chooser");
|
||||
IntegerPublisher instancePub = new IntegerTopic(builder.getTopic(INSTANCE)).publish();
|
||||
instancePub.set(m_instance);
|
||||
builder.addCloseable(instancePub);
|
||||
builder.publishConstInteger(INSTANCE, m_instance);
|
||||
builder.addStringProperty(DEFAULT, () -> m_defaultChoice, null);
|
||||
builder.addStringArrayProperty(OPTIONS, () -> m_map.keySet().toArray(new String[0]), null);
|
||||
builder.addStringProperty(
|
||||
@@ -162,12 +145,6 @@ public class SendableChooser<V> implements NTSendable, AutoCloseable {
|
||||
}
|
||||
},
|
||||
null);
|
||||
m_mutex.lock();
|
||||
try {
|
||||
m_activePubs.add(new StringTopic(builder.getTopic(ACTIVE)).publish());
|
||||
} finally {
|
||||
m_mutex.unlock();
|
||||
}
|
||||
builder.addStringProperty(
|
||||
SELECTED,
|
||||
null,
|
||||
@@ -185,9 +162,6 @@ public class SendableChooser<V> implements NTSendable, AutoCloseable {
|
||||
listener = null;
|
||||
}
|
||||
m_previousVal = val;
|
||||
for (StringPublisher pub : m_activePubs) {
|
||||
pub.set(val);
|
||||
}
|
||||
} finally {
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user