2014-02-21 13:47:30 +00:00
|
|
|
function port_handler() {
|
2014-02-21 20:12:19 +00:00
|
|
|
this.main_timeout_reference;
|
2014-02-21 13:47:30 +00:00
|
|
|
this.initial_ports = false;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
this.port_detected_callbacks = [];
|
|
|
|
this.port_removed_callbacks = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
port_handler.prototype.initialize = function() {
|
2014-02-21 20:12:19 +00:00
|
|
|
// start listening, check after 250ms
|
|
|
|
this.check();
|
|
|
|
};
|
|
|
|
|
|
|
|
port_handler.prototype.check = function() {
|
2014-02-21 13:47:30 +00:00
|
|
|
var self = this;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
serial.getDevices(function(current_ports) {
|
|
|
|
// port got removed or initial_ports wasn't initialized yet
|
|
|
|
if (self.array_difference(self.initial_ports, current_ports).length > 0 || !self.initial_ports) {
|
|
|
|
var removed_ports = self.array_difference(self.initial_ports, current_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
if (self.initial_ports != false) {
|
|
|
|
if (removed_ports.length > 1) {
|
2014-02-24 10:54:54 +00:00
|
|
|
console.log('PortHandler - Removed: ' + removed_ports);
|
2014-02-21 13:47:30 +00:00
|
|
|
} else {
|
2014-02-24 10:54:54 +00:00
|
|
|
console.log('PortHandler - Removed: ' + removed_ports[0]);
|
2014-02-21 13:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// disconnect "UI" if necessary
|
|
|
|
// Keep in mind that this routine can not fire during atmega32u4 reboot procedure !!!
|
|
|
|
if (GUI.connected_to) {
|
|
|
|
for (var i = 0; i < removed_ports.length; i++) {
|
|
|
|
if (removed_ports[i] == GUI.connected_to) {
|
|
|
|
$('div#port-picker a.connect').click();
|
2014-02-21 13:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 20:12:19 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
self.update_port_select(current_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// trigger callbacks (only after initialization)
|
|
|
|
if (self.initial_ports) {
|
2014-02-24 11:44:33 +00:00
|
|
|
for (var i = (self.port_removed_callbacks.length - 1); i >= 0; i--) {
|
2014-02-21 20:12:19 +00:00
|
|
|
var obj = self.port_removed_callbacks[i];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
// remove timeout
|
2014-02-21 20:12:19 +00:00
|
|
|
clearTimeout(obj.timer);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
// trigger callback
|
2014-02-21 20:12:19 +00:00
|
|
|
obj.code(removed_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-07 16:44:11 +00:00
|
|
|
// remove object from array
|
|
|
|
var index = self.port_removed_callbacks.indexOf(obj);
|
|
|
|
if (index > -1) self.port_removed_callbacks.splice(index, 1);
|
2014-02-21 13:47:30 +00:00
|
|
|
}
|
2014-02-21 20:12:19 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// auto-select last used port (only during initialization)
|
|
|
|
if (!self.initial_ports) {
|
|
|
|
chrome.storage.local.get('last_used_port', function(result) {
|
|
|
|
// if last_used_port was set, we try to select it
|
2014-03-08 05:25:15 +00:00
|
|
|
if (result.last_used_port) {
|
2014-02-21 20:12:19 +00:00
|
|
|
current_ports.forEach(function(port) {
|
|
|
|
if (port == result.last_used_port) {
|
|
|
|
console.log('Selecting last used port: ' + result.last_used_port);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').val(result.last_used_port);
|
2014-02-21 20:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Last used port wasn\'t saved "yet", auto-select disabled.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
if (!self.initial_ports) {
|
|
|
|
// initialize
|
2014-02-21 13:47:30 +00:00
|
|
|
self.initial_ports = current_ports;
|
2014-02-21 20:12:19 +00:00
|
|
|
} else {
|
|
|
|
for (var i = 0; i < removed_ports.length; i++) {
|
|
|
|
self.initial_ports.splice(self.initial_ports.indexOf(removed_ports[i]), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// new port detected
|
|
|
|
var new_ports = self.array_difference(current_ports, self.initial_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
if (new_ports.length) {
|
|
|
|
if (new_ports.length > 1) {
|
2014-02-24 10:54:54 +00:00
|
|
|
console.log('PortHandler - Found: ' + new_ports);
|
2014-02-21 20:12:19 +00:00
|
|
|
} else {
|
2014-02-24 10:54:54 +00:00
|
|
|
console.log('PortHandler - Found: ' + new_ports[0]);
|
2014-02-21 13:47:30 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
self.update_port_select(current_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// select / highlight new port, if connected -> select connected port
|
|
|
|
if (!GUI.connected_to) {
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').val(new_ports[0]);
|
2014-03-08 05:25:15 +00:00
|
|
|
} else {
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').val(GUI.connected_to);
|
2014-02-21 20:12:19 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// start connect procedure (if statement is valid)
|
|
|
|
if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) {
|
|
|
|
// we need firmware flasher protection over here
|
|
|
|
if (GUI.active_tab != 'firmware_flasher') {
|
|
|
|
GUI.timeout_add('auto-connect_timeout', function() {
|
|
|
|
$('div#port-picker a.connect').click();
|
|
|
|
}, 50); // small timeout so we won't get any nasty connect errors due to system initializing the bus
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// trigger callbacks
|
2014-02-24 11:44:33 +00:00
|
|
|
for (var i = (self.port_detected_callbacks.length - 1); i >= 0; i--) {
|
2014-02-21 20:12:19 +00:00
|
|
|
var obj = self.port_detected_callbacks[i];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// remove timeout
|
|
|
|
clearTimeout(obj.timer);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
// trigger callback
|
|
|
|
obj.code(new_ports);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-07 16:44:11 +00:00
|
|
|
// remove object from array
|
|
|
|
var index = self.port_detected_callbacks.indexOf(obj);
|
|
|
|
if (index > -1) self.port_detected_callbacks.splice(index, 1);
|
2014-02-21 20:12:19 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 20:12:19 +00:00
|
|
|
self.initial_ports = current_ports;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-06-16 20:00:08 +00:00
|
|
|
if (GUI.optional_usb_permissions) {
|
|
|
|
check_usb_devices();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.main_timeout_reference = setTimeout(function() {
|
|
|
|
self.check();
|
|
|
|
}, 250);
|
2014-02-21 20:12:19 +00:00
|
|
|
});
|
2014-06-08 18:10:07 +00:00
|
|
|
|
|
|
|
function check_usb_devices() {
|
|
|
|
chrome.usb.getDevices(usbDevices.STM32DFU, function(result) {
|
|
|
|
if (result.length) {
|
2014-06-20 12:29:16 +00:00
|
|
|
if (!$("div#port-picker #port [value='DFU']").length) {
|
|
|
|
$('div#port-picker #port').append('<option value="DFU">DFU</option>');
|
|
|
|
$('div#port-picker #port').val('DFU');
|
2014-06-08 18:10:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-20 12:29:16 +00:00
|
|
|
if ($("div#port-picker #port [value='DFU']").length) {
|
|
|
|
$("div#port-picker #port [value='DFU']").remove();
|
2014-06-08 18:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-02-21 13:47:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port_handler.prototype.update_port_select = function(ports) {
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').html(''); // drop previous one
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
if (ports.length > 0) {
|
|
|
|
for (var i = 0; i < ports.length; i++) {
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').append($("<option/>", {value: ports[i], text: ports[i]}));
|
2014-02-21 13:47:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-20 12:29:16 +00:00
|
|
|
$('div#port-picker #port').append($("<option/>", {value: 0, text: 'No Ports'}));
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2014-02-21 13:47:30 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
port_handler.prototype.port_detected = function(name, code, timeout, ignore_timeout) {
|
2014-02-21 13:47:30 +00:00
|
|
|
var self = this;
|
2014-02-24 10:54:54 +00:00
|
|
|
var obj = {'name': name, 'code': code, 'timeout': (timeout) ? timeout : 10000};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
if (!ignore_timeout) {
|
|
|
|
obj.timer = setTimeout(function() {
|
|
|
|
console.log('PortHandler - timeout - ' + obj.name);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
// trigger callback
|
|
|
|
code(false);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-07 16:44:11 +00:00
|
|
|
// remove object from array
|
|
|
|
var index = self.port_detected_callbacks.indexOf(obj);
|
|
|
|
if (index > -1) self.port_detected_callbacks.splice(index, 1);
|
2014-02-26 06:23:50 +00:00
|
|
|
}, (timeout) ? timeout : 10000);
|
|
|
|
} else {
|
|
|
|
obj.timer = false;
|
|
|
|
obj.timeout = false;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
this.port_detected_callbacks.push(obj);
|
2014-02-24 10:54:54 +00:00
|
|
|
|
|
|
|
return obj;
|
2014-02-21 13:47:30 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
port_handler.prototype.port_removed = function(name, code, timeout, ignore_timeout) {
|
2014-02-21 13:47:30 +00:00
|
|
|
var self = this;
|
2014-02-24 10:54:54 +00:00
|
|
|
var obj = {'name': name, 'code': code, 'timeout': (timeout) ? timeout : 10000};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
if (!ignore_timeout) {
|
|
|
|
obj.timer = setTimeout(function() {
|
|
|
|
console.log('PortHandler - timeout - ' + obj.name);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
// trigger callback
|
|
|
|
code(false);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-07 16:44:11 +00:00
|
|
|
// remove object from array
|
|
|
|
var index = self.port_removed_callbacks.indexOf(obj);
|
|
|
|
if (index > -1) self.port_removed_callbacks.splice(index, 1);
|
2014-02-26 06:23:50 +00:00
|
|
|
}, (timeout) ? timeout : 10000);
|
|
|
|
} else {
|
|
|
|
obj.timer = false;
|
|
|
|
obj.timeout = false;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
this.port_removed_callbacks.push(obj);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-24 10:54:54 +00:00
|
|
|
return obj;
|
2014-02-21 13:47:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// accepting single level array with "value" as key
|
|
|
|
port_handler.prototype.array_difference = function(firstArray, secondArray) {
|
|
|
|
var cloneArray = [];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
// create hardcopy
|
|
|
|
for (var i = 0; i < firstArray.length; i++) {
|
|
|
|
cloneArray.push(firstArray[i]);
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
for (var i = 0; i < secondArray.length; i++) {
|
|
|
|
if (cloneArray.indexOf(secondArray[i]) != -1) {
|
|
|
|
cloneArray.splice(cloneArray.indexOf(secondArray[i]), 1);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
return cloneArray;
|
|
|
|
};
|
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
port_handler.prototype.flush_callbacks = function() {
|
|
|
|
var killed = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
|
|
|
for (var i = this.port_detected_callbacks.length - 1; i >= 0; i--) {
|
2014-02-26 06:23:50 +00:00
|
|
|
if (this.port_detected_callbacks[i].timer) clearTimeout(this.port_detected_callbacks[i].timer);
|
|
|
|
this.port_detected_callbacks.splice(i, 1);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
killed++;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
for (var i = this.port_removed_callbacks.length - 1; i >= 0; i--) {
|
|
|
|
if (this.port_removed_callbacks[i].timer) clearTimeout(this.port_removed_callbacks[i].timer);
|
|
|
|
this.port_removed_callbacks.splice(i, 1);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-26 06:23:50 +00:00
|
|
|
killed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return killed;
|
|
|
|
};
|
|
|
|
|
2014-02-21 13:47:30 +00:00
|
|
|
var PortHandler = new port_handler();
|