-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb.js
108 lines (97 loc) · 3.19 KB
/
rgb.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
(function() {
'use strict';
document.addEventListener('DOMContentLoaded', event => {
let connectButton = document.querySelector("#connect");
let getdocButton = document.querySelector("#getdoc");
let showdocButton = document.querySelector("#showdoc");
let statusDisplay = document.querySelector('#status');
let redSlider = document.querySelector('#red');
let greenSlider = document.querySelector('#green');
let blueSlider = document.querySelector('#blue');
let ztdoc = document.querySelector('#ZTDoc');
let port;
let docStr;
getdocButton.textContent = '5';
function connect() {
port.connect().then(() => {
statusDisplay.textContent = '';
connectButton.textContent = 'Disconnect';
ztdoc.innerHTML = '';
docStr = '';
port.onReceive = data => {
let textDecoder = new TextDecoder();
let str = textDecoder.decode(data);
//let str = String.fromCharCode.apply(null, new Uint16Array(data));
docStr += str;
//ztdoc.insertAdjacentHTML('afterend',textDecoder.decode(data));
//ztdoc.innerHTML += textDecoder.decode(data);
//console.log("charCode\n" + str);
//console.log("decode\n" + textDecoder.decode(data));
}
port.onReceiveError = error => {
console.error(error);
};
}, error => {
statusDisplay.textContent = error;
});
}
function onUpdate() {
if (!port) {
return;
}
/*
let view = new Uint8Array(3);
view[0] = parseInt(redSlider.value);
view[1] = parseInt(greenSlider.value);
view[2] = parseInt(blueSlider.value);
port.send(view);
var str = "Google";
let buffer = new ArrayBuffer(1);
let view = new Uint8Array(buffer);
view[0] = str.charCodeAt(0);
// new DataView(buffer).setUint8(0, 'G', true;
port.send(buffer);
*/
};
redSlider.addEventListener('input', onUpdate);
greenSlider.addEventListener('input', onUpdate);
blueSlider.addEventListener('input', onUpdate);
connectButton.addEventListener('click', function() {
if (port) {
port.disconnect();
connectButton.textContent = 'Connect';
statusDisplay.textContent = '';
port = null;
} else {
serial.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
statusDisplay.textContent = error;
});
}
});
getdocButton.addEventListener('click', function() {
getdocButton.textContent = 'working';
var str = "Google";
let buffer = new ArrayBuffer(1);
let view = new Uint8Array(buffer);
view[0] = str.charCodeAt(0);
port.send(buffer);
});
showdocButton.addEventListener('click', function() {
showdocButton.textContent = 'working';
ztdoc.innerHTML = docStr;
console.log(docStr);
});
serial.getPorts().then(ports => {
if (ports.length == 0) {
statusDisplay.textContent = 'No device found.';
} else {
statusDisplay.textContent = 'Connecting...';
port = ports[0];
connect();
}
});
});
})();