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:
Tyler Veness
2021-09-19 19:58:16 -07:00
committed by GitHub
parent 179fde3a7b
commit 1ca383b23b
10 changed files with 344 additions and 1 deletions

View File

@@ -0,0 +1,48 @@
// 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" // NOLINT(build/include_order)
#include "frc/simulation/SimHooks.h"
#include "gtest/gtest.h"
using namespace frc;
TEST(DebouncerTest, DebounceRising) {
Debouncer debouncer{20_ms};
debouncer.Calculate(false);
EXPECT_FALSE(debouncer.Calculate(true));
frc::sim::StepTiming(100_ms);
EXPECT_TRUE(debouncer.Calculate(true));
}
TEST(DebouncerTest, DebounceFalling) {
Debouncer debouncer{20_ms, Debouncer::DebounceType::kFalling};
debouncer.Calculate(true);
EXPECT_TRUE(debouncer.Calculate(false));
frc::sim::StepTiming(100_ms);
EXPECT_FALSE(debouncer.Calculate(false));
}
TEST(DebouncerTest, DebounceBoth) {
Debouncer debouncer{20_ms, Debouncer::DebounceType::kBoth};
debouncer.Calculate(false);
EXPECT_FALSE(debouncer.Calculate(true));
frc::sim::StepTiming(100_ms);
EXPECT_TRUE(debouncer.Calculate(true));
EXPECT_TRUE(debouncer.Calculate(false));
frc::sim::StepTiming(100_ms);
EXPECT_FALSE(debouncer.Calculate(false));
}