WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Debouncer.hpp
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 "wpi/units/time.hpp"
9
10namespace wpi::math {
11/**
12 * A simple debounce filter for boolean streams. Requires that the boolean
13 * change value from baseline for a specified period of time before the filtered
14 * value changes.
15 */
17 public:
18 /**
19 * Type of debouncing to perform.
20 */
22 /// Rising edge.
24 /// Falling edge.
26 /// Both rising and falling edges.
28 };
29
30 /**
31 * Creates a new Debouncer.
32 *
33 * @param debounceTime The number of seconds the value must change from
34 * baseline for the filtered value to change.
35 * @param type Which type of state change the debouncing will be
36 * performed on.
37 */
38 explicit Debouncer(wpi::units::second_t debounceTime,
40
41 /**
42 * Applies the debouncer to the input stream.
43 *
44 * @param input The current value of the input stream.
45 * @return The debounced value of the input stream.
46 */
47 bool Calculate(bool input);
48
49 /**
50 * Sets the time to debounce.
51 *
52 * @param time The number of seconds the value must change from baseline
53 * for the filtered value to change.
54 */
55 constexpr void SetDebounceTime(wpi::units::second_t time) {
56 m_debounceTime = time;
57 }
58
59 /**
60 * Gets the time to debounce.
61 *
62 * @return The number of seconds the value must change from baseline
63 * for the filtered value to change.
64 */
65 constexpr wpi::units::second_t GetDebounceTime() const {
66 return m_debounceTime;
67 }
68
69 /**
70 * Set the debounce type.
71 *
72 * @param type Which type of state change the debouncing will be performed on.
73 */
74 constexpr void SetDebounceType(DebounceType type) {
75 m_debounceType = type;
76
77 m_baseline = m_debounceType == DebounceType::kFalling;
78 }
79
80 /**
81 * Get the debounce type.
82 *
83 * @return Which type of state change the debouncing will be performed on.
84 */
85 constexpr DebounceType GetDebounceType() const { return m_debounceType; }
86
87 private:
88 wpi::units::second_t m_debounceTime;
89 bool m_baseline;
90 DebounceType m_debounceType;
91
92 wpi::units::second_t m_prevTime;
93
94 void ResetTimer();
95
96 bool HasElapsed() const;
97};
98} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
DebounceType
Type of debouncing to perform.
Definition Debouncer.hpp:21
@ kBoth
Both rising and falling edges.
Definition Debouncer.hpp:27
@ kRising
Rising edge.
Definition Debouncer.hpp:23
@ kFalling
Falling edge.
Definition Debouncer.hpp:25
bool Calculate(bool input)
Applies the debouncer to the input stream.
constexpr void SetDebounceTime(wpi::units::second_t time)
Sets the time to debounce.
Definition Debouncer.hpp:55
constexpr wpi::units::second_t GetDebounceTime() const
Gets the time to debounce.
Definition Debouncer.hpp:65
Debouncer(wpi::units::second_t debounceTime, DebounceType type=DebounceType::kRising)
Creates a new Debouncer.
constexpr DebounceType GetDebounceType() const
Get the debounce type.
Definition Debouncer.hpp:85
constexpr void SetDebounceType(DebounceType type)
Set the debounce type.
Definition Debouncer.hpp:74
Definition LinearSystem.hpp:20