Replaced TouchControlReturn class with enum and getters: KISS; Derived dpad

input_events
NepEgor 3 years ago
parent fad60549c3
commit b5ab8e525b

@ -2,23 +2,15 @@
#define TOUCH_CONTROL_H #define TOUCH_CONTROL_H
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
class TouchControl class TouchControl
{ {
public: public:
class TouchControlReturn enum ControlType: uint8_t{
{ CT_NONE,
public: CT_JOYSTICK,
CT_DPAD,
enum ReturnType: uint8_t{
RT_NONE,
RT_JOYSTICK,
RT_DPAD,
};
ReturnType return_type = RT_NONE;
}; };
protected: protected:
@ -28,6 +20,8 @@ class TouchControl
int32_t pos_r; int32_t pos_r;
int32_t pos_r2; int32_t pos_r2;
ControlType control_type;
public: public:
TouchControl() {} TouchControl() {}
@ -35,8 +29,9 @@ 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, TouchControlReturn* touch_return) = 0; virtual int8_t touch(int32_t tx, int32_t ty) = 0;
ControlType getControlType() {return control_type;}
}; };
#endif #endif

@ -1,45 +1,39 @@
#ifndef TOUCH_DPAD_H #ifndef TOUCH_DPAD_H
#define TOUCH_DPAD_H #define TOUCH_DPAD_H
#include <stdint.h> #include "touch_control.h"
#include <stddef.h>
class TouchDpad class TouchDpad : public TouchControl
{ {
public: public:
enum TouchDpadType
enum DpadType
{ {
Sector4, DPAD_TYPE_SECTOR4,
Sector8 DPAD_TYPE_SECTOR8
}; };
private: 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_inner;
int32_t dead_zone_inner2; // ^ 2 int32_t dead_zone_inner2; // ^ 2
TouchDpadType type; DpadType dpad_type;
uint8_t button; uint8_t button;
public: public:
TouchDpad() {} 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 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;} uint8_t getButton() {return button;}
}; };

@ -7,14 +7,6 @@ class TouchJoystick : public TouchControl
{ {
public: public:
class TouchJoystickReturn : public TouchControl::TouchControlReturn
{
public:
int16_t x;
int16_t y;
};
private: private:
int32_t dead_zone_inner; int32_t dead_zone_inner;
@ -47,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, TouchControlReturn* touch_return); int8_t touch(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;}

@ -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_y = pos_y;
this->pos_r = pos_r; this->pos_r = pos_r;
this->pos_r2 = pos_r * pos_r; this->pos_r2 = pos_r * pos_r;
this->control_type = CT_NONE;
} }

@ -9,22 +9,21 @@
const float k1 = tanf(PI / 8.f); // tan of 22.5 deg for 8 sector dpad const float k1 = tanf(PI / 8.f); // tan of 22.5 deg for 8 sector dpad
const float k2 = 1 / k1; 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; TouchControl::init(pos_x, pos_y, pos_r);
this->pos_y = pos_y;
this->pos_r = pos_r; this->control_type = CT_DPAD;
this->pos_r2 = pos_r * pos_r;
this->dead_zone_inner = 0; this->dead_zone_inner = 0;
this->dead_zone_inner2 = 0; this->dead_zone_inner2 = 0;
this->type = type; this->dpad_type = dpad_type;
} }
void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner) 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; 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; tx -= pos_x;
ty -= pos_y; ty -= pos_y;
@ -42,11 +43,21 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty)
int32_t t2 = tx * tx + ty * 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);
button |= (ty < -tx) << 1; button |= (ty < -tx) << 1;
@ -57,7 +68,7 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty)
break; break;
case Sector8: case DPAD_TYPE_SECTOR8:
button |= (ty < tx * k2); button |= (ty < tx * k2);
button |= (ty < tx * k1) << 1; button |= (ty < tx * k1) << 1;
button |= (ty < -tx * k1) << 2; button |= (ty < -tx * k1) << 2;
@ -66,13 +77,14 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty)
switch (button) switch (button)
{ {
case 0: button = 1; break; case 0: button = 1; break;
case 1: button = 2; break; case 1: button = 5; break; // 1 & 2
case 3: button = 3; break; case 3: button = 2; break;
case 7: button = 4; break; case 7: button = 6; break; // 2 & 3
case 15: button = 5; break; case 15: button = 3; break;
case 14: button = 6; break; case 14: button = 7; break; // 3 & 4
case 12: button = 7; break; case 12: button = 4; break;
case 8: button = 8; break; case 8: button = 8; break; // 4 & 1
default: button = 9; break; // error
} }
break; break;
@ -82,5 +94,5 @@ uint8_t TouchDpad::touch(int32_t tx, int32_t ty)
} }
} }
return button; return ret;
} }

