2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
package edu.wpi.first.wpilibj.simulation;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2021-08-12 02:04:14 -04:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2018-09-20 21:59:46 -07:00
|
|
|
import edu.wpi.first.hal.HAL;
|
2020-12-29 22:45:16 -08:00
|
|
|
import edu.wpi.first.wpilibj.AnalogInput;
|
2021-08-12 02:04:14 -04:00
|
|
|
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
|
|
|
|
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
2020-12-29 22:45:16 -08:00
|
|
|
import org.junit.jupiter.api.Test;
|
2018-05-24 00:31:04 -04:00
|
|
|
|
2018-06-11 18:01:49 -04:00
|
|
|
class AnalogInputSimTest {
|
2021-08-12 02:04:14 -04:00
|
|
|
@Test
|
|
|
|
|
void setInitializeTest() {
|
|
|
|
|
HAL.initialize(500, 0);
|
|
|
|
|
|
|
|
|
|
AnalogInputSim sim = new AnalogInputSim(5);
|
|
|
|
|
BooleanCallback callback = new BooleanCallback();
|
|
|
|
|
|
|
|
|
|
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
|
|
|
|
AnalogInput input = new AnalogInput(5)) {
|
|
|
|
|
assertTrue(callback.wasTriggered());
|
|
|
|
|
assertTrue(callback.getSetValue());
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2021-08-12 02:04:14 -04:00
|
|
|
void testSetVoltage() {
|
2018-05-11 12:38:23 -07:00
|
|
|
HAL.initialize(500, 0);
|
|
|
|
|
|
2021-08-12 02:04:14 -04:00
|
|
|
AnalogInputSim sim = new AnalogInputSim(5);
|
|
|
|
|
DoubleCallback callback = new DoubleCallback();
|
|
|
|
|
|
|
|
|
|
try (CallbackStore cb = sim.registerVoltageCallback(callback, false);
|
|
|
|
|
AnalogInput input = new AnalogInput(5)) {
|
|
|
|
|
// Bootstrap the voltage to be non-zero
|
|
|
|
|
sim.setVoltage(1.0);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-07-24 13:07:11 -07:00
|
|
|
for (double i = 0; i < 5.0; i += 0.1) {
|
2021-08-12 02:04:14 -04:00
|
|
|
callback.reset();
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-08-12 02:04:14 -04:00
|
|
|
sim.setVoltage(0);
|
2020-07-24 13:07:11 -07:00
|
|
|
assertEquals(input.getVoltage(), 0, 0.001);
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2021-08-12 02:04:14 -04:00
|
|
|
callback.reset();
|
|
|
|
|
sim.setVoltage(i);
|
2020-07-24 13:07:11 -07:00
|
|
|
assertEquals(input.getVoltage(), i, 0.001);
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-12 02:04:14 -04:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testSetOverSampleBits() {
|
|
|
|
|
HAL.initialize(500, 0);
|
|
|
|
|
try (AnalogInput input = new AnalogInput(5)) {
|
|
|
|
|
input.setOversampleBits(3504);
|
|
|
|
|
assertEquals(3504, input.getOversampleBits());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|