From 6cd618e9d3086e8db3a44420f4b6120bb403155b Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 7 Mar 2025 15:54:13 -0500 Subject: [PATCH] Fixed incorrect assumptions about thumbstick and trigger axis indices. Working on mappings to common controller layouts. --- main.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index aacfc60..e168fd4 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,21 @@ #include #include +enum class XBoxButton : uint8_t +{ + ButtonA = 0, + ButtonB = 1, + ButtonX = 2, + ButtonY = 3, + BumperL = 4, + BumperR = 5, + Back = 6, + Start = 7, + TheBigOne = 8, + ThumbL = 9, + ThumbR = 10, +}; + /// Reads a joystick event from the joystick device. /// @returns 0 on success. Otherwise -1 is returned. int read_event(int fd, struct js_event *event) @@ -61,18 +76,51 @@ size_t get_button_count(int fd) } /// Current state of an axis. -struct axis_state { - short x, y; + + +struct Axes { + short LeftThumbX; + short LeftThumbY; + short RightThumbX; + short RightThumbY; + short DPadX; + short DPadY; + short LeftTrigger; + short RightTrigger; }; + /// Keeps track of the current axis state. /// @note This function assumes that axes are numbered starting from 0, and that /// the X axis is an even number, and the Y axis is an odd number. However, this /// is usually a safe assumption. /// @returns the axis that the event indicated. -size_t get_axis_state(struct js_event *event, struct axis_state axes[3]) +size_t get_axis_state(struct js_event *event, struct Axes axes) { - size_t axis = event->number / 2; + /// Mappings on my device, meaning these assumptions are incorrect. + // LStick X = 0 + // LStick Y = 1 + // LTrigger = 2 + // RStick X = 3 + // RStick Y = 4 + // RTrigger = 5 + // DirPad X = 6 + // DirPad Y = 7 + + size_t axis = event->number; + short value = event->value; + + // Appear to be mappings for XBox + if (axis == 0) axes.LeftThumbX = value; + if (axis == 1) axes.LeftThumbY = value; + if (axis == 2) axes.LeftTrigger = value; + if (axis == 3) axes.RightThumbX = value; + if (axis == 4) axes.RightThumbY = value; + if (axis == 5) axes.RightTrigger = value; + if (axis == 6) axes.DPadX = value; + if (axis == 7) axes.DPadY = value; + + /*size_t axis = event->number / 2; if (axis < 3) { @@ -82,7 +130,7 @@ size_t get_axis_state(struct js_event *event, struct axis_state axes[3]) axes[axis].y = event->value; } - return axis; + return axis;*/ } uint8_t get_num_axes(int joystick_handle) { @@ -135,23 +183,26 @@ int main(int argc, char *argv[]) const char *device; struct js_event event; - struct axis_state axes[3] = {0}; + struct Axes axes; size_t axis; - //if (argc > 1) - // device = argv[1]; - //else - // device = "/dev/input/js0"; - // - // js = open(device, O_NONBLOCK); + if (argc > 1) + device = argv[1]; + else + device = "/dev/input/js0"; + + js = open(device, O_NONBLOCK); if (js == -1) perror("Could not open joystick"); + else + { + std::cout << get_joystick_name(js) << std::endl; + std::cout << get_joystick_driver_ver(js) << std::endl; + std::cout << get_axis_count(js) << std::endl; + } - std::cout << get_joystick_name(js) << std::endl; - std::cout << get_joystick_driver_ver(js) << std::endl; - std::cout << get_axis_count(js) << std::endl; /// This loop will exit if the controller is unplugged.