Skip to content

Commit fe93884

Browse files
author
Ke, Mingze
committed
disconnect boards before execution
1 parent 1acd425 commit fe93884

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ Code.runJS = function () {
565565
var code = Blockly.JavaScript.workspaceToCode(Code.workspace);
566566
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
567567
try {
568-
eval(code);
568+
eval('disconnectBoards(function(){' + code + '});');
569569
} catch (e) {
570570
alert(MSG['badCode'].replace('%1', e));
571571
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webduino-blockly",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"main": "index.js",
55
"description": "The Visual Programming Editor for Webduino",
66
"repository": "https://github.com/webduinoio/webduino-blockly.git",

webduino-blockly.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var boards = [];
12+
1113
function boardReady(options, callback) {
1214
var board;
1315

@@ -22,13 +24,47 @@
2224
board = new webduino.WebArduino(options);
2325
}
2426
board.on(webduino.BoardEvent.READY, callback.bind(null, board));
27+
28+
boards.push(board);
2529
}
2630

2731
function boardError(device, callback) {
2832
var board = new webduino.WebArduino(device);
2933
board.on(webduino.BoardEvent.ERROR, callback.bind(null, board));
3034
}
3135

36+
function disconnectBoards(callback) {
37+
var promises = boards.map(whenClosed);
38+
Promise.all(promises).then(function (results) {
39+
boards = [];
40+
callback(results);
41+
}).catch(function (reason) {
42+
boards = [];
43+
callback(reason);
44+
});
45+
}
46+
47+
function whenClosed(board) {
48+
return new Promise(function (resolve, reject) {
49+
try {
50+
if (board._transport.isOpen) {
51+
board._transport.on('close', function () {
52+
resolve(true);
53+
});
54+
board.close();
55+
} else {
56+
resolve(true);
57+
}
58+
} catch (e) {
59+
reject(e);
60+
}
61+
});
62+
}
63+
64+
function getBoards() {
65+
return boards;
66+
}
67+
3268
function getLed(board, pin) {
3369
return new webduino.module.Led(board, board.getDigitalPin(pin));
3470
}
@@ -215,6 +251,8 @@
215251

216252
scope.boardReady = boardReady;
217253
scope.boardError = boardError;
254+
scope.disconnectBoards = disconnectBoards;
255+
scope.getBoards = getBoards;
218256
scope.getLed = getLed;
219257
scope.getRelay = getRelay;
220258
scope.getRGBLed = getRGBLed;

0 commit comments

Comments
 (0)