Skip to content

Commit 631e356

Browse files
committed
Updating chrome plugin to v8 to improve connections
1 parent 6a9a278 commit 631e356

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

src/platforms/web/chromium/crx/bundle.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Dispatcher.prototype.getBoard = function (boardId) {
4444

4545
Dispatcher.prototype.closeSerial = function (boardId) {
4646
Boards[boardId].reset();
47-
Boards[boardId].sp.close();
47+
if (Boards[boardId].connectionId >= 0) {Boards[boardId].sp.close();}
4848
Boards[boardId] = null;
4949
};
5050

@@ -564,27 +564,27 @@ function SerialPort(path, options, openImmediately, callback) {
564564
this.options = convertOptions(options);
565565

566566
this.options.serial.onReceiveError.addListener(function(info){
567-
568-
switch (info.error) {
569-
570-
case 'disconnected':
571-
case 'device_lost':
572-
case 'system_error':
573-
err = new Error('Disconnected');
574-
// send notification of disconnect
575-
if (self.options.disconnectedCallback) {
576-
self.options.disconnectedCallback(err);
577-
} else {
578-
self.emit('disconnect', err);
579-
}
580-
if(self.connectionId >= 0){
581-
self.close();
582-
}
583-
break;
584-
case 'timeout':
585-
break;
567+
if (self.connectionId == info.connectionId) {
568+
switch (info.error) {
569+
570+
case 'disconnected':
571+
case 'device_lost':
572+
case 'system_error':
573+
err = new Error('Disconnected');
574+
// send notification of disconnect
575+
if (self.options.disconnectedCallback) {
576+
self.options.disconnectedCallback(err);
577+
} else {
578+
self.emit('disconnect', err);
579+
}
580+
if(self.connectionId >= 0){
581+
self.close();
582+
}
583+
break;
584+
case 'timeout':
585+
break;
586+
}
586587
}
587-
588588
});
589589

590590
this.path = path;
@@ -8029,6 +8029,7 @@ function Board(port, options, callback) {
80298029
}.bind(this));
80308030

80318031
this.transport.on("disconnect", function() {
8032+
this.version.major = null;
80328033
this.emit("disconnect");
80338034
}.bind(this));
80348035

src/platforms/web/chromium/crx/manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Snap4Arduino connector",
33
"description": "Connect Berkeley Snap! to Arduino boards",
4-
"version": "7.0",
4+
"version": "8.0",
55
"manifest_version": 2,
66
"app": {
77
"background": {
@@ -26,7 +26,8 @@
2626
"https://educaciodigital.cat/*",
2727
"https://cesire.cat/*",
2828
"https://www.robolot.online/*",
29-
"https://campus.innovadidactic.com/*"
29+
"https://campus.innovadidactic.com/*",
30+
"https://snap2make.click/*"
3031
],
3132
"accepts_tls_channel_id": false
3233
},

src/platforms/web/chromium/root/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
}
6767
world = new WorldMorph(document.getElementById('world'));
6868

69-
// keepAlive should be handled at the plugin side
70-
world.Arduino.keepAlive = false;
69+
// keeping Alive... we need to ask to the plugin side continuosly
70+
world.Arduino.keepAlive = true;
7171

7272
ide = new IDE_Morph();
7373
ide.openIn(world);

src/platforms/web/chromium/root/plugin/arduino.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ window.onunload = function (evt) {
3737
ide.sprites.asArray().forEach(function (each) { each.arduino.disconnect(true); });
3838
};
3939

40+
Arduino.prototype.keepAlive = function () {
41+
this.board.getVersion();
42+
if (Arduino.keepAlive) {
43+
if (this.board.version.major == null) {
44+
// Connection dropped! Let's disconnect!
45+
this.gotUnplugged = true;
46+
this.board.sp.close();
47+
this.closeHandler();
48+
}
49+
}
50+
};
51+
4052
Arduino.prototype.connect = function (port) {
4153
var myself = this;
4254

@@ -59,7 +71,7 @@ Arduino.prototype.connect = function (port) {
5971
// We need to populate the board with functions that make use of the browser plugin
6072
myself.populateBoard(myself.board);
6173

62-
myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 5000);
74+
myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 3000);
6375

6476
// These should be handled at plugin side
6577
// myself.board.sp.on('disconnect', myself.disconnectHandler);
@@ -127,6 +139,7 @@ Arduino.prototype.populateBoard = function (board) {
127139
};
128140
board.i2cReadOnce = function (address, reg, callback) { postal.sendCommand('i2cReadOnce', [board.id, address, reg], function (response) { board['i2cResponse-' + Number(address)] = response; }); };
129141
board.i2cWriteReg = function (address, reg, byte) {postal.sendCommand('i2cWriteReg', [board.id, address, reg, byte]); };
142+
board.getVersion = function () { postal.sendCommand('getBoard', [board.id], function(response) {board.version.major = response.version.major}); };
130143
};
131144

132145
// Fake Buffer definition, needed by some Firmata extensions

0 commit comments

Comments
 (0)