trackball initial

gyro
NepEgor 2 years ago
parent 1328adb0e9
commit e94f737446

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

@ -25,6 +25,8 @@ class TouchControl
int8_t finger_id;
int8_t touching;
public:
TouchControl() {}
@ -34,6 +36,8 @@ class TouchControl
virtual int8_t touch(int8_t fid, int32_t tx, int32_t ty) = 0;
int8_t getTouching() {return touching;}
ControlType getControlType() {return control_type;}
};

@ -10,6 +10,14 @@ class TouchMouseJoustick : public TouchJoystick
float sensitivity;
float trackball_friction;
float trackball_vel_x;
float trackball_vel_y;
float xf;
float yf;
float time0;
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);
@ -18,7 +26,9 @@ class TouchMouseJoustick : public TouchJoystick
void setTrackballFriction(float trackball_friction);
int8_t touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx, int32_t tdy);
int8_t touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx, int32_t tdy, uint32_t time);
void updateTrackball(uint32_t time);
};
#endif

@ -15,4 +15,6 @@ void TouchControl::init(int32_t pos_x, int32_t pos_y, int32_t pos_r)
this->control_type = CT_NONE;
this->finger_id = -1;
this->touching = 0;
}

@ -56,11 +56,10 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty)
{
if (finger_id != -1 && finger_id != fid)
{
touching = 0;
return 0;
}
int8_t ret = 2;
tx -= pos_x;
ty -= pos_y;
@ -72,6 +71,7 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty)
if (t2 > pos_r2)
{
finger_id = -1;
touching = 0;
return 0;
}
else // inside inner dead_zone
@ -80,10 +80,12 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty)
if (t2 < dead_zone_inner2)
{
ret = 1;
touching = 1;
}
else // in bounds
{
touching = 2;
switch (dpad_type)
{
case DPAD_TYPE_SECTOR_4:
@ -130,5 +132,5 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty)
}
}
return ret;
return touching;
}

@ -59,11 +59,10 @@ int8_t TouchJoystick::touch(int8_t fid, int32_t tx, int32_t ty)
{
if (finger_id != -1 && finger_id != fid)
{
touching = 0;
return 0;
}
int8_t ret = 2;
tx -= pos_x;
ty -= pos_y;
@ -76,6 +75,7 @@ int8_t TouchJoystick::touch(int8_t fid, int32_t tx, int32_t ty)
if (t2 > pos_r2)
{
finger_id = -1;
touching = 0;
return 0;
}
else // inside inner dead_zone
@ -84,25 +84,29 @@ int8_t TouchJoystick::touch(int8_t fid, int32_t tx, int32_t ty)
if (t2 < dead_zone_inner2)
{
ret = 1;
touching = 1;
}
else // between dead zones
if (t2 <= dead_zone_outer2)
{
x = tx * pos2usb + usb_x;
y = ty * pos2usb + usb_y;
touching = 2;
if (t2 <= dead_zone_outer2)
{
x = tx * pos2usb + usb_x;
y = ty * pos2usb + usb_y;
}
else // in bounds outside of outer dead zone
{
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;
}
else // in bounds outside of outer dead zone
{
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;
return touching;
}

@ -9,6 +9,12 @@ void TouchMouseJoustick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16
this->control_type = CT_MOUSE_JOYSTICK;
this->sensitivity = this->pos2usb;
this->trackball_friction = 0;
this->trackball_vel_x = 0;
this->trackball_vel_y = 0;
this->time0 = 0;
}
void TouchMouseJoustick::setSensitivity(float sensitivity)
@ -21,15 +27,14 @@ 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)
int8_t TouchMouseJoustick::touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx, int32_t tdy, uint32_t time)
{
if (finger_id != -1 && finger_id != fid)
{
touching = 0;
return 0;
}
int8_t ret = 2;
tx -= pos_x;
ty -= pos_y;
@ -42,6 +47,7 @@ int8_t TouchMouseJoustick::touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx
if (t2 > pos_r2)
{
finger_id = -1;
touching = 0;
return 0;
}
else // inside the range
@ -50,26 +56,69 @@ int8_t TouchMouseJoustick::touch(int8_t fid, int32_t tx, int32_t ty, int32_t tdx
if (t2 <= dead_zone_outer2)
{
int32_t x32 = -tdx * sensitivity;
int32_t y32 = -tdy * sensitivity;
touching = 2;
xf = sensitivity * -tdx;
yf = sensitivity * -tdy;
x = x32 > usb_r? usb_r : (x32 < -usb_r? -usb_r : x32);
y = y32 > usb_r? usb_r : (y32 < -usb_r? -usb_r : y32);
xf = xf > usb_r? usb_r : (xf < -usb_r? -usb_r : xf);
yf = yf > usb_r? usb_r : (yf < -usb_r? -usb_r : yf);
x + usb_x;
y + usb_y;
float dt = time - time0;
time0 = time;
trackball_vel_x = xf / (dt * 1000.f);
trackball_vel_y = yf / (dt * 1000.f);
x = xf + usb_x;
y = yf + usb_y;
}
else // in bounds outside of outer dead zone - edge spin
{
touching = 3;
float len = sqrt(t2);
x = (tx * dead_zone_outer / len) * pos2usb + usb_x;
y = (ty * dead_zone_outer / len) * pos2usb + usb_y;
trackball_vel_x = 0;
trackball_vel_y = 0;
}
if (invert_x) x = 2 * usb_x - x;
if (invert_y) y = 2 * usb_y - y;
}
return ret;
return touching;
}
void TouchMouseJoustick::updateTrackball(uint32_t time)
{
if ((trackball_friction > 0) && !touching && (trackball_vel_x > 0))
{
if (xf > usb_x)
{
float dt = time - time0;
float dt2 = dt * dt;
float x1 = xf + trackball_vel_x * dt - trackball_friction * dt2;
if (x1 > usb_r)
{
x1 = usb_r;
}
x = usb_x + x1;
if (x < usb_x)
{
x = usb_x;
xf = usb_x;
}
}
else
{
x = usb_x;
xf = usb_x;
}
}
}

