mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add Debouncer (#3590)
Supersedes #2358 with updates and cleanups. Closes #2482 and closes #2487 because we shouldn't support both time-based and count-based debouncing approaches. Co-authored-by: oblarg <emichaelbarnett@gmail.com>
This commit is contained in:
37
wpilibc/src/main/native/cpp/Debouncer.cpp
Normal file
37
wpilibc/src/main/native/cpp/Debouncer.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/Debouncer.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
|
||||
: m_debounceTime(debounceTime), m_debounceType(type) {
|
||||
switch (type) {
|
||||
case DebounceType::kBoth: // fall-through
|
||||
case DebounceType::kRising:
|
||||
m_baseline = false;
|
||||
break;
|
||||
case DebounceType::kFalling:
|
||||
m_baseline = true;
|
||||
break;
|
||||
}
|
||||
m_timer.Start();
|
||||
}
|
||||
|
||||
bool Debouncer::Calculate(bool input) {
|
||||
if (input == m_baseline) {
|
||||
m_timer.Reset();
|
||||
}
|
||||
|
||||
if (m_timer.HasElapsed(m_debounceTime)) {
|
||||
if (m_debounceType == DebounceType::kBoth) {
|
||||
m_baseline = input;
|
||||
m_timer.Reset();
|
||||
}
|
||||
return input;
|
||||
} else {
|
||||
return m_baseline;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user