blob: 3f99d94f7d70b873e9383eac7bab2f40eb946c9c [file] [log] [blame]
James Kuszmaul41fa78a2019-12-14 20:53:14 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2019 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
James Kuszmaul41fa78a2019-12-14 20:53:14 -080010#include <hal/Types.h>
11
Philipp Schrader790cb542023-07-05 21:06:52 -070012#include <memory>
13
James Kuszmaul41fa78a2019-12-14 20:53:14 -080014namespace frc {
15class DigitalSource;
16class AnalogTrigger;
17class DMA;
18class DMASample;
19
20/**
21 * Class to read a duty cycle PWM input.
22 *
23 * <p>PWM input signals are specified with a frequency and a ratio of high to
24 * low in that frequency. There are 8 of these in the roboRIO, and they can be
25 * attached to any DigitalSource.
26 *
27 * <p>These can be combined as the input of an AnalogTrigger to a Counter in
28 * order to implement rollover checking.
29 *
30 */
31class DutyCycle {
32 friend class AnalogTrigger;
33 friend class DMA;
34 friend class DMASample;
35
36 public:
37 /**
38 * Constructs a DutyCycle input from a DigitalSource input.
39 *
40 * <p> This class does not own the inputted source.
41 *
42 * @param source The DigitalSource to use.
43 */
Philipp Schrader790cb542023-07-05 21:06:52 -070044 explicit DutyCycle(DigitalSource &source);
James Kuszmaul41fa78a2019-12-14 20:53:14 -080045 /**
46 * Constructs a DutyCycle input from a DigitalSource input.
47 *
48 * <p> This class does not own the inputted source.
49 *
50 * @param source The DigitalSource to use.
51 */
Philipp Schrader790cb542023-07-05 21:06:52 -070052 explicit DutyCycle(DigitalSource *source);
James Kuszmaul41fa78a2019-12-14 20:53:14 -080053 /**
54 * Constructs a DutyCycle input from a DigitalSource input.
55 *
56 * <p> This class does not own the inputted source.
57 *
58 * @param source The DigitalSource to use.
59 */
60 explicit DutyCycle(std::shared_ptr<DigitalSource> source);
61
62 /**
63 * Close the DutyCycle and free all resources.
64 */
65 virtual ~DutyCycle();
66
Philipp Schrader790cb542023-07-05 21:06:52 -070067 DutyCycle(DutyCycle &&) = default;
68 DutyCycle &operator=(DutyCycle &&) = default;
James Kuszmaul41fa78a2019-12-14 20:53:14 -080069
70 /**
71 * Get the frequency of the duty cycle signal.
72 *
73 * @return frequency in Hertz
74 */
75 int GetFrequency() const;
76
77 /**
78 * Get the output ratio of the duty cycle signal.
79 *
80 * <p> 0 means always low, 1 means always high.
81 *
82 * @return output ratio between 0 and 1
83 */
84 double GetOutput() const;
85
86 /**
James Kuszmaul41fa78a2019-12-14 20:53:14 -080087 * Get the scale factor of the output.
88 *
89 * <p> An output equal to this value is always high, and then linearly scales
90 * down to 0. Divide the result of getOutputRaw by this in order to get the
91 * percentage between 0 and 1.
92 *
93 * @return the output scale factor
94 */
95 unsigned int GetOutputScaleFactor() const;
96
97 /**
98 * Get the FPGA index for the DutyCycle.
99 *
100 * @return the FPGA index
101 */
102 int GetFPGAIndex() const;
103
104 /**
105 * Get the channel of the source.
106 *
107 * @return the source channel
108 */
109 int GetSourceChannel() const;
110
111 private:
112 void InitDutyCycle();
113 std::shared_ptr<DigitalSource> m_source;
114 hal::Handle<HAL_DutyCycleHandle> m_handle;
115};
116} // namespace frc