-
Notifications
You must be signed in to change notification settings - Fork 0
/
baud.js
229 lines (171 loc) · 4.36 KB
/
baud.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
function intalizeTable()
{
var clockRate = document.getElementById("clock-rate");
var baudRate = document.getElementById("baud-rate");
var speedMode = document.getElementById("speed-mode");
//make sure all buttons exist
if (clockRate != null && baudRate != null && speedMode != null)
{
new Table(clockRate, baudRate, speedMode);
}
else
{
console.error("One of the inputs is missing.")
}
}
function Table(clockRate, baudRate, speedMode)
{
this.clockRate = clockRate;
this.baudRate = baudRate;
this.speedMode = speedMode;
//initalize update events
clockRate.addEventListener("change", this.update.bind(this));
baudRate.addEventListener("change", this.update.bind(this));
speedMode.addEventListener("change", this.update.bind(this));
//update the for the first time
this.update();
}
Table.prototype.update = function ()
{
var clockRate = parseFloat(this.clockRate.value);
var baudRate = parseFloat(this.baudRate.value);
var speedMode = parseFloat(this.speedMode.value);
if (clockRate <= 0)
{
this.clockRate.classList.add("is-invalid");
return;
}
if (baudRate <= 0)
{
this.baudRate.classList.add("is-invalid");
return;
}
this.baudRate.classList.remove("is-invalid");
this.clockRate.classList.remove("is-invalid");
//get all bscales
var bscales = document.getElementsByClassName("bscale");
//define length outside of loop for efficiency
var bscaleLength = bscales.length;
//set
for (var index = 0; index < bscaleLength; index++)
{
var bscale = bscales[index];
var parentRow = bscale.parentElement;
//get bscale value
var scale = parseFloat(bscale.textContent);
/*
* Calculated bsel value
*/
//get the calculated bsel value's tag
var calcBsel = parentRow.querySelector(".calc-bsel");
//calculate "calculated" bsel value
var calcBselValue
if (scale >= 0)
{
calcBselValue = clockRate * 1000000/(Math.pow(2, scale) * (16 - 8 * speedMode) * baudRate) - 1;
}
else
{
calcBselValue = 1/(Math.pow(2,scale))*(clockRate * 1000000/((16 - 8 * speedMode) * baudRate) - 1);
}
calcBselValue = calcBselValue.toFixed(2);
calcBsel.textContent = calcBselValue;
/*
* bsel value
*/
//get bsel tag
var bsel = parentRow.querySelector(".bsel");
var bselValue;
//set bsel
if (calcBselValue > 4095)
{
bselValue = "N/A";
}
else
{
bselValue = Math.floor(parseFloat(calcBselValue) + 0.5);
}
bsel.textContent = bselValue;
/*
* real baud rate
*/
var baud = parentRow.querySelector(".baud-rate");
var baudValue;
if (bselValue == -1)
{
baudValue = "No!";
}
else
{
if (scale >= 0)
{
baudValue = clockRate * 1000000/(Math.pow(2, scale)*(16 - 8*speedMode)*(bselValue + 1));
}
else
{
baudValue = clockRate * 1000000/((16 - 8 * speedMode) * ((Math.pow(2, scale)* bselValue) + 1));
}
if (isNaN(baudValue) || !isFinite(baudValue))
{
baudValue = "No!";
}
else
{
baudValue = baudValue.toFixed(0);
}
}
baud.textContent = baudValue;
/*
* Error [%]
*/
var error = parentRow.querySelector(".error");
var percentError;
if (baudValue == "No!")
{
percentError = "N/A";
}
else
{
percentError = 100 - baudValue/baudRate*100;
percentError = percentError.toFixed(2);
}
error.textContent = percentError;
/*
* abs(Error) [%]
*/
var absErrorTag = parentRow.querySelector(".abs-error");
var absError;
if (baudValue == "No!")
{
absError = "N/A";
}
else
{
absError = Math.abs(percentError);
absError = absError.toFixed(2);
}
absErrorTag.textContent = absError;
var colorClass;
if (absError > 6 || absError == "N/A")
{
//red
colorClass = "bg-danger";
}
else if (absError > 3)
{
//yellow
colorClass = "bg-warning";
}
else
{
//green
colorClass = "bg-success";
}
//remove any previous colors
absErrorTag.classList.remove("bg-danger");
absErrorTag.classList.remove("bg-warning");
absErrorTag.classList.remove("bg-success");
absErrorTag.classList.add(colorClass);
}
}
document.addEventListener("DOMContentLoaded", intalizeTable);