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