forked from JayP823/HLSI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.js
More file actions
150 lines (134 loc) · 4.49 KB
/
Copy pathlabel.js
File metadata and controls
150 lines (134 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Generate a signal HTML display label
*
* @param {Signal Object}
* @param {String} signal par - name of variable or method to compute for label;
* example "freq". May be 'freq', 'bw', 'gn', 'mcs' and 'rate'
* @param {Object options}
*
* element: CSS node selector as a string or
* HTML element output node
*
* prefix: prefix String. You may want to have a space on the end.
*
* suffix: suffix String. You may want to have a space in the first
* character.
*
* func: user custom function(val) to change the string in the
* element.
*
*/
function Label(sig, par, opts = null) {
var output; // The HTML element that has a value to display
var scale,
unitPrefix, // example: scale = 1000 unitPrefix = 'M'
unit; // example: "Hz"
var prefix = "", suffix = "";
var func = null;
if(opts && opts.func !== undefined)
func = opts.func;
// This parseValue() get values turned into a string for most
// types of par (parameters), but for "mcs" we change it below.
//
var parseValue = function(val) {
return d3.format(".2f")(val * scale);
};
//////////////////////////////////////////////////////////
// Parse options (opts):
//////////////////////////////////////////////////////////
if(!opts)
// Setup default options:
opts = {};
if(opts.element !== undefined) {
if(typeof(opts.element) === "string") {
output = document.querySelector(opts.element);
if(output === null) {
let msg = "Failed to get element with selector: " +
opts.element;
console.log(msg);
alert(msg);
return;
}
} else
output = opts.element;
} else {
output = document.createElement("OUTPUT");
let p = document.createElement("p");
p.appendChild(output);
document.body.appendChild(p);
}
if(opts.prefix !== undefined)
prefix = opts.prefix;
if(opts.suffix !== undefined)
suffix = opts.suffix;
//
// opts done.
//////////////////////////////////////////////////////////
if(func) {
// We let par be an array of parameters if the user passed in a
// user custom string generator function.
if(!Array.isArray(par))
// This called a user custom string generator with one
// signal parameter.
sig.onChange(par, function(s, val) {
output.value = prefix + func(val) + suffix;
});
else
par.forEach(function(p) {
sig.onChange(p, function(s, val) {
output.value = prefix + func(val) + suffix;
});
});
return;
}
// TODO: This shares some code with sliders.js. Merge it into common
// code.
switch(par) {
case 'freq':
[scale, unitPrefix] = scale_units(sig[par + "_init"], 0.1);
unit = 'Hz';
break;
case 'bw':
[scale, unitPrefix] = scale_units(sig[par + "_init"], 1.0);
unit = 'Hz';
break;
case 'mcs':
scale = 1.0;
unitPrefix = '';
unit = '';
parseValue = function(val) {
if(val < 0)
val = 0;
else if(val > 11)
val = 11;
return conf.schemes[parseInt(val)].name;
};
break;
case 'gn':
scale = 1.0;
unitPrefix = '';
unit = 'dB';
break;
case 'sinr':
unitPrefix = '';
scale = 1.0;
unit = 'dB';
break;
case 'rate':
// For 'rate' scale and unitPrefix can change as we go, so
// we finish it here, because this case is not like the
// others.
sig.onChange(par, onChange = function(s, val) {
[scale, unitPrefix] = scale_units(val, 1.0);
output.value = prefix + parseValue(val) + " " +
unitPrefix + 'bits/s' + suffix;
});
return;
}
// Mung the strings into one suffix string.
suffix = " " + unitPrefix + unit + suffix;
// add callback attached to that variable/parameter
sig.onChange(par, onChange = function(s, val) {
output.value = prefix + parseValue(val) + suffix;
});
}