From d11e86e9e8cd39eb5e5e270d1a29ceae8f75d255 Mon Sep 17 00:00:00 2001 From: NepEgor Date: Thu, 5 May 2022 00:20:13 +0600 Subject: [PATCH] joystick mixing --- .../include/touch_mouse_joystick.h | 4 +- .../src/touch_mouse_joystick.cpp | 28 +++--- lib/utils/include/util_func.h | 21 +++++ lib/utils/library.json | 10 +++ src/input_mapper.cpp | 85 +++++++++++++++---- 5 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 lib/utils/include/util_func.h create mode 100644 lib/utils/library.json diff --git a/lib/touch_controls/include/touch_mouse_joystick.h b/lib/touch_controls/include/touch_mouse_joystick.h index 37a55ab..60c402d 100644 --- a/lib/touch_controls/include/touch_mouse_joystick.h +++ b/lib/touch_controls/include/touch_mouse_joystick.h @@ -13,8 +13,8 @@ class TouchMouseJoystick : public TouchJoystick float trackball_vel_x; float trackball_vel_y; - float xf; - float yf; + float dx; + float dy; float time0; diff --git a/lib/touch_controls/src/touch_mouse_joystick.cpp b/lib/touch_controls/src/touch_mouse_joystick.cpp index 9353238..3bb3ba6 100644 --- a/lib/touch_controls/src/touch_mouse_joystick.cpp +++ b/lib/touch_controls/src/touch_mouse_joystick.cpp @@ -58,20 +58,20 @@ int8_t TouchMouseJoystick::touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx { touching = 2; - xf = sensitivity * -tdx; - yf = sensitivity * -tdy; + dx = sensitivity * -tdx; + dy = sensitivity * -tdy; - xf = xf > usb_r? usb_r : (xf < -usb_r? -usb_r : xf); - yf = yf > usb_r? usb_r : (yf < -usb_r? -usb_r : yf); + dx = dx > usb_r? usb_r : (dx < -usb_r? -usb_r : dx); + dy = dy > usb_r? usb_r : (dy < -usb_r? -usb_r : dy); float dt = time - time0; time0 = time; - trackball_vel_x = xf / (dt * 1000.f); - trackball_vel_y = yf / (dt * 1000.f); + trackball_vel_x = dx / (dt * 1000.f); + trackball_vel_y = dy / (dt * 1000.f); - x = xf + usb_x; - y = yf + usb_y; + x = dx + usb_x; + y = dy + usb_y; } else // in bounds outside of outer dead zone - edge spin { @@ -99,12 +99,12 @@ void TouchMouseJoystick::updateTrackball(uint32_t time) { if (trackball_vel_x != 0) { - int8_t dir = (xf > 0) ? 1 : -1; + int8_t dir = (dx > 0) ? 1 : -1; float dt = time - time0; float dt2 = dt * dt; - float x1 = xf + trackball_vel_x * dt - trackball_friction * dt2 * dir; + float x1 = dx + trackball_vel_x * dt - trackball_friction * dt2 * dir; if ((dir * x1) > usb_r) { @@ -118,19 +118,19 @@ void TouchMouseJoystick::updateTrackball(uint32_t time) else { x = usb_x; - xf = 0; + dx = 0; trackball_vel_x = 0; } } if (trackball_vel_y != 0) { - int8_t dir = (yf > 0) ? 1 : -1; + int8_t dir = (dy > 0) ? 1 : -1; float dt = time - time0; float dt2 = dt * dt; - float y1 = yf + trackball_vel_y * dt - trackball_friction * dt2 * dir; + float y1 = dy + trackball_vel_y * dt - trackball_friction * dt2 * dir; if ((dir * y1) > usb_r) { @@ -144,7 +144,7 @@ void TouchMouseJoystick::updateTrackball(uint32_t time) else { y = usb_y; - yf = 0; + dy = 0; trackball_vel_y = 0; } } diff --git a/lib/utils/include/util_func.h b/lib/utils/include/util_func.h new file mode 100644 index 0000000..25b49f0 --- /dev/null +++ b/lib/utils/include/util_func.h @@ -0,0 +1,21 @@ +#ifndef UTIL_FUNC_H +#define UTIL_FUNC_H + +template +inline X clamp(X x, M min, M max) +{ + if (x < min) + { + return min; + } + else if (x > max) + { + return max; + } + else + { + return x; + } +} + +#endif \ No newline at end of file diff --git a/lib/utils/library.json b/lib/utils/library.json new file mode 100644 index 0000000..809beba --- /dev/null +++ b/lib/utils/library.json @@ -0,0 +1,10 @@ +{ + "name": "utils", + "version": "1.0.0", + "description": "", + "keywords": "", + "dependencies": + { + "hid_def" : "*" + } +} \ No newline at end of file diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp index 9dfdd1f..d7cfe22 100644 --- a/src/input_mapper.cpp +++ b/src/input_mapper.cpp @@ -5,6 +5,8 @@ #include +#include "util_func.h" + namespace InputMapper { USB_Device device; @@ -176,12 +178,12 @@ namespace InputMapper } } } - + void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy, uint32_t time) { for (uint8_t c = 0; c < num_controls; ++c) { - int res = 0; + int8_t res = 0; switch(tcontrols[id][c]->getControlType()) { @@ -193,8 +195,6 @@ namespace InputMapper TouchJoystick* tjoy = (TouchJoystick*)tcontrols[id][c]; res = tjoy->touch(fid, x, y); - - device.joystick(tjoy->getMappedId(), tjoy->getX(), tjoy->getY()); } break; @@ -203,8 +203,6 @@ namespace InputMapper TouchMouseJoystick* tmjoy = (TouchMouseJoystick*)tcontrols[id][c]; res = tmjoy->touch(fid, x, y, dx, dy, time); - - device.joystick(tmjoy->getMappedId(), tmjoy->getX(), tmjoy->getY()); } break; @@ -220,12 +218,6 @@ namespace InputMapper } break; } - - // if control is touched return - if (res > 0) - { - return; - } } } @@ -235,8 +227,6 @@ namespace InputMapper { for (uint8_t c = 0; c < num_controls; ++c) { - int res = 0; - switch(tcontrols[id][c]->getControlType()) { case TouchControl::CT_MOUSE_JOYSTICK: @@ -244,8 +234,6 @@ namespace InputMapper TouchMouseJoystick* tmjoy = (TouchMouseJoystick*)tcontrols[id][c]; tmjoy->updateTrackball(time); - - device.joystick(tmjoy->getMappedId(), tmjoy->getX(), tmjoy->getY()); } break; @@ -295,6 +283,71 @@ namespace InputMapper device.button(it->first, it->second > 0? it->first : 0); } + int32_t x[2] = {USB_Device::usb_joystick_x, USB_Device::usb_joystick_x}; + int32_t y[2] = {USB_Device::usb_joystick_y, USB_Device::usb_joystick_y}; + int32_t dx[2] = {0, 0}; + int32_t dy[2] = {0, 0}; + uint8_t count[2] = {0, 0}; + + for (uint8_t id = 0; id < 2; ++id) + { + for (uint8_t c = 0; c < num_controls; ++c) + { + switch(tcontrols[id][c]->getControlType()) + { + case TouchControl::CT_JOYSTICK: + { + TouchJoystick* tjoy = (TouchJoystick*)tcontrols[id][c]; + if (tjoy->getTouching() > 0) + { + x[tjoy->getMappedId()] += tjoy->getX(); + y[tjoy->getMappedId()] += tjoy->getY(); + ++count[tjoy->getMappedId()]; + } + } + break; + + case TouchControl::CT_MOUSE_JOYSTICK: + { + TouchMouseJoystick* tmjoy = (TouchMouseJoystick*)tcontrols[id][c]; + if (tmjoy->getTouching() == 2) + { + dx[tmjoy->getMappedId()] += tmjoy->getX(); + dy[tmjoy->getMappedId()] += tmjoy->getY(); + } + else if (tmjoy->getTouching() == 3) // edje spin + { + x[tmjoy->getMappedId()] += tmjoy->getX(); + y[tmjoy->getMappedId()] += tmjoy->getY(); + ++count[tmjoy->getMappedId()]; + } + } + break; + + default: + break; + } + } + } + + for (int j = 0; j < 2; ++j) + { + if (count[j] > 0) + { + x[j] = x[j] / count[j] + dx[j]; + y[j] = y[j] / count[j] + dy[j]; + + clamp(x[j], USB_Device::usb_joystick_x - USB_Device::usb_joystick_r, USB_Device::usb_joystick_x + USB_Device::usb_joystick_r); + clamp(y[j], USB_Device::usb_joystick_y - USB_Device::usb_joystick_r, USB_Device::usb_joystick_y + USB_Device::usb_joystick_r); + + device.joystick(j, x[j], y[j]); + } + else + { + device.joystick(j, USB_Device::usb_joystick_x + dx[j], USB_Device::usb_joystick_y + dy[j]); + } + } + device.sendReport(); } } \ No newline at end of file