Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/DriverStation.h" // NOLINT(build/include_order) |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 7 | #include <hal/DriverStation.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 8 | #include <units/math.h> |
| 9 | #include <units/time.h> |
| 10 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include "TestBench.h" |
| 12 | #include "frc/RobotController.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | #define EXPECT_NEAR_UNITS(val1, val2, eps) \ |
| 16 | EXPECT_LE(units::math::abs(val1 - val2), eps) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Test if the WaitForData function works |
| 20 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 21 | TEST(DriverStationTest, WaitForData) { |
| 22 | units::microsecond_t initialTime(frc::RobotController::GetFPGATime()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 24 | wpi::Event waitEvent{true}; |
| 25 | HAL_ProvideNewDataEventHandle(waitEvent.GetHandle()); |
| 26 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | // 20ms waiting intervals * 50 = 1s |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | for (int i = 0; i < 50; i++) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 29 | wpi::WaitForObject(waitEvent.GetHandle()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | } |
| 31 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 32 | HAL_RemoveNewDataEventHandle(waitEvent.GetHandle()); |
| 33 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 34 | units::microsecond_t finalTime(frc::RobotController::GetFPGATime()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 36 | EXPECT_NEAR_UNITS(1_s, finalTime - initialTime, 200_ms); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |