-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmr.js
130 lines (114 loc) · 2.85 KB
/
gmr.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
// 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 bmr{
constructor (runtime){
this.runtime = runtime;
// communication related
this.comm = runtime.ioDevices.comm;
this.session = null;
this.runtime.registerPeripheralExtension('bmr', 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: 'bmr',
name: 'boolean message reciever',
color1: '#ffe500',
color2: '#ebff00',
menuIconURI: menuIconURI,
blockIconURI: blockIconURI,
blocks: [
{
opcode: 'bmr',
blockType: BlockType.BOOLEAN,
arguments: {
when I receive ( ): {
type: ArgumentType.BOOLEAN
}
},
text: '[when I receive ( )]'
},
{
opcode: 'bmr2',
blockType: BlockType.REPORTER,
arguments: {
when I receive ( ): {
type: ArgumentType.STRING
}
},
text: '[when I receive ( )]'
},
{
opcode: 'bmr3',
blockType: BlockType.COMMAND,
arguments: {
when I receive ( ): {
type: ArgumentType.STRING
}
},
text: '[when I receive ( )]'
}
]
}
}
bmr (args, util){
const when I receive ( ) = args.when I receive ( );
return this.write(`M0 \n`);
}
bmr2 (args, util){
const when I receive ( ) = args.when I receive ( );
return this.write(`M0 \n`);
}
bmr3 (args, util){
const when I receive ( ) = args.when I receive ( );
return this.write(`M0 \n`);
}
}
module.exports = bmr;