-
Notifications
You must be signed in to change notification settings - Fork 7
/
serial.js
129 lines (103 loc) · 3.23 KB
/
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*global THREE: true,
window: true,
document: true,
requestAnimationFrame: true,
scene: true,
chrome: true,
ArrayBuffer: true,
Uint8Array: true,
Int8Array: true
*/
"use strict";
var connectionId;
// Convert string to ArrayBuffer
var convertStringToArrayBuffer = function (str) {
var i,
buf = new ArrayBuffer(str.length),
bufView = new Uint8Array(buf);
for (i = 0; i < str.length; i += 1) {
bufView[i] = str.charCodeAt(i);
}
return buf;
};
var imuData = {a: [0, 0, 0], g: [0, 0, 0], m: [0, 0, 0], b: 0};
var myString = "";
function ascii2strings(buffer) {
var lineStart = 0,
lineEnd = 0,
i,
j,
inBytes = new Int8Array(buffer);
// Scan all input bytes looking for a line ending
for (i = 0; i < inBytes.length; i += 1) {
// Do we have a line ending here?
if ((inBytes[i] === 0x0d) || (inBytes[i] === 0x0a)) {
lineEnd = i;
// Convert bytes of the line to a string
for (j = lineStart; j < lineEnd; j += 1) {
myString += String.fromCharCode(inBytes[j]);
}
// Emit the line as a string
try {
imuData = JSON.parse(myString);
} catch (ignore) {
}
myString = "";
lineStart = lineEnd + 1;
}
}
// Are there any trailing bytes?
if (lineStart !== inBytes.length) {
// Convert trailing bytes string, a part line ready for next call.
for (i = lineEnd + 1; i < inBytes.length; i += 1) {
myString += String.fromCharCode(inBytes[i]);
}
}
}
var onReceiveCallback = function (info) {
if (info.connectionId === connectionId && info.data) {
ascii2strings(info.data);
}
};
chrome.serial.onReceive.addListener(onReceiveCallback);
var onDisconnect = function (result) {
if (result) {
console.log("Disconnected from the serial port");
} else {
console.log("Disconnect failed");
}
};
var writeSerial;
var onSend = function (sendInfo) {
console.log("Serial data sent!");
if (sendInfo.error) {
console.log("Serial port send failed");
} else {
//console.log("Sent bytes = ", sendInfo.bytesSent);
writeSerial("All work and no play make Jack a dull boy");
//chrome.serial.disconnect(connectionId, onDisconnect);
}
};
var writeSerial = function (str) {
chrome.serial.send(connectionId, convertStringToArrayBuffer(str), onSend);
};
var onGetDevices = function (ports) {
var port;
if (ports.length > 0) {
for (port = 0; port < ports.length; port += 1) {
console.log(ports[port].path);
}
} else {
console.log("No serial ports found");
}
};
chrome.serial.getDevices(onGetDevices);
var onConnect = function (connectionInfo) {
// The serial port has been opened. Save its id to use later.
console.log("Connected to: ", connectionInfo);
connectionId = connectionInfo.connectionId;
// Do whatever you need to do with the opened port.
//writeSerial("Hello world!");
};
// Connect to the serial port
chrome.serial.connect("/dev/ttyACM0", {bitrate: 115200}, onConnect);