Skip to content

Commit 45104e2

Browse files
committed
Add control data 'get' functions
1 parent 89e318f commit 45104e2

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/XInput.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,34 @@ void XInputGamepad::releaseAll() {
249249
memset(tx + offset, 0x00, sizeof(tx) - offset); // Clear TX array
250250
}
251251

252+
boolean XInputGamepad::getButton(XInputControl button) {
253+
const XInputMap_Button * buttonData = getButtonFromEnum(button);
254+
if (buttonData == nullptr) return 0; // Not a button
255+
return tx[buttonData->index] & buttonData->mask;
256+
}
257+
258+
boolean XInputGamepad::getDpad(XInputControl dpad) {
259+
return getButton(dpad);
260+
}
261+
262+
uint8_t XInputGamepad::getTrigger(XInputControl trigger) {
263+
const XInputMap_Trigger * triggerData = getTriggerFromEnum(trigger);
264+
if (triggerData == nullptr) return 0; // Not a trigger
265+
return tx[triggerData->index];
266+
}
267+
268+
int16_t XInputGamepad::getJoystickX(XInputControl joy) {
269+
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
270+
if (joyData == nullptr) return 0; // Not a joystick
271+
return (tx[joyData->x_high] << 8) | tx[joyData->x_low];
272+
}
273+
274+
int16_t XInputGamepad::getJoystickY(XInputControl joy) {
275+
const XInputMap_Joystick * joyData = getJoyFromEnum(joy);
276+
if (joyData == nullptr) return 0; // Not a joystick
277+
return (tx[joyData->y_high] << 8) | tx[joyData->y_low];
278+
}
279+
252280
uint8_t XInputGamepad::getPlayer() const {
253281
return player;
254282
}

src/XInput.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class XInputGamepad {
8787

8888
void releaseAll();
8989

90+
boolean getButton(XInputControl button);
91+
boolean getDpad(XInputControl dpad);
92+
uint8_t getTrigger(XInputControl trigger);
93+
int16_t getJoystickX(XInputControl joy);
94+
int16_t getJoystickY(XInputControl joy);
95+
9096
// Received Data
9197
uint8_t getPlayer() const; // Player # assigned to the controller (0 is unassigned)
9298
uint16_t getRumble() const; // Rumble motors. MSB is large weight, LSB is small

0 commit comments

Comments
 (0)