-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserial_chrome_serial.js
105 lines (90 loc) · 3.42 KB
/
serial_chrome_serial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
Copyright 2012 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Renato Mangini (mangini@chromium.org)
Author: Luis Leao (luisleao@gmail.com)
Author: Gordon Williams (gw@pur3.co.uk)
**/
(function() {
if (typeof chrome === 'undefined' || chrome.serial===undefined) {
console.log("No chrome.serial - Chrome Serial disabled");
return;
}
if (chrome.serial.getDevices===undefined) {
// wrong chrome version
console.log("Chrome does NOT have post-M33 serial API");
return;
}
var connectionInfo;
// var connectedPort; // unused?
var connectionDisconnectCallback;
var connectionReadCallback;
var getPorts=function(callback) {
chrome.serial.getDevices(function(devices) {
var prefix = "";
// Workaround for Chrome v34 bug - http://forum.espruino.com/conversations/1056/#comment16121
// In this case, ports are reported as ttyACM0 - not /dev/ttyACM0
if (navigator.userAgent.indexOf("Linux")>=0) {
var hasSlashes = false;
devices.forEach(function(device) { if (device.path.indexOf("/")>=0) hasSlashes=true; });
if (!hasSlashes) prefix = "/dev/";
}
callback(devices.map(function(device) {
return {
path : prefix+device.path,
description : device.displayName,
usb : [device.vendorId, device.productId]};
}), true/*instantPorts*/);
});
};
var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
connectionReadCallback = receiveCallback;
connectionDisconnectCallback = disconnectCallback;
chrome.serial.connect(serialPort, {bitrate: parseInt(Espruino.Config.BAUD_RATE)},
function(cInfo) {
if (!cInfo) {
console.log("Unable to open device (connectionInfo="+cInfo+")");
openCallback(undefined);
} else {
connectionInfo = cInfo;
// connectedPort = serialPort;
console.log(cInfo);
openCallback(cInfo);
}
});
};
var closeSerial=function() {
if (connectionInfo)
chrome.serial.disconnect(connectionInfo.connectionId, connectionDisconnectCallback);
connectionReadCallback = undefined;
connectionDisconnectCallback = undefined;
connectionInfo=null;
};
var writeSerial = function(data, callback) {
chrome.serial.send(connectionInfo.connectionId, Espruino.Core.Utils.stringToArrayBuffer(data), callback);
};
// ----------------------------------------------------------
chrome.serial.onReceive.addListener(function(receiveInfo) {
if (connectionReadCallback!==undefined)
connectionReadCallback(receiveInfo.data);
});
chrome.serial.onReceiveError.addListener(function(errorInfo) {
console.error("RECEIVE ERROR:", JSON.stringify(errorInfo));
closeSerial();
});
Espruino.Core.Serial.devices.push({
"name" : "Chrome Serial",
"getPorts": getPorts,
"open": openSerial,
"write": writeSerial,
"close": closeSerial,
});
})();