-
Notifications
You must be signed in to change notification settings - Fork 7
/
flea-scope.manual.html
1346 lines (1193 loc) · 42.3 KB
/
flea-scope.manual.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<!-- FLEA-SCOPE -->
<!-- See: https://wicg.github.io/webusb/ -->
<!-- See: https://wicg.github.io/serial/ -->
<!-- top right bottom left -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=1870">
<title>Flea-Scope™</title>
<style>
body,select,input { background: #ededed }
pre { white-space: pre-wrap; word-wrap: break-word; }
a { text-decoration: none; }
#status { color: red; padding: 0px 10px 0px 10px; margin: 0px 0px 0px 0px; }
#copyright { font-size:65%; padding: 0px; margin: 0px; }
#help { font-size:75%; }
* { overflow-wrap: anywhere; word-wrap: anywhere; }
button { overflow-wrap: normal; word-wrap: normal; }
td { text-align: center; padding: 0px 10px 0px 0px; white-space: nowrap; }
html,button,input,select { font: 34px Arial, Helvetica, sans-serif; padding: 10px 8px 10px 8px; margin: 0px 0px 10px 0px; }
@media(min-width:80em){ html,button,input { font: 22px Arial, Helvetica, sans-serif; padding: 10px 8px 10px 8px; margin: 0px 0px 10px 0px; } }
@supports(-webkit-touch-callout:none) { button,input { -webkit-appearance: none; border-radius: 2px; } }
body,html { margin: 0px; }
select { direction: rtl; }
#wrapper { display: flex; padding: 10px 0px 10px 0px; }
#view { outline: red 1px solid; flex: 97; overflow-x: scroll; }
#canvas { padding: 3px; }
#view2 { flex: 3; padding: 0px 5px 0px 5px; }
#labels { width: 50px; }
#command { font-size:65%; }
.dark { filter: invert(1); background: #121212; }
</style>
</head>
<body onload='Body()'>
<h1 id="h1">Flea-Scope™</h1>
<div id="config">
<button type='button'
onclick="if (/\b(Linux|CrOS|Windows)\b/.test(navigator.userAgent) && ! navigator.userAgent.match(/android/i)) {
Comm();
} else {
Usb();
}">Start Connect</button>
<button type='button' id="start1" hidden=true onclick="Comm();">Start Connect Comm</button>
<button type='button' id="start2" hidden=true onclick="Usb();">Start Connect USB</button>
</div>
<table>
<tr>
<td>
<label for="qusec">secs/div:</label>
</td>
<td>
<label for="trigger">trigger:</label>
</td>
<td>
<label for="b0">b0:</label>
</td>
<td>
<label for="b1">b1:</label>
</td>
<td>
<label for="b2">b2:</label>
</td>
<td>
<label for="b3">b3:</label>
</td>
<td>
<label for="b4">b4:</label>
</td>
<td>
<label for="b5">b5:</label>
</td>
<td>
<label for="b6">b6:</label>
</td>
<td>
<label for="b7">b7:</label>
</td>
<td>
<label for="b8">b8:</label>
</td>
<td>
</td>
<td>
<label for="delay">delay sec:</label>
</td>
<td>
<label for="probe">probe:</label>
</td>
<td>
<label for="polar">signal:</label>
</td>
<td>
<label for="volts">volts:</label>
</td>
<td>
<label for="wave">wave out:</label>
</td>
<td>
<label for="hz">hertz out:</label>
</td>
</tr>
<tr>
<td>
<select id="qusec" onchange="ScaleCanvas(); Send(String.fromCharCode(3));">
<option value="-10">0.5u*</option>
<option value="-5">1u*</option>
<option value="-2">2.5u*</option>
<option value="1">5u</option>
<option value="2">10u</option>
<option value="5">25u</option>
<option value="10">50u</option>
<option value="20">100u</option>
<option value="50">250u</option>
<option value="100">500u</option>
<option value="200" selected>1m</option>
<option value="500">2.5m</option>
<option value="1000">5m</option>
<option value="2000">10m</option>
<option value="5000">25m</option>
<option value="10000">50m</option>
<option value="20000">100m</option>
</select>
</td>
<td>
<select id="trigger" onchange='Send(String.fromCharCode(3));'>
<option selected value="~">auto</option>
<option value="">level</option>
<option value="+">rise</option>
<option value="-">fall</option>
</select>
</td>
<td>
<select id="b0" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b1" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b2" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b3" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b4" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b5" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b6" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b7" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<select id="b8" onchange='Send(String.fromCharCode(3));'>
<option selected value="">-</option>
<option value="0">0</option>
<option value="1">1</option>
</select>
</td>
<td>
<button type="button" onclick="Clear()">clear</button>
</td>
<td>
<input type="text" id="delay" onchange='Send(String.fromCharCode(3));'' value="0u" size="2"></input>
</td>
<td>
<select id="probe" onchange='Send(String.fromCharCode(3)); Probe();'>
<option selected value="1">x1</option>
<option value="10">x10</option>
</select>
</td>
</td>
<td>
<select id="polar" onchange='Send(String.fromCharCode(3));'>
<option selected value="1">uni</option>
<option value="2">bi</option>
<option value="2.">ac</option>
</select>
<td>
<select id="volts" onchange='Send(String.fromCharCode(3));'>
<option value="0.10">0.10*</option>
<option value="0.18">0.18*</option>
<option value="0.25">0.25*</option>
<option value="0.33">0.33*</option>
<option value="0.5">0.5</option>
<option value="0.66">0.66</option>
<option value="1.0">1.0</option>
<option value="1.8">1.8</option>
<option value="2.5">2.5</option>
<option selected value="3.3">3.3</option>
<option value="5.0">5.0</option>
<option value="6.6">6.6</option>
</select>
</td>
<td>
<select id="wave" onchange="Wave();">
<option>ekg</option>
<option selected>sine</option>
<option>square</option>
<option>triangle</option>
</select>
</td>
<td>
<input type="text" id="hz" onchange="Wave();" value="1k" size="2"></input>
</td>
</tr>
</table>
<div id="wrapper">
<div id="view">
<div id="help">
<i>
To connect this graphical user interface web-page to your Flea-Scope:
<ol start=0>
<li>be sure you are using a <a href="https://en.wikipedia.org/wiki/Chromium_(web_browser)" target="_blank">Chromium-based web browser</a> that supports <a href="https://wicg.github.io/webusb/" target="_blank">WebUSB</a> and/or <a href="https://wicg.github.io/serial/" target="_blank">Web Serial</a></li>
<li>plug Flea-Scope's micro-USB connector into your computer, tablet, or phone, using a <u>USB data cable</u></li>
<ul>
<li>LED e1 (blue) should blink at 2Hz for 2 blinks, and then settle to a 1Hz blink while Flea-Scope is idle</li>
</ul>
<li><u>click or tap</u> the "Start Connect" button above to authorize web browser access to a selected USB device</li>
<ul>
<li>the web browser will open a WebUSB or Web Serial dialog box</li>
<li>you should see your Flea-Scope device listed in the WebUSB or Web Serial dialog box</li>
</ul>
<li><b><u>click or tap</u> the desired Flea-Scope USB device or serial port (or customized hostname) to select it!</b></li>
<li>finally, <u>click or tap</u> "Connect" to confirm the selected Flea-Scope USB device!</li>
</ol>
</i>
</div>
<canvas id="canvas" hidden=true></canvas>
</div>
<div id="view2">
<canvas id="labels"></canvas>
</div>
</div>
<table WIDTH="1870px">
<tr>
<td WIDTH=35%>
<button type="button" id="zero" onclick="CalZero();" title="touch probe tip to ground (GND) and press to calibrate zero">cal_zero</button>
<button type="button" id="3v3" onclick="Cal3v3();" title="touch probe tip to 3.3 volts (3V3) and press to calibrate 3.3 volts">cal_3v3</button>
<button type="button" id="pause" onclick="Pause();">pause</button>
<button type="button" id="single" onclick="Single();" disabled=true>single</button>
<button type="button" id="measure" onclick="Measure();">measure</button>
<button type="button" id="background" onclick="Background();">background</button>
<button type="button" id="bw" onclick="document.querySelector('body').classList.toggle('dark');">bw</button>
</td>
<td WIDTH=25%>
<pre id='status' color='red'></pre>
</td>
<td WIDTH=25%>
<pre id='voltage'></pre>
</td>
<td WIDTH=15%>
<pre id='command'></pre>
</td>
</table>
<p id='copyright'>
<a href=https://rtestardi.github.io/pages target=_blank>rtestardi's pages</a>
<a href=https://rtestardi.github.io/usbte/flea-scope.pdf target=_blank>Flea-Scope™ User's Guide</a>
<a href=https://rtestardi.github.io/StickOS target=_blank>StickOS® website</a>
<a href=https://github.com/rtestardi/usbte target=_blank>github.com/rtestardi/usbte</a>
<a href=https://github.com/rtestardi/StickOS2 target=_blank>github.com/rtestardi/StickOS2</a>
firmware version:
<span id="ver"></span>
<script>
'use strict';
var webpage;
var usb;
var comm;
var epin = 2;
var epout = 3;
var reader;
var writer;
var enc = new TextEncoder();
var dec = new TextDecoder();
var canvas;
var labels;
var bytes = 1024;
var discard;
var discarded;
var string = "";
var running = "";
var digital = false;
var bg = false;
var avgV = 0; // ac only
var analogV = 0; // 0..v or -v..v (must be multiplied by p!)
var measure = 0;
const MSPS = 18; // approximate target sample rate
const SAMPLES = 2000*MSPS/20; // approximate width of display canvas
const EXTRA = 200; // extra samples in case web page wants to fine-scale
const INTERLEAVE = 5; // we interleave results from 5 adcs
const DIVSV = 10;
const DIVST = 20;
const VERT = 4;
const bus_frequency = 120000000;
const bus_mhz = bus_frequency/1000000;
var msps; // actual based on mcu timer pr
var width = SAMPLES*2; // actual display width after ScaleCanvas()
var height = 256;
var bits = 9;
var ver;
var hostname = "";
var single = false;
var pause = false;
var down = false;
var waiting;
var cal_zero_x1;
var cal_3v3_x1;
var cal_zero_x10;
var cal_3v3_x10;
var oldX, newX;
var oldY, newY;
var adcfull = 6.6; // +/- at bnc
var adcrawcount = 0;
var adcrawsum = 0;
function ScaleCanvas()
{
// account for clock rounding inaccuracies at 18 Msps
var usecv = parseInt(document.getElementById("qusec").value); // this is per-division
if (usecv < 0) {
usecv = 1;
}
var pr = Math.floor(bus_mhz*usecv*INTERLEAVE/MSPS + 0.5);
msps = bus_mhz*INTERLEAVE/pr;
width = Math.floor(2*2000*msps*usecv/20);
canvas.setAttribute("width", width);
canvas.setAttribute("height", height+30*bits+20);
labels.setAttribute("width", 50);
labels.setAttribute("height", height+30*bits+20);
}
function Clear()
{
for (var b = 0; b < bits; b++) {
document.getElementById('b'+b).selectedIndex = 0;
};
document.getElementById('trigger').selectedIndex = 0;
document.getElementById('delay').value = "0u";
analogV = 0;
Send(String.fromCharCode(3));
}
function GetZero()
{
var a;
var p = parseFloat(document.getElementById("probe").value);
if (p == 1) {
a = cal_zero_x1;
} else {
a = cal_zero_x10;
}
if (a == undefined) {
document.getElementById("status").innerHTML = "please calibrate x" + p + " probe zero";
return p==1 ? 14 : 28; // default calibration values
}
return Number(a);
}
function CalZero()
{
if (adcrawcount) {
var p = parseFloat(document.getElementById("probe").value);
var t = Math.round((adcrawsum/adcrawcount - 512) * 4)/4;
if (p == 1) {
if (t >= -5 && t <= 23 || measure == 2) { // calibration limits
cal_zero_x1 = t;
once.push("cal_zero_x1 = " + (Math.round(cal_zero_x1*4)+1000));
} else {
document.getElementById("status").innerHTML = "FAILED " + t;
t = undefined;
}
} else {
if (t >= -5 && t <= 40 || measure == 2) { // calibration limits
cal_zero_x10 = t;
once.push("cal_zero_x10 = " + (Math.round(cal_zero_x10*4)+1000));
} else {
document.getElementById("status").innerHTML = "FAILED " + t;
t = undefined;
}
}
if (t != undefined) {
document.getElementById("status").innerHTML = "cal_zero_x"+p + " = " + GetZero();
}
}
}
function Get3v3()
{
var a, b;
var p = parseFloat(document.getElementById("probe").value);
if (p == 1) {
a = cal_3v3_x1;
b = cal_zero_x1;
} else {
a = cal_3v3_x10;
b = cal_zero_x10;
}
if (a == undefined) {
if (! (b == undefined)) {
document.getElementById("status").innerHTML = "cal_zero_x"+p + " = " + GetZero() + "\n" + "please calibrate x" + p + " probe 3v3";
}
return p==1 ? 259 : 270; // default calibration values
}
return Number(a);
}
function Cal3v3()
{
if (adcrawcount) {
var p = parseFloat(document.getElementById("probe").value);
var t = Math.round((adcrawsum/adcrawcount - GetZero() - 512) * p*4)/4;
if (p == 1) {
if (t >= 235 && t <= 275 || measure == 2) { // calibration limits
cal_3v3_x1 = t;
once.push("cal_3v3_x1 = " + (Math.round(cal_3v3_x1*4)+1000));
} else {
document.getElementById("status").innerHTML = "FAILED " + t;
t = undefined;
}
} else {
if (t >= 220 && t <= 300 || measure == 2) { // calibration limits
cal_3v3_x10 = t;
once.push("cal_3v3_x10 = " + (Math.round(cal_3v3_x10*4)+1000));
} else {
document.getElementById("status").innerHTML = "FAILED " + t;
t = undefined;
}
}
if (t != undefined) {
document.getElementById("status").innerHTML = "cal_3v3_x"+p + " = " + Get3v3();
}
}
}
function Probe()
{
var p = parseFloat(document.getElementById("probe").value);
var e = document.getElementById("volts");
for (var i = 0; i < e.options.length; i++) {
e.options[i].innerHTML = "" + +(e.options[i].value*p).toFixed(2);
}
}
function Pause()
{
if (! pause) {
pause = true;
document.getElementById('pause').innerHTML = "resume";
document.getElementById('pause').style.color = 'red';
document.getElementById('single').disabled = false;
document.getElementById('single').style.color = 'red';
} else {
pause = false;
document.getElementById('pause').innerHTML = "pause";
document.getElementById('pause').style.color = 'black';
document.getElementById('single').disabled = true;
document.getElementById('single').style.color = 'black';
}
}
function Single()
{
single = true;
}
function Background()
{
if (! bg) {
bg = true;
document.getElementById('background').innerHTML = "foreground";
} else {
bg = false;
document.getElementById('background').innerHTML = "background";
}
}
function Wave()
{
var pattern = document.getElementById("wave").value;
var hz = document.getElementById("hz").value;
if (hz.toLowerCase().match(/k$/)) {
hz = Math.floor(Number(hz.replace(/k$/, "")*1000));
} else if (hz.toLowerCase().match(/m$/)) {
hz = Math.floor(Number(hz.replace(/m$/,"")*1000000));
}
once.push("wave " + pattern + " " + hz);
}
// return time for fraction of display width
function usec(frac, scale, fixed)
{
var usecv = parseInt(document.getElementById("qusec").value); // this is per-division
if (usecv < 0) {
usecv = -5/usecv;
} else {
usecv *= 5;
}
usecv = usecv * DIVST * frac;
var usecs;
if (scale && usecv >= 1000) {
usecs = "" + (usecv/1000).toFixed(fixed) + "ms";
} else {
usecs = "" + (usecv).toFixed(fixed) + "us";
}
return usecs;
}
// return frequency for fraction of display width
function freq(frac, scale, fixed)
{
var usecv = parseInt(document.getElementById("qusec").value); // this is per-division
if (usecv < 0) {
usecv = -5/usecv;
} else {
usecv *= 5;
}
usecv = usecv * DIVST * frac;
var freqv = 1000000/usecv;
var freqs;
if (scale && freqv >= 1000000) {
freqs = "" + (freqv/1000000).toFixed(fixed) + "MHz";
} else if (scale && freqv >= 1000) {
freqs = "" + (freqv/1000).toFixed(fixed) + "kHz";
} else {
freqs = "" + (freqv).toFixed(fixed) + "Hz";
}
return freqs;
}
// return voltage for fraction (-1..1) of display height
function volt(frac, scale, fixed)
{
var v = parseFloat(document.getElementById("probe").value) * document.getElementById("volts").value;
var voltv = v * frac;
var volts;
if (scale && voltv < 0.01 && voltv > -0.01) {
volts = "" + (voltv*1000).toFixed(fixed) + "mV";
} else {
volts = "" + (voltv).toFixed(fixed) + "V";
}
return volts;
}
// return voltage for adc value -512..511 for -6.6V to 6.6V
function voltadc(adc, scale, fixed)
{
var voltv = (adc-512)/512*adcfull * parseFloat(document.getElementById("probe").value);
var volts;
if (scale && voltv < 0.01 && voltv > -0.01) {
volts = "" + (voltv*1000).toFixed(fixed) + "mV";
} else {
volts = "" + (voltv).toFixed(fixed) + "V";
}
return volts;
}
function ShowStatus()
{
var status = "";
if (newX != -1) {
var r = parseFloat(document.getElementById("polar").value);
status = "position: " + usec(newX/width, true, 2);
if (newY < height) {
status += ", " + volt((height/r-1-newY)/(height/r), true, 2);
}
var dx = Math.abs(oldX - newX);
var dy = Math.abs(oldY - newY);
if (oldY > 255 || newY > 255) {
dy = 0;
}
if (oldX != -1 && (dx || dy)) {
status = "delta: " + usec(dx/width, true, 2);
if (dx) {
status += ", " + freq(dx/width, true, 2);
}
if (newY < height) {
status += ", " + volt(dy/(height/r), true, 2);
}
}
}
document.getElementById("status").innerHTML = status;
}
function ShowPoints()
{
var x;
var y;
var ctx;
ctx = canvas.getContext('2d');
ctx.beginPath();
if (oldX >= 0 && oldY >= 0 && (oldX != newX || oldY != newY)) {
x = oldX;
y = oldY;
ctx.fillStyle = 'red';
ctx.arc(x, y, 4, 0, 2*Math.PI, false);
ctx.fill();
ctx.fillStyle = 'black';
}
if (newX >= 0 && newY >= 0) {
x = newX;
y = newY;
ctx.fillStyle = 'red';
ctx.arc(x, y, 4, 0, 2*Math.PI, false);
ctx.fill();
ctx.fillStyle = 'black';
}
}
function Measure()
{
if (measure) {
document.getElementById('measure').style.color = 'black';
document.getElementById('status').innerHTML = '';
measure = 0;
} else {
document.getElementById('measure').style.color = 'red';
measure = 2;
}
newX = oldX = -1;
}
// the user loaded the webpage; set up keyboard listeners
function Body()
{
webpage = location.pathname.split("/").slice(-1)[0];
if (webpage.match(/manual/)) {
document.getElementById("start1").hidden = false;
document.getElementById("start2").hidden = false;
document.getElementById("help").innerHTML += "<p>" + navigator.userAgent;
for (var vv in navigator) {
document.getElementById("help").innerHTML += vv + ", ";
}
document.getElementById("help").innerHTML += "</p>";
}
if (/\b(Linux)\b/.test(navigator.userAgent)) {
// linux gets ooo data at larger web serial buffer sizes
bytes = 256;
}
canvas = document.getElementById('canvas');
labels = document.getElementById('labels');
ScaleCanvas();
canvas.addEventListener('mousedown', function(e) {
var v = parseFloat(document.getElementById("volts").value);
var p = parseFloat(document.getElementById("probe").value);
var r = parseFloat(document.getElementById("polar").value);
down = true;
if (! measure) {
Send(String.fromCharCode(3)); // Ctrl-C
var analogY = Math.min(Math.max(e.offsetY, 0), 255); // 0..255 (0..height-1)
analogV = (height/r - (analogY+1))/(height/r)*v; // 0..v or -v..v
analogV += avgV;
document.getElementById("status").innerHTML = "trigger = " + (analogV*p).toFixed(2) + "V";
Display();
} else {
if (measure == 1) {
oldX = newX;
oldY = newY;
newX = e.offsetX;
newY = e.offsetY;
document.getElementById('measure').style.color = 'black';
measure = 0;
} else {
newX = e.offsetX;
newY = e.offsetY;
measure = 1;
}
ShowStatus();
ShowPoints();
}
});
canvas.addEventListener('mouseup', function(e) {
down = false;
});
}
// the user wants to connect to a USB device's bulk endpoints directly
async function Usb()
{
var filter = [
{ vendorId: 0x0403, productId: 0xA660 },
{ vendorId: 0x1b4f, productId: 0xA660 },
{ vendorId: 0x1b4f, productId: 0xE66E },
{ vendorId: 0x04D8, productId: 0xE66E },
];
usb = undefined;
comm = undefined;
reader = undefined;
writer = undefined;
if ('usb' in navigator) {
try {
usb = await navigator.usb.requestDevice({ filters: filter });
await usb.open();
await usb.selectConfiguration(1);
await usb.claimInterface(1);
document.getElementById("config").hidden = true;
document.getElementById("help").hidden = true;
document.getElementById("canvas").hidden = false;
discard = 1;
setTimeout(Receive, 1);
Send(String.fromCharCode(3)); // Ctrl-C
await new Promise(r => setTimeout(r, 200)); // allow autorun program to stop?
Send("echo off\n"); // XXX -- figure out how to undo?
Send("prompt off\n"); // XXX -- figure out how to undo?
Send("dim unused[8] as flash\n");
Send("dim cal_zero_x1 as flash, cal_3v3_x1 as flash\n");
Send("dim cal_zero_x10 as flash, cal_3v3_x10 as flash\n");
Wave();
discarded = false;
setTimeout(Discard, 200);
} catch(err) {
document.getElementById("help").innerHTML += "<p><font color='red'>" + err.message.replace(/</g,'<').replace(/>/g,'>') + "</font></p>";
}
} else {
document.getElementById("help").innerHTML += "<p><font color='red'>WebUSB API not found.</font></p>";
}
}
// the user wants to connect to an emulated COM port
async function Comm()
{
var filter = [
{ usbVendorId: 0x0403, usbProductId: 0xA660 },
{ usbVendorId: 0x1b4f, usbProductId: 0xA660 },
{ usbVendorId: 0x1b4f, usbProductId: 0xE66E },
{ usbVendorId: 0x04D8, usbProductId: 0xE66E },
];
usb = undefined;
comm = undefined;
reader = undefined;
writer = undefined;
if ('serial' in navigator) {
try {
comm = await navigator.serial.requestPort({ filters: filter });
await comm.open({ baudRate: 115200, bufferSize: bytes });
reader = comm.readable.getReader();
writer = comm.writable.getWriter();
document.getElementById("config").hidden = true;
document.getElementById("help").hidden = true;
document.getElementById("canvas").hidden = false;
discard = 1;
setTimeout(Receive, 1);
Send(String.fromCharCode(3)); // Ctrl-C
await new Promise(r => setTimeout(r, 200)); // allow autorun program to stop?
Send("echo off\n"); // XXX -- figure out how to undo?
Send("prompt off\n"); // XXX -- figure out how to undo?
Send("dim unused[8] as flash\n");
Send("dim cal_zero_x1 as flash, cal_3v3_x1 as flash\n");
Send("dim cal_zero_x10 as flash, cal_3v3_x10 as flash\n");
Wave();
discarded = false;
setTimeout(Discard, 200);
} catch(err) {
document.getElementById("help").innerHTML += "<p><font color='red'>" + err.message.replace(/</g,'<').replace(/>/g,'>') + "</font></p>";
}
} else {
document.getElementById("help").innerHTML += "<p><font color='red'>Web Serial API not found.</font></p>";
}
}
async function Discard()
{
if (discarded) {
discarded = false;
setTimeout(Discard, 200);
} else {
discard = 0;
setTimeout(Next, 1);
}
}
var once = [
"print cal_zero_x1, cal_3v3_x1, cal_zero_x10, cal_3v3_x10",
"ver",
"hostname"
];
async function Next()
{
if (once.length) {
SendCommand(once.shift());
} else {
if (single) {
single = false;
setTimeout(Scope, 1);
} else if (down || pause) {
Display();
setTimeout(Next, 300);
} else {
setTimeout(Scope, 1);
}
}
}
async function WarnCommand()
{
document.getElementById('command').style.color = 'red';
document.getElementById("command").innerHTML += " [waiting]";
}
async function SendCommand(command)
{
string = "";
waiting = setTimeout(WarnCommand, 3000);
document.getElementById('command').style.color = 'black';
document.getElementById("command").innerHTML = command;
Send(command + "\n");
}
async function Scope()
{
var v = parseFloat(document.getElementById("volts").value);
var p = parseFloat(document.getElementById("probe").value);
var r = parseFloat(document.getElementById("polar").value);
digital = false;
var number1 = document.getElementById("qusec").value;
var number2;
var number3;
var number4;
var number5;
for (var b = 0; b < bits; b++) {
if (document.getElementById("b"+b).value != "") {
digital = true;
}
}
if (digital) {
var mask = 0;
var sel = 0;
for (b = 0; b < bits; b++) {
if (document.getElementById("b"+b).value != "") {
mask |= 1<<b;
}
if (document.getElementById("b"+b).value == "1") {
sel |= 1<<b;
}
}
number2 = document.getElementById("trigger").value;
number2 += "0x" + sel.toString(16);
number3 = "0x" + mask.toString(16);
} else {
number2 = document.getElementById("trigger").value;
var adcscaled = (analogV+adcfull)/(adcfull*2)*1024;
if (adcscaled > 1023) {
adcscaled = 1023;
} else if (adcscaled < 0) {
adcscaled = 0;
}
number2 += Math.floor((adcscaled-512)*Get3v3()/256 + GetZero() + 512);
number3 = 0;
}
number4 = document.getElementById("delay").value;
if (number4.toLowerCase().match(/u$/)) {
number4 = Math.floor(Number(number4.replace(/u$/,""))*1);
} else if (number4.toLowerCase().match(/m$/)) {
number4 = Math.floor(Number(number4.replace(/m$/,""))*1000);
} else {
number4 = Math.floor(Number(number4)*1);
}
number4 = Math.floor(number4 * msps); // convert from us to samples
var command = "scope " + number1 + " " + number2 + " " + number3 + " " + number4;
SendCommand(command);
}
async function Display()
{
// result is in string
var lines = string.split(/\r?\n/);
var bw = document.querySelector('body').classList.contains('dark')
var ctx;
// draw vertical labels
ctx = labels.getContext('2d');
ctx.clearRect(0, 0, 50, 546);
ctx.font = `12px Verdana`;
var v = parseFloat(document.getElementById("volts").value);
var p = parseFloat(document.getElementById("probe").value);
var r = parseFloat(document.getElementById("polar").value);
var ac = document.getElementById("polar").value == "2.";
// voltage
if (document.getElementById("polar").value == 1) {
ctx.fillText("" + +(v*p).toFixed(2) + "V", 0, 10);
ctx.fillText("" + +(v*p/2).toFixed(2) + "V", 0, 128);
ctx.fillText("0V", 0, 256);
} else {
ctx.fillText("+" + +(v*p).toFixed(2) + "V", 0, 10);
if (document.getElementById("polar").value == "2") {
ctx.fillText("0V", 0, 128);
} else {
ctx.fillText("avg V", 0, 128);
}
ctx.fillText("-" + +(v*p).toFixed(2) + "V", 0, 256);
}
var b;
// bits
for (b = 0; b < bits; b++) {
sy = 305+b*30;