mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +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:
@@ -0,0 +1,54 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.wpilibj.simulation.SimHooks;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DebouncerTest {
|
||||
@Test
|
||||
void debounceRisingTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kRising);
|
||||
|
||||
debouncer.calculate(false);
|
||||
assertFalse(debouncer.calculate(true));
|
||||
|
||||
SimHooks.stepTiming(0.1);
|
||||
|
||||
assertTrue(debouncer.calculate(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void debounceFallingTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kFalling);
|
||||
|
||||
debouncer.calculate(true);
|
||||
assertTrue(debouncer.calculate(false));
|
||||
|
||||
SimHooks.stepTiming(0.1);
|
||||
|
||||
assertFalse(debouncer.calculate(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void debounceBothTest() {
|
||||
var debouncer = new Debouncer(0.02, Debouncer.DebounceType.kBoth);
|
||||
|
||||
debouncer.calculate(false);
|
||||
assertFalse(debouncer.calculate(true));
|
||||
|
||||
SimHooks.stepTiming(0.1);
|
||||
|
||||
assertTrue(debouncer.calculate(true));
|
||||
assertTrue(debouncer.calculate(false));
|
||||
|
||||
SimHooks.stepTiming(0.1);
|
||||
|
||||
assertFalse(debouncer.calculate(false));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user