@ -5,8 +5,6 @@
#include <map>
#include <Arduino.h>
namespace InputMapper
{
USB_Device device;
@ -105,6 +103,8 @@ namespace InputMapper
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.setTrackballFriction(1.f / 4000000.f);
//tjoystick_right.setTrackballFriction(0);
tjoystick_right.setDeadZoneOuter(dead_zone_outer);
tjoystick_right.setSensitivity(10);
@ -175,7 +175,7 @@ namespace InputMapper
}
}
void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy)
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)
{
@ -200,7 +200,7 @@ namespace InputMapper
{
TouchMouseJoustick* tmjoy = (TouchMouseJoustick*)tcontrols[id][c];
res = tmjoy->touch(fid, x, y, dx, dy);
res = tmjoy->touch(fid, x, y, dx, dy, time);
device.joystick(id, tmjoy->getX(), tmjoy->getY());
}
@ -227,6 +227,34 @@ namespace InputMapper
}
}
void update(uint32_t time)
{
for (uint8_t id = 0; id < 2; ++id)
{
for (uint8_t c = 0; c < num_controls; ++c)
{
int res = 0;
switch(tcontrols[id][c]->getControlType())
{
case TouchControl::CT_MOUSE_JOYSTICK:
{
TouchMouseJoustick* tmjoy = (TouchMouseJoustick*)tcontrols[id][c];
tmjoy->updateTrackball(time);
device.joystick(id, tmjoy->getX(), tmjoy->getY());
}
break;
default:
break;
}
}
}
}
void mapTriggers(uint32_t value[2])
{
static const uint32_t max = 70;

@ -110,11 +110,13 @@ void loop()
break;
}
InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y, dx, dy);
InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y, dx, dy, micros());
}
}
}
InputMapper::update(micros());
uint32_t triggers[] = {analogRead(pin_trigger[0]), analogRead(pin_trigger[1])};
InputMapper::mapTriggers(triggers);

Loading…
Cancel
Save