mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
SCRIPT Move java files
This commit is contained in:
committed by
Peter Johnson
parent
7ca1be9bae
commit
c350c5f112
16
wpiutil/src/main/java/org/wpilib/util/sendable/Sendable.java
Normal file
16
wpiutil/src/main/java/org/wpilib/util/sendable/Sendable.java
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.util.sendable;
|
||||
|
||||
/** The base interface for objects that can be sent over the network. */
|
||||
@SuppressWarnings("PMD.ImplicitFunctionalInterface")
|
||||
public interface Sendable {
|
||||
/**
|
||||
* Initializes this {@link Sendable} object.
|
||||
*
|
||||
* @param builder sendable builder
|
||||
*/
|
||||
void initSendable(SendableBuilder builder);
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.util.sendable;
|
||||
|
||||
import edu.wpi.first.util.function.BooleanConsumer;
|
||||
import edu.wpi.first.util.function.FloatConsumer;
|
||||
import edu.wpi.first.util.function.FloatSupplier;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.DoubleConsumer;
|
||||
import java.util.function.DoubleSupplier;
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Helper class for building Sendable dashboard representations. */
|
||||
public interface SendableBuilder extends AutoCloseable {
|
||||
/** The backend kinds used for the sendable builder. */
|
||||
enum BackendKind {
|
||||
/** Unknown. */
|
||||
kUnknown,
|
||||
|
||||
/** NetworkTables. */
|
||||
kNetworkTables
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the string representation of the named data type that will be used by the smart dashboard
|
||||
* for this sendable.
|
||||
*
|
||||
* @param type data type
|
||||
*/
|
||||
void setSmartDashboardType(String type);
|
||||
|
||||
/**
|
||||
* Set a flag indicating if this Sendable should be treated as an actuator. By default, this flag
|
||||
* is false.
|
||||
*
|
||||
* @param value true if actuator, false if not
|
||||
*/
|
||||
void setActuator(boolean value);
|
||||
|
||||
/**
|
||||
* Add a boolean property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addBooleanProperty(String key, BooleanSupplier getter, BooleanConsumer setter);
|
||||
|
||||
/**
|
||||
* Add a constant boolean property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstBoolean(String key, boolean value);
|
||||
|
||||
/**
|
||||
* Add an integer property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addIntegerProperty(String key, LongSupplier getter, LongConsumer setter);
|
||||
|
||||
/**
|
||||
* Add a constant integer property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstInteger(String key, long value);
|
||||
|
||||
/**
|
||||
* Add a float property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addFloatProperty(String key, FloatSupplier getter, FloatConsumer setter);
|
||||
|
||||
/**
|
||||
* Add a constant float property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstFloat(String key, float value);
|
||||
|
||||
/**
|
||||
* Add a double property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addDoubleProperty(String key, DoubleSupplier getter, DoubleConsumer setter);
|
||||
|
||||
/**
|
||||
* Add a constant double property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstDouble(String key, double value);
|
||||
|
||||
/**
|
||||
* Add a string property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addStringProperty(String key, Supplier<String> getter, Consumer<String> setter);
|
||||
|
||||
/**
|
||||
* Add a constant string property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstString(String key, String value);
|
||||
|
||||
/**
|
||||
* Add a boolean array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addBooleanArrayProperty(String key, Supplier<boolean[]> getter, Consumer<boolean[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant boolean array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstBooleanArray(String key, boolean[] value);
|
||||
|
||||
/**
|
||||
* Add an integer array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addIntegerArrayProperty(String key, Supplier<long[]> getter, Consumer<long[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant integer property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstIntegerArray(String key, long[] value);
|
||||
|
||||
/**
|
||||
* Add a float array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addFloatArrayProperty(String key, Supplier<float[]> getter, Consumer<float[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant float array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstFloatArray(String key, float[] value);
|
||||
|
||||
/**
|
||||
* Add a double array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addDoubleArrayProperty(String key, Supplier<double[]> getter, Consumer<double[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant double array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstDoubleArray(String key, double[] value);
|
||||
|
||||
/**
|
||||
* Add a string array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addStringArrayProperty(String key, Supplier<String[]> getter, Consumer<String[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant string array property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstStringArray(String key, String[] value);
|
||||
|
||||
/**
|
||||
* Add a raw property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param typeString type string
|
||||
* @param getter getter function (returns current value)
|
||||
* @param setter setter function (sets new value)
|
||||
*/
|
||||
void addRawProperty(
|
||||
String key, String typeString, Supplier<byte[]> getter, Consumer<byte[]> setter);
|
||||
|
||||
/**
|
||||
* Add a constant raw property.
|
||||
*
|
||||
* @param key property name
|
||||
* @param typeString type string
|
||||
* @param value the value
|
||||
*/
|
||||
void publishConstRaw(String key, String typeString, byte[] value);
|
||||
|
||||
/**
|
||||
* Gets the kind of backend being used.
|
||||
*
|
||||
* @return Backend kind
|
||||
*/
|
||||
BackendKind getBackendKind();
|
||||
|
||||
/**
|
||||
* Return whether this sendable has been published.
|
||||
*
|
||||
* @return True if it has been published, false if not.
|
||||
*/
|
||||
boolean isPublished();
|
||||
|
||||
/** Update the published values by calling the getters for all properties. */
|
||||
void update();
|
||||
|
||||
/** Clear properties. */
|
||||
void clearProperties();
|
||||
|
||||
/**
|
||||
* Adds a closeable. The closeable.close() will be called when close() is called.
|
||||
*
|
||||
* @param closeable closeable object
|
||||
*/
|
||||
void addCloseable(AutoCloseable closeable);
|
||||
}
|
||||
@@ -0,0 +1,354 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.util.sendable;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* The SendableRegistry class is the public interface for registering sensors and actuators for use
|
||||
* on dashboards.
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidCatchingGenericException")
|
||||
public final class SendableRegistry {
|
||||
private static class Component implements AutoCloseable {
|
||||
Component() {}
|
||||
|
||||
Component(Sendable sendable) {
|
||||
m_sendable = new WeakReference<>(sendable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
m_builder.close();
|
||||
for (AutoCloseable data : m_data) {
|
||||
if (data != null) {
|
||||
data.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WeakReference<Sendable> m_sendable;
|
||||
SendableBuilder m_builder;
|
||||
String m_name;
|
||||
String m_subsystem = "Ungrouped";
|
||||
AutoCloseable[] m_data;
|
||||
|
||||
void setName(String moduleType, int channel) {
|
||||
m_name = moduleType + "[" + channel + "]";
|
||||
}
|
||||
|
||||
void setName(String moduleType, int moduleNumber, int channel) {
|
||||
m_name = moduleType + "[" + moduleNumber + "," + channel + "]";
|
||||
}
|
||||
}
|
||||
|
||||
private static final Map<Object, Component> components = new WeakHashMap<>();
|
||||
private static int nextDataHandle;
|
||||
|
||||
private static Component getOrAdd(Sendable sendable) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp == null) {
|
||||
comp = new Component(sendable);
|
||||
components.put(sendable, comp);
|
||||
} else {
|
||||
if (comp.m_sendable == null) {
|
||||
comp.m_sendable = new WeakReference<>(sendable);
|
||||
}
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
||||
private SendableRegistry() {
|
||||
throw new UnsupportedOperationException("This is a utility class!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an object to the registry.
|
||||
*
|
||||
* @param sendable object to add
|
||||
* @param name component name
|
||||
*/
|
||||
public static synchronized void add(Sendable sendable, String name) {
|
||||
Component comp = getOrAdd(sendable);
|
||||
comp.m_name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an object to the registry.
|
||||
*
|
||||
* @param sendable object to add
|
||||
* @param moduleType A string that defines the module name in the label for the value
|
||||
* @param channel The channel number the device is plugged into
|
||||
*/
|
||||
public static synchronized void add(Sendable sendable, String moduleType, int channel) {
|
||||
Component comp = getOrAdd(sendable);
|
||||
comp.setName(moduleType, channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an object to the registry.
|
||||
*
|
||||
* @param sendable object to add
|
||||
* @param moduleType A string that defines the module name in the label for the value
|
||||
* @param moduleNumber The number of the particular module type
|
||||
* @param channel The channel number the device is plugged into
|
||||
*/
|
||||
public static synchronized void add(
|
||||
Sendable sendable, String moduleType, int moduleNumber, int channel) {
|
||||
Component comp = getOrAdd(sendable);
|
||||
comp.setName(moduleType, moduleNumber, channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an object to the registry.
|
||||
*
|
||||
* @param sendable object to add
|
||||
* @param subsystem subsystem name
|
||||
* @param name component name
|
||||
*/
|
||||
public static synchronized void add(Sendable sendable, String subsystem, String name) {
|
||||
Component comp = getOrAdd(sendable);
|
||||
comp.m_name = name;
|
||||
comp.m_subsystem = subsystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a child object to an object. Adds the child object to the registry if it's not already
|
||||
* present.
|
||||
*
|
||||
* @param parent parent object
|
||||
* @param child child object
|
||||
*/
|
||||
public static synchronized void addChild(Sendable parent, Object child) {
|
||||
Component comp = components.get(child);
|
||||
if (comp == null) {
|
||||
comp = new Component();
|
||||
components.put(child, comp);
|
||||
}
|
||||
// comp.m_parent = new WeakReference<>(parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an object from the registry.
|
||||
*
|
||||
* @param sendable object to remove
|
||||
* @return true if the object was removed; false if it was not present
|
||||
*/
|
||||
public static synchronized boolean remove(Sendable sendable) {
|
||||
Component comp = components.remove(sendable);
|
||||
if (comp != null) {
|
||||
try {
|
||||
comp.close();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return comp != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an object is in the registry.
|
||||
*
|
||||
* @param sendable object to check
|
||||
* @return True if in registry, false if not.
|
||||
*/
|
||||
public static synchronized boolean contains(Sendable sendable) {
|
||||
return components.containsKey(sendable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of an object.
|
||||
*
|
||||
* @param sendable object
|
||||
* @return Name (empty if object is not in registry)
|
||||
*/
|
||||
public static synchronized String getName(Sendable sendable) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp == null) {
|
||||
return "";
|
||||
}
|
||||
return comp.m_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of an object.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param name name
|
||||
*/
|
||||
public static synchronized void setName(Sendable sendable, String name) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null) {
|
||||
comp.m_name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of an object with a channel number.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param moduleType A string that defines the module name in the label for the value
|
||||
* @param channel The channel number the device is plugged into
|
||||
*/
|
||||
public static synchronized void setName(Sendable sendable, String moduleType, int channel) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null) {
|
||||
comp.setName(moduleType, channel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of an object with a module and channel number.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param moduleType A string that defines the module name in the label for the value
|
||||
* @param moduleNumber The number of the particular module type
|
||||
* @param channel The channel number the device is plugged into
|
||||
*/
|
||||
public static synchronized void setName(
|
||||
Sendable sendable, String moduleType, int moduleNumber, int channel) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null) {
|
||||
comp.setName(moduleType, moduleNumber, channel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets both the subsystem name and device name of an object.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param subsystem subsystem name
|
||||
* @param name device name
|
||||
*/
|
||||
public static synchronized void setName(Sendable sendable, String subsystem, String name) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null) {
|
||||
comp.m_name = name;
|
||||
comp.m_subsystem = subsystem;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the subsystem name of an object.
|
||||
*
|
||||
* @param sendable object
|
||||
* @return Subsystem name (empty if object is not in registry)
|
||||
*/
|
||||
public static synchronized String getSubsystem(Sendable sendable) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp == null) {
|
||||
return "";
|
||||
}
|
||||
return comp.m_subsystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the subsystem name of an object.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param subsystem subsystem name
|
||||
*/
|
||||
public static synchronized void setSubsystem(Sendable sendable, String subsystem) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null) {
|
||||
comp.m_subsystem = subsystem;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a unique handle for setting/getting data with setData() and getData().
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
public static synchronized int getDataHandle() {
|
||||
return nextDataHandle++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates arbitrary data with an object in the registry.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param handle data handle returned by getDataHandle()
|
||||
* @param data data to set
|
||||
* @return Previous data (may be null). If non-null, caller is responsible for calling close().
|
||||
*/
|
||||
@SuppressWarnings("PMD.CompareObjectsWithEquals")
|
||||
public static synchronized AutoCloseable setData(
|
||||
Sendable sendable, int handle, AutoCloseable data) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp == null) {
|
||||
return null;
|
||||
}
|
||||
AutoCloseable rv = null;
|
||||
if (comp.m_data == null) {
|
||||
comp.m_data = new AutoCloseable[handle + 1];
|
||||
} else if (handle < comp.m_data.length) {
|
||||
rv = comp.m_data[handle];
|
||||
} else {
|
||||
comp.m_data = Arrays.copyOf(comp.m_data, handle + 1);
|
||||
}
|
||||
if (comp.m_data[handle] != data) {
|
||||
if (comp.m_data[handle] != null) {
|
||||
try {
|
||||
comp.m_data[handle].close();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
comp.m_data[handle] = data;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets arbitrary data associated with an object in the registry.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param handle data handle returned by getDataHandle()
|
||||
* @return data (may be null if none associated)
|
||||
*/
|
||||
public static synchronized Object getData(Sendable sendable, int handle) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp == null || comp.m_data == null || handle >= comp.m_data.length) {
|
||||
return null;
|
||||
}
|
||||
return comp.m_data[handle];
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes an object in the registry to a builder.
|
||||
*
|
||||
* @param sendable object
|
||||
* @param builder sendable builder
|
||||
*/
|
||||
public static synchronized void publish(Sendable sendable, SendableBuilder builder) {
|
||||
Component comp = getOrAdd(sendable);
|
||||
if (comp.m_builder != null) {
|
||||
try {
|
||||
comp.m_builder.close();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
comp.m_builder = builder; // clear any current builder
|
||||
sendable.initSendable(comp.m_builder);
|
||||
comp.m_builder.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates network table information from an object.
|
||||
*
|
||||
* @param sendable object
|
||||
*/
|
||||
public static synchronized void update(Sendable sendable) {
|
||||
Component comp = components.get(sendable);
|
||||
if (comp != null && comp.m_builder != null) {
|
||||
comp.m_builder.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user