001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj2.command;
006
007import edu.wpi.first.wpilibj.Notifier;
008
009/**
010 * A command that starts a notifier to run the given runnable periodically in a separate thread. Has
011 * no end condition as-is; either subclass it or use {@link Command#withTimeout(double)} or {@link
012 * Command#until(java.util.function.BooleanSupplier)} to give it one.
013 *
014 * <p>WARNING: Do not use this class unless you are confident in your ability to make the executed
015 * code thread-safe. If you do not know what "thread-safe" means, that is a good sign that you
016 * should not use this class.
017 *
018 * <p>This class is provided by the NewCommands VendorDep
019 */
020public class NotifierCommand extends Command {
021  private final Notifier m_notifier;
022  private final double m_period;
023
024  /**
025   * Creates a new NotifierCommand.
026   *
027   * @param toRun the runnable for the notifier to run
028   * @param period the period at which the notifier should run, in seconds
029   * @param requirements the subsystems required by this command
030   */
031  @SuppressWarnings("this-escape")
032  public NotifierCommand(Runnable toRun, double period, Subsystem... requirements) {
033    m_notifier = new Notifier(toRun);
034    m_period = period;
035    addRequirements(requirements);
036  }
037
038  @Override
039  public void initialize() {
040    m_notifier.startPeriodic(m_period);
041  }
042
043  @Override
044  public void end(boolean interrupted) {
045    m_notifier.stop();
046  }
047}