mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-06 03:31:43 +00:00
[wpilib] GenericHID.setRumble: Fix Java integer overflow (#6529)
This commit is contained in:
@@ -250,7 +250,7 @@ public class DriverStationJNI extends JNIWrapper {
|
||||
* @see "HAL_SetJoystickOutputs"
|
||||
*/
|
||||
public static native int setJoystickOutputs(
|
||||
byte joystickNum, int outputs, short leftRumble, short rightRumble);
|
||||
byte joystickNum, int outputs, int leftRumble, int rightRumble);
|
||||
|
||||
/**
|
||||
* Gets whether a specific joystick is considered to be an XBox controller.
|
||||
|
||||
@@ -294,12 +294,11 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickButtons
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: setJoystickOutputs
|
||||
* Signature: (BISS)I
|
||||
* Signature: (BIII)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_setJoystickOutputs
|
||||
(JNIEnv*, jclass, jbyte port, jint outputs, jshort leftRumble,
|
||||
jshort rightRumble)
|
||||
(JNIEnv*, jclass, jbyte port, jint outputs, jint leftRumble, jint rightRumble)
|
||||
{
|
||||
return HAL_SetJoystickOutputs(port, outputs, leftRumble, rightRumble);
|
||||
}
|
||||
|
||||
50
wpilibc/src/test/native/cpp/GenericHIDTest.cpp
Normal file
50
wpilibc/src/test/native/cpp/GenericHIDTest.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
// 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 <frc/GenericHID.h>
|
||||
#include <frc/simulation/GenericHIDSim.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace frc;
|
||||
using RumbleType = GenericHID::RumbleType;
|
||||
static constexpr double kEpsilon = 0.0001;
|
||||
TEST(GenericHIDTest, RumbleRange) {
|
||||
GenericHID hid{0};
|
||||
sim::GenericHIDSim sim{0};
|
||||
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
double rumbleValue = i / 100.0;
|
||||
hid.SetRumble(RumbleType::kBothRumble, rumbleValue);
|
||||
EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon);
|
||||
EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kRightRumble), kEpsilon);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(GenericHIDTest, RumbleTypes) {
|
||||
GenericHID hid{0};
|
||||
sim::GenericHIDSim sim{0};
|
||||
|
||||
// Make sure both are off
|
||||
hid.SetRumble(RumbleType::kBothRumble, 0);
|
||||
EXPECT_NEAR(0, sim.GetRumble(RumbleType::kBothRumble), kEpsilon);
|
||||
|
||||
// test both
|
||||
hid.SetRumble(RumbleType::kBothRumble, 1);
|
||||
EXPECT_NEAR(1, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon);
|
||||
EXPECT_NEAR(1, sim.GetRumble(RumbleType::kRightRumble), kEpsilon);
|
||||
hid.SetRumble(RumbleType::kBothRumble, 0);
|
||||
|
||||
// test left only
|
||||
hid.SetRumble(RumbleType::kLeftRumble, 1);
|
||||
EXPECT_NEAR(1, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon);
|
||||
EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightRumble), kEpsilon);
|
||||
hid.SetRumble(RumbleType::kLeftRumble, 0);
|
||||
|
||||
// test right only
|
||||
hid.SetRumble(RumbleType::kRightRumble, 1);
|
||||
EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon);
|
||||
EXPECT_NEAR(1, sim.GetRumble(RumbleType::kRightRumble), kEpsilon);
|
||||
hid.SetRumble(RumbleType::kRightRumble, 0);
|
||||
}
|
||||
@@ -95,8 +95,8 @@ public class GenericHID {
|
||||
|
||||
private final int m_port;
|
||||
private int m_outputs;
|
||||
private short m_leftRumble;
|
||||
private short m_rightRumble;
|
||||
private int m_leftRumble;
|
||||
private int m_rightRumble;
|
||||
|
||||
/**
|
||||
* Construct an instance of a device.
|
||||
@@ -455,8 +455,7 @@ public class GenericHID {
|
||||
*/
|
||||
public void setRumble(RumbleType type, double value) {
|
||||
value = MathUtil.clamp(value, 0, 1);
|
||||
short rumbleValue = (short) (value * 65535);
|
||||
|
||||
int rumbleValue = (int) (value * 65535);
|
||||
switch (type) {
|
||||
case kLeftRumble:
|
||||
this.m_leftRumble = rumbleValue;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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.assertEquals;
|
||||
|
||||
import edu.wpi.first.wpilibj.GenericHID.RumbleType;
|
||||
import edu.wpi.first.wpilibj.simulation.GenericHIDSim;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GenericHIDTest {
|
||||
private static final double kEpsilon = 0.0001;
|
||||
|
||||
@Test
|
||||
void testRumbleRange() {
|
||||
GenericHID hid = new GenericHID(0);
|
||||
GenericHIDSim sim = new GenericHIDSim(0);
|
||||
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
double rumbleValue = i / 100.0;
|
||||
hid.setRumble(RumbleType.kBothRumble, rumbleValue);
|
||||
assertEquals(rumbleValue, sim.getRumble(RumbleType.kLeftRumble), kEpsilon);
|
||||
assertEquals(rumbleValue, sim.getRumble(RumbleType.kRightRumble), kEpsilon);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRumbleTypes() {
|
||||
GenericHID hid = new GenericHID(0);
|
||||
GenericHIDSim sim = new GenericHIDSim(0);
|
||||
|
||||
// Make sure both are off
|
||||
hid.setRumble(RumbleType.kBothRumble, 0);
|
||||
assertEquals(0, sim.getRumble(RumbleType.kBothRumble), kEpsilon);
|
||||
|
||||
// test both
|
||||
hid.setRumble(RumbleType.kBothRumble, 1);
|
||||
assertEquals(1, sim.getRumble(RumbleType.kLeftRumble), kEpsilon);
|
||||
assertEquals(1, sim.getRumble(RumbleType.kRightRumble), kEpsilon);
|
||||
hid.setRumble(RumbleType.kBothRumble, 0);
|
||||
|
||||
// test left only
|
||||
hid.setRumble(RumbleType.kLeftRumble, 1);
|
||||
assertEquals(1, sim.getRumble(RumbleType.kLeftRumble), kEpsilon);
|
||||
assertEquals(0, sim.getRumble(RumbleType.kRightRumble), kEpsilon);
|
||||
hid.setRumble(RumbleType.kLeftRumble, 0);
|
||||
|
||||
// test right only
|
||||
hid.setRumble(RumbleType.kRightRumble, 1);
|
||||
assertEquals(0, sim.getRumble(RumbleType.kLeftRumble), kEpsilon);
|
||||
assertEquals(1, sim.getRumble(RumbleType.kRightRumble), kEpsilon);
|
||||
hid.setRumble(RumbleType.kRightRumble, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user