WPILibC++ 2024.3.2
DIOSim.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 DigitalInput;
14class DigitalOutput;
15
16namespace sim {
17
18/**
19 * Class to control a simulated digital input or output.
20 */
21class DIOSim {
22 public:
23 /**
24 * Constructs from a DigitalInput object.
25 *
26 * @param input DigitalInput to simulate
27 */
28 explicit DIOSim(const DigitalInput& input);
29
30 /**
31 * Constructs from a DigitalOutput object.
32 *
33 * @param output DigitalOutput to simulate
34 */
35 explicit DIOSim(const DigitalOutput& output);
36
37 /**
38 * Constructs from an digital I/O channel number.
39 *
40 * @param channel Channel number
41 */
42 explicit DIOSim(int channel);
43
44 /**
45 * Register a callback to be run when this DIO is initialized.
46 *
47 * @param callback the callback
48 * @param initialNotify whether to run the callback with the initial state
49 * @return the CallbackStore object associated with this callback
50 */
51 [[nodiscard]]
52 std::unique_ptr<CallbackStore> RegisterInitializedCallback(
53 NotifyCallback callback, bool initialNotify);
54
55 /**
56 * Check whether this DIO has been initialized.
57 *
58 * @return true if initialized
59 */
60 bool GetInitialized() const;
61
62 /**
63 * Define whether this DIO has been initialized.
64 *
65 * @param initialized whether this object is initialized
66 */
67 void SetInitialized(bool initialized);
68
69 /**
70 * Register a callback to be run whenever the DIO value changes.
71 *
72 * @param callback the callback
73 * @param initialNotify whether the callback should be called with the
74 * initial value
75 * @return the CallbackStore object associated with this callback
76 */
77 [[nodiscard]]
78 std::unique_ptr<CallbackStore> RegisterValueCallback(NotifyCallback callback,
79 bool initialNotify);
80
81 /**
82 * Read the value of the DIO port.
83 *
84 * @return the DIO value
85 */
86 bool GetValue() const;
87
88 /**
89 * Change the DIO value.
90 *
91 * @param value the new value
92 */
93 void SetValue(bool value);
94
95 /**
96 * Register a callback to be run whenever the pulse length 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]]
103 std::unique_ptr<CallbackStore> RegisterPulseLengthCallback(
104 NotifyCallback callback, bool initialNotify);
105
106 /**
107 * Read the pulse length.
108 *
109 * @return the pulse length of this DIO port
110 */
111 double GetPulseLength() const;
112
113 /**
114 * Change the pulse length of this DIO port.
115 *
116 * @param pulseLength the new pulse length
117 */
118 void SetPulseLength(double pulseLength);
119
120 /**
121 * Register a callback to be run whenever this DIO changes to be an input.
122 *
123 * @param callback the callback
124 * @param initialNotify whether the callback should be called with the
125 * initial state
126 * @return the CallbackStore object associated with this callback
127 */
128 [[nodiscard]]
129 std::unique_ptr<CallbackStore> RegisterIsInputCallback(
130 NotifyCallback callback, bool initialNotify);
131
132 /**
133 * Check whether this DIO port is currently an Input.
134 *
135 * @return true if Input
136 */
137 bool GetIsInput() const;
138
139 /**
140 * Define whether this DIO port is an Input.
141 *
142 * @param isInput whether this DIO should be an Input
143 */
144 void SetIsInput(bool isInput);
145
146 /**
147 * Register a callback to be run whenever the filter index changes.
148 *
149 * @param callback the callback
150 * @param initialNotify whether the callback should be called with the
151 * initial value
152 * @return the CallbackStore object associated with this callback
153 */
154 [[nodiscard]]
155 std::unique_ptr<CallbackStore> RegisterFilterIndexCallback(
156 NotifyCallback callback, bool initialNotify);
157
158 /**
159 * Read the filter index.
160 *
161 * @return the filter index of this DIO port
162 */
163 int GetFilterIndex() const;
164
165 /**
166 * Change the filter index of this DIO port.
167 *
168 * @param filterIndex the new filter index
169 */
170 void SetFilterIndex(int filterIndex);
171
172 /**
173 * Reset all simulation data of this object.
174 */
175 void ResetData();
176
177 private:
178 int m_index;
179};
180} // namespace sim
181} // namespace frc
Class to read a digital input.
Definition: DigitalInput.h:27
Class to write to digital outputs.
Definition: DigitalOutput.h:25
Class to control a simulated digital input or output.
Definition: DIOSim.h:21
DIOSim(const DigitalOutput &output)
Constructs from a DigitalOutput object.
void SetInitialized(bool initialized)
Define whether this DIO has been initialized.
void SetPulseLength(double pulseLength)
Change the pulse length of this DIO port.
double GetPulseLength() const
Read the pulse length.
std::unique_ptr< CallbackStore > RegisterValueCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the DIO value changes.
DIOSim(int channel)
Constructs from an digital I/O channel number.
std::unique_ptr< CallbackStore > RegisterInitializedCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run when this DIO is initialized.
void ResetData()
Reset all simulation data of this object.
std::unique_ptr< CallbackStore > RegisterPulseLengthCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the pulse length changes.
bool GetValue() const
Read the value of the DIO port.
void SetValue(bool value)
Change the DIO value.
bool GetIsInput() const
Check whether this DIO port is currently an Input.
void SetIsInput(bool isInput)
Define whether this DIO port is an Input.
void SetFilterIndex(int filterIndex)
Change the filter index of this DIO port.
std::unique_ptr< CallbackStore > RegisterIsInputCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever this DIO changes to be an input.
DIOSim(const DigitalInput &input)
Constructs from a DigitalInput object.
bool GetInitialized() const
Check whether this DIO has been initialized.
int GetFilterIndex() const
Read the filter index.
std::unique_ptr< CallbackStore > RegisterFilterIndexCallback(NotifyCallback callback, bool initialNotify)
Register a callback to be run whenever the filter index changes.
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagPoseEstimator.h:15