Convert touch joystick classes to unit output

circular_ranges
NepEgor 2 months ago
parent 74ff536065
commit 2856cd60fd

@ -14,14 +14,10 @@ class TouchJoystick : public TouchControl
int32_t dead_zone_outer;
int32_t dead_zone_outer2; // ^ 2
int16_t x;
int16_t y;
float x;
float y;
int16_t usb_x;
int16_t usb_y;
int16_t usb_r;
float pos2usb;
float pos2unit;
bool invert_x;
bool invert_y;
@ -31,9 +27,9 @@ class TouchJoystick : public TouchControl
public:
TouchJoystick() {}
TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r);
TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r);
void init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r);
void init(int32_t pos_x, int32_t pos_y, int32_t pos_r);
void reset();
@ -48,8 +44,8 @@ class TouchJoystick : public TouchControl
TouchState touch(int8_t fid, int32_t tx, int32_t ty);
int16_t getX() {return x;}
int16_t getY() {return y;}
float getX() {return x;}
float getY() {return y;}
};
#endif

@ -16,8 +16,6 @@ class TouchMouseJoystick : public TouchJoystick
float dx;
float dy;
int32_t usb_r2;
int16_t min_delta;
int32_t min_delta2;
@ -25,7 +23,7 @@ class TouchMouseJoystick : public TouchJoystick
public:
void init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r);
void init(int32_t pos_x, int32_t pos_y, int32_t pos_r);
void reset();

@ -2,22 +2,18 @@
#include <math.h>
TouchJoystick::TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r)
TouchJoystick::TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r)
{
init(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
init(pos_x, pos_y, pos_r);
}
void TouchJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r)
void TouchJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r)
{
TouchControl::init(pos_x, pos_y, pos_r);
this->control_type = CT_JOYSTICK;
this->usb_x = usb_x;
this->usb_y = usb_y;
this->usb_r = usb_r;
this->pos2usb = (float)usb_r / pos_r;
this->pos2unit = 1.f / pos_r;
this->dead_zone_inner = 0;
this->dead_zone_inner2 = 0;
@ -27,15 +23,15 @@ 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;
this->x = 0;
this->y = 0;
}
void TouchJoystick::reset()
{
TouchControl::reset();
this->x = usb_x;
this->y = usb_y;;
this->x = 0;
this->y = 0;
}
void TouchJoystick::setDeadZoneInner(int32_t dead_zone_inner)
@ -49,7 +45,7 @@ void TouchJoystick::setDeadZoneOuter(int32_t dead_zone_outer)
this->dead_zone_outer = pos_r - dead_zone_outer; // argument is from the outer edge, atribute is from the center
this->dead_zone_outer2 = this->dead_zone_outer * this->dead_zone_outer;
pos2usb = (float)usb_r / this->dead_zone_outer;
this->pos2unit = 1.f / this->dead_zone_outer;
}
void TouchJoystick::setInvertX(bool invert_x)
@ -78,8 +74,8 @@ TouchJoystick::TouchState TouchJoystick::touch(int8_t fid, int32_t tx, int32_t t
tx -= pos_x;
ty -= pos_y;
x = usb_x;
y = usb_y;
x = 0;
y = 0;
int32_t t2 = tx * tx + ty * ty;
@ -104,19 +100,19 @@ TouchJoystick::TouchState TouchJoystick::touch(int8_t fid, int32_t tx, int32_t t
if (t2 <= dead_zone_outer2)
{
x = tx * pos2usb + usb_x;
y = ty * pos2usb + usb_y;
x = tx * pos2unit;
y = ty * pos2unit;
}
else // in bounds outside of outer dead zone
{
float factor = dead_zone_outer / sqrt(t2) * pos2usb;
float factor = dead_zone_outer / sqrt(t2) * pos2unit;
x = tx * factor + usb_x;
y = ty * factor + usb_y;
x = tx * factor;
y = ty * factor;
}
if (invert_x) x = 2 * usb_x - x;
if (invert_y) y = 2 * usb_y - y;
if (invert_x) x = -x;
if (invert_y) y = -y;
}
}

