blob: eb402cdf80a968873dfe57f0f909ccb8f033af95 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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#include "frc/Debouncer.h"
6
7using namespace frc;
8
9Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
10 : m_debounceTime(debounceTime), m_debounceType(type) {
11 switch (type) {
12 case DebounceType::kBoth: // fall-through
13 case DebounceType::kRising:
14 m_baseline = false;
15 break;
16 case DebounceType::kFalling:
17 m_baseline = true;
18 break;
19 }
20 m_timer.Start();
21}
22
23bool Debouncer::Calculate(bool input) {
24 if (input == m_baseline) {
25 m_timer.Reset();
26 }
27
28 if (m_timer.HasElapsed(m_debounceTime)) {
29 if (m_debounceType == DebounceType::kBoth) {
30 m_baseline = input;
31 m_timer.Reset();
32 }
33 return input;
34 } else {
35 return m_baseline;
36 }
37}