-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
703 lines (638 loc) · 24.8 KB
/
common.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
// MAJOR PROJECT UI
// Daniel Messham
// Date
//
// Common ui elements and settings (Hopefully)
//settigs related to appearance
let appearance = {
background:"Black",
text:"Black",
};
//settings related to io and hardware control (for whatever i use to allow for html+js to control RPi GPIO stuf)
let hardware = {
attachedDevices:[ ['id','name','state',['capibilities'],'type' ],
[] ],
hardwareName:'Raspberry Pi3',
hardwareType:['pi','arm',],
};
//settings related to security (mostly read only, many will only be editable w/ physical access for security reasons) or that could break remote access
let secure = {
DeviceName:"[DEVICE_NAME]",
WifiNetwork:['ssid','type','password']//find a way to not store password in hardcoded plaintext
}
let visualSettings = {
testToggle:false,
testNum:3,
testInt:3.0,
testText:"Hello world!",
testDropdown:[["hello0",0],["hello1",1],["hello2",2]],
testSlider:50,
testProgress:50,
testLoading:"ayylmao",
testToggleSlider:[20,false],
testInvalid:"This is an invalid list entry type",
}
let visualSettingsList = [//Used for the gui settings page, follows format of [name,type(0toggle,1number,2int,3text,4dropdown)value,locked,Options(offstate/onstate,min/max,variables)]
["toggle","testToggle",visualSettings.testToggle,],
["number","testNum",visualSettings.testNum,0,100],
["numSlider","testInt",visualSettings.testInt,0,100],
["textEntry","testText",visualSettings.testText,],
["dropdownMenu","testDropdown",visualSettings.testDropdown,],
["slider","testSlider",visualSettings.testSlider,0,100],
["progress","testProgress",visualSettings.testProgress,0,100],
["loading","testLoading"],
["invalid","testInvalidEntry",visualSettings.testInvalid],
//["toggleSlide","testToggle+Slider",visualSettings.testToggleSlider[0],visualSettings.testToggleSlider[1],0,100],
]
let relayList = []
let timeBase = 0//timer used for pacing user input
let timeDelay = 700//min time between inputs
let render
let canvasDiv
let backgroundColor = [30,45,30]
function setup(){
canvasDiv = document.getElementById('jscanvas');
let width = canvasDiv.offsetWidth;
let height = canvasDiv.offsetHeight;
let render = createCanvas(width-30, 450).parent("jscanvas");
render.style('margin', '-10px');
}
function windowResized() {
let width = canvasDiv.offsetWidth;
let height = canvasDiv.offsetHeight;
resizeCanvas(width-20, 450);
}
function labelDraw(name) {
fill('gray')
rect(0,29,width,2)
fill('lightgray')
textSize(25)
textStyle(ITALIC)
text(name,10,3, width-7, 32)
textStyle(NORMAL)
}
//drawWidth = windowWidth*0.8;
let drawWidth = 768;//canvas width
function animationLoopPercent(time){
return( (millis())%time/time)
}
//common controls
function drawButton(x,y,buttonWidth, buttonHeight, txt, accent, txtColor){
if(mouseArea(x,y,buttonWidth,buttonHeight)){
stroke(accent)
if(mouseIsPressed){
fill(accent);
stroke(txtColor)
strokeWeight(2.5)
//return true;
}
else{
fill(70)
strokeWeight(2)
stroke(accent);
}
}
else{fill(40)
stroke(accent)
strokeWeight(1.5);}
rect(x,y,buttonWidth,buttonHeight,5)
noStroke();
textSize(buttonHeight/4)
fill(txtColor)
textSize(17)
text(txt,x+5,y+5,width-5,buttonHeight-5)
return false;
}
function mouseArea(x,y,awidth,aheight){
return(mouseX>x && mouseX<x+awidth && mouseY>y && mouseY<y+aheight)
}
function mediaButton(icon,x,y,offset,size,space,bgColor,control){
fill(bgColor)//
drawButton(x + offset*size+(offset+1)*space, y+space,size, size, control, bgColor, 70)
image(icon,x + offset*size+(offset+1)*space, y+space, size, size)
return(mouseArea(x + offset*size+(offset+1)*space, y+space,size, size))
}
function progress(x,y,width,height,value,fillCol='green',bgCol='gray',txt='',borCol=bgCol,borThk=1){
strokeWeight(1);
//stroke(borCol);//progressbar bg
fill(bgCol);
rect(x,y,width, height,5);
fill(fillCol);//progressbar fill
noStroke();
rect(x+borThk,y+borThk,(width-(borThk)*2)*(value/100),height-(borThk*2),5);
fill(bgCol)
textSize(12)
text(txt,x+(width-(borThk)*2)*(value/100),y)
}
function altProgress(x,y,itemWidth,itemHeight,value,txt,visualPosOffset,spacing,maxVal=100){
let visualPos = 1 + visualPosOffset
let accent="green"
if(value<15||value>82.5){accent="red"}
else if(value<50){accent="orange"}
//bg
fill(60)
strokeWeight(1)
stroke(accent);
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-0*(itemHeight+spacing), itemHeight,5)
//fill
stroke("light"+accent);
fill(accent)
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), (itemWidth-0*(itemHeight+spacing))*(value/maxVal), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
text(txt,x+5,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(RIGHT)
text(value+"%",x+itemWidth-35,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,itemHeight+10, itemHeight-5)
textAlign(LEFT)
}
function listEntry(x,y,itemWidth,itemHeight,listTable,itemID,visualPosOffset,spacing, type=listTable[itemID][0],txt="Something has gone wrong!"){//the last one is to allow for "forced itemtypes"
textAlign(LEFT)
let accent="green"
let visualPos = itemID + visualPosOffset
//console.log("passed list init, figuring out what itemtype this entry is")
if(type=="toggle"){
//console.log("draw toggleswitch")
let txt = listTable[itemID][1] +":"+listTable[itemID][2]
if(listTable[itemID][2]==true){accent="green"}//set button accent color according to state
else{accent="darkCyan"}
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth, itemHeight, txt, accent, 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay){
if(listTable[itemID][2]==false){listTable[itemID][2]=true}//set relay state to true if it is false
else(listTable[itemID][2]=false)//set it to false if it is anything else
timeBase = millis()//reset the delay for a button press
console.log(txt + "(Toggle):" +listTable[itemID][2])
}
}
}
if(type=="number"){
//console.log("draw number select")
txt = listTable[itemID][1] +":"+listTable[itemID][2]
//the display for the label and icon
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-2*(itemHeight+spacing), itemHeight, txt, "cyan", 230);
//plus button
drawButton(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight, "+", "green", 230);
if(mouseArea(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=550){
if(listTable[itemID][2]<listTable[itemID][4]){listTable[itemID][2]++}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(num+):" +listTable[itemID][2])
}
}
//minus
drawButton(x+(itemWidth-spacing-2*itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight, "-", "red", 230);
if(mouseArea(x+(itemWidth-spacing-2*itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=550){
if(listTable[itemID][2]>listTable[itemID][3]){listTable[itemID][2]--}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(num-):" +listTable[itemID][2])
}
}
}
else if(type=="numSlider"){
//console.log("draw int slider")
let accent="green"
txt = listTable[itemID][2]
//bg
fill(60)
strokeWeight(2)
stroke(accent);
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-0*(itemHeight+spacing), itemHeight,5)
//fill
stroke("darkCyan");
fill(accent)
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), (itemWidth-0*(itemHeight+spacing))*(listTable[itemID][2]/100), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
text(listTable[itemID][1],x+5,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(RIGHT)
text(txt,x+itemWidth-35,y+((itemHeight+2*spacing)*visualPos+spacing)*visualPos+2.5*spacing,itemHeight+10, itemHeight-5)
textAlign(LEFT)
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth, itemHeight)&&mouseIsPressed){
let newval = round((mouseX-x)/itemWidth*100,1)
listTable[itemID][2] = newval
console.log(listTable[itemID][0]+" is now set to "+listTable[itemID][2])
}
}
else if(type=="textEntry"){
//console.log("draw text entry")
let txt = listTable[itemID][1] +":"+listTable[itemID][2]
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth, itemHeight, txt, "cyan", 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay&&mouseIsPressed){
timeBase = millis()//reset the delay for a button press
newval = prompt("Enter new value for "+listTable[itemID][1]+": \nDefaults to current value ", listTable[itemID][2]);
if(newval!=null){listTable[itemID][2]=newval}
console.log(txt + "(textentry):" +listTable[itemID][2])
}
}
}
else if(type=="dropdownMenu"){
//console.log("draw dropdown")
let txt = listTable[itemID][1] +":"+listTable[itemID][2]
//the display for the label
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-1*(itemHeight+spacing), itemHeight, txt, "cyan", 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-1*(itemHeight+spacing), itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay){
if(listTable[itemID][2]<listTable[itemID][4]){listTable[itemID][2]++}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(dropdownmenu-label):" +listTable[itemID][2])
}
}
//plus button
drawButton(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight, "V", "green", 230);
if(mouseArea(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay){
if(listTable[itemID][2]<listTable[itemID][4]){listTable[itemID][2]++}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(dropdownmenu-arrow):" +listTable[itemID][2])
}
}
}
else if(type=="slider"||type=="volume"){
//console.log("draw float slider")
txt = listTable[itemID][2]+"%"
let accent="green"
//bg
fill(60)
strokeWeight(2)
stroke(accent);
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-0*(itemHeight+spacing), itemHeight,5)
//fill
stroke(accent);
fill(accent)
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), (itemWidth-0*(itemHeight+spacing))*(listTable[itemID][2]/100), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
text(listTable[itemID][1],x+5,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(RIGHT)
text(txt,x+itemWidth-35,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,itemHeight+10, itemHeight-5)
textAlign(LEFT)
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth, itemHeight)&&mouseIsPressed){
let newval = round((mouseX-x)/itemWidth*100,1)
listTable[itemID][2] = newval
console.log(listTable[itemID][1]+" is now set to "+listTable[itemID][2])
}
}
else if(type=="progress"){
//console.log("draw progress")
txt = listTable[itemID][2]+"%"
let accent="green"
//bg
fill(60)
strokeWeight(1)
stroke("gray");
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth-0*(itemHeight+spacing), itemHeight,5)
//fill
stroke("light"+accent);
fill(accent)
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), (itemWidth-0*(itemHeight+spacing))*(listTable[itemID][2]/100), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
text(listTable[itemID][1],x+5,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(RIGHT)
text(txt,x+itemWidth-35,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,itemHeight+10, itemHeight-5)
textAlign(LEFT)
}
else if(type=="toggleSlide"||type=="light"){
//console.log("draw toggle and slider combo")
toggleSlide(x,y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth,itemHeight,listTable[itemID][2]+"%",listTable[itemID][2],listTable[itemID][3],0,spacing)
}
else if(type=="loading"){
//console.log("draw animated indeterminate progress")
let accent="green"
let paddleWidth=(itemHeight*4)
//bg
fill(60)
strokeWeight(1)
stroke("gray");
rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth, itemHeight,5)
//fill
stroke("light"+accent);
fill(accent)
rect((x+itemWidth-paddleWidth)*animationLoopPercent(3500),y+((itemHeight+2*spacing)*visualPos+spacing), paddleWidth, itemHeight,5)
//rect(x,y+((itemHeight+2*spacing)*visualPos+spacing), (itemWidth*animationLoopPercent(2500)), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
textAlign(CENTER)
text(listTable[itemID][1],x+5,(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(LEFT)
}
else{
let txt = listTable[itemID][1] +":"+listTable[itemID][2]
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth, itemHeight, txt, "cyan", 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing), itemWidth, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay&&mouseIsPressed){
timeBase = millis()//reset the delay for a button press
let newval = prompt("\nAn internal error occured, fallingback to user text input.\n(MAY CAUSE ERRORS,NO SANITY CHECKS AVAILABLE)\nEnter new value for "+listTable[itemID][1]+": \nDefaults to current value.", listTable[itemID][2]);
if(newval!=null){listTable[itemID][2]=newval}
console.log(txt + "(Fallback):" +listTable[itemID][2])
}
}
}
}
function toggleSlide(x,y,itemWidth,itemHeight,txt,value,toggle,visualPosOffset,spacing){
let accent="green"
let visualPos = visualPosOffset
//let txt = listTable[itemID][2]
let txt2 = value
let txt3 = "listTable[itemID][1]"
//bg
fill(60)
strokeWeight(2)
stroke(accent);
rect(x+2*(spacing+itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth-2*(itemHeight+spacing), itemHeight,5)
//fill
stroke("darkCyan");
fill(accent)
rect(x+2*(spacing+itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),(value/100)*(itemWidth-2*(itemHeight+spacing)), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
textAlign(LEFT)
text(txt,x+(2*itemHeight+3*spacing),(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-5,itemHeight-5)
textAlign(RIGHT)
text(txt2,x+itemWidth-2*(4*itemHeight+5*spacing),(y+((itemHeight+2*spacing)*visualPos+spacing))+5,2*(4*itemHeight+5*spacing), itemHeight-5)
textAlign(LEFT)
if(toggle==true){accent="green"}//set button accent color according to state
else{accent="darkCyan"}
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing), 2*itemHeight, itemHeight, toggle, "green", 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing),2*itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay){
if(toggle==false){
toggle=true
timeBase = millis()//reset the delay for a button press
console.log(txt + "(toggle+slide-toggle):" +toggle)
}
else(toggle=false)//set it to false if it is anything else
timeBase = millis()//reset the delay for a button press
console.log(txt + "(toggle+slide-toggle):" +toggle)
}
}
if(mouseArea(x+(spacing+itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth-((2*itemHeight)+spacing), itemHeight)&&mouseIsPressed){
value = round((mouseX-(x+(spacing+itemHeight)))/(itemWidth-((2*itemHeight)+spacing))*100,1)//set variable to new value
console.log(txt+" is now set to "+value)
}
return(toggle,value)
}
function toggleSlide2(x,y,itemWidth,itemHeight,sliderVal,toggleVal,title,visualPosOffset,spacing,maxVal,altval=""){
let accent="green"
let visualPos = visualPosOffset
let txt = sliderVal
let txt2 = title+altval
//bg
fill(60)
strokeWeight(2)
stroke(accent);
rect(x+2*(spacing+itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth-(4*itemHeight)-(5*spacing), itemHeight,5)
//fill
stroke("darkCyan");
fill(accent)
rect(x+2*(spacing+itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),(sliderVal/100)*(itemWidth)-((4*itemHeight)+(5*spacing)), itemHeight,5)
//text
noStroke();
fill('white')
textSize(17)
textAlign(LEFT)
text(txt2,x+2*(2*spacing+itemHeight),(y+((itemHeight+2*spacing)*visualPos+spacing))+5,width-(4*itemHeight+5*spacing),itemHeight-5)
textAlign(RIGHT)
text(txt,x+itemWidth-(4*itemHeight+5*spacing),(y+((itemHeight+2*spacing)*visualPos+spacing))+5,itemHeight+20, itemHeight-5)
textAlign(LEFT)
//textAlign(CENTER)
//text(txt,x+5,(y+((itemHeight+2*spacing))*visualPos+spacing)+5,itemWidth-2*(itemHeight+spacing),itemHeight-5)
//textAlign(LEFT)
if(toggleVal==true){accent="green"}//set button accent color according to state
else{accent="darkCyan"}
drawButton(x,y+((itemHeight+2*spacing)*visualPos+spacing),2*itemHeight, itemHeight, str(toggleVal), "green", 230);
if(mouseArea(x,y+((itemHeight+2*spacing)*visualPos+spacing),2*itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=timeDelay){
if(toggleVal==false){
toggleVal=true
timeBase = millis()//reset the delay for a button press
console.log(title + "(toggle+slide-toggle):" +toggleVal)
return(sliderVal,toggleVal)}//set relay state to true if it is false
else(toggleVal=false)//set it to false if it is anything else
timeBase = millis()//reset the delay for a button press
console.log(title + "(toggle+slide-toggle):" +toggleVal)
return(sliderVal,toggleVal)
}
}
//plus button
drawButton(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight, "+", "green", 230);
if(mouseArea(x+itemWidth-(itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=550){
if(sliderVal<maxVal){sliderVal++}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(num+):" +sliderVal)
return(sliderVal,toggleVal)
}
}
//minus
drawButton(x+(itemWidth-spacing-2*itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight, "-", "red", 230);
if(mouseArea(x+(itemWidth-spacing-2*itemHeight),y+((itemHeight+2*spacing)*visualPos+spacing),itemHeight, itemHeight)&&mouseIsPressed){//see if the mouse is in the area of a button and if it is being pressed
if(millis()-timeBase>=550){
if(sliderVal>-maxVal){sliderVal--}//set relay state to true if it is false
timeBase = millis()//reset the delay for a button press
console.log(txt + "(num-):" +sliderVal)
return(sliderVal,toggleVal)
}
}
if(mouseArea(x+(2*itemHeight+3*spacing),y+((itemHeight+2*spacing)*visualPos+spacing),itemWidth-(4*itemHeight+5*spacing), itemHeight)&&mouseIsPressed){
sliderVal = round((mouseX/(x-(4*itemHeight+5*spacing)+(itemWidth))-0.04)*100)//set variable to new value
console.log(title+" is now set to "+sliderVal)
return(sliderVal,toggleVal)
}
}
// todo: sliders, radio buttons(ui element), tabs(might just use multiple html pages), always present status bar type thing, more consistant framework, RPI GPIO interaction via .SH or .py files
//data storage&retreval placeholder values
//--battery/power
let bat0 = {
name:"RPi Battery",
id:"battery0",
type:"Li-ION",
capacityMAH:0000,
cells:1,
percentKnown:true,
percent:100,
voltage:3.7,
voltageChange:-0.0,
charging:true,
exists:true,
timeToEmpty:0,
timeToFull:0,
progressbar:0,
}
let bat1 = {
name:"Car Battery",
id:"battery1",
type:"Sealed Lead-Acid",
capacityMAH:0000,
cells:1,
percentKnown:false,
percent:00,
voltage:12,
voltageChange:-0.0,
charging:true,
exists:true,
timeToEmpty:0,
timeToFull:0,
progressbar:0,
}
let bat2 = {
name:"House Battery",
id:"battery2",
type:"Sealed Lead-Acid",
capacityMAH:0000,
cells:2,
percentKnown:false,
percent:00,
voltage:24,
voltageChange:-0.0,
charging:true,
exists:true,
timeToEmpty:0,
timeToFull:0,
progressbar:0,
}
let acIn0 = {
safeVoltageMin:110,
curVoltage:120,
safeVoltageMax:125,
amperage:0,
connected:true,
phaseCount:2,
}
let dcIn0 = {
safeVoltageMin:12,
curVoltage:13,
safeVoltageMax:15,
amperage:0,
connected:false,
voltRangebar:50,
}
let acOut0 = {
safeVoltage:110,
curVoltage:120,
safeVoltageMax:125,
amperage:0,
connected:true,
phaseCount:2,
}
let dcOut0 = {
safeVoltageMin:4.5,
curVoltage:5.2,
safeVoltageMax:5.6,
amperage:0,
connected:false,
voltRangebar:50,
}
let fuel0 = {
type:"Gasoline",
name:"Main Fuel Tank",
sensorID:"fuel0",
percent:100,
capacityLiter:68,
usageLiter:0,
flowRateLiterMin:0,
temperature:10,
usageRangeLkm:9999999,
usageRangeMpg:0,
}
//relays
let relayTable = [//id, pin, name, active
[0,"GPIO5","relay 1",false],
[1,"GPIO6","relay 2",false],
[2,"GPIO13","relay 3",false],
[3,"GPIO19","relay 4",false],
[4,"GPIO26","relay 5",false],
[5,"GPIO16","relay 6",false],
[6,"GPIO20","relay 7",false],
[7,"GPIO21","relay 8",false]
]
//gpio pins
let gpioPins = [//pin,name,type,active/state
[0,"gpio0","output",false],
[1,"gpio1","output",false],
[2,"gpio2","output",false]]
//
//console commands
function help(){
console.log("HELP - DMessham's automation system");
console.log("--- external device control ---");
console.log("relaySet(number,state) | changes state of a relay");
console.log("relayGet(number) | returns state of selected relay");
console.log("gpioSet(number,state) | changes state of a gpio pin");
console.log("gpioGet(number) | returns state of selected gpio pin");
console.log("analogSet(id,state,value) | changes value of an analog device/dac/adc");
console.log("analogGet(id) | returns of of selected analog device/dac/adc");
console.log("dataSet(id,type,state) | changes value of a digital vdevice");
console.log("dataGet(id,protocall) | returns of of selected analog device");
console.log("--- sensor ---");
console.log("batteryStatus(id) | returns(charge, chargeState, usage, capacity,voltage)");
console.log("--- internal control ---");
console.log("shutdown(time) | shutsdown device after specified amount of time");
console.log("reboot(time) | reboots device after specified amount of time");
}
//device control
function relaySet(number,state){
relayTable[number][3]=state;
let name= relayTable[number][2]
let location = name= relayTable[number][1]
console.log("Setting status of relay with id of '"+ number +"' that is Named '"+ name +"' with state '"+state+"' and location '"+location+"'");
}
function relayGet(number){
console.log("not yet tested");
let state=relayTable[number][3]
let name=relayTable[number][2]
let location = name= relayTable[number][1]
console.log("Getting status of relay with id of '"+ number +"' that is Named '"+ name +"' with state '"+state+"' and location '"+location+"'")
return(number,name,state,location)
}
function gpioSet(number,state){
console.log("not yet implemented");
}
function gpioGet(number){
console.log("not yet implemented");
}
function analogSet(id,state){
console.log("not yet implemented");
}
function analogGet(id){
console.log("not yet implemented");
let on=false
let value=255
return(on,state,brightness)
}
function dataSet(id,type,state){
console.log("not yet implemented");
}
function dataGet(id,type){
console.log("not yet implemented");
}
function batteryStatus(id){
console.log("not yet implemented");
let charge = 100
let chargeState = "Charging"
let usage=-10
let capacity = 12300
let voltage = 3.6
return(charge, chargeState, usage, capacity, voltage)
}
// control commands
function reboot(time){
console.log("not yet implemented, please manually reboot");
}
function shutdown(time){
console.log("not yet implemented, please manually shutdown");
}