diff --git a/include/input_mapper.h b/include/input_mapper.h index 50599e7..c26d546 100644 --- a/include/input_mapper.h +++ b/include/input_mapper.h @@ -7,7 +7,7 @@ namespace InputMapper { 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]); diff --git a/lib/touch_controls/include/touch_control.h b/lib/touch_controls/include/touch_control.h index 0334090..b98299f 100644 --- a/lib/touch_controls/include/touch_control.h +++ b/lib/touch_controls/include/touch_control.h @@ -10,6 +10,7 @@ class TouchControl enum ControlType: uint8_t{ CT_NONE, CT_JOYSTICK, + CT_MOUSE_JOYSTICK, CT_DPAD, }; diff --git a/lib/touch_controls/include/touch_controls_all.h b/lib/touch_controls/include/touch_controls_all.h index f7fea1c..8eae490 100644 --- a/lib/touch_controls/include/touch_controls_all.h +++ b/lib/touch_controls/include/touch_controls_all.h @@ -3,6 +3,7 @@ #include "touch_control.h" #include "touch_joystick.h" +#include "touch_mouse_joystick.h" #include "touch_dpad.h" #endif \ No newline at end of file diff --git a/lib/touch_controls/include/touch_joystick.h b/lib/touch_controls/include/touch_joystick.h index d834415..59178a8 100644 --- a/lib/touch_controls/include/touch_joystick.h +++ b/lib/touch_controls/include/touch_joystick.h @@ -7,7 +7,7 @@ class TouchJoystick : public TouchControl { public: - private: + protected: int32_t dead_zone_inner; int32_t dead_zone_inner2; // ^ 2 diff --git a/lib/touch_controls/include/touch_mouse_joystick.h b/lib/touch_controls/include/touch_mouse_joystick.h new file mode 100644 index 0000000..f39bda4 --- /dev/null +++ b/lib/touch_controls/include/touch_mouse_joystick.h @@ -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 \ No newline at end of file diff --git a/lib/touch_controls/src/touch_mouse_joystick.cpp b/lib/touch_controls/src/touch_mouse_joystick.cpp new file mode 100644 index 0000000..a37e673 --- /dev/null +++ b/lib/touch_controls/src/touch_mouse_joystick.cpp @@ -0,0 +1,67 @@ +#include "touch_mouse_joystick.h" + +#include + +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; +} diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp index d862f40..d6b5ebf 100644 --- a/src/input_mapper.cpp +++ b/src/input_mapper.cpp @@ -11,7 +11,7 @@ namespace InputMapper { USB_Device device; - TouchJoystick tjoystick_right; + TouchMouseJoustick tjoystick_right; TouchJoystick tjoystick_left; TouchDpad tdpad_right; TouchDpad tdpad_left; @@ -85,9 +85,10 @@ namespace InputMapper pos_x = 31.25 * ppmX; 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.setDeadZoneInner(dead_zone_inner); + //tjoystick_right.setDeadZoneInner(dead_zone_inner); tjoystick_right.setDeadZoneOuter(dead_zone_outer); 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) { @@ -170,20 +171,31 @@ namespace InputMapper case TouchControl::CT_JOYSTICK: { - res = tcontrols[id][c]->touch(fid, x, y); - TouchJoystick* tjoy = (TouchJoystick*)tcontrols[id][c]; + + res = tjoy->touch(fid, x, y); + device.joystick(id, tjoy->getX(), tjoy->getY()); } 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: { TouchDpad* dpad = (TouchDpad*)tcontrols[id][c]; mapDpad(id, dpad->getButton(), 0); - res = tcontrols[id][c]->touch(fid, x, y); + res = dpad->touch(fid, x, y); mapDpad(id, dpad->getButton(), 1); } diff --git a/src/main.cpp b/src/main.cpp index 081eed6..783c0d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,6 +75,8 @@ void loop() { int32_t x = -1; int32_t y = -1; + int32_t dx = 0; + int32_t dy = 0; switch (tevent[i].type) { case TET_DOWN: @@ -83,15 +85,19 @@ void loop() // trackpad is rotated 90 deg so x and y are switched x = tevent[i].fp.y; y = tevent[i].fp.x; + dx = tevent[i].fp.dy; + dy = tevent[i].fp.dx; // invert axis for the trackpads if(t == 0) { y = trackpad_maxX - y; + dy = -dy; } else { x = trackpad_maxY - x; + dx = -dx; } break; @@ -104,7 +110,7 @@ void loop() break; } - InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y); + InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y, dx, dy); } } }