From aff9eb40b0b61bea52859743509defea9e8057b1 Mon Sep 17 00:00:00 2001 From: Sergiu Weisz Date: Thu, 16 Jun 2022 09:51:45 +0200 Subject: [PATCH] Add triggers and analog push functionality --- skprx/main.c | 35 ++++++++++++++++++++++++++++++----- skprx/uapi/vitastick_uapi.h | 1 + skprx/usb_descriptors.h | 4 ++-- skprx/vitastick.yml | 1 + vpk/CMakeLists.txt | 1 + vpk/main.c | 28 ++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 7 deletions(-) diff --git a/skprx/main.c b/skprx/main.c index 3159c84..5bfb176 100644 --- a/skprx/main.c +++ b/skprx/main.c @@ -16,7 +16,7 @@ struct gamepad_report_t { uint8_t report_id; - uint16_t buttons; + uint32_t buttons; int8_t left_x; int8_t left_y; int8_t right_x; @@ -33,6 +33,7 @@ static SceUID usb_thread_id; static SceUID usb_event_flag_id; static int vitastick_driver_registered = 0; static int vitastick_driver_activated = 0; +uint8_t l2, r2, l3, r3; static int send_hid_report_desc(void) { @@ -133,10 +134,10 @@ static void fill_gamepad_report(const SceCtrlData *pad, struct gamepad_report_t if (pad->buttons & SCE_CTRL_START) gamepad->buttons |= 1 << 9; - if (pad->buttons & SCE_CTRL_L3) - gamepad->buttons |= 1 << 10; - if (pad->buttons & SCE_CTRL_R3) - gamepad->buttons |= 1 << 11; + //if (pad->buttons & SCE_CTRL_L3) + // gamepad->buttons |= 1 << 10; + //if (pad->buttons & SCE_CTRL_R3) + // gamepad->buttons |= 1 << 11; if (pad->buttons & SCE_CTRL_UP) gamepad->buttons |= 1 << 12; @@ -146,6 +147,14 @@ static void fill_gamepad_report(const SceCtrlData *pad, struct gamepad_report_t gamepad->buttons |= 1 << 14; if (pad->buttons & SCE_CTRL_LEFT) gamepad->buttons |= 1 << 15; + if (l2) + gamepad->buttons |= 1 << 16; + if (r2) + gamepad->buttons |= 1 << 17; + if (l3) + gamepad->buttons |= 1 << 10; + if (r3) + gamepad->buttons |= 1 << 11; gamepad->left_x = (int8_t)pad->lx - 128; gamepad->left_y = (int8_t)pad->ly - 128; @@ -539,3 +548,19 @@ int module_stop(SceSize argc, const void *args) return SCE_KERNEL_STOP_SUCCESS; } + +int upload_trigger_state(uint8_t triggers) +{ + unsigned long state; + int ret; + + ENTER_SYSCALL(state); + + l2 = triggers & (1 << 0); + r2 = triggers & (1 << 1); + l3 = triggers & (1 << 2); + r3 = triggers & (1 << 3); + + EXIT_SYSCALL(state); + return 0; +} diff --git a/skprx/uapi/vitastick_uapi.h b/skprx/uapi/vitastick_uapi.h index b6c137a..4a0145e 100644 --- a/skprx/uapi/vitastick_uapi.h +++ b/skprx/uapi/vitastick_uapi.h @@ -11,6 +11,7 @@ extern "C" { int vitastick_start(void); int vitastick_stop(void); +int upload_trigger_state(uint8_t triggers); #ifdef __cplusplus } diff --git a/skprx/usb_descriptors.h b/skprx/usb_descriptors.h index e69d1e1..a4b9479 100644 --- a/skprx/usb_descriptors.h +++ b/skprx/usb_descriptors.h @@ -7,10 +7,10 @@ unsigned char hid_report_descriptor[] __attribute__ ((aligned(64))) = { 0x85, 0x01, // Report ID (1) 0x05, 0x09, // Usage Page (Button) 0x19, 0x01, // Usage Minimum (0x01) - 0x29, 0x10, // Usage Maximum (0x10) + 0x29, 0x12, // Usage Maximum (0x12) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) - 0x95, 0x10, // Report Count (16) + 0x95, 0x20, // Report Count (32) 0x75, 0x01, // Report Size (1) 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) diff --git a/skprx/vitastick.yml b/skprx/vitastick.yml index e80a32f..42921cb 100644 --- a/skprx/vitastick.yml +++ b/skprx/vitastick.yml @@ -12,3 +12,4 @@ vitastick: functions: - vitastick_start - vitastick_stop + - upload_trigger_state diff --git a/vpk/CMakeLists.txt b/vpk/CMakeLists.txt index 694bf2c..1d28956 100644 --- a/vpk/CMakeLists.txt +++ b/vpk/CMakeLists.txt @@ -43,6 +43,7 @@ target_link_libraries(${SHORT_NAME} SceDisplay_stub SceCtrl_stub ScePower_stub + SceTouch_stub ) vita_create_self(${SHORT_NAME}.self ${SHORT_NAME}) diff --git a/vpk/main.c b/vpk/main.c index 1150717..af3c6eb 100644 --- a/vpk/main.c +++ b/vpk/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "vitastick_uapi.h" #include "debugScreen.h" @@ -49,8 +50,35 @@ void wait_key_press(const char *key_desc, unsigned int key_mask) SceCtrlData pad; printf("Press %s to exit.\n", key_desc); + sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, 1); + sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, 1); + sceTouchEnableTouchForce(SCE_TOUCH_PORT_FRONT); + sceTouchEnableTouchForce(SCE_TOUCH_PORT_BACK); + + SceTouchData touch_old[SCE_TOUCH_PORT_MAX_NUM]; + SceTouchData touch[SCE_TOUCH_PORT_MAX_NUM]; while (1) { + memcpy(touch_old, touch, sizeof(touch_old)); + int i; + uint8_t triggers = 0; + + /* sample both back and front surfaces */ + sceTouchPeek(SCE_TOUCH_PORT_BACK, &touch[SCE_TOUCH_PORT_BACK], 1); + + /* print every touch coordinates on that surface */ + for (i = 0; i < touch[SCE_TOUCH_PORT_BACK].reportNum; i++) { + if (touch[SCE_TOUCH_PORT_BACK].report[i].x < 800 && touch[SCE_TOUCH_PORT_BACK].report[i].y > 400) + triggers |= (1 << 0); + if (touch[SCE_TOUCH_PORT_BACK].report[i].x > 800 && touch[SCE_TOUCH_PORT_BACK].report[i].y > 400) + triggers |= (1 << 1); + if (touch[SCE_TOUCH_PORT_BACK].report[i].x > 800 && touch[SCE_TOUCH_PORT_BACK].report[i].y < 400) + triggers |= (1 << 2); + if (touch[SCE_TOUCH_PORT_BACK].report[i].x < 800 && touch[SCE_TOUCH_PORT_BACK].report[i].y < 400) + triggers |= (1 << 3); + } + + upload_trigger_state(triggers); sceCtrlReadBufferPositive(0, &pad, 1); if ((pad.buttons & key_mask) == key_mask) break;