-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (160 loc) · 6.91 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const util = require('util');
const uinput = require('uinput');
const robot = require("robotjs");
const HID = require('node-hid');
const fs = require('fs')
let keyPressed = 0, rightKeyPressed = 0, lastCoordinates, keyPressedDown = new Date();
let keyUpEvent;
let calibrationMode = true;
let device;
//Use tslib to get calibration data : TSLIB_CONFFILE=/etc/ts.conf TSLIB_CALIBFILE=/etc/pointercal TSLIB_FBDEVICE=/dev/fb0 TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate
//sudo chmod 666 /dev/uinput
let tsLibConfig = [16319, 43, -7601248, -56, 8325, -1279244, 65536, 800, 480, 0];
let handleTouchScreenEvent;
async function initialize() {
//Scan for hid devices
const devices = HID.devices();
console.info(`Number of HID devices available : ${devices.length}`);
const deviceInfo = devices.find(function (d) {
console.info(`Hid device found - vendorId : ${d.vendorId} productId : ${d.productId}`)
let isWvTouchscreen;
if (isWvTouchscreen = (d.vendorId === 0x0EEF && d.productId === 0x0005)) {
console.info(`Waveshare touchscreen found`);
}
return isWvTouchscreen;
});
//Check if tslib configuration file is available
const pathTsConfiguration = '/etc/pointercal';
if (fs.existsSync(pathTsConfiguration)) {
const fsReadFilePromise = util.promisify(fs.readFile);
const tsConfiguration = await fsReadFilePromise(pathTsConfiguration, 'utf8');
tsLibConfig = tsConfiguration.split(' ').map((d) => +d);
calibrationMode = false;
console.info(`Reading tslib calibration data in \'${pathTsConfiguration}\'. Calibration data : \'${JSON.stringify(tsLibConfig)}\'`);
}
else {
console.info(`\nCalibration mode activated. Run TsLib to get calibration data.
Example of command to run: TSLIB_CONFFILE=/etc/ts.conf TSLIB_CALIBFILE=/etc/pointercal TSLIB_FBDEVICE=/dev/fb0 TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate
Ensure that the current user has the required access rights to \'/dev/uinput\'.\n`)
}
if (calibrationMode) {
//Initialize the touchscreen uinput events needed for the calibration with tslib (https://github.com/kergoth/tslib)
//const uinputSetupPromise = util.promisify(uinput.setup);
//const uiInputCreatePromise = util.promisify(uinput.create);
const setupOptions = { EV_ABS: [uinput.ABS_X, uinput.ABS_Y], EV_KEY: [uinput.BTN_LEFT, uinput.BTN_RIGHT] };
const createOptions = {
name: 'virtualtouchscreen',
id: {
bustype: uinput.BUS_VIRTUAL,
vendor: 0x1,
product: 0x1,
version: 1
}
};
const uiInput = await uinput.setup(setupOptions);
await uiInput.create(createOptions);
handleTouchScreenEvent = (data) => {
if (!keyUpEvent) {
clearTimeout(keyUpEvent);
}
if (0xAA === data[0] && 0x01 === data[1]) {
const coordinates = {
x: parseInt(data[2].toString(16).padStart(2, '0') + data[3].toString(16).padStart(2, '0'), 16),
y: parseInt(data[4].toString(16).padStart(2, '0') + data[5].toString(16).padStart(2, '0'), 16)
};
uiInput.sendEvent(uinput.EV_ABS, uinput.ABS_X, coordinates.x, function (err) {
if (err) {
throw (err);
}
});
uiInput.sendEvent(uinput.EV_ABS, uinput.ABS_Y, coordinates.y, function (err) {
if (err) {
throw (err);
}
});
keyPressed = 1;
}
else if (0xAA === data[0] && 0x00 === data[1] && 1 === keyPressed) {
keyUpEvent = setTimeout(() => {
uiInput.keyEvent(uinput.BTN_LEFT, function (err) {
if (err) {
throw (err);
}
});
keyPressed = 0;
keyUpEvent = undefined;
}, 1000);
}
}
}
else {
//Initialize the callbacks to move the mouse pointer using RobotJs
var screenSize = robot.getScreenSize();
console.info('Screensize : ', screenSize);
robot.setMouseDelay(2);
handleTouchScreenEvent = (data) => {
if (0xAA === data[0] && 0x01 === data[1]) {
let coordinates = {
x: parseInt(data[2].toString(16).padStart(2, '0') + data[3].toString(16).padStart(2, '0'), 16),
y: parseInt(data[4].toString(16).padStart(2, '0') + data[5].toString(16).padStart(2, '0'), 16)
};
coordinates = {
x: (tsLibConfig[2] + tsLibConfig[0] * coordinates.x + tsLibConfig[1] * coordinates.y) / tsLibConfig[6],
y: (tsLibConfig[5] + tsLibConfig[3] * coordinates.x + tsLibConfig[4] * coordinates.y) / tsLibConfig[6]
};
if (0 === rightKeyPressed) {
robot.moveMouse(coordinates.x, coordinates.y);
}
if (0 === keyPressed && 0 === rightKeyPressed) {
lastCoordinates = coordinates;
keyPressedDown = new Date();
keyPressed = 1;
}
else if (0 === rightKeyPressed && lastCoordinates && (new Date().getTime() - keyPressedDown.getTime()) > 1000
&& Math.sqrt(Math.pow(coordinates.x - lastCoordinates.x, 2) + Math.pow(coordinates.y - lastCoordinates.y, 2)) < 20) {
rightKeyPressed = 1;
robot.mouseClick('right');
setTimeout(() => {
rightKeyPressed = 0;
keyPressed = 0;
}, 2000);
}
}
else if (keyPressed > 0 && 0 === rightKeyPressed) {
if (!keyUpEvent) {
clearTimeout(keyUpEvent);
}
keyUpEvent = setTimeout(() => {
if (keyPressed <= 2) {
robot.mouseClick();
}
else {
robot.mouseClick('left', true);
}
keyUpEvent = undefined;
keyPressed = 0;
}, 500);
keyPressed++;
}
return keyPressed;
}
}
return { deviceInfo };
}
const startListening = async () => {
try {
const info = await initialize();
//Configure hid device listener
if (info.deviceInfo) {
device = new HID.HID(info.deviceInfo.path);
device.on("data", function (data) {
handleTouchScreenEvent(data);
});
}
}
catch (e) {
console.error(e, e.stack);
throw e;
}
};
startListening();