@ -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); TouchControl::init(pos_x, pos_y, pos_r);
this->control_type = CT_JOYSTICK;
this->usb_x = usb_x; this->usb_x = usb_x;
this->usb_y = usb_y; this->usb_y = usb_y;
this->usb_r = usb_r; this->usb_r = usb_r;
@ -50,13 +52,8 @@ 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, TouchControlReturn* touch_return) int8_t TouchJoystick::touch(int32_t tx, int32_t ty)
{ {
if (touch_return == NULL)
{
return -1;
}
int8_t ret = 2; int8_t ret = 2;
tx -= pos_x; 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_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;
touch_return->return_type = TouchControlReturn::RT_JOYSTICK;
((TouchJoystickReturn*)touch_return)->x = x;
((TouchJoystickReturn*)touch_return)->y = y;
return ret; return ret;
} }

@ -4,41 +4,69 @@
int main() int main()
{ {
printf("\n");
int32_t pos_x = 0; int32_t pos_x = 0;
int32_t pos_y = 0; int32_t pos_y = 0;
int32_t pos_r = 10; 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); 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; uint8_t button;
printf("0 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
tx = 0; ty = 5; TouchControl* tcontrol = &tdpad;
printf("1 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
tx = 5; ty = 5; int N = sizeof(t_points) / sizeof(int32_t);
printf("12 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); for (int i = 0; i < N; i += 2)
{
int8_t res = tcontrol->touch(t_points[i], t_points[i + 1]);
tx = 5; ty = 0; if (res > 0)
printf("2 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); {
switch(tcontrol->getControlType())
{
case TouchControl::CT_NONE:
printf("CT_NONE\n");
break;
tx = 5; ty = -5; case TouchControl::CT_DPAD:
printf("23 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
tx = 0; ty = -5; button = ((TouchDpad*)tcontrol)->getButton();
printf("3 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
tx = -5; ty = -5; printf("CT_DPAD %i (%i, %i): %u\n", i / 2, t_points[i], t_points[i + 1], button);
printf("34 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
tx = -5; ty = 0; break;
printf("4 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty)); }
}
else
if (res < 0)
{
printf("Impossible Error\n");
}
else
{
printf("Touch out of bounds\n");
}
}
tx = -5; ty = 5; printf("\n");
printf("41 (%i, %i): %u\n", tx, ty, tdpad.touch(tx, ty));
return 0; return 0;
} }

@ -16,42 +16,42 @@ int main()
TouchJoystick tjoystick(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r); TouchJoystick tjoystick(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
int32_t touch_x = 150 + 2000; int32_t touch_x = 150 + 1000;
int32_t touch_y = 350 + 0; int32_t touch_y = 350 + 20;
int16_t x; int16_t x;
int16_t y; int16_t y;
TouchControl::TouchControlReturn touch_return;
TouchControl* tcontrol = &tjoystick; 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) if (res > 0)
{ {
switch(touch_return.return_type) switch(tcontrol->getControlType())
{ {
case TouchControl::TouchControlReturn::RT_NONE: case TouchControl::CT_NONE:
printf("RT_NONE\n"); printf("CT_NONE\n");
break; break;
case TouchControl::TouchControlReturn::RT_JOYSTICK: case TouchControl::CT_JOYSTICK:
x = ((TouchJoystick::TouchJoystickReturn*)&touch_return)->x;
y = ((TouchJoystick::TouchJoystickReturn*)&touch_return)->y; x = ((TouchJoystick*)tcontrol)->getX();
printf("RT_JOYSTICK (%i, %i)\n", x, y); y = ((TouchJoystick*)tcontrol)->getY();
printf("CT_JOYSTICK (%i, %i)\n", x, y);
break; break;
} }
} }
else else
if (res < 0) if (res < 0)
{ {
printf("Pointer Error\n"); printf("Impossible Error\n");
} }
else else
{ {
printf("Touch out of bounds\n"); printf("Touch out of bounds\n");
} }
printf("\n"); printf("\n");
return 0; return 0;

@ -0,0 +1,88 @@
#include "touch_controls_all.h"
#include <stdio.h>
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;
}
Loading…
Cancel
Save