@ -2,20 +2,18 @@
#include <math.h>
void TouchMouseJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t usb_x, int16_t usb_y, int16_t usb_r)
void TouchMouseJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r)
{
TouchJoystick::init(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
TouchJoystick::init(pos_x, pos_y, pos_r);
this->control_type = CT_MOUSE_JOYSTICK;
this->sensitivity = this->pos2usb;
this->sensitivity = this->pos2unit;
this->trackball_friction = 0;
this->trackball_vel_x = 0;
this->trackball_vel_y = 0;
this->usb_r2 = usb_r * usb_r;
this->min_delta = 0;
this->min_delta2 = 0;
@ -33,7 +31,7 @@ void TouchMouseJoystick::reset()
void TouchMouseJoystick::setSensitivity(float sensitivity)
{
this->sensitivity = this->pos2usb * sensitivity;
this->sensitivity = this->pos2unit * sensitivity;
}
void TouchMouseJoystick::setTrackballFriction(float trackball_friction)
@ -58,8 +56,8 @@ TouchMouseJoystick::TouchState TouchMouseJoystick::touch(int8_t fid, int32_t tx,
tx -= pos_x;
ty -= pos_y;
x = usb_x;
y = usb_y;
x = 0;
y = 0;
int32_t t2 = tx * tx + ty * ty;
@ -91,9 +89,9 @@ TouchMouseJoystick::TouchState TouchMouseJoystick::touch(int8_t fid, int32_t tx,
dy = dy * factor;
}
else
if (d2 > usb_r2)
if (d2 > 1.f)
{
float factor = usb_r / sqrt(d2);
float factor = 1.f / sqrt(d2);
dx = dx * factor;
dy = dy * factor;
@ -105,8 +103,8 @@ TouchMouseJoystick::TouchState TouchMouseJoystick::touch(int8_t fid, int32_t tx,
trackball_vel_x = dx / (dt * 1000.f);
trackball_vel_y = dy / (dt * 1000.f);
x = dx + usb_x;
y = dy + usb_y;
x = dx;
y = dy;
}
else // in bounds outside of outer dead zone - edge spin
{
@ -114,15 +112,15 @@ TouchMouseJoystick::TouchState TouchMouseJoystick::touch(int8_t fid, int32_t tx,
float len = sqrt(t2);
x = (tx * dead_zone_outer / len) * pos2usb + usb_x;
y = (ty * dead_zone_outer / len) * pos2usb + usb_y;
x = (tx * dead_zone_outer / len) * pos2unit;
y = (ty * dead_zone_outer / len) * pos2unit;
trackball_vel_x = 0;
trackball_vel_y = 0;
}
if (invert_x) x = 2 * usb_x - x;
if (invert_y) y = 2 * usb_y - y;
if (invert_x) x = -x;
if (invert_y) y = -y;
}
return touching;
@ -142,18 +140,18 @@ void TouchMouseJoystick::updateTrackball(uint32_t time)
float x1 = dx + trackball_vel_x * dt - trackball_friction * dt2 * dir;
if ((dir * x1) > usb_r)
if ((dir * x1) > 1.f)
{
x1 = dir * usb_r;
x1 = dir;
}
if ((dir * x1) > 0)
{
x = x1 + usb_x;
x = x1;
}
else
{
x = usb_x;
x = 0;
dx = 0;
trackball_vel_x = 0;
}
@ -168,18 +166,18 @@ void TouchMouseJoystick::updateTrackball(uint32_t time)
float y1 = dy + trackball_vel_y * dt - trackball_friction * dt2 * dir;
if ((dir * y1) > usb_r)
if ((dir * y1) > 1.f)
{
y1 = dir * usb_r;
y1 = dir;
}
if ((dir * y1) > 0)
{
y = y1 + usb_y;
y = y1;
}
else
{
y = usb_y;
y = 0;
dy = 0;
trackball_vel_y = 0;
}

@ -110,7 +110,7 @@ namespace InputMapper
int32_t dead_zone_inner = 3 * ppmX;
int32_t dead_zone_outer = 20 * ppmX;
tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
tjoystick_left.init(pos_x, pos_y, pos_r);
tjoystick_left.setDeadZoneInner(dead_zone_inner);
tjoystick_left.setDeadZoneOuter(dead_zone_outer);
tjoystick_left.setMappedId(0);
@ -118,14 +118,14 @@ namespace InputMapper
pos_x = 31.25 * ppmX;
pos_y = (103.9 - 31.25) * ppmY;
tjoystick_right_wheel.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
tjoystick_right_wheel.init(pos_x, pos_y, pos_r);
tjoystick_right_wheel.setDeadZoneInner(0);
tjoystick_right_wheel.setDeadZoneOuter(dead_zone_outer);
tjoystick_right_wheel.setMappedId(1);
dead_zone_outer = 10 * ppmX;
tjoystick_right.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
tjoystick_right.init(pos_x, pos_y, pos_r);
//tjoystick_right.setDeadZoneInner(dead_zone_inner);
tjoystick_right.setTrackballFriction(1.f / 8000000.f);
//tjoystick_right.setTrackballFriction(0);

Loading…
Cancel
Save