From 268447b1525014df0f66eac86b044991eb7bc341 Mon Sep 17 00:00:00 2001 From: NepEgor Date: Sat, 13 Nov 2021 00:15:35 +0300 Subject: [PATCH] Swithced dpad output to xinput scancodes; fixed joystick inversion --- lib/touch_controls/include/touch_dpad.h | 2 -- lib/touch_controls/src/touch_dpad.cpp | 39 +++++++++++++---------- lib/touch_controls/src/touch_joystick.cpp | 7 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/touch_controls/include/touch_dpad.h b/lib/touch_controls/include/touch_dpad.h index ada8b43..2132e17 100644 --- a/lib/touch_controls/include/touch_dpad.h +++ b/lib/touch_controls/include/touch_dpad.h @@ -13,8 +13,6 @@ class TouchDpad : public TouchControl DPAD_TYPE_SECTOR8 }; - static const uint8_t NOT_PRESSED = 15; - private: int32_t dead_zone_inner; diff --git a/lib/touch_controls/src/touch_dpad.cpp b/lib/touch_controls/src/touch_dpad.cpp index 5b92523..5416191 100644 --- a/lib/touch_controls/src/touch_dpad.cpp +++ b/lib/touch_controls/src/touch_dpad.cpp @@ -27,6 +27,8 @@ void TouchDpad::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_ this->invert_x = 1; this->invert_y = 1; + + this->button = 0; } void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner) @@ -45,6 +47,11 @@ void TouchDpad::setInvertY(bool invert_y) this->invert_y = invert_y ? -1 : 1; } +#define XINPUT_DPAD_UP 0b0001 +#define XINPUT_DPAD_DOWN 0b0010 +#define XINPUT_DPAD_LEFT 0b0100 +#define XINPUT_DPAD_RIGHT 0b1000 + int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) { if (finger_id != -1 && finger_id != fid) @@ -57,7 +64,7 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) tx -= pos_x; ty -= pos_y; - button = NOT_PRESSED; + button = 0; int32_t t2 = tx * tx + ty * ty; @@ -77,8 +84,6 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) } else // in bounds { - button = 0; - switch (dpad_type) { case DPAD_TYPE_SECTOR4: @@ -87,12 +92,12 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) switch (button) { - case 0b00: button = 0; break; - case 0b01: button = 2; break; - case 0b11: button = 4; break; - case 0b10: button = 6; break; + case 0b00: button = XINPUT_DPAD_UP; break; + case 0b01: button = XINPUT_DPAD_RIGHT; break; + case 0b11: button = XINPUT_DPAD_DOWN; break; + case 0b10: button = XINPUT_DPAD_LEFT; break; - default: button = NOT_PRESSED; break; + default: button = 0; break; } break; @@ -105,16 +110,16 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) switch (button) { - case 0: button = 0; break; - case 1: button = 1; break; - case 3: button = 2; break; - case 7: button = 3; break; - case 15: button = 4; break; - case 14: button = 5; break; - case 12: button = 6; break; - case 8: button = 7; break; + case 0: button = XINPUT_DPAD_UP; break; + case 1: button = XINPUT_DPAD_UP | XINPUT_DPAD_RIGHT; break; + case 3: button = XINPUT_DPAD_RIGHT; break; + case 7: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_RIGHT; break; + case 15: button = XINPUT_DPAD_DOWN; break; + case 14: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_LEFT; break; + case 12: button = XINPUT_DPAD_LEFT; break; + case 8: button = XINPUT_DPAD_UP | XINPUT_DPAD_LEFT; break; - default: button = NOT_PRESSED; break; + default: button = 0; break; } break; diff --git a/lib/touch_controls/src/touch_joystick.cpp b/lib/touch_controls/src/touch_joystick.cpp index 6a5fe60..2b47d44 100644 --- a/lib/touch_controls/src/touch_joystick.cpp +++ b/lib/touch_controls/src/touch_joystick.cpp @@ -26,6 +26,9 @@ void TouchJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t us this->invert_x = false; this->invert_y = false; + + this->x = usb_x; + this->y = usb_y; } void TouchJoystick::setDeadZoneInner(int32_t dead_zone_inner) @@ -97,8 +100,8 @@ int8_t TouchJoystick::touch(int8_t fid, int32_t tx, int32_t ty) y = (ty * dead_zone_outer / len) * pos2usb + usb_y; } - if (invert_x) x = usb_x + usb_r - x; - if (invert_y) y = usb_y + usb_r - y; + if (invert_x) x = 2 * usb_x - x; + if (invert_y) y = 2 * usb_y - y; } return ret;