WPILibC++ 2023.4.3
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]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
55 NotifyCallback callback, bool initialNotify);
56
57 /**
58 * Check whether this duty cycle input has been initialized.
59 *
60 * @return true if initialized
61 */
62 bool GetInitialized() const;
63
64 /**
65 * Define whether this duty cycle input has been initialized.
66 *
67 * @param initialized whether this object is initialized
68 */
69 void SetInitialized(bool initialized);
70
71 /**
72 * Register a callback to be run whenever the frequency changes.
73 *
74 * @param callback the callback
75 * @param initialNotify whether to call the callback with the initial state
76 * @return the CallbackStore object associated with this callback
77 */
78 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterFrequencyCallback(
79 NotifyCallback callback, bool initialNotify);
80
81 /**
82 * Measure the frequency.
83 *
84 * @return the duty cycle frequency
85 */
86 int GetFrequency() const;
87
88 /**
89 * Change the duty cycle frequency.
90 *
91 * @param frequency the new frequency
92 */
93 void SetFrequency(int frequency);
94
95 /**
96 * Register a callback to be run whenever the output changes.
97 *
98 * @param callback the callback
99 * @param initialNotify whether to call the callback with the initial state
100 * @return the CallbackStore object associated with this callback
101 */
102 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterOutputCallback(
103 NotifyCallback callback, bool initialNotify);
104
105 /**
106 * Measure the output from this duty cycle port.
107 *
108 * @return the output value
109 */
110 double GetOutput() const;
111
112 /**
113 * Change the duty cycle output.
114 *
115 * @param output the new output value
116 */
117 void SetOutput(double output);
118
119 /**
120 * Reset all simulation data for the duty cycle output.
121 */
122 void ResetData();
123
124 private:
125 explicit DutyCycleSim(int index) : m_index{index} {}
126
127 int m_index;
128};
129} // namespace sim
130} // 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: AprilTagFieldLayout.h:22