Handling input events from the trackpads

input_events
NepEgor 3 years ago
parent 1aab181022
commit 5d4c0ad2f3

@ -22,6 +22,8 @@ class TouchControl
ControlType control_type; ControlType control_type;
int8_t finger_id;
public: public:
TouchControl() {} TouchControl() {}
@ -29,7 +31,7 @@ class TouchControl
virtual void init(int32_t pos_x, int32_t pos_y, int32_t pos_r); virtual void init(int32_t pos_x, int32_t pos_y, int32_t pos_r);
virtual int8_t touch(int32_t tx, int32_t ty) = 0; virtual int8_t touch(int8_t fid, int32_t tx, int32_t ty) = 0;
ControlType getControlType() {return control_type;} ControlType getControlType() {return control_type;}
}; };

@ -41,7 +41,7 @@ class TouchDpad : public TouchControl
void setInvertX(bool invert_x = true); void setInvertX(bool invert_x = true);
void setInvertY(bool invert_y = true); void setInvertY(bool invert_y = true);
int8_t touch(int32_t tx, int32_t ty); int8_t touch(int8_t fid, int32_t tx, int32_t ty);
uint8_t getButton() {return button;} uint8_t getButton() {return button;}
}; };

@ -39,7 +39,7 @@ class TouchJoystick : public TouchControl
void setInvertX(bool invert_x = true); void setInvertX(bool invert_x = true);
void setInvertY(bool invert_y = true); void setInvertY(bool invert_y = true);
int8_t touch(int32_t tx, int32_t ty); int8_t touch(int8_t fid, int32_t tx, int32_t ty);
int16_t getX() {return x;} int16_t getX() {return x;}
int16_t getY() {return y;} int16_t getY() {return y;}

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

@ -45,8 +45,13 @@ void TouchDpad::setInvertY(bool invert_y)
this->invert_y = invert_y ? -1 : 1; this->invert_y = invert_y ? -1 : 1;
} }
int8_t TouchDpad::touch(int32_t tx, int32_t ty) int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty)
{ {
if (finger_id != -1 && finger_id != fid)
{
return 0;
}
int8_t ret = 2; int8_t ret = 2;
tx -= pos_x; tx -= pos_x;
@ -59,9 +64,13 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
// outside the range // outside the range
if (t2 > pos_r2) if (t2 > pos_r2)
{ {
ret = 0; finger_id = -1;
return 0;
} }
else // inside inner dead_zone else // inside inner dead_zone
{
finger_id = fid;
if (t2 < dead_zone_inner2) if (t2 < dead_zone_inner2)
{ {
ret = 1; ret = 1;
@ -114,6 +123,7 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
break; break;
} }
} }
}
return ret; return ret;
} }

@ -52,24 +52,33 @@ void TouchJoystick::setInvertY(bool invert_y)
this->invert_y = invert_y; this->invert_y = invert_y;
} }
int8_t TouchJoystick::touch(int32_t tx, int32_t ty) int8_t TouchJoystick::touch(int8_t fid, int32_t tx, int32_t ty)
{ {
if (finger_id != -1 && finger_id != fid)
{
return 0;
}
int8_t ret = 2; int8_t ret = 2;
tx -= pos_x; tx -= pos_x;
ty -= pos_y; ty -= pos_y;
int32_t t2 = tx * tx + ty * ty;
x = usb_x; x = usb_x;
y = usb_y; y = usb_y;
int32_t t2 = tx * tx + ty * ty;
// outside the range // outside the range
if (t2 > pos_r2) if (t2 > pos_r2)
{ {
ret = 0; finger_id = -1;
return 0;
} }
else // inside inner dead_zone else // inside inner dead_zone
{
finger_id = fid;
if (t2 < dead_zone_inner2) if (t2 < dead_zone_inner2)
{ {
ret = 1; ret = 1;
@ -90,6 +99,7 @@ int8_t TouchJoystick::touch(int32_t tx, int32_t ty)
if (invert_x) x = usb_x + usb_r - x; if (invert_x) x = usb_x + usb_r - x;
if (invert_y) y = usb_y + usb_r - y; if (invert_y) y = usb_y + usb_r - y;
}
return ret; return ret;
} }

@ -84,24 +84,42 @@ void setup()
digitalWrite(PC13, HIGH); digitalWrite(PC13, HIGH);
} }
uint8_t tevent_size;
TouchEvent tevent[5];
void loop() void loop()
{ {
uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN); int8_t ret = trackpad_right.poll(tevent, tevent_size);
uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN);
FingerPosition* fp; if (ret > 0)
int8_t fingers_touching = trackpad_right.poll(&fp);
if (fingers_touching > 0)
{ {
if (fp != NULL) for (uint8_t i = 0; i < tevent_size; ++i)
{ {
for (int8_t id = 0; id < TrackPad::fingers_num; ++id) int32_t x = -1;
{ int32_t y = -1;
if (fingers_touching & (1 << id)) switch (tevent[i].type)
{ {
case TET_DOWN:
case TET_MOVE:
x = tevent[i].fp.x;
y = tevent[i].fp.y;
break;
case TET_UP:
break;
default:
break;
}
//Serial.printf("%u\n", tevent[i].type);
for (uint8_t c = 0; c < num_controls; ++c) for (uint8_t c = 0; c < num_controls; ++c)
{ {
int8_t res = tcontrols[c]->touch(fp[id].y, fp[id].x); int8_t res = tcontrols[c]->touch(tevent[i].finger_id, y, x);
if (res < 0) if (res < 0)
{ {
Serial.printf("Impossible Error\n"); Serial.printf("Impossible Error\n");
@ -130,14 +148,9 @@ void loop()
} }
} }
} }
}
} uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN);
else uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN);
if (fingers_touching == 0)
{
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
device.dpad(TouchDpad::NOT_PRESSED);
}
device.trigger_right(right_trigger); device.trigger_right(right_trigger);
device.button(0, right_tp_click); device.button(0, right_tp_click);

Loading…
Cancel
Save