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
137
138
// 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 <stdint.h>
#include <array>
#include <wpi/mutex.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "frc/DigitalSource.h"
namespace frc {
class Encoder;
class Counter;
/**
* Class to enable glitch filtering on a set of digital inputs.
*
* This class will manage adding and removing digital inputs from a FPGA glitch
* filter. The filter lets the user configure the time that an input must remain
* high or low before it is classified as high or low.
*/
class DigitalGlitchFilter : public wpi::Sendable,
public wpi::SendableHelper<DigitalGlitchFilter> {
public:
DigitalGlitchFilter();
~DigitalGlitchFilter() override;
DigitalGlitchFilter(DigitalGlitchFilter&&) = default;
DigitalGlitchFilter& operator=(DigitalGlitchFilter&&) = default;
/**
* Assigns the DigitalSource to this glitch filter.
*
* @param input The DigitalSource to add.
*/
void Add(DigitalSource* input);
/**
* Assigns the Encoder to this glitch filter.
*
* @param input The Encoder to add.
*/
void Add(Encoder* input);
/**
* Assigns the Counter to this glitch filter.
*
* @param input The Counter to add.
*/
void Add(Counter* input);
/**
* Removes a digital input from this filter.
*
* Removes the DigitalSource from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The DigitalSource to remove.
*/
void Remove(DigitalSource* input);
/**
* Removes an encoder from this filter.
*
* Removes the Encoder from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The Encoder to remove.
*/
void Remove(Encoder* input);
/**
* Removes a counter from this filter.
*
* Removes the Counter from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The Counter to remove.
*/
void Remove(Counter* input);
/**
* Sets the number of cycles that the input must not change state for.
*
* @param fpgaCycles The number of FPGA cycles.
*/
void SetPeriodCycles(int fpgaCycles);
/**
* Sets the number of nanoseconds that the input must not change state for.
*
* @param nanoseconds The number of nanoseconds.
*/
void SetPeriodNanoSeconds(uint64_t nanoseconds);
/**
* Gets the number of cycles that the input must not change state for.
*
* @return The number of cycles.
*/
int GetPeriodCycles();
/**
* Gets the number of nanoseconds that the input must not change state for.
*
* @return The number of nanoseconds.
*/
uint64_t GetPeriodNanoSeconds();
void InitSendable(wpi::SendableBuilder& builder) override;
private:
int m_channelIndex;
// Sets the filter for the input to be the requested index. A value of 0
// disables the filter, and the filter value must be between 1 and 3,
// inclusive.
static void DoAdd(DigitalSource* input, int requested_index);
/**
* Allocates the next available filter index, or -1 if there are no filters
* available.
* @return the filter index
*/
static int AllocateFilterIndex();
static wpi::mutex m_mutex;
static std::array<bool, 3> m_filterAllocated;
};
} // namespace frc