joystick mixing

gyro
NepEgor 2 years ago
parent b120ad07ee
commit d11e86e9e8

@ -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;

@ -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;
}
}

@ -0,0 +1,21 @@
#ifndef UTIL_FUNC_H
#define UTIL_FUNC_H
template<class X, class M>
inline X clamp(X x, M min, M max)
{
if (x < min)
{
return min;
}
else if (x > max)
{
return max;
}
else
{
return x;
}
}
#endif

@ -0,0 +1,10 @@
{
"name": "utils",
"version": "1.0.0",
"description": "",
"keywords": "",
"dependencies":
{
"hid_def" : "*"
}
}

@ -5,6 +5,8 @@
#include <map>
#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();
}
}
Loading…
Cancel
Save