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

input_events
NepEgor 3 years ago
parent fad60549c3
commit b5ab8e525b

@ -2,25 +2,17 @@
#define TOUCH_CONTROL_H
#include <stdint.h>
#include <stddef.h>
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

@ -1,45 +1,39 @@
#ifndef TOUCH_DPAD_H
#define TOUCH_DPAD_H
#include <stdint.h>
#include <stddef.h>
#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;}
};

@ -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;}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;

@ -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