-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystickExtension.js
76 lines (62 loc) · 2.18 KB
/
joystickExtension.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
// joystickExtension.js
// Shane M. Clements, November 2013
// Joystick Scratch Extension
//
// This is an extension for development and testing of the Scratch Javascript Extension API.
new (function() {
var device = null;
var input = null;
var poller = null;
var ext = this;
ext._deviceConnected = function(dev) {
if(device) return;
device = dev;
device.open();
poller = setInterval(function() {
input = device.read(48);
}, 10);
// setInterval(function() { console.log(input); }, 100);
};
ext._deviceRemoved = function(dev) {
if(device != dev) return;
device = null;
stopPolling();
};
function stopPolling() {
if(poller) clearInterval(poller);
poller = null;
}
ext._shutdown = function() {
if(poller) clearInterval(poller);
poller = null;
if(device) device.close();
device = null;
}
ext._getStatus = function() {
if(!device) return {status: 1, msg: 'Controller disconnected'};
return {status: 2, msg: 'Controller connected'};
}
// Converts a byte into a value of the range -1 -> 1 with two decimal places of precision
function convertByteStr(byte) { return (parseInt(byte, 16) - 128) / 128; }
ext.readJoystick = function(name) {
var retval = null;
switch(name) {
case 'leftX': retval = convertByteStr(input[12] + input[13]); break;
case 'leftY': retval = -convertByteStr(input[14] + input[15]); break;
case 'rightX': retval = convertByteStr(input[16] + input[17]); break;
case 'rightY': retval = -convertByteStr(input[18] + input[19]); break;
}
// If it's hardly off center then treat it as centered
if(Math.abs(retval) < 0.1) retval = 0;
return retval.toFixed(2);
}
var descriptor = {
blocks: [
['r', 'get joystick %m.joystickPart', 'readJoystick', 'leftX']
],
menus: {
joystickPart: ['leftX', 'leftY', 'rightX', 'rightY']
}
};
ScratchExtensions.register('Joystick', descriptor, ext, {type: 'hid', vendor:0x054c, product:0x0268});
})();