mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpilibj] Fix java async interrupts (#3559)
This commit is contained in:
45
wpilibc/src/test/native/cpp/InterruptTest.cpp
Normal file
45
wpilibc/src/test/native/cpp/InterruptTest.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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 <atomic>
|
||||
|
||||
#include <hal/HAL.h>
|
||||
|
||||
#include "frc/AsynchronousInterrupt.h"
|
||||
#include "frc/DigitalInput.h"
|
||||
#include "frc/Timer.h"
|
||||
#include "frc/simulation/DIOSim.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace frc {
|
||||
using namespace frc::sim;
|
||||
TEST(InterruptTest, AsynchronousInterrupt) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
std::atomic_int counter{0};
|
||||
std::atomic_bool hasFired{false};
|
||||
|
||||
DigitalInput di{0};
|
||||
AsynchronousInterrupt interrupt{di, [&](bool rising, bool falling) {
|
||||
counter++;
|
||||
hasFired = true;
|
||||
}};
|
||||
|
||||
interrupt.Enable();
|
||||
frc::Wait(0.5_s);
|
||||
DIOSim digitalSim{di};
|
||||
digitalSim.SetValue(false);
|
||||
frc::Wait(20_ms);
|
||||
digitalSim.SetValue(true);
|
||||
frc::Wait(10_ms);
|
||||
|
||||
int count = 0;
|
||||
while (!hasFired) {
|
||||
frc::Wait(5_ms);
|
||||
count++;
|
||||
ASSERT_TRUE(count < 1000);
|
||||
}
|
||||
ASSERT_EQ(1, counter.load());
|
||||
}
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user