diff --git a/lib/touch_controls/include/touch_control.h b/lib/touch_controls/include/touch_control.h index 0a0c80e..0334090 100644 --- a/lib/touch_controls/include/touch_control.h +++ b/lib/touch_controls/include/touch_control.h @@ -22,6 +22,8 @@ class TouchControl ControlType control_type; + int8_t finger_id; + public: TouchControl() {} @@ -29,7 +31,7 @@ class TouchControl 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;} }; diff --git a/lib/touch_controls/include/touch_dpad.h b/lib/touch_controls/include/touch_dpad.h index 362f8cd..ada8b43 100644 --- a/lib/touch_controls/include/touch_dpad.h +++ b/lib/touch_controls/include/touch_dpad.h @@ -41,7 +41,7 @@ class TouchDpad : public TouchControl void setInvertX(bool invert_x = 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;} }; diff --git a/lib/touch_controls/include/touch_joystick.h b/lib/touch_controls/include/touch_joystick.h index b7b23cd..d834415 100644 --- a/lib/touch_controls/include/touch_joystick.h +++ b/lib/touch_controls/include/touch_joystick.h @@ -39,7 +39,7 @@ class TouchJoystick : public TouchControl void setInvertX(bool invert_x = 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 getY() {return y;} diff --git a/lib/touch_controls/src/touch_control.cpp b/lib/touch_controls/src/touch_control.cpp index a31cfb7..9f06d9f 100644 --- a/lib/touch_controls/src/touch_control.cpp +++ b/lib/touch_controls/src/touch_control.cpp @@ -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->control_type = CT_NONE; + + this->finger_id = -1; } diff --git a/lib/touch_controls/src/touch_dpad.cpp b/lib/touch_controls/src/touch_dpad.cpp index 6a221ea..5b92523 100644 --- a/lib/touch_controls/src/touch_dpad.cpp +++ b/lib/touch_controls/src/touch_dpad.cpp @@ -45,8 +45,13 @@ void TouchDpad::setInvertY(bool invert_y) 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; tx -= pos_x; @@ -59,61 +64,66 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty) // outside the range if (t2 > pos_r2) { - ret = 0; + finger_id = -1; + return 0; } else // inside inner dead_zone - if (t2 < dead_zone_inner2) - { - ret = 1; - } - else // in bounds { - button = 0; - - switch (dpad_type) + finger_id = fid; + + if (t2 < dead_zone_inner2) + { + ret = 1; + } + else // in bounds { - case DPAD_TYPE_SECTOR4: - button |= (invert_y * ty > invert_x * -tx); - button |= (invert_y * ty > invert_x * tx) << 1; - - switch (button) - { - case 0b00: button = 0; break; - case 0b01: button = 2; break; - case 0b11: button = 4; break; - case 0b10: button = 6; break; - - default: button = NOT_PRESSED; break; - } - - break; - - case DPAD_TYPE_SECTOR8: - button |= (invert_y * ty > invert_x * -tx * k2); - button |= (invert_y * ty > invert_x * -tx * k1) << 1; - button |= (invert_y * ty > invert_x * tx * k1) << 2; - button |= (invert_y * ty > invert_x * tx * k2) << 3; - - switch (button) - { - case 0: button = 0; break; - case 1: button = 1; break; - case 3: button = 2; break; - case 7: button = 3; break; - case 15: button = 4; break; - case 14: button = 5; break; - case 12: button = 6; break; - case 8: button = 7; break; - - default: button = NOT_PRESSED; break; - } - - break; - - default: - break; + button = 0; + + switch (dpad_type) + { + case DPAD_TYPE_SECTOR4: + button |= (invert_y * ty > invert_x * -tx); + button |= (invert_y * ty > invert_x * tx) << 1; + + switch (button) + { + case 0b00: button = 0; break; + case 0b01: button = 2; break; + case 0b11: button = 4; break; + case 0b10: button = 6; break; + + default: button = NOT_PRESSED; break; + } + + break; + + case DPAD_TYPE_SECTOR8: + button |= (invert_y * ty > invert_x * -tx * k2); + button |= (invert_y * ty > invert_x * -tx * k1) << 1; + button |= (invert_y * ty > invert_x * tx * k1) << 2; + button |= (invert_y * ty > invert_x * tx * k2) << 3; + + switch (button) + { + case 0: button = 0; break; + case 1: button = 1; break; + case 3: button = 2; break; + case 7: button = 3; break; + case 15: button = 4; break; + case 14: button = 5; break; + case 12: button = 6; break; + case 8: button = 7; break; + + default: button = NOT_PRESSED; break; + } + + break; + + default: + break; + } } } - + return ret; } \ No newline at end of file diff --git a/lib/touch_controls/src/touch_joystick.cpp b/lib/touch_controls/src/touch_joystick.cpp index 3c18c6f..6a5fe60 100644 --- a/lib/touch_controls/src/touch_joystick.cpp +++ b/lib/touch_controls/src/touch_joystick.cpp @@ -52,44 +52,54 @@ void TouchJoystick::setInvertY(bool 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; tx -= pos_x; ty -= pos_y; - int32_t t2 = tx * tx + ty * ty; - x = usb_x; y = usb_y; + int32_t t2 = tx * tx + ty * ty; + // outside the range if (t2 > pos_r2) { - ret = 0; + finger_id = -1; + return 0; } else // inside inner dead_zone - if (t2 < dead_zone_inner2) { - ret = 1; + finger_id = fid; + + if (t2 < dead_zone_inner2) + { + ret = 1; + } + else // between dead zones + 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 = usb_x + usb_r - x; + if (invert_y) y = usb_y + usb_r - y; } - else // between dead zones - 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 = usb_x + usb_r - x; - if (invert_y) y = usb_y + usb_r - y; - + return ret; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e127acc..c473d30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,60 +84,73 @@ void setup() digitalWrite(PC13, HIGH); } +uint8_t tevent_size; +TouchEvent tevent[5]; + void loop() { - uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN); - uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN); - - FingerPosition* fp; - int8_t fingers_touching = trackpad_right.poll(&fp); - if (fingers_touching > 0) + int8_t ret = trackpad_right.poll(tevent, tevent_size); + + if (ret > 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; + switch (tevent[i].type) { - if (fingers_touching & (1 << id)) + 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) + { + int8_t res = tcontrols[c]->touch(tevent[i].finger_id, y, x); + if (res < 0) + { + Serial.printf("Impossible Error\n"); + break; + } + + switch(tcontrols[c]->getControlType()) + { + case TouchControl::CT_NONE: + Serial.printf("Control type not set\n"); + break; + + case TouchControl::CT_JOYSTICK: + device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); + break; + + case TouchControl::CT_DPAD: + device.dpad(((TouchDpad*)tcontrols[c])->getButton()); + break; + } + + if (res > 0) { - for (uint8_t c = 0; c < num_controls; ++c) - { - int8_t res = tcontrols[c]->touch(fp[id].y, fp[id].x); - if (res < 0) - { - Serial.printf("Impossible Error\n"); - break; - } - - switch(tcontrols[c]->getControlType()) - { - case TouchControl::CT_NONE: - Serial.printf("Control type not set\n"); - break; - - case TouchControl::CT_JOYSTICK: - device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); - break; - - case TouchControl::CT_DPAD: - device.dpad(((TouchDpad*)tcontrols[c])->getButton()); - break; - } - - if (res > 0) - { - break; - } - } + break; } } } } - else - if (fingers_touching == 0) - { - device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y); - device.dpad(TouchDpad::NOT_PRESSED); - } + + uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN); + uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN); device.trigger_right(right_trigger); device.button(0, right_tp_click);