Class TunableRegistry

java.lang.Object
org.wpilib.tunable.TunableRegistry

public final class TunableRegistry extends Object
Global registry for tunable handlers and tunable backends.
  • Method Details

    • setReportWarning

      public static void setReportWarning(Consumer<String> func)
      Set function used for reporting warning messages (e.g. type mismatches).

      The reporting function must not throw.

      Parameters:
      func - reporting function; pass null to use default
    • getReportWarning

      public static Consumer<String> getReportWarning()
      Get function used for reporting warning messages.
      Returns:
      reporting function
    • reportWarning

      public static void reportWarning(String msg)
      Report a warning message (e.g. type mismatch).
      Parameters:
      msg - warning message
    • registerTypeHandler

      public static <T> void registerTypeHandler(Class<T> cls, TunableRegistry.TypeHandler<T> handler)
      Registers a handler for tuning objects of a particular type. The handler should populate the provided TunableTable name as appropriate for the object's data.
      Type Parameters:
      T - class
      Parameters:
      cls - class object
      handler - handler (accepts TunableTable, entry name, and object)
    • registerBackend

      public static void registerBackend(String prefix, TunableBackend backend)
      Registers a backend for creating tunables. When calling getBackend(), the longest prefix match is used.
      Parameters:
      prefix - prefix for tunables covered by this backend
      backend - backend
    • getTypeHandler

      public static <T> TunableRegistry.TypeHandler<T> getTypeHandler(T value)
      Gets the handler for logging an object. Should generally only be used by Tunable.
      Type Parameters:
      T - class
      Parameters:
      value - value
      Returns:
      handler or null if no match
    • getBackend

      public static TunableBackend getBackend(String path)
      Gets the backend for creating a tunable. Should generally only be used internally or by custom backends.
      Parameters:
      path - full name
      Returns:
      tunable backend, or a no-op backend if no match
    • resetChangedAfterUpdate

      public static void resetChangedAfterUpdate(TunableBase tunable)
      Resets a tunable's changed flag after the current update cycle finishes.

      Backends should call this after publishing a changed tunable so every alias in every backend can observe the shared changed flag before it is cleared.

      Parameters:
      tunable - tunable to reset
    • runAfterUpdate

      public static void runAfterUpdate(Runnable callback)
      Runs a callback after the current update cycle finishes resetting changed flags.

      Backends should use this for callbacks that react to tuned values so callback mutations are observed as new changes in the next update cycle.

      Parameters:
      callback - callback to run; must not throw
    • updateComplexIfNeeded

      public static void updateComplexIfNeeded(ComplexTunable tunable)
      Updates a complex tunable if it has not already been updated during the current registry update.

      This should generally only be used by TunableBackend implementations.

      Parameters:
      tunable - tunable to update
    • getTable

      public static TunableTable getTable(String path)
      Get a tunable table for a particular name.
      Parameters:
      path - full name
      Returns:
      tunable table
    • publishComplexChildren

      public static void publishComplexChildren(String path, ComplexTunable tunable)
      Publishes a complex tunable's children.

      This should generally only be used by TunableBackend implementations after they publish the complex parent entry.

      Parameters:
      path - normalized path
      tunable - the tunable
    • publish

      public static boolean publish(String path, TunableBase tunable)
      Publishes a tunable.
      Parameters:
      path - normalized path
      tunable - the tunable
      Returns:
      true if the backend accepted the tunable
    • publish

      public static boolean publish(String path, ComplexTunable tunable)
      Publishes a complex tunable.
      Parameters:
      path - normalized path
      tunable - the tunable
      Returns:
      true if the backend accepted the tunable
    • remove

      public static void remove(String path)
      Removes a tunable.
      Parameters:
      path - normalized path
    • remove

      public static void remove(ComplexTunable tunable)
      Removes every tracked publication for a complex tunable.
      Parameters:
      tunable - complex tunable to remove
    • update

      public static void update()
      Updates all tunable values from backends.
    • withUpdateMutex

      public static void withUpdateMutex(Runnable func)
      Execute a function while holding the same mutex used for update(). As tunables are not thread-safe, this is designed to allow external synchronization for tunables used in secondary threads outside of the robot main loop.

      Every competing access, including main-loop access, must use this method or another safe handoff. The update mutex is held while backends and user callbacks run, so the function should be short and must not allow a mutable tunable value to escape and be accessed after the function completes.

      Parameters:
      func - function to execute
    • reset

      public static void reset()
      Clear all registered types and backends and closes all entries. Should typically only be used by unit test code.