-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
326 lines (301 loc) · 8.99 KB
/
index.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
input, textarea, select {
-webkit-transition: all 0.30s ease-in-out;
-moz-transition: all 0.30s ease-in-out;
-ms-transition: all 0.30s ease-in-out;
-o-transition: all 0.30s ease-in-out;
outline: none;
padding: 3px 0px 3px 3px;
border: 1px solid #DDDDDD;
}
input:focus, textarea:focus, select:focus {
box-shadow: 0 0 5px rgba(81, 203, 238, 1);
padding: 3px 0px 3px 3px;
border: 1px solid rgba(81, 203, 238, 1);
}
#log {
height: 20vh;
width: 100%;
overflow: scroll;
background-color: black;
color:white;
white-space: pre;
}
div{
padding-right:2px;
}
.MbPCM{
background-color:DodgerBlue;
color:white;
border:1px solid blue;
border-radius:3px;
padding:2px;
font-size:16px;
margin:2px;
display:inline-block;
}
</style>
<div class="wrapper">
<div>
<button id="connect" onClick="connect()">Connect</button>
<input id="input" type="text" onKeyPress="handle(event)" disabled />
<button id="sendButton" onClick="send()" disabled>Send</button>
<input type="file" id="myFile">
</div>
<div id="log"></div>
<div id="main" style="display:flex;"></div>
</div>
<script>
//<script src="source.js">script>
var input = document.getElementById("myFile");
var ids=0;
var currParent = 0;
var RTNavailable = false;
var lastRTNByte = "";
input.addEventListener("change", function () {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', function (e) {
parseFile(e.target.result);
});
reader.readAsBinaryString(myFile);
}
});
async function gv(devID,regID,mask){
sendtoCOM('2',devID,regID,mask,'0');
return await waitForRTN();
}
function sv(devID,regID,mask,sval){
sendtoCOM('1',devID,regID,mask,sval);
return;
}
async function svr(devID,regID,mask,sval){
sendtoCOM('1',devID,regID,mask,sval);
return await waitForRTN();
}
async function sendTB(devID,regID,mask){
sendtoCOM('3',devID,regID,mask,'0x00');
}
async function waitForRTN(){
while(RTNavailable==false) {
await delay(50);
}
RTNavailable = false;
return lastRTNByte;
}
function parseFile(data){
var lines = data.split("\n");
for(var i=0;i<lines.length;i++){
var paras = lines[i].split("!");
switch(paras[0]){
case "CB": //toggle bit. GUI: checkbox
addCheckBox(paras);
break;
case "BT": //send commands GUI: button
addButton(paras);
break;
case "TB": //value GUI: Text box
contiBox(paras);
break;
case "SO": //setFromOptions GUI: combo box
addCombo(paras);
break;
case "FR": //Frame GUI: div
addFrame(paras[1],paras[2]);
break;
}
}
}
function addFrame(caption,style){
var id = genrateID();
currParent = 0;
var element = addZNode('<div id="'+id+'"><div class="MbPCM"><b>'+caption+'</b></div><div>');
element.setAttribute('style',style);
currParent = id;
}
function addCheckBox(arr){
var id="";if(arr[2]!='0'){id = ' id="'+id+'" ';}
var checked ="";if(arr[3]==1){checked="checked";}else{checked="";}
var onclickScriptWrapper = "async function oclick(e)" + arr[4] + " oclick(this);"; // script format: {script;}
var zNodelabel = addZNode('<div>'+ arr[1] + '</div><input type="checkbox" ' + id + ' onclick="' + onclickScriptWrapper + '" '+ checked +'>');
}
function tb(el,devID,regID,mask){ //direct use as script for checkbox.
if(el.checked==true){
sendtoCOM("1",devID,regID,mask,"1");
}else{
sendtoCOM("1",devID,regID,mask,"0");
}
}
function addButton(arr){ //random id generation,
// BT|Caption|ID|script
var id="";if(arr[2]!='0'){id = 'id="'+id+'"';}
var onclickScriptWrapper = "async function oclick(e)" + arr[3] + " oclick(this);"; // script format: {script;}
var zNode = addZNode('<button '+id+' onclick="'+ onclickScriptWrapper +'">'+ arr[1] +'</button>');
}
function contiBox(arr){
// BT|Caption|ID|script
var id="";if(arr[2]!='0'){id = ' id="'+id+'" ';}
var onclickScriptWrapper = "async function oclick(ev,e){if(ev.key=='Enter')" + arr[4] + "} oclick(event,this);"; // script format: {script;}
var node = addZNode('<div>'+ arr[1] + '</div><input type="text" '+ id +' style="width:29%" value="'+ arr[3] +'" onkeydown="'+ onclickScriptWrapper +'">');
}
function addCombo(arr){
var id="";if(arr[2]!='0'){id = ' id="'+id+'" ';}
var onclickScriptWrapper = "async function oclick(e)" + arr[5] + " oclick(this);"; // script format: {script;}
var ih = '<div>'+ arr[1] + '</div>';
ih += '<select style="width:30%"'+ id +'onchange="'+ onclickScriptWrapper +'">';
var oplist = arr[4].split("|");
for(var i=0;i<oplist.length;i++){
var ops = oplist[i].split(":");
if(i!=arr[3]){
ih += '<option value="' + ops[1] + '">' + ops[0] + '</option>';
}else{
ih += '<option value="' + ops[1] + '" selected>' + ops[0] + '</option>';
}
}
ih += '</select>';
var z = addZNode(ih);
}
function sosend(el,devID,regID,mask){
sendtoCOM('1',devID,regID,mask,el.options[el.selectedIndex].value);
}
function addZNode(text){
var zNodelabel = document.createElement ('div');
zNodelabel.setAttribute("style","display: flex; justify-content: flex-end;margin: 5px 1px 3px 0px;");
zNodelabel.innerHTML = text;
if(currParent==0){
document.getElementById("main").appendChild (zNodelabel);
}else{
document.getElementById(currParent).appendChild (zNodelabel);
}
return zNodelabel;
}
function sendtoCOM(type,devId,regID,mask,value){
var out = '';
out+= String.fromCharCode(parsee(type));
out+= String.fromCharCode(parsee(devId));
out+= String.fromCharCode(parsee(regID));
out+= String.fromCharCode(parsee(mask));
out+= String.fromCharCode(parsee(value));
writeToStream(out);
}
function parsee(ss){
var s = ss + "";
if(s.length<3){
return parseInt(s,10);
}
var radix = s.substring(0,2);
var valu = s.substring(2);
switch(radix){
case "0x":
return parseInt(valu,16);
break;
case "0b":
return parseInt(valu,2);
break;
default:
return parseInt(s,10);
break;
}
}
function genrateID(){
ids++;
return ids;
}
function $(id){
return document.getElementById(id);
}
const delay = ms => new Promise(res => setTimeout(res, ms));
const log = document.getElementById("log")
function send() {
const toSend = document.getElementById("input").value
writeToStream(toSend)
document.getElementById("input").value = ""
}
function handle(e) {
if (e.keyCode === 13) {
e.preventDefault();
send();
}
}
async function connect() {
const inputField = document.getElementById("input");
inputField.disabled = false;
inputField.focus();
inputField.select();
document.getElementById("sendButton").disabled = false;
document.getElementById("connect").disabled = true;
port = await navigator.serial.requestPort();
// - Wait for the port to open.
await port.open({ baudRate: 115200 });
console.log('Open');
let decoder = new TextDecoderStream();
inputDone = port.readable.pipeTo(decoder.writable);
inputStream = decoder.readable;
const encoder = new TextEncoderStream();
outputDone = encoder.readable.pipeTo(port.writable);
outputStream = encoder.writable;
reader = inputStream.getReader();
readLoop();
}
function writeToStream(line) {
const writer = outputStream.getWriter();
//console.log('[SEND]', line);
writer.write(line);
writer.releaseLock();
}
function displayString(str){
var out = "";
for (var i = 0; i < str.length; ++i) {
var code = str.charCodeAt(i);
out += code + ',';
}
console.log('ByteArray:',out);
}
function logout(line){
log.textContent += line; //new line was lost during splitting.
log.scrollTop = log.scrollHeight;
}
async function readLoop() {
console.log('Readloop');
var line = "";
while (true) {
const { value, done } = await reader.read();
//console.log('value', value);
//displayString(value);
line += value;
if(value.slice(-1)=='\n'){ //if line has '\n' in end.
//console.log('Newline detected:',line);
var lines = line.split('\n');
for(var i=0;i<lines.length-1;i++){ //last line is always empty
//console.log(i+'th line:',lines[i]);
if(lines[i].substring(0,5)==':RTN:'){ //if line starts with :RTN:
//console.log(':RTN: detected:',lines[i].substring(5).slice(0,-1));
RTNavailable = true;
lastRTNByte = lines[i].substring(5).slice(0,-1);
}
log.textContent += lines[i] +'\n'; //new line was lost during splitting.
log.scrollTop = log.scrollHeight;
}
line = "";
}
if (done) {
console.log('[readLoop] DONE', done);
reader.releaseLock();
break;
}
}
}
</script>
</body>
</html>