2014-01-18 10:44:43 +00:00
|
|
|
var serial = {
|
2014-07-01 02:13:33 +00:00
|
|
|
connectionId: -1,
|
|
|
|
bitrate: 0,
|
|
|
|
bytes_received: 0,
|
|
|
|
bytes_sent: 0,
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-28 23:59:39 +00:00
|
|
|
transmitting: false,
|
|
|
|
output_buffer: [],
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
connect: function(path, options, callback) {
|
|
|
|
var self = this;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
chrome.serial.connect(path, options, function(connectionInfo) {
|
2014-02-03 09:02:49 +00:00
|
|
|
if (connectionInfo !== undefined) {
|
|
|
|
self.connectionId = connectionInfo.connectionId;
|
2014-03-28 23:59:39 +00:00
|
|
|
self.bitrate = connectionInfo.bitrate;
|
2014-02-03 09:02:49 +00:00
|
|
|
self.bytes_received = 0;
|
|
|
|
self.bytes_sent = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-03 09:02:49 +00:00
|
|
|
self.onReceive.addListener(function log_bytes_received(info) {
|
|
|
|
self.bytes_received += info.data.byteLength;
|
|
|
|
});
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-06-28 12:25:11 +00:00
|
|
|
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
|
|
|
|
console.error(info);
|
2014-07-16 11:07:24 +00:00
|
|
|
googleAnalytics.sendException('Serial: ' + info.error, false);
|
2014-06-28 12:25:11 +00:00
|
|
|
|
2014-07-01 10:48:20 +00:00
|
|
|
switch (info.error) {
|
|
|
|
case 'system_error': // we might be able to recover from this one
|
|
|
|
chrome.serial.setPaused(self.connectionId, false, get_status);
|
|
|
|
|
|
|
|
function get_status() {
|
|
|
|
self.getInfo(crunch_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
function crunch_status(info) {
|
|
|
|
if (!info.paused) {
|
|
|
|
console.log('SERIAL: Connection recovered from last onReceiveError');
|
2014-07-16 11:07:24 +00:00
|
|
|
googleAnalytics.sendException('Serial: onReceiveError - recovered', false);
|
2014-07-01 10:48:20 +00:00
|
|
|
} else {
|
|
|
|
console.log('SERIAL: Connection did not recover from last onReceiveError, disconnecting');
|
|
|
|
GUI.log('Unrecoverable <span style="color: red">failure</span> of serial connection, disconnecting...');
|
2014-07-16 11:07:24 +00:00
|
|
|
googleAnalytics.sendException('Serial: onReceiveError - unrecoverable', false);
|
2014-07-01 10:48:20 +00:00
|
|
|
|
2014-07-02 11:54:48 +00:00
|
|
|
if (GUI.connected_to || GUI.connecting_to) {
|
2014-07-01 10:48:20 +00:00
|
|
|
$('a.connect').click();
|
|
|
|
} else {
|
|
|
|
self.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'timeout':
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case 'device_lost':
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case 'disconnected':
|
|
|
|
// TODO
|
|
|
|
break;
|
2014-07-01 02:13:33 +00:00
|
|
|
}
|
2014-06-28 12:25:11 +00:00
|
|
|
});
|
|
|
|
|
2014-01-25 12:48:31 +00:00
|
|
|
console.log('SERIAL: Connection opened with ID: ' + connectionInfo.connectionId + ', Baud: ' + connectionInfo.bitrate);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-07-01 02:13:33 +00:00
|
|
|
if (callback) callback(connectionInfo);
|
2014-02-03 09:02:49 +00:00
|
|
|
} else {
|
|
|
|
console.log('SERIAL: Failed to open serial port');
|
2014-07-16 11:07:24 +00:00
|
|
|
googleAnalytics.sendException('Serial: FailedToOpen', false);
|
2014-07-01 02:13:33 +00:00
|
|
|
if (callback) callback(false);
|
2014-01-25 12:48:31 +00:00
|
|
|
}
|
2014-01-18 10:44:43 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
disconnect: function(callback) {
|
|
|
|
var self = this;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 23:48:21 +00:00
|
|
|
self.empty_output_buffer();
|
2014-01-27 00:48:34 +00:00
|
|
|
|
|
|
|
// remove listeners
|
2014-01-31 14:51:47 +00:00
|
|
|
for (var i = (self.onReceive.listeners.length - 1); i >= 0; i--) {
|
|
|
|
self.onReceive.removeListener(self.onReceive.listeners[i]);
|
2014-01-27 00:48:34 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-06-28 12:25:11 +00:00
|
|
|
for (var i = (self.onReceiveError.listeners.length - 1); i >= 0; i--) {
|
|
|
|
self.onReceiveError.removeListener(self.onReceiveError.listeners[i]);
|
|
|
|
}
|
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
chrome.serial.disconnect(this.connectionId, function(result) {
|
2014-01-25 12:48:31 +00:00
|
|
|
if (result) {
|
|
|
|
console.log('SERIAL: Connection with ID: ' + self.connectionId + ' closed');
|
|
|
|
} else {
|
|
|
|
console.log('SERIAL: Failed to close connection with ID: ' + self.connectionId + ' closed');
|
2014-07-16 11:07:24 +00:00
|
|
|
googleAnalytics.sendException('Serial: FailedToClose', false);
|
2014-01-25 12:48:31 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-27 20:14:49 +00:00
|
|
|
console.log('SERIAL: Statistics - Sent: ' + self.bytes_sent + ' bytes, Received: ' + self.bytes_received + ' bytes');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
self.connectionId = -1;
|
2014-03-28 23:59:39 +00:00
|
|
|
self.bitrate = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-07-01 02:13:33 +00:00
|
|
|
if (callback) callback(result);
|
2014-01-18 10:44:43 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getDevices: function(callback) {
|
|
|
|
chrome.serial.getDevices(function(devices_array) {
|
|
|
|
var devices = [];
|
|
|
|
devices_array.forEach(function(device) {
|
|
|
|
devices.push(device.path);
|
|
|
|
});
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-18 10:44:43 +00:00
|
|
|
callback(devices);
|
|
|
|
});
|
|
|
|
},
|
2014-07-01 10:48:20 +00:00
|
|
|
getInfo: function(callback) {
|
|
|
|
chrome.serial.getInfo(this.connectionId, callback);
|
|
|
|
},
|
2014-05-02 23:03:24 +00:00
|
|
|
getControlSignals: function(callback) {
|
|
|
|
chrome.serial.getControlSignals(this.connectionId, callback);
|
|
|
|
},
|
2014-07-16 12:06:15 +00:00
|
|
|
setControlSignals: function(signals, callback) {
|
|
|
|
chrome.serial.setControlSignals(this.connectionId, signals, callback);
|
|
|
|
},
|
2014-01-18 10:44:43 +00:00
|
|
|
send: function(data, callback) {
|
2014-01-26 19:01:46 +00:00
|
|
|
var self = this;
|
|
|
|
self.output_buffer.push({'data': data, 'callback': callback});
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 19:01:46 +00:00
|
|
|
if (!self.transmitting) {
|
|
|
|
self.transmitting = true;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-29 21:27:21 +00:00
|
|
|
function sending() {
|
2014-01-26 20:37:19 +00:00
|
|
|
// store inside separate variables in case array gets destroyed
|
|
|
|
var data = self.output_buffer[0].data;
|
|
|
|
var callback = self.output_buffer[0].callback;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 20:37:19 +00:00
|
|
|
chrome.serial.send(self.connectionId, data, function(sendInfo) {
|
|
|
|
callback(sendInfo);
|
2014-01-26 19:01:46 +00:00
|
|
|
self.output_buffer.shift();
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-27 00:48:34 +00:00
|
|
|
self.bytes_sent += sendInfo.bytesSent;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 19:19:47 +00:00
|
|
|
if (self.output_buffer.length) {
|
2014-01-26 20:37:19 +00:00
|
|
|
// keep the buffer withing reasonable limits
|
|
|
|
while (self.output_buffer.length > 500) {
|
|
|
|
self.output_buffer.pop();
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 19:01:46 +00:00
|
|
|
sending();
|
|
|
|
} else {
|
|
|
|
self.transmitting = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-26 19:01:46 +00:00
|
|
|
sending();
|
|
|
|
}
|
2014-01-18 10:44:43 +00:00
|
|
|
},
|
2014-01-31 14:51:47 +00:00
|
|
|
onReceive: {
|
|
|
|
listeners: [],
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-31 14:51:47 +00:00
|
|
|
addListener: function(function_reference) {
|
|
|
|
var listener = chrome.serial.onReceive.addListener(function_reference);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-31 14:51:47 +00:00
|
|
|
this.listeners.push(function_reference);
|
|
|
|
},
|
2014-03-08 05:25:15 +00:00
|
|
|
removeListener: function(function_reference) {
|
2014-01-31 14:51:47 +00:00
|
|
|
for (var i = (this.listeners.length - 1); i >= 0; i--) {
|
|
|
|
if (this.listeners[i] == function_reference) {
|
|
|
|
chrome.serial.onReceive.removeListener(function_reference);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-31 14:51:47 +00:00
|
|
|
this.listeners.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2014-01-31 14:51:47 +00:00
|
|
|
}
|
|
|
|
},
|
2014-06-28 12:25:11 +00:00
|
|
|
onReceiveError: {
|
|
|
|
listeners: [],
|
|
|
|
|
|
|
|
addListener: function(function_reference) {
|
|
|
|
var listener = chrome.serial.onReceiveError.addListener(function_reference);
|
|
|
|
|
|
|
|
this.listeners.push(function_reference);
|
|
|
|
},
|
|
|
|
removeListener: function(function_reference) {
|
|
|
|
for (var i = (this.listeners.length - 1); i >= 0; i--) {
|
|
|
|
if (this.listeners[i] == function_reference) {
|
|
|
|
chrome.serial.onReceiveError.removeListener(function_reference);
|
|
|
|
|
|
|
|
this.listeners.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-01-26 20:37:19 +00:00
|
|
|
empty_output_buffer: function() {
|
|
|
|
this.output_buffer = [];
|
|
|
|
this.transmitting = false;
|
2014-01-18 10:44:43 +00:00
|
|
|
}
|
|
|
|
};
|