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