Class Notifier

java.lang.Object
edu.wpi.first.wpilibj.Notifier
All Implemented Interfaces:
AutoCloseable

public class Notifier
extends Object
implements AutoCloseable
Notifiers run a user-provided callback function on a separate thread.

If startSingle() is used, the callback will run once. If startPeriodic() is used, the callback will run repeatedly with the given period until stop() is called.

  • Constructor Summary

    Constructors 
    Constructor Description
    Notifier​(Runnable callback)
    Create a Notifier with the given callback.
  • Method Summary

    Modifier and Type Method Description
    void close()  
    void setCallback​(Runnable callback)
    Change the callback function.
    static boolean setHALThreadPriority​(boolean realTime, int priority)
    Sets the HAL notifier thread priority.
    void setHandler​(Runnable callback)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use setCallback() instead.
    void setName​(String name)
    Sets the name of the notifier.
    void startPeriodic​(double periodSeconds)
    Run the callback periodically with the given period.
    void startSingle​(double delaySeconds)
    Run the callback once after the given delay.
    void stop()
    Stop further callback invocations.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Notifier

      public Notifier​(Runnable callback)
      Create a Notifier with the given callback.

      Configure when the callback runs with startSingle() or startPeriodic().

      Parameters:
      callback - The callback to run.
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • setName

      public void setName​(String name)
      Sets the name of the notifier. Used for debugging purposes only.
      Parameters:
      name - Name
    • setHandler

      @Deprecated(forRemoval=true, since="2024") public void setHandler​(Runnable callback)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use setCallback() instead.
      Change the callback function.
      Parameters:
      callback - The callback function.
    • setCallback

      public void setCallback​(Runnable callback)
      Change the callback function.
      Parameters:
      callback - The callback function.
    • startSingle

      public void startSingle​(double delaySeconds)
      Run the callback once after the given delay.
      Parameters:
      delaySeconds - Time in seconds to wait before the callback is called.
    • startPeriodic

      public void startPeriodic​(double periodSeconds)
      Run the callback periodically with the given period.

      The user-provided callback should be written so that it completes before the next time it's scheduled to run.

      Parameters:
      periodSeconds - Period in seconds after which to to call the callback starting one period after the call to this method.
    • stop

      public void stop()
      Stop further callback invocations.

      No further periodic callbacks will occur. Single invocations will also be cancelled if they haven't yet occurred.

      If a callback invocation is in progress, this function will block until the callback is complete.

    • setHALThreadPriority

      public static boolean setHALThreadPriority​(boolean realTime, int priority)
      Sets the HAL notifier thread priority.

      The HAL notifier thread is responsible for managing the FPGA's notifier interrupt and waking up user's Notifiers when it's their time to run. Giving the HAL notifier thread real-time priority helps ensure the user's real-time Notifiers, if any, are notified to run in a timely manner.

      Parameters:
      realTime - Set to true to set a real-time priority, false for standard priority.
      priority - Priority to set the thread to. For real-time, this is 1-99 with 99 being highest. For non-real-time, this is forced to 0. See "man 7 sched" for more details.
      Returns:
      True on success.