-
Notifications
You must be signed in to change notification settings - Fork 1
/
onefile.html
1400 lines (1180 loc) · 38.4 KB
/
onefile.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" >
<head>
<meta charset="UTF-8">
<title>SparkFun BLE Settings App</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100%;
height: 100vh;
background-image: linear-gradient(to top, #1e1e1e 100%, #68e0cf 200%);
display: flex;
justify-content: center;
align-items: start;
}
button {
border: 0;
outline: 0;
}
.container {
margin: 40px 0;
width: 450px;
padding: 10px 25px;
background: #202020;
border-radius: 10px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.45), 0 4px 8px rgba(0, 0, 0, 0.35), 0 8px 12px rgba(0, 0, 0, 0.15);
font-family: "Arial";
}
.container h2.title {
font-size: 1.75rem;
margin: 10px -5px;
margin-bottom: 30px;
color: #fff;
}
.field-title {
position: absolute;
top: -10px;
left: 8px;
transform: translateY(-50%);
font-weight: 800;
color: rgba(255, 255, 255, 0.5);
text-transform: uppercase;
font-size: 0.65rem;
pointer-events: none;
user-select: none;
}
.settings h2.title {
font-size: 1rem;
margin: 20px -5px;
margin-bottom: 6px;
color: rgba(255, 255, 255, 0.9);
text-transform: uppercase;
}
.options {
width: 100%;
height: auto;
margin: 50px 0;
}
.range__slider {
position: relative;
width: 100%;
height: calc(65px - 10px);
display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
margin: 16px 0;
}
.range__slider::before, .range__slider::after {
position: absolute;
color: #fff;
font-size: 0.9rem;
font-weight: bold;
}
.range__slider::before {
content: attr(data-min);
left: 10px;
}
.range__slider::after {
content: attr(data-max);
right: 10px;
}
.range__slider .length__title::after {
content: attr(data-length);
position: absolute;
right: -30px;
font-variant-numeric: tabular-nums;
color: #fff;
}
#slider {
-webkit-appearance: none;
width: calc(100% - (70px));
height: 2px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.314);
outline: none;
padding: 0;
margin: 0;
cursor: pointer;
}
#slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: white;
cursor: pointer;
transition: all 0.15s ease-in-out;
}
#slider::-webkit-slider-thumb:hover {
background: #d4d4d4;
transform: scale(1.2);
}
#slider::-moz-range-thumb {
width: 20px;
height: 20px;
border: 0;
border-radius: 50%;
background: white;
cursor: pointer;
transition: background 0.15s ease-in-out;
}
#slider::-moz-range-thumb:hover {
background: #d4d4d4;
}
.settings {
position: relative;
height: auto;
widows: 100%;
display: flex;
flex-direction: column;
}
.settings .setting {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .setting input {
opacity: 0;
position: absolute;
}
.settings .setting input + label {
user-select: none;
}
.settings .setting input + label::before, .settings .setting input + label::after {
content: "";
position: absolute;
transition: 150ms cubic-bezier(0.24, 0, 0.5, 1);
transform: translateY(-50%);
top: 50%;
right: 10px;
cursor: pointer;
}
.settings .setting input + label::before {
height: 30px;
width: 50px;
border-radius: 30px;
background: rgba(214, 214, 214, 0.434);
}
.settings .setting input + label::after {
height: 24px;
width: 24px;
border-radius: 60px;
right: 32px;
background: #fff;
}
.settings .setting input:checked + label:before {
background: #5d68e2;
transition: all 150ms cubic-bezier(0, 0, 0, 0.1);
}
.settings .setting input:checked + label:after {
right: 14px;
}
.settings .setting input:focus + label:before {
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.75);
}
.settings .setting input:disabled + label:before, .settings .setting input:disabled + label:after {
cursor: not-allowed;
}
.settings .setting input:disabled + label:before {
background: #4f4f6a;
}
.settings .setting input:disabled + label:after {
background: #2b2b2b;
}
.settings .text-prop {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .text-prop input {
position: absolute;
right: 12px;
background: #2b2b2b;
font-weight: bold;
font-size: 1.1rem;
width:45%;
text-align: left;
color: #fff;
}
.settings .text-prop input + label {
user-select: none;
}
.settings .number-prop {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .number-prop input {
position: absolute;
right: 12px;
background: #2b2b2b;
font-weight: bold;
font-size: 1.1rem;
width:45%;
text-align: right;
color: #fff;
}
.settings .number-prop input + label {
user-select: none;
}
.settings .date-prop {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .date-prop input {
position: absolute;
right: 12px;
background: #2b2b2b;
font-weight: bold;
font-size: 1.2rem;
width:45%;
text-align: left;
color: #fff;
}
.settings .date-prop input + label {
user-select: none;
}
.settings .date-prop input::-webkit-calendar-picker-indicator {
filter: invert(1);
}
.settings .time-prop {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .time-prop input {
position: absolute;
right: 12px;
background: #2b2b2b;
font-weight: bold;
font-size: 1.2rem;
width:45%;
text-align: left;
color: #fff;
}
.settings .time-prop input + label {
user-select: none;
}
.settings .time-prop input::-webkit-calendar-picker-indicator {
filter: invert(1);
}
.settings .select-prop {
position: relative;
width: 100%;
height: calc(65px - 10px);
background: rgba(255, 255, 255, 0.08);
border-radius: 8px;
display: flex;
align-items: center;
padding: 10px 25px;
color: #fff;
margin-bottom: 8px;
}
.settings .select-prop select {
position: absolute;
right: 12px;
background: #2b2b2b;
font-weight: bold;
font-size: 1.2rem;
width:45%;
text-align: left;
color: #fff;
}
.settings .select-prop input + label {
user-select: none;
}
input[type=number]:invalid{
background-color: #FFAAAA;
}
.btn.generate {
user-select: none;
position: relative;
width: 100%;
height: 50px;
margin: 10px 0;
border-radius: 8px;
color: #fff;
border: none;
background: #4193F7;
letter-spacing: 1px;
font-weight: bold;
text-transform: uppercase;
cursor: pointer;
transition: all 150ms ease;
}
.btn.generate:active {
transform: translateY(-3%);
box-shadow: 0 4px 8px rgba(255, 255, 255, 0.08);
}
.support {
position: fixed;
right: 10px;
bottom: 10px;
padding: 10px;
display: flex;
}
a {
margin: 0 20px;
color: #fff;
font-size: 2rem;
transition: all 400ms ease;
}
a:hover {
color: #222;
}
.sparkfun-corner svg{
position:absolute;
right:0;
top:0;
mix-blend-mode:lighten;
color:#000000;
fill:#ff0000;
clip-path: polygon(0 0, 100% 0, 100% 100%);
}
.sparkfun-corner:hover svg { mix-blend-mode: normal;}
.sparkfun-corner:hover .sparkfun-flame {color: #ffffff;}
#myProgress {
width: 100%;
background-color: #0a0e31;
display: none;
}
#myBar {
width: 1%;
height: 3px;
background-color: #0B1EDF;
}
.alert-box {
font-family: "Arial";
padding: 18px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 10px;
visibility: hidden;
opacity: 0;
font-size: 1.2rem;
position: absolute;
top: 20px;
left: 0;
right: 0;
margin:auto;
width: 70%;
}
.alert-failure {
color: #a94442;
background-color: #ffbebe;
border-color: #ebccd1;
}
.alert-box.show{
visibility: visible;
opacity: 1;
transition: opacity .5s linear;
}
.alert-box.hide{
visibility: hidden;
opacity: 0;
transition: visibility 0s .5s,opacity .5s linear;
}
.alert-warning {
color: #8a8d3b;
background-color: #fcf8a3;
border-color: #faebcc;
}
</style>
<!-- Add some branding -->
<a class="sparkfun-corner" href="https://sparkfun.com" title="SparkFun Electronics" target="_blank">
<svg width="80" height="80" viewbox="0 0 80 80">
<title>SparkFun Electronics</title>
<path d="M 80 80 L 0 0 L 80 0 Z" />
<path class="sparkfun-flame" d="M 71.04176 9.716209 C 71.59436 12.04736 69.83418 12.371568 69.83418 12.371568 C 68.60866 12.588144 66.63235 11.374004 65.63697 10.162488 C 64.41146 8.668329 64.56634 7.493129 65.519715 6.449624 C 66.95218 4.8653347 69.40453 5.4774366 69.40453 5.4774366 C 64.91155 1.5357454 60.232625 4.766016 60.232625 4.766016 C 56.06473 7.63445 57.260934 11.545515 61.021926 14.957797 C 64.41146 18.041933 61.70403 21.423588 58.28956 20.98431 C 55.948787 20.685915 54.99104 18.807607 55.55326 17.65472 C 56.037605 16.668532 57.71771 16.062118 57.71771 16.062118 C 55.787776 15.27763 53.51613 16.132997 53.51613 16.132997 C 51.4755 16.902172 49.793203 18.588843 49.927524 22.813615 L 49.927524 42.4 C 52.03685 39.799333 54.51632 37.154475 54.51632 37.154475 C 57.29287 34.17097 58.559955 32.15484 61.411326 32.39067 C 66.806485 32.724502 70.70792 30.327723 73.36022 26.48185 C 78.45217 19.085437 74.463666 10.821405 71.04176 9.716209 Z" fill="currentColor"/>
</svg>
</a>
</head>
<body>
<div class="container">
<h2 class="title" id='settings-title'>SparkFun Settings</h2>
<button class="btn generate" id="connect">Connect to Device</button>
<div id="myProgress">
<div id="myBar"></div>
</div>
<div class="settings" id="settings-container">
</div>
</div>
<div class="alert-box alert-failure hide" id="message-error">Failure Alert</div>
<div class="alert-box alert-warning hide" id="message-warning">Warning Alert</div>
<!-- javascript -->
<script type="text/javascript">
// App to connect to an embedded device (like SparkFun OpenLog) and manage settings via BLE
// SETUP - Define the Target BLE Service
//--------------------------------------------------------------------------------------
const kTargetServiceUUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const kTargetServiceName = "SparkFun Board";
//--------------------------------------------------------------------------------------
console.clear();
//--------------------------------------------------------------------------------------
// Property Creation and Management
//
// Dynamically create and manage properties
//--------------------------------------------------------------------------------------
// BLE Codes for our service
const kBLEDescSFEPropCoreUUID = 0xA101;
var bIsConnected = false;
// Simple Data marshling code
const textDecoder = new TextDecoder();
function dataToText(data){ return textDecoder.decode(data);}
const textEncoder = new TextEncoder();
function textToData(text){ return textEncoder.encode(text);}
const targetID = 'settings-container';
const currentProperties=[];
let deviceName=kTargetServiceName;
function setDeviceName(name){
deviceName = name;
document.getElementById("settings-title").innerHTML= name+ " Settings";
}
// DEBUG
let debugLoadTime=0;
const progressBar = {
increment: 1,
set_value: function(value){
document.getElementById("myBar").style.width=value + "%";
},
add_value: function(value){
let element = document.getElementById("myBar");
element.style.width = (parseInt(element.style.width)+value) + "%";
},
start: function(){
this.set_value(0);
document.getElementById("myProgress").style.display="flex";
},
end: function(){
document.getElementById("myProgress").style.display="none";
}
};
const messageBox = {
msg_error : document.querySelector("#message-error"),
msg_warning : document.querySelector("#message-warning"),
_show_msg : function(msg, message, temporary){
msg.innerHTML = message;
msg.classList.replace("hide", "show");
if(temporary)
window.setTimeout(() =>this._hide_msg(msg), 4000);
},
_hide_msg : function(msg){
msg.classList.replace("show", "hide");
},
showError: function(message, temporary=true){
this._show_msg(this.msg_error, message, temporary);
},
showWarning: function(message, temporary=true){
this._show_msg(this.msg_warning, message, temporary);
},
hideMessages: function(){
this._hide_msg(this.msg_error);
this._hide_msg(this.msg_warning);
}
}
//---------------------------------------
// property things
//---------------------------------------
// Used to main unique ids for property objects
let namecnt = 0;
const kBlkRange = 0x02;
const kBlkTitle = 0x01;
const kBlkSelectOps = 0x03;
const kBlkIncrement = 0x04;
// Base property class
class Property{
constructor(bleChar, name, order){
this.characteristic = bleChar;
this.name = name;
this.order = order;
this.ID = "property-" + namecnt++; // unique id for the DOM
this.div = null;
this.timer=-1;
this.group=null;
this.title=null;
}
processBlk(blkType, value, iPos){
if(blkType != kBlkTitle){
return iPos;
}
let len = value.getUint8(iPos++);
this.title = dataToText(new DataView(value.buffer, iPos, len));
return iPos + len;
}
init(){
// For now - enable notifications on all values ...
this.enableNotifications();
// check for group title
return new Promise( (resolve) => {
// title?
if(this.title != null && this.title.length > 2){
this.group= document.createElement("div");
this.group.innerHTML = ` <h2 class="title">`+ this.title + `</h2>`;
document.getElementById(targetID).appendChild(this.group);
}
this.generateElement();
resolve(0);
}).catch(error => {
console.log("Error setting up property: ", this.name);
});
}
updateValue(value){}
update(){
this.characteristic.readValue().then( value =>{
this.updateValue(value);
});
}
enableNotifications(){
if(this.characteristic){
this.characteristic.startNotifications().then( _ =>{
let target = this;
this.characteristic.addEventListener('characteristicvaluechanged', function(event){
target.updateValue(event.target.value);
});
}).catch(error => {
//console.log("Notifications not available.");
// just eat this
});
}
}
deleteElement(){
if(this.div){
this.div.remove();
this.div=null;
}
if(this.group){
this.group.remove();
this.group=null;
}
}
}
// -------------------------------------------
class boolProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="setting">
<input type="checkbox" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
// event handler wireup - on change event, save the new value to BLE device
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
this.inputField.checked = value.getUint8(0, true);
}
saveValue(){
// Get the value from the input field and save it to the characteristic
let buff = new ArrayBuffer(1);
let newValue = new Uint8Array(buff);
newValue[0] = this.inputField.checked;
this.characteristic.writeValue(buff);
}
}
//-------------------------------------------------------------
const range_fill = "#0B1EDF";
const range_background = "rgba(255, 255, 255, 0.214)";
class rangeProperty extends Property{
processBlk(blkType, value, iPos){
if(blkType != kBlkRange){
return super.processBlk(blkType, value, iPos);
}
this.min = value.getInt32(iPos,true);
this.max = value.getInt32(iPos+4, true);
return iPos + 8;
}
_updateUX(value){
//set text
this.txtValue.setAttribute("data-length", ' ' + value);
// set gutter color -
const percentage = (100 * (value - this.input.min)) / (this.input.max - this.input.min);
const bg = `linear-gradient(90deg, ${range_fill} ${percentage}%, ${range_background} ${percentage + 0.1}%)`;
this.input.style.background = bg;
}
//------------------------
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="length range__slider" data-min="`+ this.min +`" data-max="` + this.max +`">
<div class="length__title field-title" data-length='0'>`+ this.name +`:</div>
<input id="slider" type="range" min="`+ this.min +`" max="`+ this.max +`" value="16" />
</div>
`;
this.input = this.div.querySelector('input');
this.txtValue = this.div.querySelector('.length__title');
document.getElementById(targetID).appendChild(this.div);
// Update slider values - event handler
this.input.addEventListener("input", event => {
this._updateUX(event.target.value);
});
// On change, set the underlying value
this.input.addEventListener("change", () => {
this.saveValue();
});
// and now update to the current value
this.update();
}
updateValue(value){
this.input.value = value.getInt32(0, true);
this._updateUX(this.input.value);
}
saveValue(){
// Get the value from the input field and save it to the characteristic
let buff = new ArrayBuffer(4);
let newValue = new Int32Array(buff);
newValue[0] = this.input.value;
this.characteristic.writeValue(buff);
}
}
//-------------------------------------------------------------
// textProperty Object
class textProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="text-prop">
<input type="text" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
this.inputField.value = dataToText(value);
}
saveValue(){
// Get the value from the input field and save it to the characteristic
//console.log("Save Value Text: " + this.inputField.value);
this.characteristic.writeValue(textToData(this.inputField.value));
}
}
//-------------------------------------------------------------
// intProperty Object
class intProperty extends Property{
constructor(bleChar, name, order){
super(bleChar, name, order);
this.increment = 1;
}
processBlk(blkType, value, iPos){
if(blkType != kBlkIncrement){
return super.processBlk(blkType, value, iPos);
}
this.increment = value.getUint32(iPos,true);
return iPos + 4;
}
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="number-prop">
<input type="number" id="`+ this.ID + `" step="` + this.increment + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// get the value from the BLE char and place it in the field
this.inputField.value = value.getInt32(0, true);
}
saveValue(){
// The arrows in the number field can fire off rapid events, which
// can hammer the update, so buffer using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
let buff = new ArrayBuffer(4);
let newValue = new Int32Array(buff);
newValue[0] = this.inputField.value;
this.characteristic.writeValue(buff);
}, 1500);
}
}
//-------------------------------------------------------------
// dateProperty Object
class dateProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="date-prop">
<input type="date" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// get the value from the BLE char and place it in the field
// Check for a valid date format...
let digits = dataToText(value).split("-", 3).concat(['','']);
// if any of the digits are not a digit, default to current date
if(isNaN(digits[0]) || isNaN(digits[1]) || isNaN(digits[2])){
let currTime = new Date();
digits = [currTime.getFullYear(), currTime.getMonth(), currTime.getDay()];
}
if(digits[0].length == 2){ // short year value?
digits[0] = '20'+digits[0];
}
// set value - pad month and day numbers with zeros
this.inputField.value = [digits[0], ('0'+digits[1]).slice(-2), ('0'+digits[2]).slice(-2)].join('-');
}
saveValue(){
// Typing in the timefield is slow, so you can hammer the BLE
// char with multiple sets as the date changes. Buffer this using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
this.characteristic.writeValue(textToData(this.inputField.value));
}, 1500);
}
}
//-------------------------------------------------------------
// timeProperty Object
class timeProperty extends Property{
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="time-prop">
<input type="time" id="`+ this.ID + `" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();
});
this.update();
}
updateValue(value){
// Check for a valid time - split value, concat empty value to ensure 2 elements
let digits = dataToText(value).split(":", 2).concat('');
// if any of the time value are not a digit, default to current time
if(isNaN(digits[0]) || isNaN(digits[1])){
let currTime = new Date();
digits = [currTime.getHours(), currTime.getMinutes()];
}
// set value - pad numbers with zeros
this.inputField.value = [('0'+digits[0]).slice(-2), ('0'+digits[1]).slice(-2)].join(':');
}
saveValue(){
// Typing in the timefield is slow, so you can hammer the BLE
// char with multiple sets as the date changes. Buffer this using a timer
window.clearTimeout(this.timer);
this.timer = window.setTimeout(()=>{
// Get the value from the input field and save it to the characteristic
this.characteristic.writeValue(textToData(this.inputField.value));
}, 1500);
}
}
class floatProperty extends Property{
processBlk(blkType, value, iPos){
if(blkType != kBlkIncrement){
return super.processBlk(blkType, value, iPos);
}
this.increment = value.getFloat32(iPos,true).toFixed(5); //round it
return iPos + 4;
}
constructor(bleChar, name, order){
super(bleChar, name, order);
this.increment = .01;
}
generateElement(){
this.div = document.createElement("div");
this.div.innerHTML = `
<div class="number-prop">
<input type="number" id="`+ this.ID + `" step="`+this.increment+`" />
<label for="` + this.ID + `">` + this.name + `</label>
</div>
`;
document.getElementById(targetID).appendChild(this.div);
this.inputField = this.div.querySelector('input');
this.inputField.addEventListener("change", () => {
this.saveValue();