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