Skip to content

Commit

Permalink
123: Changed trigger USB configuration range to [0,127]
Browse files Browse the repository at this point in the history
  • Loading branch information
James Smith authored and James Smith committed Jan 19, 2025
1 parent 3a50566 commit 244b6da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
29 changes: 21 additions & 8 deletions src/hal/Usb/Client/Hid/UsbGamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ bool UsbGamepad::isButtonPressed()
|| currentButtons != 0
|| isAnalogPressed(currentLeftAnalog[0])
|| isAnalogPressed(currentLeftAnalog[1])
|| currentLeftAnalog[2] > (MIN_ANALOG_VALUE + ANALOG_PRESSED_TOL)
|| isAnalogPressed(currentLeftAnalog[2])
|| isAnalogPressed(currentRightAnalog[0])
|| isAnalogPressed(currentRightAnalog[1])
|| currentRightAnalog[2] > (MIN_ANALOG_VALUE + ANALOG_PRESSED_TOL));
|| isAnalogPressed(currentRightAnalog[2])
);
}

//--------------------------------------------------------------------+
Expand Down Expand Up @@ -73,9 +74,9 @@ void UsbGamepad::setAnalogThumbY(bool isLeft, int8_t y)
buttonsUpdated = buttonsUpdated || (y != lastY);
}

