-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (98 loc) · 2.42 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
// create by scratch3-extension generator
const ArgumentType = Scratch.ArgumentType;
const BlockType = Scratch.BlockType;
const formatMessage = Scratch.formatMessage;
const log = Scratch.log;
const menuIconURI = null;
const blockIconURI = null;
class testExt{
constructor (runtime){
this.runtime = runtime;
// communication related
this.comm = runtime.ioDevices.comm;
this.session = null;
this.runtime.registerPeripheralExtension('testExt', this);
// session callbacks
this.reporter = null;
this.onmessage = this.onmessage.bind(this);
this.onclose = this.onclose.bind(this);
this.write = this.write.bind(this);
// string op
this.decoder = new TextDecoder();
this.lineBuffer = '';
}
onclose (){
this.session = null;
}
write (data, parser = null){
if (this.session){
return new Promise(resolve => {
if (parser){
this.reporter = {
parser,
resolve
}
}
this.session.write(data);
})
}
}
onmessage (data){
const dataStr = this.decoder.decode(data);
this.lineBuffer += dataStr;
if (this.lineBuffer.indexOf('\n') !== -1){
const lines = this.lineBuffer.split('\n');
this.lineBuffer = lines.pop();
for (const l of lines){
if (this.reporter){
const {parser, resolve} = this.reporter;
resolve(parser(l));
};
}
}
}
scan (){
this.comm.getDeviceList().then(result => {
this.runtime.emit(this.runtime.constructor.PERIPHERAL_LIST_UPDATE, result);
});
}
getInfo (){
return {
id: 'testExt',
name: 'Test',
color1: '#0FBD8C',
color2: '#0DA57A',
menuIconURI: menuIconURI,
blockIconURI: blockIconURI,
blocks: [
{
opcode: 'Distance',
blockType: BlockType.REPORTER,
arguments: {
X1: {
type: ArgumentType.STRING
},
Y1: {
type: ArgumentType.STRING
},
X2: {
type: ArgumentType.STRING
},
Y2: {
type: ArgumentType.STRING
}
},
text: 'distance from x: [X1] y: [Y1] to x: [X2] y: [Y2]'
}
]
}
}
Distance (args, util){
const X1 = args.X1;
const Y1 = args.Y1;
const X2 = args.X2;
const Y2 = args.Y2;
return this.write(`bruh`);
}
}
module.exports = testExt;