WPILibC++ 2023.4.3-108-ge5452e3
DutyCycleSim.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 <memory>
8
10
11namespace frc {
12
13class DutyCycle;
14
15namespace sim {
16
17/**
18 * Class to control a simulated duty cycle digital input.
19 */
21 public:
22 /**
23 * Constructs from a DutyCycle object.
24 *
25 * @param dutyCycle DutyCycle to simulate
26 */
27 explicit DutyCycleSim(const DutyCycle& dutyCycle);
28
29 /**
30 * Creates a DutyCycleSim for a digital input channel.
31 *
32 * @param channel digital input channel
33 * @return Simulated object
34 * @throws std::out_of_range if no DutyCycle is configured for that channel
35 */
36 static DutyCycleSim CreateForChannel(int channel);
37
38 /**
39 * Creates a DutyCycleSim for a simulated index.
40 * The index is incremented for each simulated DutyCycle.
41 *
42 * @param index simulator index
43 * @return Simulated object
44 */
45 static DutyCycleSim CreateForIndex(int index);
46
47 /**
48 * Register a callback to be run when this duty cycle input is initialized.
49 *
50 * @param callback the callback
51 * @param initialNotify whether to run the callback with the initial state
52 * @return the CallbackStore object associated with this callback
53 */
54 [[nodiscard]]
55 std::unique_ptr<CallbackStore> RegisterInitializedCallback(
56 NotifyCallback callback, bool initialNotify);
57
58 /**
59 * Check whether this duty cycle input has been initialized.
60 *
61 * @return true if initialized
62 */
63 bool GetInitialized() const;
64
65 /**
66 * Define whether this duty cycle input has been initialized.
67 *
68 * @param initialized whether this object is initialized
69 */
70 void SetInitialized(bool initialized);
71
72 /**
73 * Register a callback to be run whenever the frequency changes.
74 *
75 * @param callback the callback
76 * @param initialNotify whether to call the callback with the initial state
77 * @return the CallbackStore object associated with this callback
78 */
79 [[nodiscard]]
80 std::unique_ptr<CallbackStore> RegisterFrequencyCallback(
81 NotifyCallback callback, bool initialNotify);
82
83 /**
84 * Measure the frequency.
85 *
86 * @return the duty cycle frequency
87 */
88 int GetFrequency() const;
89
90 /**
91 * Change the duty cycle frequency.
92 *
93 * @param frequency the new frequency
94 */
95 void SetFrequency(int frequency);
96
97 /**
98 * Register a callback to be run whenever the output changes.
99 *
100 * @param callback the callback
101 * @param initialNotify whether to call the callback with the initial state
102 * @return the CallbackStore object associated with this callback
103 */
104 [[nodiscard]]
105 std::unique_ptr<CallbackStore> RegisterOutputCallback(NotifyCallback callback,
106 bool initialNotify);
107
108 /**
109 * Measure the output from this duty cycle port.
110 *
111 * @return the output value
112 */
113 double GetOutput() const;
114
115 /**
116 * Change the duty cycle output.
117 *
118 * @param output the new output value
119 */
120 void SetOutput(double output);
121
122 /**
123 * Reset all simulation data for the duty cycle output.
124 */
125 void ResetData();
126
127 private:
128 explicit DutyCycleSim(int index) : m_index{index} {}
129
130 int m_index;
131};
132} // namespace sim
133} // namespace frc
Class to read a duty cycle PWM input.
Definition: DutyCycle.h:31
Class to control a simulated duty cycle digital input.
Definition: DutyCycleSim.h:20
int GetFrequency() const
Measure the frequency.
void SetInitialized(bool initialized)
Define whether this duty cycle input has been initialized.
void SetFrequency(int frequency)
Change the duty cycle frequency.
std::unique_ptr< CallbackStore > RegisterFrequencyCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the frequency changes.
double GetOutput() const
Measure the output from this duty cycle port.
std::unique_ptr< CallbackStore > RegisterOutputCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the output changes.
void ResetData()
Reset all simulation data for the duty cycle output.
DutyCycleSim(const DutyCycle &dutyCycle)
Constructs from a DutyCycle object.
void SetOutput(double output)
Change the duty cycle output.
static DutyCycleSim CreateForIndex(int index)
Creates a DutyCycleSim for a simulated index.
std::unique_ptr< CallbackStore > RegisterInitializedCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when this duty cycle input is initialized.
static DutyCycleSim CreateForChannel(int channel)
Creates a DutyCycleSim for a digital input channel.
bool GetInitialized() const
Check whether this duty cycle input has been initialized.
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagPoseEstimator.h:15