mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-28 13:39:44 +07:00
111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
const DEVICE_PATH = '/dev/ttyACM0';
|
|
const serial = chrome.serial;
|
|
|
|
/* Interprets an ArrayBuffer as UTF-8 encoded string data. */
|
|
var ab2str = function(buf) {
|
|
var bufView = new Uint8Array(buf);
|
|
var encodedString = String.fromCharCode.apply(null, bufView);
|
|
return decodeURIComponent(escape(encodedString));
|
|
};
|
|
|
|
/* Converts a string to UTF-8 encoding in a Uint8Array; returns the array buffer. */
|
|
var str2ab = function(str) {
|
|
var encodedString = unescape(encodeURIComponent(str));
|
|
var bytes = new Uint8Array(encodedString.length);
|
|
for (var i = 0; i < encodedString.length; ++i) {
|
|
bytes[i] = encodedString.charCodeAt(i);
|
|
}
|
|
return bytes.buffer;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////
|
|
|
|
var SerialConnection = function() {
|
|
this.connectionId = -1;
|
|
this.lineBuffer = "";
|
|
this.boundOnReceive = this.onReceive.bind(this);
|
|
this.boundOnReceiveError = this.onReceiveError.bind(this);
|
|
this.onConnect = new chrome.Event();
|
|
this.onReadLine = new chrome.Event();
|
|
this.onError = new chrome.Event();
|
|
};
|
|
|
|
SerialConnection.prototype.onConnectComplete = function(connectionInfo) {
|
|
if (!connectionInfo) {
|
|
log("Connection failed.");
|
|
return;
|
|
}
|
|
this.connectionId = connectionInfo.connectionId;
|
|
chrome.serial.onReceive.addListener(this.boundOnReceive);
|
|
chrome.serial.onReceiveError.addListener(this.boundOnReceiveError);
|
|
this.onConnect.dispatch();
|
|
};
|
|
|
|
SerialConnection.prototype.onReceive = function(receiveInfo) {
|
|
if (receiveInfo.connectionId !== this.connectionId) {
|
|
return;
|
|
}
|
|
|
|
this.lineBuffer += ab2str(receiveInfo.data);
|
|
|
|
var index;
|
|
while ((index = this.lineBuffer.indexOf('\n')) >= 0) {
|
|
var line = this.lineBuffer.substr(0, index + 1);
|
|
this.onReadLine.dispatch(line);
|
|
this.lineBuffer = this.lineBuffer.substr(index + 1);
|
|
}
|
|
};
|
|
|
|
SerialConnection.prototype.onReceiveError = function(errorInfo) {
|
|
if (errorInfo.connectionId === this.connectionId) {
|
|
this.onError.dispatch(errorInfo.error);
|
|
}
|
|
};
|
|
|
|
SerialConnection.prototype.connect = function(path) {
|
|
serial.connect(path, this.onConnectComplete.bind(this))
|
|
};
|
|
|
|
SerialConnection.prototype.send = function(msg) {
|
|
if (this.connectionId < 0) {
|
|
throw 'Invalid connection';
|
|
}
|
|
serial.send(this.connectionId, str2ab(msg), function() {});
|
|
};
|
|
|
|
SerialConnection.prototype.disconnect = function() {
|
|
if (this.connectionId < 0) {
|
|
throw 'Invalid connection';
|
|
}
|
|
serial.disconnect(this.connectionId, function() {});
|
|
};
|
|
|
|
////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////
|
|
|
|
var connection = new SerialConnection();
|
|
|
|
connection.onConnect.addListener(function() {
|
|
log('connected to: ' + DEVICE_PATH);
|
|
connection.send("hello arduino");
|
|
});
|
|
|
|
connection.onReadLine.addListener(function(line) {
|
|
log('read line: ' + line);
|
|
});
|
|
|
|
connection.connect(DEVICE_PATH);
|
|
|
|
function log(msg) {
|
|
var buffer = document.querySelector('#buffer');
|
|
buffer.innerHTML += msg + '<br/>';
|
|
}
|
|
|
|
var is_on = false;
|
|
document.querySelector('button').addEventListener('click', function() {
|
|
is_on = !is_on;
|
|
connection.send(is_on ? 'y' : 'n');
|
|
});
|
|
|