-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_serial_GPIO.html
More file actions
556 lines (503 loc) · 14.4 KB
/
web_serial_GPIO.html
File metadata and controls
556 lines (503 loc) · 14.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>GPIO Test Tool</title>
<style>
h1 { font-size:20pt; background-color:#4F4F4F; color:#FFFFFF; font-weight : normal;}
h2 { font-size:16pt; }
h3 { font-size:8pt; }
fieldset { font-size:12pt; line-height:0.1em; padding: 20px;}
body { font-size:12pt; color:#444444; }
footer{ font-size:4pt; }
</style>
</head>
<body>
<h1><span>GPIOテストツール </span></h1>
<h2><span id="msg_string" >USBシリアルI2C変換基板のGPIOの制御が可能です</span></h2>
<fieldset>
<legend>GPIO入出力設定</legend>
<div>
<p>GPIO0</p>
<input type="radio" id="input0" name="gpio0" value="input0" checked onchange="Gpio0Change();">
<label for="input0">入力</label>
<input type="radio" id="output0" name="gpio0" value="output0" onchange="Gpio0Change();">
<label for="output0">出力</label>
</div>
<div>
<p>GPIO1</p>
<input type="radio" id="input1" name="gpio1" value="input1" checked onchange="Gpio1Change();">
<label for="input1">入力</label>
<input type="radio" id="output1" name="gpio1" value="output1" onchange="Gpio1Change();">
<label for="output1">出力</label>
</div>
<div>
<p>GPIO2</p>
<input type="radio" id="input2" name="gpio2" value="input2" checked onchange="Gpio2Change();">
<label for="input2">入力</label>
<input type="radio" id="output2" name="gpio2" value="output2" onchange="Gpio2Change();">
<label for="output2">出力</label>
</div>
<div>
<p>GPIO3</p>
<input type="radio" id="input3" name="gpio3" value="input3" checked onchange="Gpio3Change();">
<label for="input3">入力</label>
<input type="radio" id="output3" name="gpio3" value="output3" onchange="Gpio3Change();">
<label for="output3">出力</label>
</div>
<div>
<p>GPIO4</p>
<input type="radio" id="input4" name="gpio4" value="input4" checked onchange="Gpio4Change();">
<label for="input4">入力</label>
<input type="radio" id="output4" name="gpio4" value="output4" onchange="Gpio4Change();">
<label for="output4">出力</label>
</div>
<div>
<p>GPIO5</p>
<input type="radio" id="input5" name="gpio5" value="input5" checked onchange="Gpio5Change();">
<label for="input5">入力</label>
<input type="radio" id="output5" name="gpio5" value="output5" onchange="Gpio5Change();">
<label for="output5">出力</label>
</div>
<div>
<p>GPIO6</p>
<input type="radio" id="input6" name="gpio6" value="input6" checked onchange="Gpio6Change();">
<label for="input6">入力</label>
<input type="radio" id="output6" name="gpio6" value="output6" onchange="Gpio6Change();">
<label for="output6">出力</label>
</div>
<div>
<p>GPIO7</p>
<input type="radio" id="input7" name="gpio7" value="input7" checked onchange="Gpio7Change();">
<label for="input7">入力</label>
<input type="radio" id="output7" name="gpio7" value="output7" onchange="Gpio7Change();">
<label for="output7">出力</label>
</div>
</fieldset>
<br/>
<fieldset>
<legend>GPIO ON/OFF</legend>
<div>
<p>GPIO0</p>
<input type="radio" id="off0" name="sw0" value="off0" checked disabled>
<label for="off0">OFF</label>
<input type="radio" id="on0" name="sw0" value="on0" disabled>
<label for="on0">ON</label>
</div>
<div>
<p>GPIO1</p>
<input type="radio" id="off1" name="sw1" value="off1" checked disabled>
<label for="off1">OFF</label>
<input type="radio" id="on1" name="sw1" value="on1" disabled>
<label for="on1">ON</label>
</div>
<div>
<p>GPIO2</p>
<input type="radio" id="off2" name="sw2" value="off2" checked disabled>
<label for="off2">OFF</label>
<input type="radio" id="on2" name="sw2" value="on2" disabled>
<label for="on2">ON</label>
</div>
<div>
<p>GPIO3</p>
<input type="radio" id="off3" name="sw3" value="off3" checked disabled>
<label for="off3">OFF</label>
<input type="radio" id="on3" name="sw3" value="on3" disabled>
<label for="on3">ON</label>
</div>
<div>
<p>GPIO4</p>
<input type="radio" id="off4" name="sw4" value="off4" checked disabled>
<label for="off4">OFF</label>
<input type="radio" id="on4" name="sw4" value="on4" disabled>
<label for="on4">ON</label>
</div>
<div>
<p>GPIO5</p>
<input type="radio" id="off5" name="sw5" value="off5" checked disabled>
<label for="off5">OFF</label>
<input type="radio" id="on5" name="sw5" value="on5" disabled>
<label for="on5">ON</label>
</div>
<div>
<p>GPIO6</p>
<input type="radio" id="off6" name="sw6" value="off6" checked disabled>
<label for="off6">OFF</label>
<input type="radio" id="on6" name="sw6" value="on6" disabled>
<label for="on6">ON</label>
</div>
<div>
<p>GPIO7</p>
<input type="radio" id="off7" name="sw7" value="off7" checked disabled>
<label for="off7">OFF</label>
<input type="radio" id="on7" name="sw7" value="on7" disabled>
<label for="on7">ON</label>
</div>
</fieldset>
<br/>
<button id="connect-button" type="button" disabled>接続</button>
<button id="io-button" type="button" disabled>入出力設定</button>
<button id="state-button" type="button" disabled>ON/OFF設定</button>
<h3>
<span>
本ツールは<a href="https://www.switch-science.com/catalog/6214/">USBシリアルI2C変換基板</a>とブラウザでIOテストをするツールです</span>
</h3>
<h3><span>オフラインやローカル環境でも動作可能です</span></h3>
<h3><span>ブラウザはEdgeもしくはChromeのみ対応しています</span></h3>
<footer>
<p>Copyright (c) 2022 Crescent All Rights Reserved.</p>
</footer>
<script>
/* GPIO Tool
* Copyright (c) 2022
* K.Watanabe,Crescent
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
const connectButton = document.getElementById ('connect-button');
const initButton = document.getElementById ('io-button');
const getButton = document.getElementById ('state-button');
const input0 = document.getElementById("input0");
const input1 = document.getElementById("input1");
const input2 = document.getElementById("input2");
const input3 = document.getElementById("input3");
const input4 = document.getElementById("input4");
const input5 = document.getElementById("input5");
const input6 = document.getElementById("input6");
const input7 = document.getElementById("input7");
const off0 = document.getElementById("off0");
const off1 = document.getElementById("off1");
const off2 = document.getElementById("off2");
const off3 = document.getElementById("off3");
const off4 = document.getElementById("off4");
const off5 = document.getElementById("off5");
const off6 = document.getElementById("off6");
const off7 = document.getElementById("off7");
const on0 = document.getElementById("on0");
const on1 = document.getElementById("on1");
const on2 = document.getElementById("on2");
const on3 = document.getElementById("on3");
const on4 = document.getElementById("on4");
const on5 = document.getElementById("on5");
const on6 = document.getElementById("on6");
const on7 = document.getElementById("on7");
let port;
let keepReading = true;
let reader;
let dataIndex = 0;
let str = "";
let closed;
let dataArry = Array(6);
//Wait Function
function Sleep( milli_second )
{
var start = new Date();
while( new Date() - start < milli_second );
}
//Button0 Changed Event
function Gpio0Change()
{
if(input0.checked==true)
{
off0.disabled=true;
on0.disabled=true;
}
else
{
off0.disabled=false;
on0.disabled=false;
}
}
//Button1 Changed Event
function Gpio1Change()
{
if(input1.checked==true)
{
off1.disabled=true;
on1.disabled=true;
}
else
{
off1.disabled=false;
on1.disabled=false;
}
}
//Button2 Changed Event
function Gpio2Change()
{
if(input2.checked==true)
{
off2.disabled=true;
on2.disabled=true;
}
else
{
off2.disabled=false;
on2.disabled=false;
}
}
//Button3 Changed Event
function Gpio3Change()
{
if(input3.checked==true)
{
off3.disabled=true;
on3.disabled=true;
}
else
{
off3.disabled=false;
on3.disabled=false;
}
}
//Button4 Changed Event
function Gpio4Change()
{
if(input4.checked==true)
{
off4.disabled=true;
on4.disabled=true;
}
else
{
off4.disabled=false;
on4.disabled=false;
}
}
//Button5 Changed Event
function Gpio5Change()
{
if(input5.checked==true)
{
off5.disabled=true;
on5.disabled=true;
}
else
{
off5.disabled=false;
on5.disabled=false;
}
}
//Button6 Changed Event
function Gpio6Change()
{
if(input6.checked==true)
{
off6.disabled=true;
on6.disabled=true;
}
else
{
off6.disabled=false;
on6.disabled=false;
}
}
//Button7 Changed Event
function Gpio7Change()
{
if(input7.checked==true)
{
off7.disabled=true;
on7.disabled=true;
}
else
{
off7.disabled=false;
on7.disabled=false;
}
}
//Com Open & Recieve Data
async function getReader()
{
port = await navigator.serial.requestPort({});
await port.open({ baudRate: 9600 });
connectButton.innerText = '切断';
initButton.disabled = false;
getButton.disabled = false;
//Set GPIO Direction
initButton.addEventListener('click', (event) =>
{
if (port && port.writable)
{
var gpio0=0b00;
var gpio1=0b00;
var gpio2=0b00;
var gpio3=0b00;
var gpio4=0b00;
var gpio5=0b00;
var gpio6=0b00;
var gpio7=0b00;
if(input0.checked==true)
gpio0=0b01;//Input
else
gpio0=0b10;//Output
if(input1.checked==true)
gpio1=0b01;//Input
else
gpio1=0b10;//Output
if(input2.checked==true)
gpio2=0b01;//Input
else
gpio2=0b10;//Output
if(input3.checked==true)
gpio3=0b01;//Input
else
gpio3=0b10;//Output
if(input4.checked==true)
gpio4=0b01;//Input
else
gpio4=0b10;//Output
if(input5.checked==true)
gpio5=0b01;//Input
else
gpio5=0b10;//Output
if(input6.checked==true)
gpio6=0b01;//Input
else
gpio6=0b10;//Output
if(input7.checked==true)
gpio7=0b01;//Input
else
gpio7=0b10;//Output
var gpioPortConf1 = gpio0 + (gpio1<<2) + (gpio2<<4) + (gpio3<<6);
var gpioPortConf2 = gpio4 + (gpio5<<2) + (gpio6<<4) + (gpio7<<6);
var state=0x00;//Output All Off
const bytes = new Uint8Array([0x57, 0x02, gpioPortConf1, 0x03, gpioPortConf2, 0x04, state, 0x50]);
const writer = port.writable.getWriter();
writer.write(bytes);
//console.log(bytes);
writer.releaseLock();
}
});
//Set and Get GPUO State
getButton.addEventListener('click', (event) =>
{
var io_state=0;
if (port && port.writable)
{
if(off0.checked==true)
io_state = io_state & ~(1 << 0);
else
io_state = io_state | (1 << 0);
if(off1.checked==true)
io_state = io_state & ~(1 << 1);
else
io_state = io_state | (1 << 1);
if(off2.checked==true)
io_state = io_state & ~(1 << 2);
else
io_state = io_state | (1 << 2);
if(off3.checked==true)
io_state = io_state & ~(1 << 3);
else
io_state = io_state | (1 << 3);
if(off4.checked==true)
io_state = io_state & ~(1 << 4);
else
io_state = io_state | (1 << 4);
if(off5.checked==true)
io_state = io_state & ~(1 << 5);
else
io_state = io_state | (1 << 5);
if(off6.checked==true)
io_state = io_state & ~(1 << 6);
else
io_state = io_state | (1 << 6);
if(off7.checked==true)
io_state = io_state & ~(1 << 7);
else
io_state = io_state | (1 << 7);
const bytes = new Uint8Array([0x4F, io_state, 0x50, 0x49, 0x50]);
const writer = port.writable.getWriter();
writer.write(bytes);
//console.log(bytes);
writer.releaseLock();
}
});
while (port.readable && keepReading)
{
reader = port.readable.getReader();
try {
while (true)
{
const { value, done } = await reader.read();
if (done)
{
reader.releaseLock();
break;
}
if (value)
{
if((value>>0)&0x01)
on0.checked=true;
else
off0.checked=true;
if((value>>1)&0x01)
on1.checked=true;
else
off1.checked=true;
if((value>>2)&0x01)
on2.checked=true;
else
off2.checked=true;
if((value>>3)&0x01)
on3.checked=true;
else
off3.checked=true;
if((value>>4)&0x01)
on4.checked=true;
else
off4.checked=true;
if((value>>5)&0x01)
on5.checked=true;
else
off5.checked=true;
if((value>>6)&0x01)
on6.checked=true;
else
off6.checked=true;
if((value>>7)&0x01)
on7.checked=true;
else
off7.checked=true;
}
}
}
catch (error)
{
}
finally
{
reader.releaseLock();
await port.close();
}
}
}
if ('serial' in navigator)
{
connectButton.addEventListener('click', async ()=>
{
if (port)
{
keepReading = false;
connectButton.innerText = '接続';
initButton.disabled = true;
getButton.disabled = true;
reader.cancel();
await closed;
port = undefined;
}
else
{
closed = getReader();
}
});
connectButton.disabled = false;
}
else
{
alert('お使いのブラウザは対応していません Edgeもしくはchromeを使用してください');
}
</script>
</body>
</html>