mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
Fixed minimum number of joystick axes (#696)
This commit is contained in:
committed by
Peter Johnson
parent
efc7770e9b
commit
595b1df380
@@ -7,8 +7,6 @@
|
||||
|
||||
#include "Joystick.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
#include <HAL/HAL.h>
|
||||
@@ -18,8 +16,6 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
constexpr int Joystick::kMinNumAxes;
|
||||
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
/**
|
||||
@@ -30,13 +26,12 @@ constexpr double kPi = 3.14159265358979323846;
|
||||
* @param port The port on the Driver Station that the joystick is plugged into
|
||||
* (0-5).
|
||||
*/
|
||||
Joystick::Joystick(int port)
|
||||
: GenericHID(port), m_axes(std::max(GetAxisCount(), kMinNumAxes)) {
|
||||
m_axes[static_cast<int>(Axis::kX)] = kDefaultXAxis;
|
||||
m_axes[static_cast<int>(Axis::kY)] = kDefaultYAxis;
|
||||
m_axes[static_cast<int>(Axis::kZ)] = kDefaultZAxis;
|
||||
m_axes[static_cast<int>(Axis::kTwist)] = kDefaultTwistAxis;
|
||||
m_axes[static_cast<int>(Axis::kThrottle)] = kDefaultThrottleAxis;
|
||||
Joystick::Joystick(int port) : GenericHID(port) {
|
||||
m_axes[Axis::kX] = kDefaultXAxis;
|
||||
m_axes[Axis::kY] = kDefaultYAxis;
|
||||
m_axes[Axis::kZ] = kDefaultZAxis;
|
||||
m_axes[Axis::kTwist] = kDefaultTwistAxis;
|
||||
m_axes[Axis::kThrottle] = kDefaultThrottleAxis;
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
|
||||
}
|
||||
@@ -56,9 +51,7 @@ void Joystick::SetAxisChannel(AxisType axis, int channel) {
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
void Joystick::SetXChannel(int channel) {
|
||||
m_axes[static_cast<int>(Axis::kX)] = channel;
|
||||
}
|
||||
void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; }
|
||||
|
||||
/**
|
||||
* Set the channel associated with the Y axis.
|
||||
@@ -66,9 +59,7 @@ void Joystick::SetXChannel(int channel) {
|
||||
* @param axis The axis to set the channel for.
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
void Joystick::SetYChannel(int channel) {
|
||||
m_axes[static_cast<int>(Axis::kY)] = channel;
|
||||
}
|
||||
void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; }
|
||||
|
||||
/**
|
||||
* Set the channel associated with the Z axis.
|
||||
@@ -76,9 +67,7 @@ void Joystick::SetYChannel(int channel) {
|
||||
* @param axis The axis to set the channel for.
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
void Joystick::SetZChannel(int channel) {
|
||||
m_axes[static_cast<int>(Axis::kZ)] = channel;
|
||||
}
|
||||
void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; }
|
||||
|
||||
/**
|
||||
* Set the channel associated with the twist axis.
|
||||
@@ -86,9 +75,7 @@ void Joystick::SetZChannel(int channel) {
|
||||
* @param axis The axis to set the channel for.
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
void Joystick::SetTwistChannel(int channel) {
|
||||
m_axes[static_cast<int>(Axis::kTwist)] = channel;
|
||||
}
|
||||
void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; }
|
||||
|
||||
/**
|
||||
* Set the channel associated with the throttle axis.
|
||||
@@ -97,7 +84,7 @@ void Joystick::SetTwistChannel(int channel) {
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
void Joystick::SetThrottleChannel(int channel) {
|
||||
m_axes[static_cast<int>(Axis::kThrottle)] = channel;
|
||||
m_axes[Axis::kThrottle] = channel;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,39 +92,35 @@ void Joystick::SetThrottleChannel(int channel) {
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
int Joystick::GetXChannel() const { return m_axes[static_cast<int>(Axis::kX)]; }
|
||||
int Joystick::GetXChannel() const { return m_axes[Axis::kX]; }
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the Y axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
int Joystick::GetYChannel() const { return m_axes[static_cast<int>(Axis::kY)]; }
|
||||
int Joystick::GetYChannel() const { return m_axes[Axis::kY]; }
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the Z axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
int Joystick::GetZChannel() const { return m_axes[static_cast<int>(Axis::kZ)]; }
|
||||
int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; }
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the twist axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
int Joystick::GetTwistChannel() const {
|
||||
return m_axes[static_cast<int>(Axis::kTwist)];
|
||||
}
|
||||
int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; }
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the throttle axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
int Joystick::GetThrottleChannel() const {
|
||||
return m_axes[static_cast<int>(Axis::kThrottle)];
|
||||
}
|
||||
int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; }
|
||||
|
||||
/**
|
||||
* Get the X value of the joystick.
|
||||
@@ -223,9 +206,7 @@ double Joystick::GetAxis(AxisType axis) const {
|
||||
*
|
||||
* @return The state of the trigger.
|
||||
*/
|
||||
bool Joystick::GetTrigger() const {
|
||||
return GetRawButton(static_cast<int>(Button::kTrigger));
|
||||
}
|
||||
bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); }
|
||||
|
||||
/**
|
||||
* Whether the trigger was pressed since the last check.
|
||||
@@ -233,7 +214,7 @@ bool Joystick::GetTrigger() const {
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
bool Joystick::GetTriggerPressed() {
|
||||
return GetRawButtonPressed(static_cast<int>(Button::kTrigger));
|
||||
return GetRawButtonPressed(Button::kTrigger);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,7 +223,7 @@ bool Joystick::GetTriggerPressed() {
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
bool Joystick::GetTriggerReleased() {
|
||||
return GetRawButtonReleased(static_cast<int>(Button::kTrigger));
|
||||
return GetRawButtonReleased(Button::kTrigger);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,27 +233,21 @@ bool Joystick::GetTriggerReleased() {
|
||||
*
|
||||
* @return The state of the top button.
|
||||
*/
|
||||
bool Joystick::GetTop() const {
|
||||
return GetRawButton(static_cast<int>(Button::kTop));
|
||||
}
|
||||
bool Joystick::GetTop() const { return GetRawButton(Button::kTop); }
|
||||
|
||||
/**
|
||||
* Whether the top button was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
bool Joystick::GetTopPressed() {
|
||||
return GetRawButtonPressed(static_cast<int>(Button::kTop));
|
||||
}
|
||||
bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); }
|
||||
|
||||
/**
|
||||
* Whether the top button was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
bool Joystick::GetTopReleased() {
|
||||
return GetRawButtonReleased(static_cast<int>(Button::kTop));
|
||||
}
|
||||
bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); }
|
||||
|
||||
Joystick* Joystick::GetStickForPort(int port) {
|
||||
static std::array<std::unique_ptr<Joystick>, DriverStation::kJoystickPorts>
|
||||
@@ -295,7 +270,7 @@ Joystick* Joystick::GetStickForPort(int port) {
|
||||
*/
|
||||
bool Joystick::GetButton(ButtonType button) const {
|
||||
int temp = button;
|
||||
return GetRawButton(static_cast<int>(static_cast<Button>(temp)));
|
||||
return GetRawButton(static_cast<Button>(temp));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
#include <support/deprecated.h>
|
||||
|
||||
@@ -30,7 +30,6 @@ class Joystick : public GenericHID {
|
||||
static constexpr int kDefaultZAxis = 2;
|
||||
static constexpr int kDefaultTwistAxis = 2;
|
||||
static constexpr int kDefaultThrottleAxis = 3;
|
||||
static constexpr int kMinNumAxes = 4;
|
||||
|
||||
enum AxisType { kXAxis, kYAxis, kZAxis, kTwistAxis, kThrottleAxis };
|
||||
enum ButtonType { kTriggerButton, kTopButton };
|
||||
@@ -87,10 +86,10 @@ class Joystick : public GenericHID {
|
||||
double GetDirectionDegrees() const;
|
||||
|
||||
private:
|
||||
enum class Axis { kX, kY, kZ, kTwist, kThrottle };
|
||||
enum class Button { kTrigger = 1, kTop = 2 };
|
||||
enum Axis { kX, kY, kZ, kTwist, kThrottle, kNumAxes };
|
||||
enum Button { kTrigger = 1, kTop = 2 };
|
||||
|
||||
std::vector<int> m_axes;
|
||||
std::array<int, Axis::kNumAxes> m_axes;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -23,7 +23,6 @@ public class Joystick extends GenericHID {
|
||||
static final byte kDefaultZAxis = 2;
|
||||
static final byte kDefaultTwistAxis = 2;
|
||||
static final byte kDefaultThrottleAxis = 3;
|
||||
static final byte kMinNumAxes = 4;
|
||||
|
||||
/**
|
||||
* Represents an analog axis on a joystick.
|
||||
@@ -71,7 +70,7 @@ public class Joystick extends GenericHID {
|
||||
* Represents an analog axis on a joystick.
|
||||
*/
|
||||
private enum Axis {
|
||||
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4);
|
||||
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4), kNumAxes(5);
|
||||
|
||||
@SuppressWarnings("MemberName")
|
||||
public final int value;
|
||||
@@ -81,7 +80,7 @@ public class Joystick extends GenericHID {
|
||||
}
|
||||
}
|
||||
|
||||
private final byte[] m_axes;
|
||||
private final byte[] m_axes = new byte[Axis.kNumAxes.value];
|
||||
|
||||
/**
|
||||
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
|
||||
@@ -92,8 +91,6 @@ public class Joystick extends GenericHID {
|
||||
public Joystick(final int port) {
|
||||
super(port);
|
||||
|
||||
m_axes = new byte[Math.max(getAxisCount(), kMinNumAxes)];
|
||||
|
||||
m_axes[Axis.kX.value] = kDefaultXAxis;
|
||||
m_axes[Axis.kY.value] = kDefaultYAxis;
|
||||
m_axes[Axis.kZ.value] = kDefaultZAxis;
|
||||
|
||||
Reference in New Issue
Block a user