WPILibC++ 2023.4.3
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]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
52 NotifyCallback callback, bool initialNotify);
53
54 /**
55 * Check whether this DIO has been initialized.
56 *
57 * @return true if initialized
58 */
59 bool GetInitialized() const;
60
61 /**
62 * Define whether this DIO has been initialized.
63 *
64 * @param initialized whether this object is initialized
65 */
66 void SetInitialized(bool initialized);
67
68 /**
69 * Register a callback to be run whenever the DIO value changes.
70 *
71 * @param callback the callback
72 * @param initialNotify whether the callback should be called with the
73 * initial value
74 * @return the CallbackStore object associated with this callback
75 */
76 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterValueCallback(
77 NotifyCallback callback, bool initialNotify);
78
79 /**
80 * Read the value of the DIO port.
81 *
82 * @return the DIO value
83 */
84 bool GetValue() const;
85
86 /**
87 * Change the DIO value.
88 *
89 * @param value the new value
90 */
91 void SetValue(bool value);
92
93 /**
94 * Register a callback to be run whenever the pulse length changes.
95 *
96 * @param callback the callback
97 * @param initialNotify whether to call the callback with the initial state
98 * @return the CallbackStore object associated with this callback
99 */
100 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterPulseLengthCallback(
101 NotifyCallback callback, bool initialNotify);
102
103 /**
104 * Read the pulse length.
105 *
106 * @return the pulse length of this DIO port
107 */
108 double GetPulseLength() const;
109
110 /**
111 * Change the pulse length of this DIO port.
112 *
113 * @param pulseLength the new pulse length
114 */
115 void SetPulseLength(double pulseLength);
116
117 /**
118 * Register a callback to be run whenever this DIO changes to be an input.
119 *
120 * @param callback the callback
121 * @param initialNotify whether the callback should be called with the
122 * initial state
123 * @return the CallbackStore object associated with this callback
124 */
125 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterIsInputCallback(
126 NotifyCallback callback, bool initialNotify);
127
128 /**
129 * Check whether this DIO port is currently an Input.
130 *
131 * @return true if Input
132 */
133 bool GetIsInput() const;
134
135 /**
136 * Define whether this DIO port is an Input.
137 *
138 * @param isInput whether this DIO should be an Input
139 */
140 void SetIsInput(bool isInput);
141
142 /**
143 * Register a callback to be run whenever the filter index changes.
144 *
145 * @param callback the callback
146 * @param initialNotify whether the callback should be called with the
147 * initial value
148 * @return the CallbackStore object associated with this callback
149 */
150 [[nodiscard]] std::unique_ptr<CallbackStore> RegisterFilterIndexCallback(
151 NotifyCallback callback, bool initialNotify);
152
153 /**
154 * Read the filter index.
155 *
156 * @return the filter index of this DIO port
157 */
158 int GetFilterIndex() const;
159
160 /**
161 * Change the filter index of this DIO port.
162 *
163 * @param filterIndex the new filter index
164 */
165 void SetFilterIndex(int filterIndex);
166
167 /**
168 * Reset all simulation data of this object.
169 */
170 void ResetData();
171
172 private:
173 int m_index;
174};
175} // namespace sim
176} // 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.
Definition: core.h:1240
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition: CallbackStore.h:14
Definition: AprilTagFieldLayout.h:22