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#withInterrupt(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 */ 018public class NotifierCommand extends CommandBase { 019 protected final Notifier m_notifier; 020 protected final double m_period; 021 022 /** 023 * Creates a new NotifierCommand. 024 * 025 * @param toRun the runnable for the notifier to run 026 * @param period the period at which the notifier should run, in seconds 027 * @param requirements the subsystems required by this command 028 */ 029 public NotifierCommand(Runnable toRun, double period, Subsystem... requirements) { 030 m_notifier = new Notifier(toRun); 031 m_period = period; 032 addRequirements(requirements); 033 } 034 035 @Override 036 public void initialize() { 037 m_notifier.startPeriodic(m_period); 038 } 039 040 @Override 041 public void end(boolean interrupted) { 042 m_notifier.stop(); 043 } 044}