Skip to content

Commit

Permalink
Updating chrome plugin to v8 to improve connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jguille2 committed May 22, 2023
1 parent 6a9a278 commit 631e356
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
43 changes: 22 additions & 21 deletions src/platforms/web/chromium/crx/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Dispatcher.prototype.getBoard = function (boardId) {

Dispatcher.prototype.closeSerial = function (boardId) {
Boards[boardId].reset();
Boards[boardId].sp.close();
if (Boards[boardId].connectionId >= 0) {Boards[boardId].sp.close();}
Boards[boardId] = null;
};

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

this.options.serial.onReceiveError.addListener(function(info){

switch (info.error) {

case 'disconnected':
case 'device_lost':
case 'system_error':
err = new Error('Disconnected');
// send notification of disconnect
if (self.options.disconnectedCallback) {
self.options.disconnectedCallback(err);
} else {
self.emit('disconnect', err);
}
if(self.connectionId >= 0){
self.close();
}
break;
case 'timeout':
break;
if (self.connectionId == info.connectionId) {
switch (info.error) {

case 'disconnected':
case 'device_lost':
case 'system_error':
err = new Error('Disconnected');
// send notification of disconnect
if (self.options.disconnectedCallback) {
self.options.disconnectedCallback(err);
} else {
self.emit('disconnect', err);
}
if(self.connectionId >= 0){
self.close();
}
break;
case 'timeout':
break;
}
}

});

this.path = path;
Expand Down Expand Up @@ -8029,6 +8029,7 @@ function Board(port, options, callback) {
}.bind(this));

this.transport.on("disconnect", function() {
this.version.major = null;
this.emit("disconnect");
}.bind(this));

Expand Down
5 changes: 3 additions & 2 deletions src/platforms/web/chromium/crx/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Snap4Arduino connector",
"description": "Connect Berkeley Snap! to Arduino boards",
"version": "7.0",
"version": "8.0",
"manifest_version": 2,
"app": {
"background": {
Expand All @@ -26,7 +26,8 @@
"https://educaciodigital.cat/*",
"https://cesire.cat/*",
"https://www.robolot.online/*",
"https://campus.innovadidactic.com/*"
"https://campus.innovadidactic.com/*",
"https://snap2make.click/*"
],
"accepts_tls_channel_id": false
},
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/web/chromium/root/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
}
world = new WorldMorph(document.getElementById('world'));

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

ide = new IDE_Morph();
ide.openIn(world);
Expand Down
15 changes: 14 additions & 1 deletion src/platforms/web/chromium/root/plugin/arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ window.onunload = function (evt) {
ide.sprites.asArray().forEach(function (each) { each.arduino.disconnect(true); });
};

Arduino.prototype.keepAlive = function () {
this.board.getVersion();
if (Arduino.keepAlive) {
if (this.board.version.major == null) {
// Connection dropped! Let's disconnect!
this.gotUnplugged = true;
this.board.sp.close();
this.closeHandler();
}
}
};

Arduino.prototype.connect = function (port) {
var myself = this;

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

myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 5000);
myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 3000);

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

// Fake Buffer definition, needed by some Firmata extensions
Expand Down

0 comments on commit 631e356

Please sign in to comment.