Added mouse joystick

gyro
NepEgor 2 years ago
parent da4fbef4c7
commit 7f4be55ecf

@ -7,7 +7,7 @@ namespace InputMapper
{ {
void begin(); void begin();
void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y); void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy);
void mapTriggers(uint32_t value[2]); void mapTriggers(uint32_t value[2]);

@ -10,6 +10,7 @@ class TouchControl
enum ControlType: uint8_t{ enum ControlType: uint8_t{
CT_NONE, CT_NONE,
CT_JOYSTICK, CT_JOYSTICK,
CT_MOUSE_JOYSTICK,
CT_DPAD, CT_DPAD,
}; };

@ -3,6 +3,7 @@
#include "touch_control.h" #include "touch_control.h"
#include "touch_joystick.h" #include "touch_joystick.h"
#include "touch_mouse_joystick.h"
#include "touch_dpad.h" #include "touch_dpad.h"
#endif #endif

@ -7,7 +7,7 @@ class TouchJoystick : public TouchControl
{ {
public: public:
private: protected:
int32_t dead_zone_inner; int32_t dead_zone_inner;
int32_t dead_zone_inner2; // ^ 2 int32_t dead_zone_inner2; // ^ 2

@ -0,0 +1,22 @@
#ifndef TOUCH_MOUSE_JOYSTICK_H
#define TOUCH_MOUSE_JOYSTICK_H
#include "touch_joystick.h"
class TouchMouseJoustick : public TouchJoystick
{
protected:
float delta2usb;
float trackball_friction;
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 setTrackballFriction(float trackball_friction);
int8_t touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx, int32_t tdy);
};
#endif

@ -0,0 +1,67 @@
#include "touch_mouse_joystick.h"
#include <math.h>
void TouchMouseJoustick::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)
{
TouchJoystick::init(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
this->control_type = CT_MOUSE_JOYSTICK;
this->delta2usb = this->pos2usb * 30;
}
void TouchMouseJoustick::setTrackballFriction(float trackball_friction)
{
this->trackball_friction = trackball_friction;
}
int8_t TouchMouseJoustick::touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx, int32_t tdy)
{
if (finger_id != -1 && finger_id != fid)
{
return 0;
}
int8_t ret = 2;
tx -= pos_x;
ty -= pos_y;
x = usb_x;
y = usb_y;
int32_t t2 = tx * tx + ty * ty;
// outside the range
if (t2 > pos_r2)
{
finger_id = -1;
return 0;
}
else // inside the range
{
finger_id = fid;
if (t2 <= dead_zone_outer2)
{
int32_t x32 = -tdx * delta2usb + usb_x;
int32_t y32 = -tdy * delta2usb + usb_y;
x = x32 > usb_r? usb_r : (x32 < -usb_r? -usb_r : x32);
y = y32 > usb_r? usb_r : (y32 < -usb_r? -usb_r : y32);
}
else // in bounds outside of outer dead zone - edge spin
{
float len = sqrt(t2);
x = (tx * dead_zone_outer / len) * pos2usb + usb_x;
y = (ty * dead_zone_outer / len) * pos2usb + usb_y;
}
if (invert_x) x = 2 * usb_x - x;
if (invert_y) y = 2 * usb_y - y;
}
return ret;
}

@ -11,7 +11,7 @@ namespace InputMapper
{ {
USB_Device device; USB_Device device;
TouchJoystick tjoystick_right; TouchMouseJoustick tjoystick_right;
TouchJoystick tjoystick_left; TouchJoystick tjoystick_left;
TouchDpad tdpad_right; TouchDpad tdpad_right;
TouchDpad tdpad_left; TouchDpad tdpad_left;
@ -85,9 +85,10 @@ namespace InputMapper
pos_x = 31.25 * ppmX; pos_x = 31.25 * ppmX;
pos_y = (103.9 - 31.25) * ppmY; pos_y = (103.9 - 31.25) * ppmY;
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, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
tjoystick_right.setDeadZoneInner(dead_zone_inner); //tjoystick_right.setDeadZoneInner(dead_zone_inner);
tjoystick_right.setDeadZoneOuter(dead_zone_outer); tjoystick_right.setDeadZoneOuter(dead_zone_outer);
pos_x = 20.636 * ppmX; pos_x = 20.636 * ppmX;
@ -157,7 +158,7 @@ namespace InputMapper
} }
} }
void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y) void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy)
{ {
for (uint8_t c = 0; c < num_controls; ++c) for (uint8_t c = 0; c < num_controls; ++c)
{ {
@ -170,20 +171,31 @@ namespace InputMapper
case TouchControl::CT_JOYSTICK: case TouchControl::CT_JOYSTICK:
{ {
res = tcontrols[id][c]->touch(fid, x, y);
TouchJoystick* tjoy = (TouchJoystick*)tcontrols[id][c]; TouchJoystick* tjoy = (TouchJoystick*)tcontrols[id][c];
res = tjoy->touch(fid, x, y);
device.joystick(id, tjoy->getX(), tjoy->getY()); device.joystick(id, tjoy->getX(), tjoy->getY());
} }
break; break;
case TouchControl::CT_MOUSE_JOYSTICK:
{
TouchMouseJoustick* tmjoy = (TouchMouseJoustick*)tcontrols[id][c];
res = tmjoy->touch(fid, x, y, dx, dy);
device.joystick(id, tmjoy->getX(), tmjoy->getY());
}
break;
case TouchControl::CT_DPAD: case TouchControl::CT_DPAD:
{ {
TouchDpad* dpad = (TouchDpad*)tcontrols[id][c]; TouchDpad* dpad = (TouchDpad*)tcontrols[id][c];
mapDpad(id, dpad->getButton(), 0); mapDpad(id, dpad->getButton(), 0);
res = tcontrols[id][c]->touch(fid, x, y); res = dpad->touch(fid, x, y);
mapDpad(id, dpad->getButton(), 1); mapDpad(id, dpad->getButton(), 1);
} }

@ -75,6 +75,8 @@ void loop()
{ {
int32_t x = -1; int32_t x = -1;
int32_t y = -1; int32_t y = -1;
int32_t dx = 0;
int32_t dy = 0;
switch (tevent[i].type) switch (tevent[i].type)
{ {
case TET_DOWN: case TET_DOWN:
@ -83,15 +85,19 @@ void loop()
// trackpad is rotated 90 deg so x and y are switched // trackpad is rotated 90 deg so x and y are switched
x = tevent[i].fp.y; x = tevent[i].fp.y;
y = tevent[i].fp.x; y = tevent[i].fp.x;
dx = tevent[i].fp.dy;
dy = tevent[i].fp.dx;
// invert axis for the trackpads // invert axis for the trackpads
if(t == 0) if(t == 0)
{ {
y = trackpad_maxX - y; y = trackpad_maxX - y;
dy = -dy;
} }
else else
{ {
x = trackpad_maxY - x; x = trackpad_maxY - x;
dx = -dx;
} }
break; break;
@ -104,7 +110,7 @@ void loop()
break; break;
} }
InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y); InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y, dx, dy);
} }
} }
} }

Loading…
Cancel
Save