From d2cac95fe42a5aded2ca804b77ddfc184d335eb3 Mon Sep 17 00:00:00 2001 From: NepEgor Date: Sat, 13 Nov 2021 22:39:05 +0300 Subject: [PATCH] Added 2nd trackpad mappings; switched up and down on dpad --- lib/touch_controls/src/touch_dpad.cpp | 20 +++---- lib/usb_device/include/usb_device.h | 6 +-- lib/usb_device/src/usb_device.cpp | 29 +++++------ src/input_mapper.cpp | 75 +++++++++++++++++---------- src/main.cpp | 70 ++++++++++++++++--------- 5 files changed, 118 insertions(+), 82 deletions(-) diff --git a/lib/touch_controls/src/touch_dpad.cpp b/lib/touch_controls/src/touch_dpad.cpp index 5416191..3d33719 100644 --- a/lib/touch_controls/src/touch_dpad.cpp +++ b/lib/touch_controls/src/touch_dpad.cpp @@ -92,9 +92,9 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) switch (button) { - case 0b00: button = XINPUT_DPAD_UP; break; + case 0b00: button = XINPUT_DPAD_DOWN; break; case 0b01: button = XINPUT_DPAD_RIGHT; break; - case 0b11: button = XINPUT_DPAD_DOWN; break; + case 0b11: button = XINPUT_DPAD_UP; break; case 0b10: button = XINPUT_DPAD_LEFT; break; default: button = 0; break; @@ -110,14 +110,14 @@ int8_t TouchDpad::touch(int8_t fid, int32_t tx, int32_t ty) switch (button) { - case 0: button = XINPUT_DPAD_UP; break; - case 1: button = XINPUT_DPAD_UP | XINPUT_DPAD_RIGHT; break; - case 3: button = XINPUT_DPAD_RIGHT; break; - case 7: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_RIGHT; break; - case 15: button = XINPUT_DPAD_DOWN; break; - case 14: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_LEFT; break; - case 12: button = XINPUT_DPAD_LEFT; break; - case 8: button = XINPUT_DPAD_UP | XINPUT_DPAD_LEFT; break; + case 0: button = XINPUT_DPAD_DOWN; break; + case 1: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_RIGHT; break; + case 3: button = XINPUT_DPAD_RIGHT; break; + case 7: button = XINPUT_DPAD_UP | XINPUT_DPAD_RIGHT; break; + case 15: button = XINPUT_DPAD_UP; break; + case 14: button = XINPUT_DPAD_UP | XINPUT_DPAD_LEFT; break; + case 12: button = XINPUT_DPAD_LEFT; break; + case 8: button = XINPUT_DPAD_DOWN | XINPUT_DPAD_LEFT; break; default: button = 0; break; } diff --git a/lib/usb_device/include/usb_device.h b/lib/usb_device/include/usb_device.h index 5c50cc6..5d70efe 100644 --- a/lib/usb_device/include/usb_device.h +++ b/lib/usb_device/include/usb_device.h @@ -43,11 +43,9 @@ class USB_Device void button(uint8_t button, bool val); void dpad(uint8_t dir); - void joystick_left(int16_t x, int16_t y); - void joystick_right(int16_t x, int16_t y); + void joystick(uint8_t id, int16_t x, int16_t y); - void trigger_left(uint8_t val); - void trigger_right(uint8_t val); + void triggers(uint8_t values[2]); void sendReport(); }; diff --git a/lib/usb_device/src/usb_device.cpp b/lib/usb_device/src/usb_device.cpp index 3ccec42..7419089 100644 --- a/lib/usb_device/src/usb_device.cpp +++ b/lib/usb_device/src/usb_device.cpp @@ -48,26 +48,23 @@ void USB_Device::dpad(uint8_t dir) xinput_report.dpad = dir & 0b1111; } -void USB_Device::joystick_left(int16_t x, int16_t y) +void USB_Device::joystick(uint8_t id, int16_t x, int16_t y) { - xinput_report.lx = x; - xinput_report.ly = y; -} - -void USB_Device::joystick_right(int16_t x, int16_t y) -{ - xinput_report.rx = x; - xinput_report.ry = y; -} - -void USB_Device::trigger_left(uint8_t val) -{ - xinput_report.trigger_left = val & 0xFF; + if (id == 0) + { + xinput_report.lx = x; + xinput_report.ly = y; + } + else{ + xinput_report.rx = x; + xinput_report.ry = y; + } } -void USB_Device::trigger_right(uint8_t val) +void USB_Device::triggers(uint8_t values[2]) { - xinput_report.trigger_right = val & 0xFF; + xinput_report.trigger_left = values[0] & 0xFF; + xinput_report.trigger_right = values[1] & 0xFF; } void USB_Device::sendReport() diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp index 1e268dd..bc76489 100644 --- a/src/input_mapper.cpp +++ b/src/input_mapper.cpp @@ -12,13 +12,19 @@ namespace InputMapper TouchDpad tdpad_right; TouchDpad tdpad_left; - TouchControl* tcontrols[] = + TouchControl* tcontrols[2][2] = { - &tjoystick_right, - &tdpad_right, + { + &tjoystick_left, + &tdpad_left, + }, + { + &tjoystick_right, + &tdpad_right, + } }; - uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*); + const uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*[2]); void begin() @@ -29,25 +35,24 @@ namespace InputMapper int32_t pos_y = (103.9 - 31.25) * ppmY; int32_t pos_r = 70 * ppmX / 2; int32_t dead_zone_inner = 3 * ppmX; - int32_t dead_zone_outer = 13 * ppmX; + int32_t dead_zone_outer = 20 * ppmX; + + 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); + + pos_x = 31.25 * ppmX; + pos_y = (103.9 - 31.25) * ppmY; tjoystick_right.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); tjoystick_right.setDeadZoneInner(dead_zone_inner); tjoystick_right.setDeadZoneOuter(dead_zone_outer); - tjoystick_right.setInvertX(); - //tjoystick_right.setInvertY(); - pos_x = (62.5 - 20.636) * ppmX; + pos_x = 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.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4); 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); device.begin(); } @@ -56,13 +61,9 @@ namespace InputMapper { for (uint8_t c = 0; c < num_controls; ++c) { - int8_t res = tcontrols[c]->touch(fid, x, y); - if (res < 0) - { - break; - } + int8_t res = tcontrols[id][c]->touch(fid, x, y); - switch(tcontrols[c]->getControlType()) + switch(tcontrols[id][c]->getControlType()) { case TouchControl::CT_NONE: break; @@ -70,30 +71,50 @@ namespace InputMapper case TouchControl::CT_JOYSTICK: //Serial.printf("%i, %i\n", ((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); //device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); - device.joystick_right(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); + device.joystick(id, ((TouchJoystick*)tcontrols[id][c])->getX(), ((TouchJoystick*)tcontrols[id][c])->getY()); break; case TouchControl::CT_DPAD: - device.dpad(((TouchDpad*)tcontrols[c])->getButton()); + device.dpad(((TouchDpad*)tcontrols[id][c])->getButton()); break; } + // if control is touched return if (res > 0) { - break; + return; } } } void mapTriggers(uint32_t values[2]) { - //device.trigger_left(values[0]); - device.trigger_right(values[1]); + uint8_t mapped_values[2]; + + mapped_values[0] = 0; + + // 160 - 540 + if (values[1] < 160) + { + mapped_values[1] = 0; + } + else if(values[1] >= 540) + { + mapped_values[1] = 0xFF; + } + else + { + mapped_values[1] = (values[1] - 160) * 0xFF / (540 - 160); + } + + device.triggers(mapped_values); } void mapButtons(uint16_t buttons) { - device.buttons(buttons >> 4); + uint16_t mappedButtons = buttons >> 4; + + device.buttons(mappedButtons); } void sendReport() diff --git a/src/main.cpp b/src/main.cpp index 9b92406..dce75e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,8 @@ const uint8_t pin_trackpad_clock[2] = {PB6, PB8}; TrackPad trackpad[2]; // 0 - left, 1 - right +int32_t trackpad_maxX, trackpad_maxY; + void int_touchpad_0(){trackpad[0].int_on_clock();} void int_touchpad_1(){trackpad[1].int_on_clock();} @@ -27,13 +29,14 @@ void setup() pinMode(pin_trigger[1], INPUT_ANALOG); pinMode(pin_trackpad_click[1], INPUT_PULLDOWN); - //attachInterrupt(pin_trackpad_clock[0], int_touchpad_0, FALLING); - //trackpad[0].initialize(pin_trackpad_clock[0], pin_trackpad_data[0]); + attachInterrupt(pin_trackpad_clock[0], int_touchpad_0, FALLING); + trackpad[0].initialize(pin_trackpad_clock[0], pin_trackpad_data[0]); attachInterrupt(pin_trackpad_clock[1], int_touchpad_1, FALLING); trackpad[1].initialize(pin_trackpad_clock[1], pin_trackpad_data[1]); - + trackpad_maxX = trackpad[0].getMaxX(); + trackpad_maxY = trackpad[0].getMaxY(); InputMapper::begin(); @@ -46,39 +49,56 @@ TouchEvent tevent[5]; void loop() { - if (trackpad[1].poll(tevent, tevent_size) > 0) + for (uint8_t t = 0; t < 2; ++t) { - for (uint8_t i = 0; i < tevent_size; ++i) + if (trackpad[t].poll(tevent, tevent_size) > 0) { - int32_t x = -1; - int32_t y = -1; - switch (tevent[i].type) + for (uint8_t i = 0; i < tevent_size; ++i) { - case TET_DOWN: - case TET_MOVE: - - x = tevent[i].fp.x; - y = tevent[i].fp.y; - - break; + int32_t x = -1; + int32_t y = -1; + switch (tevent[i].type) + { + case TET_DOWN: + case TET_MOVE: + + // trackpad is rotated 90 deg so x and y are switched + x = tevent[i].fp.y; + y = tevent[i].fp.x; + + // invert axis for the trackpads + if(t == 0) + { + y = trackpad_maxX - y; + } + else + { + x = trackpad_maxY - x; + } + + break; + + case TET_UP: + + break; + + default: + break; + } - case TET_UP: - - break; - - default: - break; + InputMapper::mapTrackpad(t, tevent[i].finger_id, x, y); } - - InputMapper::mapTrackpad(1, tevent[i].finger_id, y, x); // trackpad is rotated 90 deg so x and y are flipped } } - + uint32_t triggers[] = {0, analogRead(pin_trigger[1])}; + //Serial.printf("T %u %u\n", triggers[0], triggers[1]); + InputMapper::mapTriggers(triggers); uint16_t right_tp_click = digitalRead(pin_trackpad_click[1]); - InputMapper::mapButtons(right_tp_click << 7); // R3 + InputMapper::mapButtons(right_tp_click << (5 + 8)); // B InputMapper::sendReport(); + }