-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
99 lines (88 loc) · 2.44 KB
/
utils.js
File metadata and controls
99 lines (88 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import HID from "node-hid";
import robot from "robotjs";
import {
BUTTONS,
LEFT_SENSE_PRODUCT_ID,
RIGHT_SENSE_PRODUCT_ID,
SONY_VENDOR_ID,
} from "./constants.js";
import { mapInputs } from "./mapper.js";
export const getSenseDevices = () => {
const devices = HID.devices();
const senseControllers = devices.filter(
(device) =>
(device.vendorId === SONY_VENDOR_ID &&
device.productId === LEFT_SENSE_PRODUCT_ID) ||
device.productId === RIGHT_SENSE_PRODUCT_ID
);
if (senseControllers.length !== 2) {
throw new Error(
"☠️ Could not detect Sense Controllers. Make sure to connect both controllers via Bluetooth."
);
}
return true;
};
export const readRawInput = (device) => {
let lastData = null;
const interval = 24;
device.getFeatureReport(0x05, 128);
const prevState = {};
device.on("data", (data) => {
const inputs = mapInputs(data);
Object.entries(inputs).map(([key, value]) => {
lastData = data;
const currentState = !!value;
if (!!currentState && !prevState[key]) {
inputHandler(key);
}
prevState[key] = currentState;
});
});
setInterval(() => {
if (lastData) {
// moveMouseWithGyro(lastData);
moveMouseWithStick(lastData);
}
}, interval);
};
export const inputHandler = (key) => {
switch (key) {
case BUTTONS.SQUARE:
case BUTTONS.CROSS:
robot.mouseClick();
break;
case BUTTONS.L2:
case BUTTONS.R2:
robot.mouseClick("right");
break;
default:
break;
}
};
export const normalizeStick = (value) => {
return (value - 128) / 128;
};
export const moveMouseWithStick = (data) => {
const xInput = normalizeStick(data[2]);
const yInput = normalizeStick(data[3]);
const deadzone = 0.15;
const speed = 17;
if (Math.abs(xInput) > deadzone || Math.abs(yInput) > deadzone) {
const { x, y } = robot.getMousePos();
robot.moveMouse(x + xInput * speed, y + yInput * speed);
}
};
export const normalizeGyro = (low, high) => {
let value = (high << 8) | low;
return value > 0x7fff ? value - 0x10000 : value;
};
export const moveMouseWithGyro = (data) => {
const gyrox = normalizeGyro(data[17], data[18]);
const gyroy = normalizeGyro(data[21], data[22]);
const deadzone = 70;
const speed = 0.08;
if (Math.abs(gyrox) > deadzone || Math.abs(gyroy) > deadzone) {
let { x, y } = robot.getMousePos();
robot.moveMouse(x - gyroy * speed, y - gyrox * speed);
}
};