Skip to content

Instantly share code, notes, and snippets.

@PeterJohnson
Created January 27, 2024 18:33
Show Gist options
  • Save PeterJohnson/dcc5c9ab64c1efcd5ebd9f8fef03d5f5 to your computer and use it in GitHub Desktop.
Save PeterJohnson/dcc5c9ab64c1efcd5ebd9f8fef03d5f5 to your computer and use it in GitHub Desktop.
Sendable v2
// 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;
/**
* Interface for sendable objects.
*
* <p>Idiomatically, classes that support sendable operation should provide a static final
* member named "sendable" that provides an instance of an implementation of this interface.
*
* @param <T> object type
*/
public interface SendableImpl<T> {
String getTypeString();
void add(T obj, SendableStore store);
void remove(T obj, SendableStore store);
}
// 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.EnumSet;
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 SendableStore extends AutoCloseable {
/** Event triggers. */
enum Event {
kDisabledInit,
kTeleopInit,
kAutonomousInit,
kTestInit,
kDsConnected
}
/** Options for subscribers. */
static class SubscribeOption {
// TODO
// typeString: set a custom type string
// periodic: request publishers provide updates at the given period
// sendAll: request publisher send all changes
// keepDuplicates: request publisher send values even if duplicate
// disableRemote: excludes updates coming from any other machine
// disableLocal: excludes updates coming from this machine
// excludePublisher: excludes updates due to publishX by the same sendable
}
/** Options for publishers. */
static class PublishOption {
// TODO
// typeString: set a custom type string
// property: set a property (may be specified multiple times)
// setDefault: publish the given default value immediately
// initial: call callback and publish a new value immediately
// periodic: call callback and publish new values at the given period (rather than the default)
// onlyInitial: do not periodically call the callback, just publish the initial value
}
/**
* Adds a child SendableStore.
*
* @param name child name
* @param obj child object
* @param sendableImpl sendable implementation for child
*/
<T> SendableStore addChild(String name, T obj, SendableImpl<T> sendableImpl);
/**
* Removes a child SendableStore.
*
* @param name child name
* @param obj child object
* @param sendableImpl sendable implementation for child
*/
<T> void removeChild(String name, T obj, SendableImpl<T> sendableImpl);
/**
* Adds a function that should be called for event handling of particular events.
*
* @param events events
* @param func function
*/
void addEventHandler(EnumSet<Event> events, Consumer<Event> func);
/**
* Subscribes to changes to a topic. The onChange callback will be called (on the robot main thread)
* for each new value.
*
* @param name topic name
* @param onChange callback for new values
* @param options subscriber options
*/
void subscribeBoolean(String name, BooleanConsumer onChange, SubscribeOption... options);
/**
* Sets up a callback to publish changes to a topic. By default, a call will be made each robot
* periodic loop to the provided getValue callback. The frequency of callbacks can be changed with
* publisher options. Alternatively, the getBooleanPublisher() function can be used to instead get
* "push" functionality.
*
* @param name topic name
* @param getValue callback called to get new values
* @param options publisher options
*/
void publishBoolean(String name, BooleanSupplier getValue, PublishOption... options);
/**
* Gets a publisher. Updates to the topic will be published when the returned function is called.
* For built-in "pull" functionality, use publishBoolean() instead.
*
* @param name topic name
* @param removed callback called when publisher is removed
* @param options publisher options
* @return publish function
*/
BooleanConsumer getBooleanPublisher(String name, Consumer<BooleanConsumer> removed, PublishOption... options);
// A bunch more variants for all the different types...
/**
* 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 publishers. */
void update();
/** Clear all publishers and subscribers. */
void clear();
/**
* Adds a closeable. The closeable.close() will be called when close() is called.
*
* @param closeable closeable object
*/
void addCloseable(AutoCloseable closeable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment