blob: 3109926b3b49638ea2fe51de7aeb57004893570a [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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.
4
James Kuszmaulb13e13f2023-11-22 20:44:04 -08005#include <gtest/gtest.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07006#include <hal/HAL.h>
7
8#include "CrossConnects.h"
9#include "LifetimeWrappers.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070010
11using namespace hlt;
12
13class DutyCycleTest : public ::testing::TestWithParam<std::pair<int, int>> {};
14
15TEST_P(DutyCycleTest, DutyCycle) {
16 auto param = GetParam();
17
18 int32_t status = 0;
19 PWMHandle pwmHandle(param.first, &status);
20 ASSERT_NE(pwmHandle, HAL_kInvalidHandle);
21 ASSERT_EQ(0, status);
22
23 // Ensure our PWM is disabled, and set up properly
James Kuszmaulb13e13f2023-11-22 20:44:04 -080024 HAL_SetPWMPulseTimeMicroseconds(pwmHandle, 0, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070025 ASSERT_EQ(0, status);
James Kuszmaulb13e13f2023-11-22 20:44:04 -080026 HAL_SetPWMConfigMicroseconds(pwmHandle, 2000, 1000, 1000, 0, 0, &status);
27 HAL_SetPWMConfigMicroseconds(pwmHandle, 5050, 2525, 2525, 2525, 0, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070028 ASSERT_EQ(0, status);
29 HAL_SetPWMPeriodScale(pwmHandle, 0, &status);
30 ASSERT_EQ(0, status);
31
32 DIOHandle dioHandle{param.second, true, &status};
33 ASSERT_EQ(0, status);
34
35 DutyCycleHandle dutyCycle{dioHandle, &status};
36 ASSERT_EQ(0, status);
37
38 HAL_SetPWMSpeed(pwmHandle, 0.5, &status);
39 ASSERT_EQ(0, status);
40
41 // Sleep enough time for the frequency to converge
42 usleep(3500000);
43
James Kuszmaulb13e13f2023-11-22 20:44:04 -080044 ASSERT_NEAR(
45 1000 / 5.05,
46 static_cast<double>(HAL_GetDutyCycleFrequency(dutyCycle, &status)), 1);
Austin Schuh812d0d12021-11-04 20:16:48 -070047
48 // TODO measure output
49}
50
51INSTANTIATE_TEST_SUITE_P(DutyCycleCrossConnTests, DutyCycleTest,
52 ::testing::ValuesIn(PWMCrossConnects));