[wpilib] SendableChooser: implement Sendable instead of NTSendable (#5718)

This commit is contained in:
Gold856
2023-10-05 00:15:34 -04:00
committed by GitHub
parent a4030c670f
commit 6576d9b474
6 changed files with 12 additions and 56 deletions

View File

@@ -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();
}