From 189cb612b178e5c1f2c96ad179e30811e56ea77a Mon Sep 17 00:00:00 2001 From: NepEgor Date: Mon, 18 Oct 2021 23:46:29 +0300 Subject: [PATCH] Added trackpad with a touch joystick on it --- Readme.md | 5 +- include/usbd_descriptors.h | 2 +- lib/touch_joystick/include/touch_joystick.h | 38 +++++++++ lib/touch_joystick/library.json | 6 ++ lib/touch_joystick/src/touch_joystick.cpp | 41 ++++++++++ platformio.ini | 8 +- src/main.cpp | 85 ++++++++++++++++----- src/usbd_decriptors.cpp | 10 +-- test/test_1/test_touch_joystick.cpp | 37 +++++++++ 9 files changed, 205 insertions(+), 27 deletions(-) create mode 100644 lib/touch_joystick/include/touch_joystick.h create mode 100644 lib/touch_joystick/library.json create mode 100644 lib/touch_joystick/src/touch_joystick.cpp create mode 100644 test/test_1/test_touch_joystick.cpp diff --git a/Readme.md b/Readme.md index a8803ed..40b49f5 100644 --- a/Readme.md +++ b/Readme.md @@ -4,10 +4,13 @@ Game controller with trackpads Inspired by Steam Controller Hardware: -* We Act studios Black Pill stm32f411ce +* WeAct Studio Black Pill stm32f411ce * elan 33200v-3600 * Bunch of 3d prints +Requires ps2elantech library +https://github.com/NepEgor/ps2elantech + Credits: * hid_def: https://github.com/katyo/hid_def diff --git a/include/usbd_descriptors.h b/include/usbd_descriptors.h index a2623c5..17a38fe 100644 --- a/include/usbd_descriptors.h +++ b/include/usbd_descriptors.h @@ -6,7 +6,7 @@ #define USB_HID_CUSTOM_CONFIG_DESC_SIZ 34U #define USB_HID_CUSTOM_DESC_SIZ 9U #define USB_LEN_DEV_QUALIFIER_DESC 0x0AU -#define HID_CUSTOM_REPORT_DESC_SIZE 29U +#define HID_CUSTOM_REPORT_DESC_SIZE 30U //#define HID_KEYBOARD_REPORT_DESC_SIZE 45U diff --git a/lib/touch_joystick/include/touch_joystick.h b/lib/touch_joystick/include/touch_joystick.h new file mode 100644 index 0000000..421a16b --- /dev/null +++ b/lib/touch_joystick/include/touch_joystick.h @@ -0,0 +1,38 @@ +#ifndef TOUCH_JOYSTICK_H +#define TOUCH_JOYSTICK_H + +#include +#include + +class TouchJoystick +{ + private: + + // position of joystick on trackpad + int32_t pos_x; + int32_t pos_y; + int32_t pos_r; + int32_t pos_r2; // pos_r ^ 2 + + // joystick values for usb report + int16_t x; + int16_t y; + + int16_t max_x; + int16_t max_y; + + float pos2usb_x; + float pos2usb_y; + + public: + + TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t max_x, int16_t max_y); + + uint8_t touch(int32_t tx, int32_t ty, int16_t* rx = NULL, int16_t* ry = NULL); + + int16_t getX() {return x;} + int16_t getY() {return y;} + +}; + +#endif \ No newline at end of file diff --git a/lib/touch_joystick/library.json b/lib/touch_joystick/library.json new file mode 100644 index 0000000..4a8bbb4 --- /dev/null +++ b/lib/touch_joystick/library.json @@ -0,0 +1,6 @@ +{ + "name": "touch_joystick", + "version": "1.0.0", + "description": "", + "keywords": "" +} \ No newline at end of file diff --git a/lib/touch_joystick/src/touch_joystick.cpp b/lib/touch_joystick/src/touch_joystick.cpp new file mode 100644 index 0000000..962a928 --- /dev/null +++ b/lib/touch_joystick/src/touch_joystick.cpp @@ -0,0 +1,41 @@ +#include "touch_joystick.h" + +TouchJoystick::TouchJoystick(int32_t pos_x, int32_t pos_y, int32_t pos_r, int16_t max_x, int16_t max_y) +{ + this->pos_x = pos_x; + this->pos_y = pos_y; + this->pos_r = pos_r; + this->pos_r2 = pos_r * pos_r; + + this->max_x = max_x; + this->max_y = max_y; + + this->pos2usb_x = (float)max_x / pos_r; + this->pos2usb_y = (float)max_y / pos_r; +} + +uint8_t TouchJoystick::touch(int32_t tx, int32_t ty, int16_t* rx, int16_t* ry) +{ + tx -= pos_x; + ty -= pos_y; + + // if out of joystick bounds return error + if ((tx * tx) + (ty * ty) > pos_r2) + { + x = 0; + y = 0; + + return 1; + } + + x = tx * pos2usb_x; + y = ty * pos2usb_y; + + if (rx != NULL && ry != NULL) + { + *rx = x; + *ry = y; + } + + return 0; +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 6d91827..1a4daef 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,4 +22,10 @@ build_flags = -D USBD_VID=0x0483 -D USBD_PID=0x0483 -D USB_MANUFACTURER="Goshi" - -D USB_PRODUCT="\"BLACKPILL_F411CE\"" \ No newline at end of file + -D USB_PRODUCT="\"BLACKPILL_F411CE\"" + + +; test +[env:native] +platform = native + diff --git a/src/main.cpp b/src/main.cpp index ec3dca3..5b73a09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,20 +4,45 @@ //USBD_Device device; +#include "trackpad.h" +TrackPad trackpad_right(0); +TrackPad trackpad_left(1); + +void int_touchpad_right(){trackpad_right.int_on_clock();} +void int_touchpad_left(){trackpad_left.int_on_clock();} + const uint8_t TRIGGER_RIGHT_PIN = PA3; const uint8_t TRACKPAD_CLICK_RIGHT_PIN = PB4; +const uint8_t DATA_PIN_right = PB9; +const uint8_t CLOCK_PIN_right = PB8; +const uint8_t DATA_PIN_left = PB7; +const uint8_t CLOCK_PIN_left = PB6; + +#include "touch_joystick.h" +// x_max 3276 +// y_max 1872 +const int32_t pos_x = 31.25 * 1872.0 / 62.5; +const int32_t pos_y = (103.9 - 31.25) * 3276.0 / 103.9; +const int32_t pos_r = 60 * 1872.0 / 62.5 / 2; +const int16_t max_x = 0x7FFF; +const int16_t max_y = -0x7FFF; +TouchJoystick tjoystick(pos_x, pos_y, pos_r, max_x, max_y); typedef struct { - uint16_t x:16; - uint16_t y:16; - uint16_t r:16; -} __packed PedalsReport_t; + int16_t x:16; + int16_t y:16; + int16_t r:16; +} __packed TestReport_t; -PedalsReport_t PedalsReport; +TestReport_t testReport; void setup() { + // Turn on LED + pinMode(PC13, OUTPUT); + digitalWrite(PC13, LOW); + Serial.begin(256000); pinMode(TRIGGER_RIGHT_PIN, INPUT_ANALOG); @@ -25,13 +50,18 @@ void setup() //device.begin(); - printf("HID Init\n"); - HID_Custom_Init(); - printf("HID Init success\n"); + testReport = { 0, 0, 0 }; + + attachInterrupt(CLOCK_PIN_right, int_touchpad_right, FALLING); + trackpad_right.initialize(CLOCK_PIN_right, DATA_PIN_right); - PedalsReport = { 0, 1024/3, 2048/3 }; + //attachInterrupt(CLOCK_PIN_left, int_touchpad_left, FALLING); + //trackpad_left.initialize(CLOCK_PIN_left, DATA_PIN_left); + + // Turn off LED + digitalWrite(PC13, HIGH); } void loop() @@ -42,15 +72,32 @@ void loop() //Serial.printf("RT: %u RTPC: %u\n", right_trigger, right_tp_click); - PedalsReport.x = (PedalsReport.x + 10) % 1024; - PedalsReport.y = (PedalsReport.y + 20) % 1024; - PedalsReport.r = (PedalsReport.r + 30) % 1024; - - //Serial.printf("%u %u %u\n", PedalsReport.x, PedalsReport.y, PedalsReport.r); - - //device.move(PedalsReport.x, PedalsReport.y); + testReport.r = (int16_t)(right_trigger % 1024); - HID_Custom_sendReport((uint8_t*)(&PedalsReport), 6); - - delay(100); + FingerPosition* fp; + int8_t fingers_touching = trackpad_right.poll(&fp); + + if (fingers_touching > 0) + { + if (fp != NULL) + { + int16_t x, y; + tjoystick.touch(fp[0].y, fp[0].x, &x, &y); + testReport.x = x; + testReport.y = y; + } + } + else if (fingers_touching == 0) + { + testReport.x = 0; + testReport.y = 0; + } + + //Serial.printf("%u %u %u\n", testReport.x, testReport.y, testReport.r); + + //device.move(testReport.x, testReport.y); + + HID_Custom_sendReport((uint8_t*)(&testReport), 6); + + //delay(100); } diff --git a/src/usbd_decriptors.cpp b/src/usbd_decriptors.cpp index 3fb8cb8..b1b7dcc 100644 --- a/src/usbd_decriptors.cpp +++ b/src/usbd_decriptors.cpp @@ -175,11 +175,11 @@ __ALIGN_BEGIN uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE] __ALIG HID_COLLECTION(APPLICATION), HID_COLLECTION(PHYSICAL), HID_USAGE_PAGE(GENERIC_DESKTOP), - HID_USAGE(SLIDER), - HID_USAGE(RY), - HID_USAGE(RX), - HID_LOGICAL_MINIMUM(1, 0), - HID_LOGICAL_MAXIMUM(2, 1023), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(Z), + HID_LOGICAL_MINIMUM(2, -32767), + HID_LOGICAL_MAXIMUM(2, 32767), HID_REPORT_SIZE(16), HID_REPORT_COUNT(3), HID_INPUT(DATA, VARIABLE, ABSOLUTE), diff --git a/test/test_1/test_touch_joystick.cpp b/test/test_1/test_touch_joystick.cpp new file mode 100644 index 0000000..f510d27 --- /dev/null +++ b/test/test_1/test_touch_joystick.cpp @@ -0,0 +1,37 @@ +#include "touch_joystick.h" + +#include + +int main() +{ + printf("\n"); + + int32_t pos_x = 150; + int32_t pos_y = 350; + int32_t pos_r = 2000; + + int16_t max_x = 1023; + int16_t max_y = 1023; + + TouchJoystick tjoystick(pos_x, pos_y, pos_r, max_x, max_y); + + int32_t touch_x = 10000; + int32_t touch_y = 200; + + int16_t x; + int16_t y; + + if (!tjoystick.touch(touch_x, touch_y, &x, &y)) + { + printf("USB coords: (%i, %i)\n", x, y); + } + else + { + printf("Touch out of bounds\n"); + } + + + printf("\n"); + + return 0; +} \ No newline at end of file