-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscratch2mip.js
157 lines (140 loc) · 4.57 KB
/
scratch2mip.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
(function(ext) {
var ws;
var when_near = false;
var when_far = false;
var when_clear = false;
var radar = '';
ext._shutdown = function() {};
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
ext.connect = function() {
ws = new WebSocket('ws://localhost:8888');
ws.onmessage = function(evt) {
data = JSON.parse(evt.data);
if (data.command == 'radar') {
if (data.status == 'clear') {
when_clear = true;
radar = 'clear';
} else if (data.status == 'far') {
when_far = true;
radar = 'far';
} else if (data.status == 'near') {
when_near = true;
radar = 'near';
}
}
}
}
ext.forward = function(steps) {
if (ws) {
ws.send(JSON.stringify({command: 'forward', steps: steps}));
}
};
ext.backward = function(steps) {
if (ws) {
ws.send(JSON.stringify({command: 'backward', steps: steps}));
}
}
ext.right = function(degrees) {
if (ws) {
ws.send(JSON.stringify({command: 'right', degrees: degrees}));
}
};
ext.left = function(degrees) {
if (ws) {
ws.send(JSON.stringify({command: 'left', degrees: degrees}));
}
}
ext.set_radar_on = function() {
if (ws) {
ws.send(JSON.stringify({command: 'set_radar_on'}));
}
}
ext.play_sound = function(sound_id) {
if (ws) {
ws.send(JSON.stringify({command: 'play_sound', sound_id: sound_id}));
}
}
ext.set_chest_led = function(r, g, b) {
if (ws) {
ws.send(JSON.stringify({command: 'set_chest_led', r: r, g: g, b: b}));
}
}
ext.when_clear = function() {
if (when_clear) {
when_clear = false;
return true;
}
return false;
}
ext.when_far = function() {
if (when_far) {
when_far = false;
return true;
}
return false;
}
ext.when_near = function() {
if (when_near) {
when_near = false;
return true;
}
return false;
}
ext.get_radar = function() {
return radar;
}
var lang = ((navigator.language || navigator.userLanguage) == 'ja') ? 'ja' : 'en';
var locale = {
ja: {
connect: '接続する',
turn_right: '右に %n 度回す',
turn_left: '左に %n 度回す',
move_forward: '%n 歩前進させる',
move_backward: '%n 歩後退させる',
set_radar_on: 'レーダーをオンにする',
play_sound: '音を鳴らす id: %n (1~106)',
set_chest_led: '胸のLEDを点ける R: %n G: %n B: %n',
when_clear: '障害物がないとき',
when_far: '障害物が遠いとき',
when_near: '障害物が近いとき',
get_radar: '障害物との距離(1:なし 2:遠い 3:近い)'
},
en: {
connect: 'connect',
turn_right: 'turn right %n degrees',
turn_left: 'turn left %n degrees',
move_forward: 'move forward %n steps',
move_backward: 'move backward %n steps',
set_radar_on: 'set radar on',
play_sound: 'play sound id: %n (1~106)',
set_chest_led: 'set chest led with R: %n G: %n B: %n',
when_clear: 'when clear',
when_far: 'when far',
when_near: 'when near',
get_radar: 'get radar(1:clear 2:far 3:near)'
},
}
var descriptor = {
blocks: [
[' ', 'MiP: ' + locale[lang].connect, 'connect'],
[' ', 'MiP: ' + locale[lang].turn_right, 'right', 90],
[' ', 'MiP: ' + locale[lang].turn_left, 'left', 90],
[' ', 'MiP: ' + locale[lang].move_forward, 'forward'],
[' ', 'MiP: ' + locale[lang].move_backward, 'backward'],
[' ', 'MiP: ' + locale[lang].set_radar_on, 'set_radar_on'],
[' ', 'MiP: ' + locale[lang].play_sound, 'play_sound', 1],
[' ', 'MiP: ' + locale[lang].set_chest_led, 'set_chest_led', 255, 0, 0],
['h', 'MiP: ' + locale[lang].when_clear, 'when_clear'],
['h', 'MiP: ' + locale[lang].when_far, 'when_far'],
['h', 'MiP: ' + locale[lang].when_near, 'when_near'],
['r', 'MiP: ' + locale[lang].get_radar, 'get_radar'],
],
menus: {
// radar_mode: ['radar', 'gesture']
radar_mode: ['radar']
}
};
ScratchExtensions.register('Scratch2MiP', descriptor, ext);
})({});