void UsbGamepad::setAnalogTrigger(bool isLeft, int8_t z)
void UsbGamepad::setAnalogTrigger(bool isLeft, uint8_t z)
{
z = limit_value(z, MIN_ANALOG_VALUE, MAX_ANALOG_VALUE);
z = limit_value(z, MIN_TRIGGER_VALUE, MAX_TRIGGER_VALUE);
int8_t lastZ = 0;
if (isLeft)
{
Expand Down Expand Up @@ -158,10 +159,10 @@ void UsbGamepad::updateAllReleased()
{
currentLeftAnalog[0] = 0;
currentLeftAnalog[1] = 0;
currentLeftAnalog[2] = MIN_ANALOG_VALUE;
currentLeftAnalog[2] = 0;
currentRightAnalog[0] = 0;
currentRightAnalog[1] = 0;
currentRightAnalog[2] = MIN_ANALOG_VALUE;
currentRightAnalog[2] = 0;
currentDpad[DPAD_UP] = false;
currentDpad[DPAD_DOWN] = false;
currentDpad[DPAD_LEFT] = false;
Expand Down Expand Up @@ -234,15 +235,27 @@ bool UsbGamepad::send(bool force)
}
}

typedef struct TU_ATTR_PACKED
{
int8_t x; ///< Delta x movement of left analog-stick
int8_t y; ///< Delta y movement of left analog-stick
uint8_t z; ///< Delta z movement of right analog-joystick
uint8_t rz; ///< Delta Rz movement of right analog-joystick
int8_t rx; ///< Delta Rx movement of analog left trigger
int8_t ry; ///< Delta Ry movement of analog right trigger
uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat
uint32_t buttons; ///< Buttons mask for currently pressed buttons
}dc_hid_gamepad_report_t;

uint8_t UsbGamepad::getReportSize()
{
return sizeof(hid_gamepad_report_t);
return sizeof(dc_hid_gamepad_report_t);
}

uint16_t UsbGamepad::getReport(uint8_t *buffer, uint16_t reqlen)
{
// Build the report
hid_gamepad_report_t report;
dc_hid_gamepad_report_t report;
report.x = currentLeftAnalog[0];
report.y = currentLeftAnalog[1];
report.z = currentLeftAnalog[2];
Expand Down
12 changes: 8 additions & 4 deletions src/hal/Usb/Client/Hid/UsbGamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class UsbGamepad : public UsbControllerDevice
//! Sets the analog trigger value (Z)
//! @param[in] isLeft true for left, false for right
//! @param[in] z Value between -128 and 127
void setAnalogTrigger(bool isLeft, int8_t z);
void setAnalogTrigger(bool isLeft, uint8_t z);
//! @param[in] isLeft true for left, false for right
//! @returns the current analog stick X value
int8_t getAnalogThumbX(bool isLeft);
Expand Down Expand Up @@ -118,7 +118,7 @@ class UsbGamepad : public UsbControllerDevice
private:
//! @param[in] analog The analog value to check
//! @returns true if the given analog is considered "pressed"
inline bool isAnalogPressed(int8_t analog)
inline bool isAnalogPressed(int16_t analog)
{
return (analog > ANALOG_PRESSED_TOL || analog < -ANALOG_PRESSED_TOL);
}
Expand All @@ -129,6 +129,10 @@ class UsbGamepad : public UsbControllerDevice
static const int8_t MIN_ANALOG_VALUE = -128;
//! Maximum analog value defined in USB HID descriptors
static const int8_t MAX_ANALOG_VALUE = 127;
//! Minimum trigger value defined in USB HID descriptors
static const uint8_t MIN_TRIGGER_VALUE = 0;
//! Maximum trigger value defined in USB HID descriptors
static const uint8_t MAX_TRIGGER_VALUE = 127;
//! Tolerance for when analog is considered "pressed" for status LED
static const int8_t ANALOG_PRESSED_TOL = 5;

Expand All @@ -137,9 +141,9 @@ class UsbGamepad : public UsbControllerDevice
//! The report ID to use when sending keys to host
const uint8_t reportId;
//! Current left analog states (x,y,z)
int8_t currentLeftAnalog[3];
int16_t currentLeftAnalog[3];
//! Current right analog states (x,y,z)
int8_t currentRightAnalog[3];
int16_t currentRightAnalog[3];
//! Current d-pad buttons
bool currentDpad[DPAD_COUNT];
//! Current button states
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void UsbGamepadDreamcastControllerObserver::setControllerCondition(const Control
mUsbController.setDigitalPad(UsbGamepad::DPAD_LEFT, 0 == controllerCondition.left);
mUsbController.setDigitalPad(UsbGamepad::DPAD_RIGHT, 0 == controllerCondition.right);

mUsbController.setAnalogTrigger(true, static_cast<int32_t>(controllerCondition.l) - 128);
mUsbController.setAnalogTrigger(false, static_cast<int32_t>(controllerCondition.r) - 128);
mUsbController.setAnalogTrigger(true, controllerCondition.l / 2);
mUsbController.setAnalogTrigger(false, controllerCondition.r / 2);

mUsbController.setAnalogThumbX(true, static_cast<int32_t>(controllerCondition.lAnalogLR) - 128);
mUsbController.setAnalogThumbY(true, static_cast<int32_t>(controllerCondition.lAnalogUD) - 128);
Expand Down
18 changes: 16 additions & 2 deletions src/hal/Usb/Client/Hid/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,31 @@ uint8_t get_usb_descriptor_number_of_gamepads()
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
/* Report ID if any */\
__VA_ARGS__ \
/* 8 bit X, Y, Z, Rz, Rx, Ry (min -127, max 127 ) */ \
/* 8 bit X, Y (min -128, max 127 ) */ \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
HID_LOGICAL_MIN ( 0x80 ) ,\
HID_LOGICAL_MAX ( 0x7f ) ,\
HID_REPORT_COUNT ( 2 ) ,\
HID_REPORT_SIZE ( 8 ) ,\
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
/* 8 bit Z, Rz (min 0, max 127 ) */ \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
HID_LOGICAL_MIN ( 0x00 ) ,\
HID_LOGICAL_MAX ( 0x7f ) ,\
HID_REPORT_COUNT ( 2 ) ,\
HID_REPORT_SIZE ( 8 ) ,\
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
/* 8 bit Rx, Ry (min -128, max 127 ) */ \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
HID_LOGICAL_MIN ( 0x80 ) ,\
HID_LOGICAL_MAX ( 0x7f ) ,\
HID_REPORT_COUNT ( 6 ) ,\
HID_REPORT_COUNT ( 2 ) ,\
HID_REPORT_SIZE ( 8 ) ,\
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
/* 8 bit DPad/Hat Button Map */ \
Expand Down

0 comments on commit 244b6da

Please sign in to comment.