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
};
static const uint8_t NOT_PRESSED = 15;
private:
int32_t dead_zone_inner;
@ -22,6 +24,9 @@ class TouchDpad : public TouchControl
uint8_t button;
int8_t invert_x;
int8_t invert_y;
public:
TouchDpad() {}
@ -33,6 +38,9 @@ class TouchDpad : public TouchControl
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);
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->dpad_type = dpad_type;
this->invert_x = 1;
this->invert_y = 1;
}
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;
}
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 ret = 2;
@ -39,7 +52,7 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
tx -= pos_x;
ty -= pos_y;
button = 0;
button = NOT_PRESSED;
int32_t t2 = tx * tx + ty * ty;
@ -55,36 +68,44 @@ int8_t TouchDpad::touch(int32_t tx, int32_t ty)
}
else // in bounds
{
button = 0;
switch (dpad_type)
{
case DPAD_TYPE_SECTOR4:
button |= (ty < tx);
button |= (ty < -tx) << 1;
if (button == 0b11) button = 0b10;
else if (button == 0b10) button = 0b11;
button |= (invert_y * ty > invert_x * -tx);
button |= (invert_y * ty > invert_x * tx) << 1;
++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;
case DPAD_TYPE_SECTOR8:
button |= (ty < tx * k2);
button |= (ty < tx * k1) << 1;
button |= (ty < -tx * k1) << 2;
button |= (ty < -tx * k2) << 3;
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 = 1; break;
case 1: button = 5; break; // 1 & 2
case 0: button = 0; break;
case 1: button = 1; break;
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
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;

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

@ -25,6 +25,15 @@ TouchJoystick tjoystick_left;
TouchDpad tdpad_right;
TouchDpad tdpad_left;
TouchControl* tcontrols[] =
{
&tjoystick_right,
&tdpad_right,
};
uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*);
void setup()
{
// Turn on LED
@ -57,6 +66,14 @@ void setup()
tjoystick_right.setInvertX();
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.setDeadZoneInner(dead_zone_inner);
//tjoystick_left.setDeadZoneOuter(dead_zone_outer);
@ -78,48 +95,48 @@ void loop()
{
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))
{
int8_t res = tjoystick_right.touch(fp[id].y, fp[id].x);
if (res > 0)
for (uint8_t c = 0; c < num_controls; ++c)
{
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:
Serial.printf("Control type not set\n");
break;
case TouchControl::CT_JOYSTICK:
int16_t x = tjoystick_right.getX();
int16_t y = tjoystick_right.getY();
device.joystick_left(x, y);
Serial.printf("(%i, %i) (%i, %i)\n", fp[id].x, fp[id].y, x, y);
device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY());
break;
case TouchControl::CT_DPAD:
device.dpad(((TouchDpad*)tcontrols[c])->getButton());
break;
}
}
else
if (res < 0)
{
Serial.printf("Impossible Error\n");
}
else
{
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
if (res > 0)
{
break;
}
}
}
}
}
}
else
if (fingers_touching == 0)
{
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
device.dpad(TouchDpad::NOT_PRESSED);
}
device.trigger_right(right_trigger);

Loading…
Cancel
Save