-
Notifications
You must be signed in to change notification settings - Fork 1
/
bellboy.js
2444 lines (2170 loc) · 95 KB
/
bellboy.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
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
// Copyright (c) 2017,2018,2019 Peter Budd. All rights reserved
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// * The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE AUTHORS AND COPYRIGHT HOLDERS, HOWEVER,
// ACCEPT LIABILITY FOR DEATH OR PERSONAL INJURY CAUSED BY NEGLIGENCE AND FOR ALL MATTERS LIABILITY
// FOR WHICH MAY NOT BE LAWFULLY LIMITED OR EXCLUDED UNDER ENGLISH LAW
// This script forms part of the Bell-Boy project to measure the force applied by a bell ringer to a tower
// bell rope. The Bell-Boy uses rotational acceleration of the bell as a proxy for force applied. The
// hardware is currently a Pi Zero running Arch Linux.
// I pulled snippets of code from loads of places so if any of you recognise anything you wrote - thanks!
// The script below is the javascript for the front end. It makes extensive use of HTML5 canvases
// so HTML5 is required. Websockets are also used to pull data from the Bell-Boy device.
var BGCOLOUR="#686888";
var nonLive=false;
var wsHost="10.0.0.1"
var sampleInterval = 0.008; // seconds for each sample (default 125 times/sec). Updated by SAMP: command
var collectInterval = 0.1; // update display in ms - here 10 times/sec
var batteryLevel = 100;
var sleepTime = 0.5;
var canvasBD = document.getElementById("canvasBD");
var ctxBD = canvasBD.getContext("2d");
var canvasBDt= document.getElementById("canvasBDt");
var ctxBDt = canvasBDt.getContext("2d");
var posBS2 = 0; // position on canvas of BS2
var posHS1 = 0;
var posHS2 = 0;
var posBS1 = 0;
var posCB = 0; // position on canvas of bell (and timers)
var CBwidth = 64; // width of bell on canvas
var BDwidth = 0; // width of individual stroke sections on canvas
var strokeTimer=0; // used for time displays under animated bell
var canvasAT=document.getElementById("canvasAT");
var ctxAT=canvasAT.getContext("2d");
var canvasATt=document.getElementById("canvasATt");
var ctxATt=canvasATt.getContext("2d");
var currentATmargin = 0;
var currentATpixels = 1;
var ATbottomMargin=45; // pixels at bottom of AT canvas
var ATtopMargin = 30; // pixels at top of AT canvas
var ATstepWidth = 30;
var wsOpened = false;
//var lastWinWidth = window.innerWidth;
var scaleValue = 0.0;
var ROIRanges = [[-20,90],[-20,70],[-10,60],[-10,50],[-10,40],[-10,30]];
var currentROI=3;
var ROIU = ROIRanges[currentROI][0];
var ROIL = ROIRanges[currentROI][1];
var playbackRanges = [200, 150, 100, 80, 50, 30, 10]; // available playback speeds (percent)
var currentPlaybackSpeed = 2;
var targets = [ "none", "-10", "-7", "-5", "-2" , "0", "2", "5", "7" , "10" ]; // these are target balance angles
var scales = [ 300, 200, 100, 80, 50, 30 ];
var currentPlaybackPosition = 1;
var playintervalID = null;
var statusintervalID = null;
var liveintervalID = null;
var changeInterval = null;
var CPM = 31.0;
var changeStep = null;
var openHandstroke = 1.0;
var TchangeInterval = null;
var TopenHandstroke = null;
var TchangeStep = null;
var gravityValue = 0.0;
var ropeValueB = 0.0;
var ropeValueH = 0.0;
var currentStatus = 0;
var DOWNLOADINGFILE=1;
var RECORDINGSESSION=2;
var SESSIONLOADED=4;
var TEMPLATEDISPLAYED=8;
var PLAYBACK=16;
var LASTHS1=32;
var LASTHS2=64;
var LASTBS1=128;
var LASTBS2=256;
var HELPDISPLAYED=512;
var PAUSED=1024;
// var GRIDLINESDISPLAYED=2048;
//var LIVEVIEW=4096;
var ABORTFLAG = 8192;
var SKIPMAIN = 16384;
var SKIPTEMPLATE = 32768;
var WOBBLINGATSTAND = 65536;
var targetAngleHand=null;
var targetAngleBack=null;
var currentSwingDisplayed=null;
var TcurrentSwingDisplayed=null;
var sample = [];
var ringTimes = [];
var switchPoints = [];
var template = [];
var TringTimes = [];
var TswitchPoints = [];
var swingStarts = [];
var TswingStarts = [];
var averagePullStrength=[];
var TaveragePullStrength=[];
var fileList=[];
ws = new WebSocket("ws://" + wsHost);
////////////////////////////////////////////////////////////////
// SETUP SELECT BOX FUNCTIONS //
////////////////////////////////////////////////////////////////
document.getElementById("targetSelectHand").options.length = 0;
document.getElementById("targetSelectBack").options.length = 0;
for (var i=0; i < targets.length; i++) {
var option = document.createElement("option");
option.text=targets[i];
document.getElementById("targetSelectHand").add(option);
option = document.createElement("option");
option.text=targets[i];
document.getElementById("targetSelectBack").add(option);
}
document.getElementById("zoomSelect").options.length = 0;
for (var i=0; i < ROIRanges.length; i++) {
var option = document.createElement("option");
option.text=ROIRanges[i][0].toString() + " to " + ROIRanges[i][1].toString();
document.getElementById("zoomSelect").add(option);
}
document.getElementById("zoomSelect").selectedIndex = currentROI.toString();
document.getElementById("bellsSelect").options.length = 0;
for (var i=3; i<13; i++) {
var option = document.createElement("option");
option.text=i.toString();
document.getElementById("bellsSelect").add(option);
}
document.getElementById("bellsSelect").selectedIndex = 3; // default number of bells = 6
var option = document.createElement("option");
option.text="Auto";
document.getElementById("chimeSelect").add(option);
for (var i=250; i < 500; i += 20) {
var option = document.createElement("option");
option.text=i.toString();
document.getElementById("chimeSelect").add(option);
}
document.getElementById("chimeSelect").selectedIndex = 0;
document.getElementById("speedSelect").options.length = 0;
for (var i=0; i < playbackRanges.length; i++) {
var option = document.createElement("option");
option.text=playbackRanges[i].toString() + "%"
document.getElementById("speedSelect").add(option);
}
document.getElementById("speedSelect").selectedIndex = currentPlaybackSpeed;
document.getElementById("scaleSelect").options.length = 0;
for (var i=0; i < scales.length; i++) {
var option = document.createElement("option");
option.text=scales[i].toString()
document.getElementById("scaleSelect").add(option);
}
document.getElementById("scaleSelect").selectedIndex = 2; // start at 100
scaleValue=parseFloat(scales[document.getElementById("scaleSelect").selectedIndex])
////////////////////////////////////////////////////////////////
// WEBSOCKET HANDLER FUNCTIONS //
////////////////////////////////////////////////////////////////
ws.onopen = function(){
wsOpened=true;
setStatus("Link open");
fileList=[];
document.getElementById("openSelect").options.length = 0;
ws.send("FILE:");
var d = new Date();
if (!nonLive){
ws.send("DATE:" + parseInt(d.getTime()/1000));
ws.send("SAMP:");
calculateTimings();
}
};
ws.onmessage = function (event) {
var dataBack = event.data.split("\n");
for (var i=0; i<dataBack.length; i++) {
if (dataBack[i].length > 3) parseResult(dataBack[i]);
}
};
function parseResult(dataBack) {
if (dataBack.slice(0,5) == "FILE:"){
fileList[fileList.length] = dataBack.slice(5);
return;
}
if (dataBack.slice(0,5) == "EFIL:"){
fileList.sort();
for(var i = 0; i<fileList.length; ++i){
var option = document.createElement("option");
option.text=fileList[i];
document.getElementById("openSelect").add(option);
}
fileList=[];
return;
}
if (dataBack.slice(0,5) == "DATA:"){
var sampArray = dataBack.split("DATA:");
for (var i = 0; i < sampArray.length; i++) {
if (sampArray[i].length < 20) continue; // have more checks for good data
var entries = sampArray[i].split(",");
sample[sample.length] = [parseFloat(entries[0].slice(2)), parseFloat(entries[1].slice(2)), parseFloat(entries[2].slice(2)), parseFloat(entries[3].slice(2)), parseFloat(entries[4].slice(2))];
var data = parseFloat(entries[3].slice(2));
if(data != 0 || sample.length <= 8){
var value = parseFloat(entries[4].slice(2));
if(data == 1 || data == 2) ringTimes[ringTimes.length] = value;
if(data == 7) switchPoints[switchPoints.length] = value;
if(sample.length == 1) gravityValue = value;
if(sample.length == 2) {
if (value < 3) value = 3;
if (value > 12) value = 12;
document.getElementById("bellsSelect").selectedIndex = Math.round(value - 3);
calculateTimings();
}
if(sample.length == 3) {
if (value < 20) value = 20;
if (value > 50) value = 50;
CPM = Math.round(value);
calculateTimings()
}
if(sample.length == 4) {
openHandstroke = Math.round(value*10)/10;
if(openHandstroke < 0.0) openHandstroke = 0.0;
if(openHandstroke > 2.2) openHandstroke = 2.2;
calculateTimings();
}
if(sample.length == 5){
if(value < 250){
document.getElementById("chimeSelect").selectedIndex = 0; // auto detection
} else {
document.getElementById("chimeSelect").selectedIndex = Math.round(1+((value-250)/20));
}
}
//skip tare value
if(sample.length == 7) ropeValueB = value;
if(sample.length == 8) ropeValueH = value;
}
}
setStatus("Loaded: " + sample.length.toString());
return;
}
if (dataBack.slice(0,5) == "LIVE:"){
if (!(currentStatus & RECORDINGSESSION)) return; // server may push data after stopping
var sampArray = dataBack.split("LIVE:");
for (var i = 0; i < sampArray.length; i++) {
if (sampArray[i].length < 20) continue; // have more checks for good data
var entries = sampArray[i].split(",");
sample[sample.length] = [parseFloat(entries[0].slice(2)), parseFloat(entries[1].slice(2)), parseFloat(entries[2].slice(2)), parseFloat(entries[3].slice(2)), parseFloat(entries[4].slice(2))];
var data = parseFloat(entries[3].slice(2));
if(data != 0 || sample.length <= 8){
var value = parseFloat(entries[4].slice(2));
if(data == 1 || data == 2) ringTimes[ringTimes.length] = value; // need to check if this is zero length
if(data == 7) switchPoints[switchPoints.length] = value;
if(sample.length == 1) gravityValue = value;
if(sample.length == 2) {
if (value < 3) value = 3;
if (value > 12) value = 12;
document.getElementById("bellsSelect").selectedIndex = Math.round(value - 3);
calculateTimings();
}
if(sample.length == 3) {
if (value < 20) value = 20;
if (value > 50) value = 50;
CPM = Math.round(value);
calculateTimings()
}
if(sample.length == 4) {
openHandstroke = Math.round(value*10)/10;
if(openHandstroke < 0.0) openHandstroke = 0.0;
if(openHandstroke > 2.2) openHandstroke = 2.2;
calculateTimings();
}
if(sample.length == 5){
if(value < 250){
document.getElementById("chimeSelect").selectedIndex = 0; // auto detection
} else {
document.getElementById("chimeSelect").selectedIndex = Math.round(1+((value-250)/20));
}
}
//skip tare value
if(sample.length == 7) ropeValueB = value;
if(sample.length == 8) ropeValueH = value;
}
}
return;
}
if ((dataBack.slice(0,5) == "LFIN:") || (dataBack.slice(0,5) == "STPD:")) {
if ((currentStatus & DOWNLOADINGFILE) !=0){
currentStatus &= ~DOWNLOADINGFILE;
} else if (currentStatus & RECORDINGSESSION) {
currentStatus &= ~RECORDINGSESSION;
}
if ((currentStatus & ABORTFLAG) !=0) {
currentStatus &= ~ABORTFLAG;
sample = [];
swingStarts=[];
currentPlaybackPosition = 1;
currentSwingDisplayed = null;
clearStave();
if(currentStatus & TEMPLATEDISPLAYED) drawStrokeT();
return;
}
currentStatus |= SESSIONLOADED;
currentStatus |= SKIPMAIN;
updateIcons();
swingStarts=[];
swingStarts[0] = 3;
currentPlaybackPosition = 1;
currentSwingDisplayed = null;
if((currentStatus & TEMPLATEDISPLAYED) && (dataBack.slice(0,5) == "LFIN:")) { clearStave(); drawStrokeT(); }
if(sample.length < 100){
setStatus("Insufficient data found!");
return;
}
var j=0;
var arrayLength = sample.length;
while (j < arrayLength && sample[j][0] < 90.0) j++; // move forward through sample to next swing
for (; j < arrayLength; j++){
if (sample[j][0] < 90.0 && sample[j][1] < 0.0) {
swingStarts[swingStarts.length] = j;
while (j < arrayLength && sample[j][0] < 90.0) j++; // move forward through sample to next swing
while (j < arrayLength && sample[j][0] >= 90.0) j++;
if (j == arrayLength ) break;
}
}
if (switchPoints.length == 0 || sample[switchPoints[0]][0] > 180) switchPoints.unshift(3); // a "just in case" should start at stand not be detected
setStatus(swingStarts.length.toString() + " strokes found.");
calculatePullStrengths();
document.getElementById("openSelect").options.length = 0;
ws.send("FILE:");
return;
}
if (dataBack.slice(0,5) == "STRT:"){
setStatus("Recording started");
return;
}
if (dataBack.slice(0,5) == "EIMU:"){
setStatus("IMU not active. Is one connected to device? Aborting");
if (currentStatus & RECORDINGSESSION) {
document.getElementById("recordIcon").onclick()
ws.send("STOP:");
}
currentStatus |= ABORTFLAG;
return;
}
if (dataBack.slice(0,5) == "SAMP:"){
sampleInterval = parseFloat(dataBack.slice(5));
return;
}
if (dataBack.slice(0,5) == "BATT:"){
batteryLevel = parseInt(dataBack.slice(5));
document.getElementById("batteryIcon").title = "Battery: " + batteryLevel + "%";
if(batteryLevel <= 10){
document.getElementById("batteryIcon").src = "battery0.png"
} else if(batteryLevel <= 40){
document.getElementById("batteryIcon").src = "battery50.png"
}
return;
}
if (dataBack.slice(0,5) == "EWAI:"){
setStatus("Now calibrating. Please keep bell down and at rest...");
return;
}
if (dataBack.slice(0,5) == "EPUL:"){
setStatus("Now calibrating. Please raise bell...");
return;
}
if (dataBack.slice(0,5) == "ESET:"){
setStatus("Now calibrating. Please set bell normally...");
return;
}
if (dataBack.slice(0,5) == "ESWI:"){
if(parseInt(dataBack.slice(5)) == 5) {
setStatus("Now calibrating. Please swing bell at least " + parseInt(dataBack.slice(5)) + " times...");
} else if (parseInt(dataBack.slice(5)) != 1){
setStatus("Now calibrating. Please swing bell at least " + parseInt(dataBack.slice(5)) + " more times...");
} else {
setStatus("Now calibrating. Please swing bell at least " + parseInt(dataBack.slice(5)) + " more time...");
}
return;
}
if (dataBack.slice(0,5) == "EDEF:"){
setStatus("Error loading calibration. Default values used.");
return;
}
if (dataBack.slice(0,5) == "EYEC:"){
var entries = dataBack.split(",");
// var roll = 360-parseFloat(entries[0].slice(5));
// var pitch = 360-parseFloat(entries[1]);
// var yaw = 0.0; //360-parseFloat(entries[2]);
// document.querySelector("section").style.transform= "rotateX(" + roll + "deg) rotateZ(-" + pitch + "deg) rotateY(" + yaw + "deg)";
// return;
// https://www.allaboutcircuits.com/projects/bosch-absolute-orientation-sensor-bno055/
var x = -parseFloat(entries[4]);
var y = parseFloat(entries[6]);
var z = parseFloat(entries[5]);
var w = parseFloat(entries[3]);
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var xy = x * y2;
var xz = x * z2;
var yy = y * y2;
var yz = y * z2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
document.querySelector("section").style.transform= "matrix3D(" + (1 - (yy + zz)) + "," + (xy + wz) + "," + (xz - wy) + "," + 0 + "," + (xy - wz) + "," + (1 - (xx + zz)) + "," + (yz + wx) + "," + 0 + "," + (xz + wy) + "," + (yz - wx) + "," + (1-(xx + yy)) + "," + 0 + "," + 0 + "," + 0 + "," + 0 + "," + 1 +")";
// document.querySelector("section").style.transform= "matrix3D(" + (1 - (yy + zz)) + "," + (xy - wz) + "," + (xz + wy) + "," + 0 + "," + (xy + wz) + "," + (1 - (xx + zz)) + "," + (yz - wx) + "," + 0 + "," + (xz - wy) + "," + (yz + wx) + "," + (1-(xx + yy)) + "," + 0 + "," + 0 + "," + 0 + "," + 0 + "," + 1 +")";
return;
}
if (dataBack.slice(0,5) == "ESTD:"){
setStatus("Bell not at stand. Aborting");
if ((currentStatus & RECORDINGSESSION) != 0) {
document.getElementById("recordIcon").onclick()
}
ws.send("STOP:");
currentStatus |= ABORTFLAG;
return;
}
if (dataBack.slice(0,5) == "EMOV:"){
setStatus("Bell moving. Aborting");
if ((currentStatus & RECORDINGSESSION) != 0) {
document.getElementById("recordIcon").onclick()
}
ws.send("STOP:");
currentStatus |= ABORTFLAG;
return;
}
if (dataBack.slice(0,5) == "EOVF:"){
setStatus("Data overflow!");
return;
}
setStatus(dataBack);
};
ws.onclose = function(event){
wsOpened=false;
setStatus("Error. Please close any other Bell-Boy windows and press refresh.");
if ((currentStatus & RECORDINGSESSION) != 0 ) {
currentStatus &= ~RECORDINGSESSION;
updateIcons();
if (liveintervalID != null) clearInterval(liveintervalID);
liveintervalID=null;
currentStatus &= ~(LASTHS1 | LASTBS1 | LASTHS2 | LASTBS2);
clearBell();
currentPlaybackPosition=1;
}
};
////////////////////////////////////////////////////////////////
// DATA FUNCTIONS //
////////////////////////////////////////////////////////////////
function playbackSample(){
// playintervalID=setInterval(playbackSample,sampleInterval*(100/playbackRanges[currentPlaybackSpeed][1]));
// sampleInterval*collectChunk*(100/playbackRanges[currentPlaybackSpeed][1])
var normalChunk = collectInterval/sampleInterval;
var iterations = Math.round((playbackRanges[currentPlaybackSpeed]/100.0) * normalChunk); // do this so that slower speeds are smoother
if (currentPlaybackPosition + iterations > sample.length-1){
iterations = sample.length-currentPlaybackPosition-1;
}
drawSamples(currentPlaybackPosition,iterations);
currentPlaybackPosition += iterations;
if (currentPlaybackPosition >= sample.length-1){ // reached end of sample, so stop
currentStatus &= ~PLAYBACK
updateIcons();
if (playintervalID != null) clearInterval(playintervalID);
playintervalID=null;
currentStatus &= ~(LASTHS1 | LASTBS1 | LASTHS2 | LASTBS2);
currentPlaybackPosition =1; // reset to 1 so that dataEntryOld works
}
}
function calculatePullStrengths(){
pullStrengths=[];
var totalPull = 0.0;
if(switchPoints.length == 0) return;
var i = null, j=null, k=null,m = null;
for (j=0; j<switchPoints.length; ++j){
totalPull=0.0;
i=sample[switchPoints[j]][0]; // angle at direction change
// for (k=switchPoints[j]; k>0 && Math.abs(sample[k][0] - i) < 3 ;k--); // find point 5 degrees back from direction change
// totalPull = sample[k][1]*sample[k][1];
// for (m=1;(k+m)<sample.length && Math.abs(sample[k+m][0] - i) < 3; m++); // add up accns to point 5 degrees forward from direction change
// totalPull += sample[m][1]*sample[m][1];
for (k=switchPoints[j]; k>0 && Math.abs(sample[k][0] - i) < 5 ;k--); // find point 5 degrees back from direction change
for (m=1;(k+m)<sample.length && Math.abs(sample[k+m][0] - i) < 5; m++){ // add up accns to point 5 degrees forward from direction change
pull = Math.abs(sample[k+m][2]);
if(pull < 25) continue; // ignore stuff below noise floor
totalPull += pull;
}
pullStrengths[pullStrengths.length] = totalPull/m;
// console.log((totalPull/m).toString() + " ");
}
}
function showLive(){
var iterations = (sample.length-1) - currentPlaybackPosition;
if (iterations < 5) return; // draw not fewer than 5 samples at a time
drawSamples(currentPlaybackPosition,iterations);
currentPlaybackPosition += iterations;
setStatus("Loaded: " + currentPlaybackPosition.toString());
}
function drawTimer(position, templated){
var timeElapsed = ((position-strokeTimer)*sampleInterval);
if (timeElapsed < 0.2) return; // assume a glitch
ctxBD.font = "12px sans serif";
ctxBD.fillStyle = "white";
if ((currentStatus & LASTBS2) != 0 ) { // i.e. timing backstroke
ctxBD.clearRect(posCB+1, ctxBD.canvas.height-20, CBwidth-2, 10);
if (timeElapsed > 0.5) {
ctxBD.textAlign = "center";
ctxBD.fillText("B " +timeElapsed.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-10);
}
} else {
ctxBD.clearRect(posCB+1, ctxBD.canvas.height-30, CBwidth-2, 10);
if (timeElapsed > 0.5) {
ctxBD.textAlign = "center";
ctxBD.fillText("H " + timeElapsed.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-20);
}
}
strokeTimer=position;
}
function drawStroke(){
if (currentSwingDisplayed == null) return;
var startpoint = swingStarts[currentSwingDisplayed];
var endpoint = 0;
if(currentSwingDisplayed == swingStarts.length-1) {
endpoint = sample.length - 3
} else {
endpoint = swingStarts[currentSwingDisplayed+1];
}
drawSamples(startpoint,endpoint-startpoint);
currentStatus &= ~(LASTHS1 | LASTBS1 | LASTHS2 | LASTBS2);
ctxBD.clearRect(posCB+1, ctxBD.canvas.height-30, CBwidth-2, 20);
ctxBD.textAlign = "center";
ctxBD.font = "12px sans serif";
ctxBD.fillStyle = "white";
var handstrokelength = 0.0;
var backstrokelength = 0.0;
if(ringTimes.length > (currentSwingDisplayed * 2)) handstrokelength = ringTimes[(currentSwingDisplayed * 2)];
if(ringTimes.length > (currentSwingDisplayed * 2)+1) backstrokelength = ringTimes[(currentSwingDisplayed * 2)+1];
if(handstrokelength != 0.0){
ctxBD.fillText("H " + handstrokelength.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-20);
}
if(backstrokelength != 0.0){
ctxBD.fillText("B " + backstrokelength.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-10);
}
clearStave();
ctxAT.font = "14px sans serif";
ctxAT.fillStyle = "white";
ctxAT.textAlign = "start";
ctxAT.textBaseline="bottom";
var totalHeight = ctxAT.canvas.height-ATbottomMargin-ATtopMargin;
var range = totalHeight/4.0; //(0.833*totalHeight)/5.0; //range of +- 2 change "steps"
var position = 0.0;
var overflow = 0;
var colour = "white";
var time = 0;
var cross = 0;
var currentDrawRing = 0;
var overflow = 0;
var time = 0;
var height = 0;
var rise = 0;
var i = 0;
var offset = 0;
i = Math.floor(-(ctxATt.canvas.width/2)/ATstepWidth)-1;
for(;;i++){
cross = 0;
overflow = 0;
currentDrawRing = (currentSwingDisplayed*2)+i;
// if((Math.floor(i/2)+currentSwingDisplayed)+1 > swingStarts.length -1) break;
// if((Math.floor(i/2)+currentSwingDisplayed)+1 < 0) continue;
if(currentDrawRing < 0) continue;
if(currentDrawRing > ringTimes.length -1) break;
if(currentDrawRing > switchPoints.length -1) break;
if(currentDrawRing > pullStrengths.length -1) break;
offset = (i*ATstepWidth)+ctxATt.canvas.width/2;
if(offset > ctxATt.canvas.width) break;
if(offset < -ATstepWidth) continue;
time = ringTimes[currentDrawRing];
height = 10 + 25 * (pullStrengths[currentDrawRing]/scaleValue);
if(height > 30) height = 30;
if(!(i%2)){
position = -range*(time - (changeInterval + (openHandstroke*changeStep)))/changeStep;
rise = sample[switchPoints[currentDrawRing]][0] * (height/10.0); // show a rise of +-10 degrees (consider making this +- 20)
} else {
position = -range*(time - changeInterval)/changeStep;
rise = (360-sample[switchPoints[currentDrawRing]][0]) * (height/10.0); // show a rise of +-10 degrees (consider making this += 20)
}
if(rise >= height) rise = height;
if(rise <= -height) rise = -height;
if(position < -totalHeight/2.0){
overflow = 1;
position = -totalHeight/2.0;
height = 10;
}
if(position > totalHeight/2.0){
overflow = 1;
position = totalHeight/2.0
height = 10;
}
position += (ATtopMargin + totalHeight/2.0);
colour = "white";
if(overflow) {colour = "rgba(240,128,128,1)"; cross = 1; }
drawBar(offset,ctxAT,!(i%2),position,height,rise,cross,colour);
if(!(i%2)){
ctxAT.fillText((Math.floor(i/2)+currentSwingDisplayed+1).toFixed(0), offset-20, ctxAT.canvas.height);
}
}
ctxAT.beginPath();
}
function drawStrokeT(){
if (TcurrentSwingDisplayed == null) return;
if ((currentStatus & TEMPLATEDISPLAYED) == 0) return;
var startpoint = TswingStarts[TcurrentSwingDisplayed];
var endpoint = 0;
if(TcurrentSwingDisplayed == TswingStarts.length-1) {
endpoint = template.length - 3
} else {
endpoint = TswingStarts[TcurrentSwingDisplayed+1];
}
drawSamplesOnTemplate(startpoint,endpoint-startpoint);
ctxBD.clearRect(posCB+1, ctxBD.canvas.height-60, CBwidth-2, 20);
ctxBD.textAlign = "center";
ctxBD.font = "12px sans serif";
ctxBD.fillStyle = "rgba(240,240,0,1)";
var handstrokelength = 0.0;
var backstrokelength = 0.0;
if(TringTimes.length > (TcurrentSwingDisplayed * 2)) handstrokelength = TringTimes[(TcurrentSwingDisplayed * 2)];
if(TringTimes.length > (TcurrentSwingDisplayed * 2)+1) backstrokelength = TringTimes[(TcurrentSwingDisplayed * 2)+1];
if(handstrokelength != 0.0){
ctxBD.fillText("H " + handstrokelength.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-50);
}
if(backstrokelength != 0.0){
ctxBD.fillText("B " + backstrokelength.toFixed(2)+"s", 2*BDwidth + 32, ctxBD.canvas.height-40);
}
ctxAT.font = "14px sans serif";
ctxAT.textAlign = "start";
ctxAT.textBaseline="bottom";
ctxAT.fillStyle = "rgba(240,240,0,0.6)";
ctxAT.strokeStyle = "rgba(240,240,0,0.6)";
ctxAT.beginPath();
var totalHeight = ctxAT.canvas.height-ATbottomMargin-ATtopMargin;
var range = totalHeight/4.0; //(0.833*totalHeight)/5.0; //range of +- 2 change "steps"
var position = 0.0;
var overflow = 0;
var time = 0;
var cross = 0;
var currentDrawRing = 0;
var overflow = 0;
var time = 0;
var height = 0;
var rise = 0;
var i = 0;
var offset = 0;
i = Math.floor(-(ctxATt.canvas.width/2)/ATstepWidth)-1;
for(;;i++){
cross = 0;
overflow = 0;
currentDrawRing = (TcurrentSwingDisplayed*2)+i;
// if((Math.floor(i/2)+currentSwingDisplayed)+1 > swingStarts.length -1) break;
// if((Math.floor(i/2)+currentSwingDisplayed)+1 < 0) continue;
if(currentDrawRing < 0) continue;
if(currentDrawRing > TringTimes.length -1) break;
if(currentDrawRing > TswitchPoints.length -1) break;
if(currentDrawRing > TpullStrengths.length -1) break;
offset = (i*ATstepWidth)+ctxATt.canvas.width/2;
if(offset > ctxATt.canvas.width) break;
if(offset < -ATstepWidth) continue;
time = TringTimes[currentDrawRing];
height = 10 + 25 * (TpullStrengths[currentDrawRing]/scaleValue);
if(height > 30) height = 30;
if(!(i%2)){
position = -range*(time - (TchangeInterval + (TopenHandstroke*TchangeStep)))/TchangeStep;
rise = template[TswitchPoints[currentDrawRing]][0] * (height/10.0); // show a rise of +-10 degrees (consider making this += 20)
} else {
position = -range*(time - TchangeInterval)/TchangeStep;
rise = (360-template[TswitchPoints[currentDrawRing]][0]) * (height/10.0); // show a rise of +-10 degrees (consider making this += 20)
}
if(rise >= height) rise = height;
if(rise <= -height) rise = -height;
if(position < -totalHeight/2.0){
overflow = 1;
position = -totalHeight/2.0;
}
if(position > totalHeight/2.0){
overflow = 1;
position = totalHeight/2.0
}
position += (ATtopMargin + totalHeight/2.0);
if(overflow) {cross = 1; height = 3;}
if(!(i%2)){
if(cross){
ctxAT.moveTo(offset-16, position-height);
ctxAT.lineTo(offset-10, position+height);
ctxAT.moveTo(offset-10, position-height);
ctxAT.lineTo(offset-16, position+height);
} else {
ctxAT.fillRect(offset-16, position+rise, 6, height-rise);
}
ctxAT.rect(offset-16, position-height, 6, 2 * height);
ctxAT.fillText((Math.floor(i/2)+TcurrentSwingDisplayed+1).toFixed(0), offset+5, ctxAT.canvas.height);
} else {
if(cross){
ctxAT.moveTo(offset-18, position-height);
ctxAT.lineTo(offset-12, position+height);
ctxAT.moveTo(offset-12, position-height);
ctxAT.lineTo(offset-18, position+height);
} else {
ctxAT.fillRect(offset-18, position+rise, 6, height-rise);
}
ctxAT.rect(offset-18, position-height, 6, 2 * height);
}
ctxAT.stroke();
}
ctxAT.beginPath();
}
function calculateTimings(){
var numberOfBells = document.getElementById("bellsSelect").selectedIndex + 3;
changeInterval = 60.0/CPM;
changeStep = changeInterval/numberOfBells;
var pealTime = 0.0;
if(numberOfBells > 7){ // not sure this is calculated correctly, as it does not take into account an extra beat for open handstroke
pealTime = (changeInterval * 5000.0);
} else {
pealTime = (changeInterval * 5040.0);
}
var hours = Math.floor(pealTime/3600.0);
var minutes = "0" + Math.round((pealTime % 3600)/60.0);
document.getElementById("CPM").innerHTML=CPM.toFixed(1)+" ";
document.getElementById("CPM").title = "Peal time: " + hours + ":" + minutes.substr(minutes.length-2,2);
document.getElementById("OH").innerHTML=openHandstroke.toFixed(1)+" ";
if(!nonLive){
ws.send("BELS:" + numberOfBells + "," + CPM + "," + openHandstroke.toFixed(1) + "," + document.getElementById("chimeSelect").options[document.getElementById("chimeSelect").selectedIndex].text);
}
drawPowerScales();
foreclick(0); // redraw display with new values
}
function drawSamples(position,iterations){
var stepSize = (BDwidth * 1.0)/(ROIL-ROIU);
for (var i = 0; i < iterations; i++){
var dataEntryOld = sample[position + i -1].slice();
var dataEntryCurrent = sample[position + i].slice();
var dataEntryNext = sample[position + i + 1].slice();
if((currentStatus & RECORDINGSESSION) != 0 || (currentStatus & PLAYBACK) != 0 ) {
if(dataEntryCurrent[3] == 2) drawLiveTime(dataEntryCurrent[4],false,position+i);
if(dataEntryCurrent[3] == 1) drawLiveTime(dataEntryCurrent[4],true,position+i);
drawBell(180-dataEntryCurrent[0]);
}
// drawAT(dataEntryCurrent[2]);
if (dataEntryCurrent[0] > ROIU && dataEntryCurrent[0] < ROIL && dataEntryCurrent[1] >= 0) { // within ROI for HS1
if (dataEntryNext[0] >= ROIL) {
dataEntryCurrent[0] = ROIL; // tidy up at end of ROI
}
var startAngle = Math.max(ROIU,dataEntryOld[0]);
var endAngle = dataEntryCurrent[0];
if (endAngle <= startAngle) {
continue; // for moment don't swap, just do nothing
}
if ((currentStatus & LASTHS1) == 0 ) { // clear highest part of this and previous ROI if this is the first time
var pixelsFromROIL = (ROIL - startAngle) * stepSize;
ctxBD.clearRect(posBS2 + pixelsFromROIL, 14, 2 * (BDwidth - pixelsFromROIL), ctxBD.canvas.height - 14 - 25);
if ((currentStatus & LASTBS2) != 0 ) {
drawTimer(position + i, false);
} else {
strokeTimer = 0; // not started yet
}
currentStatus &= ~(LASTBS2 | LASTBS1 | LASTHS2);
currentStatus |= LASTHS1;
}
var pixelsFromROIU = (startAngle - ROIU) * stepSize;
var pixelWidth = (endAngle - startAngle) * stepSize;
var pixelHeight = (ctxBD.canvas.height - 14 - 25) * Math.min(dataEntryCurrent[2]/scaleValue,1);
if (pixelHeight < 0) pixelHeight = 0;
ctxBD.clearRect(posHS1 + pixelsFromROIU, 14, pixelWidth + 1, ctxBD.canvas.height - 14 - 25);
ctxBD.fillStyle="white";
ctxBD.fillRect(posHS1 + pixelsFromROIU, ctxBD.canvas.height - 25, pixelWidth, (0-pixelHeight));
} else if (dataEntryCurrent[0] > (360 - ROIL) && dataEntryCurrent[0] < (360 - ROIU) && dataEntryCurrent[1] >= 0) { // within ROI for HS2
var endAngle = Math.max(ROIU, 360-dataEntryCurrent[0]);
var startAngle = Math.min(360-dataEntryOld[0], ROIL);
if (endAngle >= startAngle) {
continue; // for moment don't swap, just do nothing
}
if ((currentStatus & LASTHS2) == 0 ) { // set flags if we have not been before this swing
currentStatus &= ~(LASTHS1 | LASTBS1 | LASTBS2);
currentStatus |= LASTHS2;
}
var pixelsFromROIL = (ROIL - startAngle) * stepSize;
var pixelWidth = (startAngle - endAngle) * stepSize;
var pixelHeight = (ctxBD.canvas.height - 14 - 25) * Math.min(-(dataEntryCurrent[2])/scaleValue,1);
if (pixelHeight < 0) pixelHeight = 0;
ctxBD.clearRect(posHS2 + pixelsFromROIL, 14, pixelWidth + 1, ctxBD.canvas.height - 14 - 25);
ctxBD.fillStyle="white";
ctxBD.fillRect(posHS2 + pixelsFromROIL, ctxBD.canvas.height - 25, pixelWidth, (0-pixelHeight));
} else if (dataEntryCurrent[0] > (360 - ROIL) && dataEntryCurrent[0] < (360 - ROIU) && dataEntryCurrent[1] < 0) { // within ROI for BS1
if (dataEntryNext[0] <= (360 - ROIL)) {
dataEntryCurrent[0] = (360 - ROIL); // tidy up at end of ROI
}
var startAngle = Math.max(ROIU,360-dataEntryOld[0]);
var endAngle = Math.min(ROIL,360-dataEntryCurrent[0]);
if (startAngle >= endAngle) {
continue; // for moment don't swap, just do nothing
}
if ((currentStatus & LASTBS1) == 0 ) { // clear highest part of this and previous ROI if this is the first time
var pixelsFromROIL = (ROIL - startAngle) * stepSize;
ctxBD.clearRect(posHS2 + pixelsFromROIL, 14, 2 * (BDwidth - pixelsFromROIL), ctxBD.canvas.height - 14 - 25);
if ((currentStatus & LASTHS2) != 0 ) {
drawTimer(position + i, false);
}
currentStatus &= ~(LASTBS2 | LASTHS1 | LASTHS2);
currentStatus |= LASTBS1;
}
var pixelsFromROIU = (startAngle - ROIU) * stepSize;
var pixelWidth = (endAngle - startAngle) * stepSize;
var pixelHeight = (ctxBD.canvas.height - 14 - 25) * Math.min(-(dataEntryCurrent[2])/scaleValue,1)
if (pixelHeight < 0) pixelHeight = 0;
ctxBD.clearRect(posBS1 + pixelsFromROIU, 14, pixelWidth + 1, ctxBD.canvas.height - 14 - 25);
ctxBD.fillStyle="white";
ctxBD.fillRect(posBS1 + pixelsFromROIU, ctxBD.canvas.height - 25, pixelWidth, (0-pixelHeight));
} else if (dataEntryCurrent[0] > ROIU && dataEntryCurrent[0] < ROIL && dataEntryCurrent[1] < 0) { // within ROI for BS2
var endAngle = Math.max(ROIU,dataEntryCurrent[0]);
var startAngle = Math.min(dataEntryOld[0],ROIL);
if (endAngle >= startAngle) {
continue; // for moment don't swap, just do nothing
}
if ((currentStatus & LASTBS2) == 0 ) { // set flags if we have not been before this swing
if(currentStatus & LASTHS1) currentStatus |= WOBBLINGATSTAND; // the only way this can happen is with a wobble - this flags stave not to draw anything
currentStatus &= ~(LASTHS1 | LASTBS1 | LASTHS2);
currentStatus |= LASTBS2;
}
var pixelsFromROIL = (ROIL - startAngle) * stepSize;
var pixelWidth = (startAngle - endAngle) * stepSize;
var pixelHeight = (ctxBD.canvas.height - 14 - 25) * Math.min((dataEntryCurrent[2])/scaleValue,1);
if (pixelHeight < 0) pixelHeight = 0;
ctxBD.clearRect(posBS2 + pixelsFromROIL, 14, pixelWidth + 1, ctxBD.canvas.height - 14 - 25);
ctxBD.fillStyle="white";
ctxBD.fillRect(posBS2 + pixelsFromROIL, ctxBD.canvas.height - 25, pixelWidth, (0-pixelHeight));
}
}
}
function drawLiveTime(time, HS, currentPosition){
currentATmargin += ATstepWidth;
if (currentATmargin >= ctxAT.canvas.width/2) currentATmargin -= ctxAT.canvas.width/2;
ctxAT.clearRect(currentATmargin-20 ,0,ATstepWidth,ctxAT.canvas.height);
ctxAT.clearRect(currentATmargin-20+ctxAT.canvas.width/2,0,ATstepWidth,ctxAT.canvas.height);
var totalHeight = ctxAT.canvas.height-ATbottomMargin-ATtopMargin;
var range = totalHeight/4.0; //(0.833*totalHeight)/5.0; //range of +- 2 change "steps"
var position = 0.0;
if(HS) {
position = -range*(time - (changeInterval + (openHandstroke*changeStep)))/changeStep; // this does a full beat ? should this be half a beat
} else {
position = -range*(time - changeInterval)/changeStep;
}
var overflow = 0;
if(position < -totalHeight/2.0){
overflow = 1;
position = -totalHeight/2.0;
}
if(position > totalHeight/2.0){
overflow = 1;
position = totalHeight/2.0
}
position += (ATtopMargin + totalHeight/2.0);
var colour = "white";
if(overflow) colour = "red";
var height = 0;
var cross = 0;
var j = 0;
if(switchPoints.length != 0){
for(j = switchPoints.length - 1; j >= 0 && switchPoints[j] > currentPosition; j--); // find switchpoint preceding the current position
j = switchPoints[j];
}
if (currentPosition - j > 300 || !switchPoints.length){ // 300 = 2.4 seconds worth of samples - not possible to ring this slowly unless really holding up - assume bad data
height = 10; //default height because the switchPoint is not close enough
cross = 1;
} else {
var i=sample[j][0]; // angle at direction change
for (k=j; k>0 && Math.abs(sample[k][0] - i) < 5 ;k--);
var totalPull = 0.0;
var pull = 0.0;
for (var m=1;(k+m)<sample.length && Math.abs(sample[k+m][0] - i) < 5; m++){ // add up accns to point 5 degrees forward from direction change
pull = Math.abs(sample[k+m][2]);
if(pull < 25) continue; // ignore stuff below noise floor
totalPull += pull;
}
height = 10 + 25 * ((totalPull/m)/scaleValue);
}
if(height > 30) height = 30;
var rise = 0.0;
if(HS){
rise=sample[j][0] * (height/10.0); // show a rise of +-10 degrees (consider making this += 20)
} else {
rise=(360-sample[j][0]) * (height/10.0);
}
if(rise >= height) rise = height;
if(rise <= -height) rise = -height;
drawBar(currentATmargin,ctxAT,HS,position,height,rise,cross,colour);
drawBar(currentATmargin+ctxAT.canvas.width/2,ctxAT,HS,position,height,rise,cross,colour);
document.getElementById("canvasAT").style.marginLeft = (currentATmargin * -1) + "px";