Added trackpad with a touch joystick on it

input_events
NepEgor 3 years ago
parent 1832bdca43
commit 189cb612b1

@ -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

@ -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

@ -0,0 +1,38 @@
#ifndef TOUCH_JOYSTICK_H
#define TOUCH_JOYSTICK_H
#include <stdint.h>
#include <stddef.h>
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

@ -0,0 +1,6 @@
{
"name": "touch_joystick",
"version": "1.0.0",
"description": "",
"keywords": ""
}

@ -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;
}

@ -22,4 +22,10 @@ build_flags =
-D USBD_VID=0x0483
-D USBD_PID=0x0483
-D USB_MANUFACTURER="Goshi"
-D USB_PRODUCT="\"BLACKPILL_F411CE\""
-D USB_PRODUCT="\"BLACKPILL_F411CE\""
; test
[env:native]
platform = native

@ -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);
}

@ -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),

@ -0,0 +1,37 @@
#include "touch_joystick.h"
#include <stdio.h>
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;
}
Loading…
Cancel
Save