Added trackpad with a touch joystick on it
parent
1832bdca43
commit
189cb612b1
@ -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;
|
||||
}
|
@ -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…
Reference in New Issue