From b5ab8e525b316aee89dcdc9b533a42a6ccdd3033 Mon Sep 17 00:00:00 2001 From: NepEgor Date: Tue, 26 Oct 2021 21:46:27 +0300 Subject: [PATCH] Replaced TouchControlReturn class with enum and getters: KISS; Derived dpad --- lib/touch_controls/include/touch_control.h | 23 ++--- lib/touch_controls/include/touch_dpad.h | 28 +++--- lib/touch_controls/include/touch_joystick.h | 10 +-- lib/touch_controls/src/touch_control.cpp | 2 + lib/touch_controls/src/touch_dpad.cpp | 54 +++++++----- lib/touch_controls/src/touch_joystick.cpp | 15 +--- test/test_dpad/test_touch_dpad.cpp | 68 +++++++++----- test/test_joystick/test_touch_joystick.cpp | 26 +++--- .../test_multiple_controls.cpp | 88 +++++++++++++++++++ 9 files changed, 209 insertions(+), 105 deletions(-) create mode 100644 test/test_multiple_controls/test_multiple_controls.cpp diff --git a/lib/touch_controls/include/touch_control.h b/lib/touch_controls/include/touch_control.h index 8cc9cdf..0a0c80e 100644 --- a/lib/touch_controls/include/touch_control.h +++ b/lib/touch_controls/include/touch_control.h @@ -2,25 +2,17 @@ #define TOUCH_CONTROL_H #include -#include class TouchControl { public: - class TouchControlReturn - { - public: - - enum ReturnType: uint8_t{ - RT_NONE, - RT_JOYSTICK, - RT_DPAD, - }; - - ReturnType return_type = RT_NONE; + enum ControlType: uint8_t{ + CT_NONE, + CT_JOYSTICK, + CT_DPAD, }; - + protected: int32_t pos_x; @@ -28,6 +20,8 @@ class TouchControl int32_t pos_r; int32_t pos_r2; + ControlType control_type; + public: TouchControl() {} @@ -35,8 +29,9 @@ 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, TouchControlReturn* touch_return) = 0; + virtual int8_t touch(int32_t tx, int32_t ty) = 0; + ControlType getControlType() {return control_type;} }; #endif \ No newline at end of file diff --git a/lib/touch_controls/include/touch_dpad.h b/lib/touch_controls/include/touch_dpad.h index 092a009..302b3b6 100644 --- a/lib/touch_controls/include/touch_dpad.h +++ b/lib/touch_controls/include/touch_dpad.h @@ -1,45 +1,39 @@ #ifndef TOUCH_DPAD_H #define TOUCH_DPAD_H -#include -#include +#include "touch_control.h" -class TouchDpad +class TouchDpad : public TouchControl { public: - enum TouchDpadType + + enum DpadType { - Sector4, - Sector8 + DPAD_TYPE_SECTOR4, + DPAD_TYPE_SECTOR8 }; private: - // position of dpad on trackpad - int32_t pos_x; - int32_t pos_y; - int32_t pos_r; - int32_t pos_r2; // pos_r ^ 2 - int32_t dead_zone_inner; int32_t dead_zone_inner2; // ^ 2 - TouchDpadType type; + DpadType dpad_type; uint8_t button; public: TouchDpad() {} - TouchDpad(int32_t pos_x, int32_t pos_y, int32_t pos_r, TouchDpadType type = Sector4); + TouchDpad(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_type = DPAD_TYPE_SECTOR4); - void init(int32_t pos_x, int32_t pos_y, int32_t pos_r, TouchDpadType type = Sector4); + void init(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_type = DPAD_TYPE_SECTOR4); void setDeadZoneInner(int32_t dead_zone_inner); - void setType(TouchDpadType type) {this->type = type;} + void setType(DpadType dpad_type) {this->dpad_type = dpad_type;} - uint8_t touch(int32_t tx, int32_t ty); + int8_t touch(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 8031069..b7b23cd 100644 --- a/lib/touch_controls/include/touch_joystick.h +++ b/lib/touch_controls/include/touch_joystick.h @@ -7,14 +7,6 @@ class TouchJoystick : public TouchControl { public: - class TouchJoystickReturn : public TouchControl::TouchControlReturn - { - public: - - int16_t x; - int16_t y; - }; - private: int32_t dead_zone_inner; @@ -47,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, TouchControlReturn* touch_return); + int8_t touch(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 ee49dd9..a31cfb7 100644 --- a/lib/touch_controls/src/touch_control.cpp +++ b/lib/touch_controls/src/touch_control.cpp @@ -11,4 +11,6 @@ void TouchControl::init(int32_t pos_x, int32_t pos_y, int32_t pos_r) this->pos_y = pos_y; this->pos_r = pos_r; this->pos_r2 = pos_r * pos_r; + + this->control_type = CT_NONE; } diff --git a/lib/touch_controls/src/touch_dpad.cpp b/lib/touch_controls/src/touch_dpad.cpp index bf46604..225f76c 100644 --- a/lib/touch_controls/src/touch_dpad.cpp +++ b/lib/touch_controls/src/touch_dpad.cpp @@ -9,22 +9,21 @@ const float k1 = tanf(PI / 8.f); // tan of 22.5 deg for 8 sector dpad const float k2 = 1 / k1; -TouchDpad::TouchDpad(int32_t pos_x, int32_t pos_y, int32_t pos_r, TouchDpadType type) +TouchDpad::TouchDpad(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_type) { - init(pos_x, pos_y, pos_r, type); + init(pos_x, pos_y, pos_r, dpad_type); } -void TouchDpad::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, TouchDpadType type) +void TouchDpad::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_type) { - this->pos_x = pos_x; - this->pos_y = pos_y; - this->pos_r = pos_r; - this->pos_r2 = pos_r * pos_r; + TouchControl::init(pos_x, pos_y, pos_r); + + this->control_type = CT_DPAD; this->dead_zone_inner = 0; this->dead_zone_inner2 = 0; - this->type = type; + this->dpad_type = dpad_type; } void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner) @@ -33,8 +32,10 @@ void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner) this->dead_zone_inner2 = dead_zone_inner * dead_zone_inner; } -uint8_t TouchDpad::touch(int32_t tx, int32_t ty) +int8_t TouchDpad::touch(int32_t tx, int32_t ty) { + int8_t ret = 2; + tx -= pos_x; ty -= pos_y; @@ -42,11 +43,21 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty) int32_t t2 = tx * tx + ty * ty; - if (t2 >= dead_zone_inner2 && t2 <= pos_r2) + // outside the range + if (t2 > pos_r2) + { + ret = 0; + } + else // inside inner dead_zone + if (t2 < dead_zone_inner2) + { + ret = 1; + } + else // in bounds { - switch (type) + switch (dpad_type) { - case Sector4: + case DPAD_TYPE_SECTOR4: button |= (ty < tx); button |= (ty < -tx) << 1; @@ -57,7 +68,7 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty) break; - case Sector8: + case DPAD_TYPE_SECTOR8: button |= (ty < tx * k2); button |= (ty < tx * k1) << 1; button |= (ty < -tx * k1) << 2; @@ -66,13 +77,14 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty) switch (button) { case 0: button = 1; break; - case 1: button = 2; break; - case 3: button = 3; break; - case 7: button = 4; break; - case 15: button = 5; break; - case 14: button = 6; break; - case 12: button = 7; break; - case 8: button = 8; break; + case 1: button = 5; break; // 1 & 2 + case 3: button = 2; break; + case 7: button = 6; break; // 2 & 3 + case 15: button = 3; break; + case 14: button = 7; break; // 3 & 4 + case 12: button = 4; break; + case 8: button = 8; break; // 4 & 1 + default: button = 9; break; // error } break; @@ -82,5 +94,5 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty) } } - return button; + 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 8c26918..3c18c6f 100644 --- a/lib/touch_controls/src/touch_joystick.cpp +++ b/lib/touch_controls/src/touch_joystick.cpp @@ -11,6 +11,8 @@ void TouchJoystick::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t us { TouchControl::init(pos_x, pos_y, pos_r); + this->control_type = CT_JOYSTICK; + this->usb_x = usb_x; this->usb_y = usb_y; this->usb_r = usb_r; @@ -50,13 +52,8 @@ void TouchJoystick::setInvertY(bool invert_y) this->invert_y = invert_y; } -int8_t TouchJoystick::touch(int32_t tx, int32_t ty, TouchControlReturn* touch_return) -{ - if (touch_return == NULL) - { - return -1; - } - +int8_t TouchJoystick::touch(int32_t tx, int32_t ty) +{ int8_t ret = 2; tx -= pos_x; @@ -94,9 +91,5 @@ int8_t TouchJoystick::touch(int32_t tx, int32_t ty, TouchControlReturn* touch_re if (invert_x) x = usb_x + usb_r - x; if (invert_y) y = usb_y + usb_r - y; - touch_return->return_type = TouchControlReturn::RT_JOYSTICK; - ((TouchJoystickReturn*)touch_return)->x = x; - ((TouchJoystickReturn*)touch_return)->y = y; - return ret; } \ No newline at end of file diff --git a/test/test_dpad/test_touch_dpad.cpp b/test/test_dpad/test_touch_dpad.cpp index 0642960..2fd7aeb 100644 --- a/test/test_dpad/test_touch_dpad.cpp +++ b/test/test_dpad/test_touch_dpad.cpp @@ -4,41 +4,69 @@ int main() { + printf("\n"); + int32_t pos_x = 0; int32_t pos_y = 0; int32_t pos_r = 10; - TouchDpad tdpad(pos_x, pos_y, pos_r, TouchDpad::Sector8); + TouchDpad tdpad(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4); tdpad.setDeadZoneInner(2); - int32_t tx, ty; + //printf("0 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + + int32_t t_points[] = + { + 0, 0, + 0, 5, + 5, 5, + 5, 0, + 5, -5, + 0, -5, + -5, -5, + -5, 0, + -5, 5, + 11, 11, + }; - tx = 0; ty = 0; - printf("0 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + uint8_t button; - tx = 0; ty = 5; - printf("1 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + TouchControl* tcontrol = &tdpad; - tx = 5; ty = 5; - printf("12 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + int N = sizeof(t_points) / sizeof(int32_t); + for (int i = 0; i < N; i += 2) + { + int8_t res = tcontrol->touch(t_points[i], t_points[i + 1]); - tx = 5; ty = 0; - printf("2 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + if (res > 0) + { + switch(tcontrol->getControlType()) + { + case TouchControl::CT_NONE: + printf("CT_NONE\n"); + break; - tx = 5; ty = -5; - printf("23 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + case TouchControl::CT_DPAD: - tx = 0; ty = -5; - printf("3 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + button = ((TouchDpad*)tcontrol)->getButton(); - tx = -5; ty = -5; - printf("34 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + printf("CT_DPAD %i (%i, %i): %u\n", i / 2, t_points[i], t_points[i + 1], button); - tx = -5; ty = 0; - printf("4 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + break; + } + } + else + if (res < 0) + { + printf("Impossible Error\n"); + } + else + { + printf("Touch out of bounds\n"); + } + } - tx = -5; ty = 5; - printf("41 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); + printf("\n"); return 0; } \ No newline at end of file diff --git a/test/test_joystick/test_touch_joystick.cpp b/test/test_joystick/test_touch_joystick.cpp index 8429a76..efbe626 100644 --- a/test/test_joystick/test_touch_joystick.cpp +++ b/test/test_joystick/test_touch_joystick.cpp @@ -16,42 +16,42 @@ int main() TouchJoystick tjoystick(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r); - int32_t touch_x = 150 + 2000; - int32_t touch_y = 350 + 0; + int32_t touch_x = 150 + 1000; + int32_t touch_y = 350 + 20; int16_t x; int16_t y; - TouchControl::TouchControlReturn touch_return; TouchControl* tcontrol = &tjoystick; - int8_t res = tcontrol->touch(touch_x, touch_y, &touch_return); + int8_t res = tcontrol->touch(touch_x, touch_y); if (res > 0) { - switch(touch_return.return_type) + switch(tcontrol->getControlType()) { - case TouchControl::TouchControlReturn::RT_NONE: - printf("RT_NONE\n"); + case TouchControl::CT_NONE: + printf("CT_NONE\n"); break; - case TouchControl::TouchControlReturn::RT_JOYSTICK: - x = ((TouchJoystick::TouchJoystickReturn*)&touch_return)->x; - y = ((TouchJoystick::TouchJoystickReturn*)&touch_return)->y; - printf("RT_JOYSTICK (%i, %i)\n", x, y); + case TouchControl::CT_JOYSTICK: + + x = ((TouchJoystick*)tcontrol)->getX(); + y = ((TouchJoystick*)tcontrol)->getY(); + + printf("CT_JOYSTICK (%i, %i)\n", x, y); break; } } else if (res < 0) { - printf("Pointer Error\n"); + printf("Impossible Error\n"); } else { printf("Touch out of bounds\n"); } - printf("\n"); return 0; diff --git a/test/test_multiple_controls/test_multiple_controls.cpp b/test/test_multiple_controls/test_multiple_controls.cpp new file mode 100644 index 0000000..48adc73 --- /dev/null +++ b/test/test_multiple_controls/test_multiple_controls.cpp @@ -0,0 +1,88 @@ +#include "touch_controls_all.h" + +#include + +int main() +{ + printf("\n"); + + int32_t t_points[] = + { + 0, 0, + 0, 500, + 500, 500, + 500, 0, + 500, -500, + 0, -500, + -500, -500, + -500, 0, + -500, 500, + + 3000, 3000, + + 3000, 0, + 2000, 0, + 4000, 0, + 3000, 1000, + 3000,-1000, + }; + + int num_points = sizeof(t_points) / sizeof(int32_t); + + TouchJoystick tjoystick(3000, 0, 1000, 512, 512, 512); + + TouchDpad tdpad(0, 0, 1000, TouchDpad::DPAD_TYPE_SECTOR8); + tdpad.setDeadZoneInner(100); + + TouchControl* tcontrols[] = + { + &tjoystick, + &tdpad, + }; + + int num_controls = sizeof(tcontrols) / sizeof(TouchControl*); + + for (int j = 0; j < num_points; j += 2) + { + for (int i = 0; i < num_controls; ++i) + { + printf("Touch %i at (%i, %i) ", i, t_points[j], t_points[j + 1]); + int8_t res = tcontrols[i]->touch(t_points[j], t_points[j + 1]); + if (res > 0) + { + switch(tcontrols[i]->getControlType()) + { + case TouchControl::CT_NONE: + printf("CT_NONE\n"); + break; + + case TouchControl::CT_JOYSTICK: + printf("CT_JOYSTICK (%i, %i)\n", + ((TouchJoystick*)tcontrols[i])->getX(), + ((TouchJoystick*)tcontrols[i])->getY() + ); + break; + + case TouchControl::CT_DPAD: + printf("CT_DPAD %u\n", ((TouchDpad*)tcontrols[i])->getButton()); + break; + } + + break; + } + else + if (res < 0) + { + printf("Impossible Error\n"); + } + else + { + printf("not touched\n", i); + } + } + } + + printf("\nTest end\n"); + + return 0; +} \ No newline at end of file