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.
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/GenericHID.h"
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/DriverStation.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2022-06-14 08:48:16 +03:00
|
|
|
#include "frc/event/BooleanEvent.h"
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-06-15 23:06:03 -07:00
|
|
|
GenericHID::GenericHID(int port) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (port < 0 || port >= DriverStation::kJoystickPorts) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(warn::BadJoystickIndex, "port {} out of range", port);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
m_port = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GenericHID::GetRawButton(int button) const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickButton(m_port, button);
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
bool GenericHID::GetRawButtonPressed(int button) {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickButtonPressed(m_port, button);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GenericHID::GetRawButtonReleased(int button) {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickButtonReleased(m_port, button);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-14 08:48:16 +03:00
|
|
|
BooleanEvent GenericHID::Button(int button, EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(loop,
|
|
|
|
|
[this, button]() { return this->GetRawButton(button); });
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
double GenericHID::GetRawAxis(int axis) const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickAxis(m_port, axis);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int GenericHID::GetPOV(int pov) const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickPOV(m_port, pov);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2022-10-23 22:09:44 +03:00
|
|
|
BooleanEvent GenericHID::POV(int angle, EventLoop* loop) const {
|
|
|
|
|
return POV(0, angle, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POV(int pov, int angle, EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(
|
|
|
|
|
loop, [this, pov, angle] { return this->GetPOV(pov) == angle; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVUp(EventLoop* loop) const {
|
|
|
|
|
return POV(0, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVUpRight(EventLoop* loop) const {
|
|
|
|
|
return POV(45, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVRight(EventLoop* loop) const {
|
|
|
|
|
return POV(90, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVDownRight(EventLoop* loop) const {
|
|
|
|
|
return POV(135, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVDown(EventLoop* loop) const {
|
|
|
|
|
return POV(180, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVDownLeft(EventLoop* loop) const {
|
|
|
|
|
return POV(225, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVLeft(EventLoop* loop) const {
|
|
|
|
|
return POV(270, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVUpLeft(EventLoop* loop) const {
|
|
|
|
|
return POV(315, loop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::POVCenter(EventLoop* loop) const {
|
|
|
|
|
return POV(360, loop);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 14:29:14 -05:00
|
|
|
BooleanEvent GenericHID::AxisLessThan(int axis, double threshold,
|
|
|
|
|
EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(loop, [this, axis, threshold]() {
|
|
|
|
|
return this->GetRawAxis(axis) < threshold;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BooleanEvent GenericHID::AxisGreaterThan(int axis, double threshold,
|
|
|
|
|
EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(loop, [this, axis, threshold]() {
|
|
|
|
|
return this->GetRawAxis(axis) > threshold;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int GenericHID::GetAxisCount() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickAxisCount(m_port);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int GenericHID::GetPOVCount() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickPOVCount(m_port);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
int GenericHID::GetButtonCount() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetStickButtonCount(m_port);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2020-11-13 14:11:10 -05:00
|
|
|
bool GenericHID::IsConnected() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::IsJoystickConnected(m_port);
|
2020-11-13 14:11:10 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
GenericHID::HIDType GenericHID::GetType() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return static_cast<HIDType>(DriverStation::GetJoystickType(m_port));
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-19 11:36:44 -07:00
|
|
|
std::string GenericHID::GetName() const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetJoystickName(m_port);
|
2019-10-19 11:36:44 -07:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
int GenericHID::GetAxisType(int axis) const {
|
2021-06-15 23:06:03 -07:00
|
|
|
return DriverStation::GetJoystickAxisType(m_port, axis);
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int GenericHID::GetPort() const {
|
|
|
|
|
return m_port;
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
void GenericHID::SetOutput(int outputNumber, bool value) {
|
|
|
|
|
m_outputs =
|
|
|
|
|
(m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
|
|
|
|
|
|
|
|
|
|
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GenericHID::SetOutputs(int value) {
|
|
|
|
|
m_outputs = value;
|
|
|
|
|
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GenericHID::SetRumble(RumbleType type, double value) {
|
2022-12-24 14:28:52 -05:00
|
|
|
value = std::clamp(value, 0.0, 1.0);
|
|
|
|
|
double rumbleValue = value * 65535;
|
|
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
if (type == kLeftRumble) {
|
2022-12-24 14:28:52 -05:00
|
|
|
m_leftRumble = rumbleValue;
|
|
|
|
|
} else if (type == kRightRumble) {
|
|
|
|
|
m_rightRumble = rumbleValue;
|
2016-11-18 23:05:37 -08:00
|
|
|
} else {
|
2022-12-24 14:28:52 -05:00
|
|
|
m_leftRumble = rumbleValue;
|
|
|
|
|
m_rightRumble = rumbleValue;
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
2022-12-24 14:28:52 -05:00
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
|
|
|
|
}
|