Added axis inversion to dpad; Mult controls in main; TODO multitouch

input_events
NepEgor 3 years ago
parent df99a0c180
commit 1aab181022

@ -13,6 +13,8 @@ class TouchDpad : public TouchControl
DPAD_TYPE_SECTOR8 DPAD_TYPE_SECTOR8
}; };
static const uint8_t NOT_PRESSED = 15;
private: private:
int32_t dead_zone_inner; int32_t dead_zone_inner;
@ -22,6 +24,9 @@ class TouchDpad : public TouchControl
uint8_t button; uint8_t button;
int8_t invert_x;
int8_t invert_y;
public: public:
TouchDpad() {} TouchDpad() {}
@ -33,6 +38,9 @@ class TouchDpad : public TouchControl
void setType(DpadType dpad_type) {this->dpad_type = dpad_type;} void setType(DpadType dpad_type) {this->dpad_type = dpad_type;}
void setInvertX(bool invert_x = true);
void setInvertY(bool invert_y = true);
int8_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;}

@ -24,6 +24,9 @@ void TouchDpad::init(int32_t pos_x, int32_t pos_y, int32_t pos_r, DpadType dpad_
this->dead_zone_inner2 = 0; this->dead_zone_inner2 = 0;
this->dpad_type = dpad_type; this->dpad_type = dpad_type;
this->invert_x = 1;
this->invert_y = 1;
} }
void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner) void TouchDpad::setDeadZoneInner(int32_t dead_zone_inner)
@ -32,6 +35,16 @@ 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;
} }
void TouchDpad::setInvertX(bool invert_x)
{
this->invert_x = invert_x ? -1 : 1;
}
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(int32_t tx, int32_t ty)
{ {
int8_t ret = 2; int8_t ret = 2;
@ -39,7 +52,7 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
tx -= pos_x; tx -= pos_x;
ty -= pos_y; ty -= pos_y;
button = 0; button = NOT_PRESSED;
int32_t t2 = tx * tx + ty * ty; int32_t t2 = tx * tx + ty * ty;
@ -55,36 +68,44 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
} }
else // in bounds else // in bounds
{ {
button = 0;
switch (dpad_type) switch (dpad_type)
{ {
case DPAD_TYPE_SECTOR4: case DPAD_TYPE_SECTOR4:
button |= (ty < tx); button |= (invert_y * ty > invert_x * -tx);
button |= (ty < -tx) << 1; button |= (invert_y * ty > invert_x * tx) << 1;
if (button == 0b11) button = 0b10;
else if (button == 0b10) button = 0b11;
++button; 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; break;
case DPAD_TYPE_SECTOR8: case DPAD_TYPE_SECTOR8:
button |= (ty < tx * k2); button |= (invert_y * ty > invert_x * -tx * k2);
button |= (ty < tx * k1) << 1; button |= (invert_y * ty > invert_x * -tx * k1) << 1;
button |= (ty < -tx * k1) << 2; button |= (invert_y * ty > invert_x * tx * k1) << 2;
button |= (ty < -tx * k2) << 3; button |= (invert_y * ty > invert_x * tx * k2) << 3;
switch (button) switch (button)
{ {
case 0: button = 1; break; case 0: button = 0; break;
case 1: button = 5; break; // 1 & 2 case 1: button = 1; break;
case 3: button = 2; break; case 3: button = 2; break;
case 7: button = 6; break; // 2 & 3 case 7: button = 3; break;
case 15: button = 3; break; case 15: button = 4; break;
case 14: button = 7; break; // 3 & 4 case 14: button = 5; break;
case 12: button = 4; break; case 12: button = 6; break;
case 8: button = 8; break; // 4 & 1 case 8: button = 7; break;
default: button = 9; break; // error
default: button = NOT_PRESSED; break;
} }
break; break;

@ -3,11 +3,9 @@
#include <stdint.h> #include <stdint.h>
//#define HID_CUSTOM_REPORT_DESC_SIZE 34U
#define HID_JOYSTICK_REPORT_DESC_SIZE 89U #define HID_JOYSTICK_REPORT_DESC_SIZE 89U
// USB Custom Report Descriptor // USB Joystick Report Descriptor
//extern uint8_t USBD_HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE];
extern uint8_t USBD_HID_Joystick_ReportDesc[HID_JOYSTICK_REPORT_DESC_SIZE]; extern uint8_t USBD_HID_Joystick_ReportDesc[HID_JOYSTICK_REPORT_DESC_SIZE];
struct __attribute__((packed)) USBD_HID_Joystick_Report struct __attribute__((packed)) USBD_HID_Joystick_Report

@ -25,6 +25,15 @@ TouchJoystick tjoystick_left;
TouchDpad tdpad_right; TouchDpad tdpad_right;
TouchDpad tdpad_left; TouchDpad tdpad_left;
TouchControl* tcontrols[] =
{
&tjoystick_right,
&tdpad_right,
};
uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*);
void setup() void setup()
{ {
// Turn on LED // Turn on LED
@ -57,6 +66,14 @@ void setup()
tjoystick_right.setInvertX(); tjoystick_right.setInvertX();
tjoystick_right.setInvertY(); tjoystick_right.setInvertY();
pos_x = (62.5 - 20.636) * ppmX;
pos_y = 20.636 * ppmY;
pos_r = 45 * ppmX / 2;
tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR8);
tdpad_right.setDeadZoneInner(dead_zone_inner);
tdpad_right.setInvertX();
tdpad_right.setInvertY();
//tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); //tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
//tjoystick_left.setDeadZoneInner(dead_zone_inner); //tjoystick_left.setDeadZoneInner(dead_zone_inner);
//tjoystick_left.setDeadZoneOuter(dead_zone_outer); //tjoystick_left.setDeadZoneOuter(dead_zone_outer);
@ -78,48 +95,48 @@ void loop()
{ {
if (fp != NULL) if (fp != NULL)
{ {
for (uint8_t id = 0; id < TrackPad::fingers_num; ++id) for (int8_t id = 0; id < TrackPad::fingers_num; ++id)
{ {
if (fingers_touching & (1 << id)) if (fingers_touching & (1 << id))
{ {
int8_t res = tjoystick_right.touch(fp[id].y, fp[id].x); for (uint8_t c = 0; c < num_controls; ++c)
if (res > 0)
{ {
switch(tjoystick_right.getControlType()) 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: case TouchControl::CT_NONE:
Serial.printf("Control type not set\n");
break; break;
case TouchControl::CT_JOYSTICK: case TouchControl::CT_JOYSTICK:
device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY());
int16_t x = tjoystick_right.getX(); break;
int16_t y = tjoystick_right.getY();
device.joystick_left(x, y); case TouchControl::CT_DPAD:
device.dpad(((TouchDpad*)tcontrols[c])->getButton());
Serial.printf("(%i, %i) (%i, %i)\n", fp[id].x, fp[id].y, x, y);
break; break;
} }
}
else if (res > 0)
if (res < 0) {
{ break;
Serial.printf("Impossible Error\n"); }
}
else
{
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
} }
} }
} }
} }
} }
else else
if (fingers_touching == 0) if (fingers_touching == 0)
{ {
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y); 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);

Loading…
Cancel
Save