blob: cfc925615424b25fe2d06b1fbfd4740c32ba7ca5 [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
10#include <memory>
11
12#include <hal/Types.h>
13
14namespace 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 */
44 explicit DutyCycle(DigitalSource& source);
45 /**
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 */
52 explicit DutyCycle(DigitalSource* source);
53 /**
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
67 DutyCycle(DutyCycle&&) = default;
68 DutyCycle& operator=(DutyCycle&&) = default;
69
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 /**
87 * Get the raw output ratio of the duty cycle signal.
88 *
89 * <p> 0 means always low, an output equal to
90 * GetOutputScaleFactor() means always high.
91 *
92 * @return output ratio in raw units
93 */
94 unsigned int GetOutputRaw() const;
95
96 /**
97 * Get the scale factor of the output.
98 *
99 * <p> An output equal to this value is always high, and then linearly scales
100 * down to 0. Divide the result of getOutputRaw by this in order to get the
101 * percentage between 0 and 1.
102 *
103 * @return the output scale factor
104 */
105 unsigned int GetOutputScaleFactor() const;
106
107 /**
108 * Get the FPGA index for the DutyCycle.
109 *
110 * @return the FPGA index
111 */
112 int GetFPGAIndex() const;
113
114 /**
115 * Get the channel of the source.
116 *
117 * @return the source channel
118 */
119 int GetSourceChannel() const;
120
121 private:
122 void InitDutyCycle();
123 std::shared_ptr<DigitalSource> m_source;
124 hal::Handle<HAL_DutyCycleHandle> m_handle;
125};
126} // namespace frc