Fixed dead zones; tweaked joystick usb parameters

input_events
NepEgor 3 years ago
parent 30df99c1ae
commit fe1de544fa

@ -6,7 +6,7 @@
#define USB_HID_CUSTOM_CONFIG_DESC_SIZ 34U
#define USB_HID_CUSTOM_DESC_SIZ 9U
#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
#define HID_CUSTOM_REPORT_DESC_SIZE 30U
#define HID_CUSTOM_REPORT_DESC_SIZE 34U
//#define HID_KEYBOARD_REPORT_DESC_SIZE 45U

@ -23,23 +23,25 @@ class TouchJoystick
int16_t x;
int16_t y;
int16_t min_x;
int16_t min_y;
int16_t max_x;
int16_t max_y;
int16_t cen_x;
int16_t cen_y;
int16_t usb_x;
int16_t usb_y;
int16_t usb_r;
float pos2usb_x;
float pos2usb_y;
float pos2usb;
bool invert_x;
bool invert_y;
public:
TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t min_x, int16_t min_y, int16_t max_x, int16_t max_y);
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);
void setDeadZoneInner(int32_t dead_zone_inner);
void setDeadZoneOuter(int32_t dead_zone_outer);
void setInvertX(bool invert_x = true);
void setInvertY(bool invert_y = true);
uint8_t touch(int32_t tx, int32_t ty, int16_t* rx = NULL, int16_t* ry = NULL);
int16_t getX() {return x;}

@ -1,27 +1,27 @@
#include "touch_joystick.h"
TouchJoystick::TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t min_x, int16_t min_y, int16_t max_x, int16_t max_y)
#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)
{
this->pos_x = pos_x;
this->pos_y = pos_y;
this->pos_r = pos_r;
this->pos_r2 = pos_r * pos_r;
this->min_x = min_x;
this->min_y = min_y;
this->max_x = max_x;
this->max_y = max_y;
this->cen_x = (max_x - min_x) / 2;
this->cen_y = (max_y - min_y) / 2;
this->usb_x = usb_x;
this->usb_y = usb_y;
this->usb_r = usb_r;
this->pos2usb_x = (float)(max_x - min_x) / pos_r;
this->pos2usb_y = (float)(max_y - min_y) / pos_r;
this->pos2usb = (float)usb_r / pos_r;
this->dead_zone_inner = 0;
this->dead_zone_inner2 = 0;
this->dead_zone_outer = pos_r;
this->dead_zone_outer2 = pos_r2;
this->invert_x = false;
this->invert_y = false;
}
void TouchJoystick::setDeadZoneInner(int32_t dead_zone_inner)
@ -35,8 +35,17 @@ 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_x = (float)(max_x - min_x) * pos_r / this->dead_zone_outer;
pos2usb_y = (float)(max_y - min_y) * pos_r / this->dead_zone_outer;
pos2usb = (float)usb_r / this->dead_zone_outer;
}
void TouchJoystick::setInvertX(bool invert_x)
{
this->invert_x = invert_x;
}
void TouchJoystick::setInvertY(bool invert_y)
{
this->invert_y = invert_y;
}
uint8_t TouchJoystick::touch(int32_t tx, int32_t ty, int16_t* rx, int16_t* ry)
@ -46,29 +55,33 @@ uint8_t TouchJoystick::touch(int32_t tx, int32_t ty, int16_t* rx, int16_t* ry)
tx -= pos_x;
ty -= pos_y;
int32_t tx2 = tx * tx;
int32_t ty2 = ty * ty;
int32_t t2 = tx * tx + ty * ty;
// if inside inner dead_zone or outside the range
if (tx2 + ty2 < dead_zone_inner2 || tx2 + ty2 > pos_r2)
if (t2 < dead_zone_inner2 || t2 > pos_r2)
{
x = cen_x;
y = cen_y;
x = usb_x;
y = usb_y;
ret = -1;
}
else // between dead zones
if (tx2 + ty2 <= dead_zone_outer2)
if (t2 <= dead_zone_outer2)
{
x = tx * pos2usb_x + min_x;
y = ty * pos2usb_y + min_y;
x = tx * pos2usb + usb_x;
y = ty * pos2usb + usb_y;
}
else // in bounds outside of outer dead zone
{
x = tx * pos2usb_x + min_x; // TODO dead zone
y = ty * pos2usb_y + min_y;
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 = usb_x + usb_r - x;
if (invert_y) y = usb_y + usb_r - y;
if (rx != NULL && ry != NULL)
{
*rx = x;

@ -24,9 +24,12 @@ const uint8_t CLOCK_PIN_left = PB6;
const int32_t pos_x = 31.25 * 1872.0 / 62.5;
const int32_t pos_y = (103.9 - 31.25) * 3276.0 / 103.9;
const int32_t pos_r = 60 * 1872.0 / 62.5 / 2;
const int16_t max_x = 0x7FFF;
const int16_t max_y = -0x7FFF;
TouchJoystick tjoystick(pos_x, pos_y, pos_r, max_x, max_y);
const int32_t dead_zone_inner = 3 * 1872.0 / 62.5 / 2;
const int32_t dead_zone_outer = 20 * 1872.0 / 62.5 / 2;
const int16_t usb_x = 512;
const int16_t usb_y = 512;
const int16_t usb_r = 512;
TouchJoystick tjoystick(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
typedef struct
{
@ -50,13 +53,17 @@ void setup()
//device.begin();
HID_Custom_Init();
tjoystick.setDeadZoneInner(dead_zone_inner);
tjoystick.setDeadZoneOuter(dead_zone_outer);
tjoystick.setInvertY(true);
testReport = { 0, 0, 0 };
attachInterrupt(CLOCK_PIN_right, int_touchpad_right, FALLING);
trackpad_right.initialize(CLOCK_PIN_right, DATA_PIN_right);
HID_Custom_Init();
//attachInterrupt(CLOCK_PIN_left, int_touchpad_left, FALLING);
//trackpad_left.initialize(CLOCK_PIN_left, DATA_PIN_left);
@ -89,8 +96,8 @@ void loop()
}
else if (fingers_touching == 0)
{
testReport.x = 0;
testReport.y = 0;
testReport.x = usb_x;
testReport.y = usb_y;
}
//Serial.printf("%u %u %u\n", testReport.x, testReport.y, testReport.r);

@ -178,12 +178,13 @@ __ALIGN_BEGIN uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE] __ALIG
HID_USAGE(X),
HID_USAGE(Y),
HID_USAGE(Z),
HID_LOGICAL_MINIMUM(2, -32767),
HID_LOGICAL_MAXIMUM(2, 32767),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(2, 1024),
HID_PHYSICAL_MINIMUM(1, 0),
HID_PHYSICAL_MAXIMUM(2, 1024),
HID_REPORT_SIZE(16),
HID_REPORT_COUNT(3),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_END_COLLECTION(PHYSICAL),
//HID_END_COLLECTION(APPLICATION)
0xC0
HID_END_COLLECTION(APPLICATION)
};

@ -10,10 +10,11 @@ int main()
int32_t pos_y = 350;
int32_t pos_r = 2000;
int16_t max_x = 1023;
int16_t max_y = 1023;
int16_t usb_x = 512;
int16_t usb_y = 512;
int16_t usb_r = 512;
TouchJoystick tjoystick(pos_x, pos_y, pos_r, max_x, max_y);
TouchJoystick tjoystick(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
int32_t touch_x = 10000;
int32_t touch_y = 200;

Loading…
Cancel
Save