WPILibC++ 2023.4.3
NotifierCommand.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <initializer_list>
9#include <span>
10
11#include <frc/Notifier.h>
12#include <units/time.h>
13
16
17namespace frc2 {
18/**
19 * A command that starts a notifier to run the given runnable periodically in a
20 * separate thread. Has no end condition as-is; either subclass it or use
21 * Command::WithTimeout(double) or Command::Until(BooleanSupplier) to
22 * give it one.
23 *
24 * <p>WARNING: Do not use this class unless you are confident in your ability to
25 * make the executed code thread-safe. If you do not know what "thread-safe"
26 * means, that is a good sign that you should not use this class.
27 *
28 * This class is provided by the NewCommands VendorDep
29 */
30class NotifierCommand : public CommandHelper<CommandBase, NotifierCommand> {
31 public:
32 /**
33 * Creates a new NotifierCommand.
34 *
35 * @param toRun the runnable for the notifier to run
36 * @param period the period at which the notifier should run
37 * @param requirements the subsystems required by this command
38 */
39 NotifierCommand(std::function<void()> toRun, units::second_t period,
40 std::initializer_list<Subsystem*> requirements);
41
42 /**
43 * Creates a new NotifierCommand.
44 *
45 * @param toRun the runnable for the notifier to run
46 * @param period the period at which the notifier should run
47 * @param requirements the subsystems required by this command
48 */
49 NotifierCommand(std::function<void()> toRun, units::second_t period,
50 std::span<Subsystem* const> requirements = {});
51
53
55
56 void Initialize() override;
57
58 void End(bool interrupted) override;
59
60 private:
61 std::function<void()> m_toRun;
62 frc::Notifier m_notifier;
63 units::second_t m_period;
64};
65} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A command that starts a notifier to run the given runnable periodically in a separate thread.
Definition: NotifierCommand.h:30
NotifierCommand(NotifierCommand &&other)
NotifierCommand(const NotifierCommand &other)
void Initialize() override
The initial subroutine of a command.
NotifierCommand(std::function< void()> toRun, units::second_t period, std::span< Subsystem *const > requirements={})
Creates a new NotifierCommand.
NotifierCommand(std::function< void()> toRun, units::second_t period, std::initializer_list< Subsystem * > requirements)
Creates a new NotifierCommand.
void End(bool interrupted) override
The action to take when the command ends.
Notifiers run a callback function on a separate thread at a specified period.
Definition: Notifier.h:29
Definition: InstantCommand.h:14